Kubernetes 1.19.0——deployment(1)

Pod是不健壮的,随时都面临挂掉的风险,且本身是没有可再生性的

如果pod个数需要太多,创建起来会比较麻烦

deployment相当于一个机器人,控制器,用来创建pod

如果某个pod挂掉了,deployment会重新生成一个,保证环境里有3个pod

K8S 1.17.x 之前创建deploy的命令选项很多,但从1.18.x开始就变得少了

deployment创建出来的每个pod使用相同的一个模板

labels表示创建出来的每个pod具备的标签

通过修改yaml文件中replicas为3,自动为我们创建3个副本
[root@vms61 chap5-deploy]# cat web1.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web1
  name: web1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web1
    spec:
      containers:
      - image: nginx
        imagePullPolicy: IfNotPresent
        name: nginx
        resources: {}
status: {}
[root@vms61 chap5-deploy]# kubectl apply -f web1.yaml 
deployment.apps/web1 created
[root@vms61 chap5-deploy]# kubectl get pods -o wide
NAME                    READY   STATUS    RESTARTS   AGE   IP              NODE    NOMINATED NODE   READINESS GATES
web1-5bfb6d8dcc-7dvxm   1/1     Running   0          7s    10.244.116.47   vms63   <none>           <none>
web1-5bfb6d8dcc-gnvhf   1/1     Running   0          6s    10.244.116.46   vms63   <none>           <none>
web1-5bfb6d8dcc-qz77s   1/1     Running   0          6s    10.244.196.51   vms62   <none>           <none>
[root@vms61 chap5-deploy]# 
[root@vms61 chap5-deploy]# kubectl get pods -o wide
NAME                    READY   STATUS    RESTARTS   AGE   IP              NODE    NOMINATED NODE   READINESS GATES
web1-5bfb6d8dcc-7dvxm   1/1     Running   0          22m   10.244.116.47   vms63   <none>           <none>
web1-5bfb6d8dcc-gnvhf   1/1     Running   0          22m   10.244.116.46   vms63   <none>           <none>
web1-5bfb6d8dcc-qz77s   1/1     Running   0          22m   10.244.196.51   vms62   <none>           <none>
[root@vms61 chap5-deploy]# kubectl delete pod web1-5bfb6d8dcc-7dvxm --force
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "web1-5bfb6d8dcc-7dvxm" force deleted
[root@vms61 chap5-deploy]# kubectl get pods -o wide
NAME                    READY   STATUS              RESTARTS   AGE   IP              NODE    NOMINATED NODE   READINESS GATES
web1-5bfb6d8dcc-fg8js   0/1     ContainerCreating   0          2s    <none>          vms63   <none>           <none>
web1-5bfb6d8dcc-gnvhf   1/1     Running             0          22m   10.244.116.46   vms63   <none>           <none>
web1-5bfb6d8dcc-qz77s   1/1     Running             0          22m   10.244.196.51   vms62   <none>           <none>
[root@vms61 chap5-deploy]# kubectl get pods -o wide
NAME                    READY   STATUS    RESTARTS   AGE   IP              NODE    NOMINATED NODE   READINESS GATES
web1-5bfb6d8dcc-fg8js   1/1     Running   0          19s   10.244.116.48   vms63   <none>           <none>
web1-5bfb6d8dcc-gnvhf   1/1     Running   0          22m   10.244.116.46   vms63   <none>           <none>
web1-5bfb6d8dcc-qz77s   1/1     Running   0          22m   10.244.196.51   vms62   <none>           <none>

即便我们强行删除pod,deployment也会帮我们重新拉起

Web1这个deployment是通过标签为app=web1的标签来定位管理的是哪些pod
[root@vms61 chap5-deploy]# cat web1.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web1
  name: web1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web1
        app1: web1
        app2: web2
    spec:
      containers:
      - image: nginx
        imagePullPolicy: IfNotPresent
        name: nginx
        resources: {}
status: {}
[root@vms61 chap5-deploy]# kubectl apply -f web1.yaml 
deployment.apps/web1 created
[root@vms61 chap5-deploy]# kubectl get pods -o wide
NAME                   READY   STATUS    RESTARTS   AGE   IP              NODE    NOMINATED NODE   READINESS GATES
web1-bc6bbd46c-gkfn9   1/1     Running   0          4s    10.244.196.52   vms62   <none>           <none>
web1-bc6bbd46c-n74zn   1/1     Running   0          4s    10.244.116.49   vms63   <none>           <none>
web1-bc6bbd46c-p2cbv   1/1     Running   0          4s    10.244.116.50   vms63   <none>           <none>
[root@vms61 chap5-deploy]# kubectl get pods --show-labels 
NAME                   READY   STATUS    RESTARTS   AGE   LABELS
web1-bc6bbd46c-gkfn9   1/1     Running   0          38s   app1=web1,app2=web2,app=web1,pod-template-hash=bc6bbd46c
web1-bc6bbd46c-n74zn   1/1     Running   0          38s   app1=web1,app2=web2,app=web1,pod-template-hash=bc6bbd46c
web1-bc6bbd46c-p2cbv   1/1     Running   0          38s   app1=web1,app2=web2,app=web1,pod-template-hash=bc6bbd46c

上图中matchLabel里的标签一定要在template下的metada下的labels字段中匹配到,否则会报错

修改yaml使其挂载到pod中/xx目录并传递变量myenv至pod中
目录挂载以及变量传递均成功
[root@vms61 chap5-deploy]# cat web1.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web1
  name: web1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web1
        app1: web1
        app2: web2
    spec:
      volumes:
      - name: v1
        emptyDir: {}
      containers:
      - image: nginx
        imagePullPolicy: IfNotPresent
        name: nginx
        volumeMounts:
        - name: v1
          mountPath: /xx
        ports:
        - containerPort: 80
        env:
        - name: myenv1
          value: haha1
        - name: myenv2
          value: haha2
        resources: {}
status: {}
[root@vms61 chap5-deploy]# kubectl apply -f web1.yaml 
deployment.apps/web1 created
[root@vms61 chap5-deploy]# kubectl get pods
NAME                    READY   STATUS    RESTARTS   AGE
web1-77cc489b4b-89pfh   1/1     Running   0          6s
web1-77cc489b4b-bl8p6   1/1     Running   0          6s
web1-77cc489b4b-q8lgb   1/1     Running   0          6s
[root@vms61 chap5-deploy]# kubectl exec -it web1-77cc489b4b-89pfh -- bash
root@web1-77cc489b4b-89pfh:/# ls /xx
root@web1-77cc489b4b-89pfh:/# echo $myenv1
haha1
root@web1-77cc489b4b-89pfh:/# echo $myenv2
haha2
root@web1-77cc489b4b-89pfh:/# exit
exit

如果想要10个副本,只需要告诉deployment即可(这里不作演示)

kubectl scale deployment nginx –replicas=10

或者

kubectl edit deployment nginx

正文完