K8S 之 YAML文件创建各种角色

一、创建单个nginx-pod文件

apiVersion: v1                 #指定api版本,此值必须在kubectl apiversion中 
kind: Pod                        #指定创建资源的角色/类型(pod\deployment\svc)
metadata:                       #资源的元数据/属性
  name: nginx                 #资源的名字,在同一个namespace中必须唯一,kubectl显示名
  labels:                          #设定资源的标签
    app: web                    #为app标签打上web字段
  namespace: test          #存放的空间
spec:                              #指定该资源的内容 
  containers:                  #该POD运行容器的相关信息
    - name: nginx-test    #容器的名称,docker ps 看到的名称
      image: test-harbor.cedarhd.com/public/nginx:curl        #容器使用的镜像
      ports:                      #指定容器的端口
        - containerPort: 80                      #容器使用的端口

[root@test-nodes1 ~]# kubectl create -f nginx-pod.yaml 
pod/nginx created
[root@test-nodes1 ~]# kubectl get pod -o wide -n test
NAME    READY   STATUS    RESTARTS   AGE   IP           NODE                      NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          13s   172.7.21.7   test-nodes1.cedarhd.com   <none>           <none>

二、创建deployment控制器nginx-dp的yaml文件

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-dp
  namespace: test
spec:
  replicas: 2              #复本数
  template:               #deployment模板内容
    metadata:
      labels:
        app: nginx-proxy
    spec:
      containers:
        - name: nginx
          image: test-harbor.cedarhd.com/public/nginx:curl
          ports:
            - containerPort: 80

[root@test-nodes1 ~]# kubectl create -f nginx-dp.yaml 
deployment.extensions/nginx-dp created
[root@test-nodes1 ~]# kubectl get pod -o wide -n test
NAME                        READY   STATUS    RESTARTS   AGE     IP           NODE                      NOMINATED NODE   READINESS GATES
nginx-dp-856666d759-cwqfc   1/1     Running   0          9m57s   172.7.21.7   test-nodes1.cedarhd.com   <none>           <none>
nginx-dp-856666d759-lvr9c   1/1     Running   0          9m57s   172.7.22.7   test-nodes2.cedarhd.com   <none>           <none>

NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-dp   2/2     2            2           8s
NAME                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-dp-856666d759   2         2         2       8s

三、创建svc服务并关联上面的deployment(通过app:nginx-proxy)

apiVersion: v1
kind: Service
metadata:
  name: nginx-dp         #svc的显示名
  namespace: test
spec:
  ports:
    - port: 80
      targetPort: 80
  selector:
    app: nginx-proxy     #关联当前集群上app为nginx-proxy字段

[root@test-nodes1 ~]# kubectl get svc -n test
NAME       TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
nginx-dp   ClusterIP   192.168.249.33   <none>        80/TCP    6s
TCP  192.168.249.33:80 nq         #相应的映射
  -> 172.7.21.7:80                Masq    1      0          0         
  -> 172.7.22.7:80                Masq    1      0          0  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章