k8s之deployment应用

Deployment 为 Pod 和 ReplicaSet 提供了一个声明式定义 (declarative) 方法,用来替代以前的 ReplicationController 来方便的管理应用。在k8s集群中使用deloyment来达到pod预期的副本数
典型的用例如下:

  • 使用 Deployment 来创建 ReplicaSet。ReplicaSet 在后台创建 pod。检查启动状态,看它是成功还是失败。
  • 然后,通过更新 Deployment 的 PodTemplateSpec 字段来声明 Pod 的新状态。这会创建一个新的 ReplicaSet,Deployment 会按照控制的速率将 pod 从旧的 ReplicaSet 移动到新的 ReplicaSet 中。
  • 如果当前状态不稳定,回滚到之前的 Deployment revision。每次回滚都会更新 Deployment 的 revision。
  • 扩容 Deployment 以满足更高的负载。
  • 暂停 Deployment 来应用 PodTemplateSpec 的多个修复,然后恢复上线。
  • 根据 Deployment 的状态判断上线是否 hang 住了。
  • 清除旧的不必要的 ReplicaSet。
    具体应用如下:

编写deployment.yaml脚本

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: deployment
  labels:
    app: nginx   
spec:
  replicas: 3
  revisionHistoryLimit: 20     #保留历史版本,默认是10
  progressDeadlineSeconds: 180s #部署超时时间(s),默认是600
  strategy: #策略
    type: RollingUpdates  #滚动更新策略(就是杀死一部分,就启动一部分,在更新过程中,存在两个版本pod,type=Recreate:在创建出新的pod之前会先杀掉所有已存在的pod)
    rollingUpdate:  #滚动更新(当type为RollingUpdate时生效,用于为RollingUpdate设置参数,支持两个属性)
      maxSurge: 25% #最大额外可以存在的副本数,可以为百分比,也可以为整数(默认为25%)
      maxUnavaliable: 25% #最大不可用状态的pod的最大值,可以为百分比,也可以为整数(默认为25%)
  selector:   #选择器,通过它指定该控制器管理哪些pod                                                                                                                                                             
    matchLabels:
      app: nginx
  template:     #模板,当副本数量不足时,会根据下面的模板创建pod副本
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx-deploy
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
          - containerPort: 81

运行脚本如下:

[root@weizb-1 ~]# kubectl apply -f deployment.yaml 
deployment.apps/nginx-deploy created
[root@weizb-1 ~]# 

查看deployment,结果如下:
看到已有3个pod状态为running,且labels和设置的一样,如下:

[root@k8s-master-1 deployment]# kubectl get pod -n deployment --show-labels
NAME                                READY   STATUS    RESTARTS   AGE     LABELS
nginx-deployment-75b96b6748-5r64m   1/1     Running   0          4m53s   app=nginx,pod-template-hash=75b96b6748
nginx-deployment-75b96b6748-6sxd6   1/1     Running   0          4m53s   app=nginx,pod-template-hash=75b96b6748
nginx-deployment-75b96b6748-rmd6b   1/1     Running   0          4m53s   app=nginx,pod-template-hash=75b96b6748
[root@k8s-master-1 deployment]# 

如下查看deployment:

[root@k8s-master-1 deployment]# kubectl get deployment -n deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           6m56s
[root@k8s-master-1 deployment]# 

如下查看创建rs副本:

[root@k8s-master-1 deployment]# kubectl get rs -n deployment
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-75b96b6748   3         3         3       8m20s
[root@k8s-master-1 deployment]#

查看deloyment的信息,如下:

[root@k8s-master-1 ~]# kubectl describe deployment nginx-deployment
Name:                   nginx-deployment
Namespace:              deployment
CreationTimestamp:      Sat, 20 Feb 2021 15:31:13 +0800
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=nginx
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   test-deployment:
    Image:        nginx:latest
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  
    Mounts:       
  Volumes:        
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  
NewReplicaSet:   nginx-deployment-75b96b6748 (3/3 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  10m   deployment-controller  Scaled up replica set nginx-deployment-75b96b6748 to 3

deployment的pod进行滚动更新

查看此时deployment rollout状态,如下:

[root@k8s-master-1 ~]# 
[root@k8s-master-1 ~]# kubectl rollout status  deployment/nginx-deployment -n deployment
deployment "nginx-deployment" successfully rolled out
[root@k8s-master-1 ~]# 

更新pod的镜像

更新pod的nginx镜像的为v1.9,如下:
确认现在使用的镜像为nginx:latest,如下:

 [root@k8s-master-1 ~]# kubectl get rs -n deployment  -o wide
NAME                          DESIRED   CURRENT   READY   AGE   CONTAINERS        IMAGES         SELECTOR
nginx-deployment-75b96b6748   3         3         3       27m   test-deployment   nginx:latest   app=nginx,pod-template-hash=75b96b6748
修改nginx镜像,如下:
[root@k8s-master-1 ~]# kubectl  set image deployment nginx-deployment test-deployment=nginx:v1.9 --record -n deployment
deployment.apps/nginx-deployment image updated
    
可以直接编辑deployment来修改信息结果一样,如下:
[root@k8s-master-1 ~]# kubectl edit deployment/nginx-deploy

查看pod,正在删除重建,如下:

[root@k8s-master-1 ~]# kubectl get pod -n deployment  -o wide
NAME                                READY   STATUS              RESTARTS   AGE   IP           NODE         NOMINATED NODE   READINESS GATES
nginx-deployment-56d9f665d7-j7lqs   1/1     Running             0          4s    10.244.5.5   k8s-node-3              
nginx-deployment-56d9f665d7-q5648   0/1     ContainerCreating   0          1s           k8s-node-3              
nginx-deployment-75b96b6748-5r64m   1/1     Running             0          30m   10.244.1.4   k8s-node-1              
nginx-deployment-75b96b6748-6sxd6   1/1     Terminating         0          30m   10.244.5.4   k8s-node-3              
nginx-deployment-75b96b6748-rmd6b   1/1     Running             0          30m   10.244.4.5   k8s-node-2              
[root@k8s-master-1 ~]# kubectl get pod -n deployment  -o wide
NAME                                READY   STATUS              RESTARTS   AGE   IP           NODE         NOMINATED NODE   READINESS GATES
nginx-deployment-56d9f665d7-j7lqs   1/1     Running             0          10s   10.244.5.5   k8s-node-3              
nginx-deployment-56d9f665d7-lhnbf   0/1     ContainerCreating   0          4s           k8s-node-2              
nginx-deployment-56d9f665d7-q5648   1/1     Running             0          7s    10.244.5.6   k8s-node-3              
nginx-deployment-75b96b6748-5r64m   1/1     Running             0          30m   10.244.1.4   k8s-node-1              
nginx-deployment-75b96b6748-6sxd6   0/1     Terminating         0          30m   10.244.5.4   k8s-node-3              
nginx-deployment-75b96b6748-rmd6b   0/1     Terminating         0          30m   10.244.4.5   k8s-node-2              
[root@k8s-master-1 ~]# kubectl get pod -n deployment  -o wide
NAME                                READY   STATUS        RESTARTS   AGE   IP           NODE         NOMINATED NODE   READINESS GATES
nginx-deployment-56d9f665d7-j7lqs   1/1     Running       0          15s   10.244.5.5   k8s-node-3              
nginx-deployment-56d9f665d7-lhnbf   1/1     Running       0          9s    10.244.4.6   k8s-node-2              
nginx-deployment-56d9f665d7-q5648   1/1     Running       0          12s   10.244.5.6   k8s-node-3              
nginx-deployment-75b96b6748-5r64m   0/1     Terminating   0          30m   10.244.1.4   k8s-node-1              
nginx-deployment-75b96b6748-6sxd6   0/1     Terminating   0          30m   10.244.5.4   k8s-node-3              
nginx-deployment-75b96b6748-rmd6b   0/1     Terminating   0          30m   10.244.4.5   k8s-node-2              
[root@k8s-master-1 ~]# kubectl get pod -n deployment  -o wide
NAME                                READY   STATUS    RESTARTS   AGE    IP           NODE         NOMINATED NODE   READINESS GATES
nginx-deployment-56d9f665d7-j7lqs   1/1     Running   0          2m9s   10.244.5.5   k8s-node-3              
nginx-deployment-56d9f665d7-lhnbf   1/1     Running   0          2m3s   10.244.4.6   k8s-node-2              
nginx-deployment-56d9f665d7-q5648   1/1     Running   0          2m6s   10.244.5.6   k8s-node-3              

查看replicaset,副本镜像已经变成nginx:v1.9,如下:

[root@k8s-master-1 ~]# kubectl get rs -n deployment  -o wide
NAME                          DESIRED   CURRENT   READY   AGE    CONTAINERS        IMAGES         SELECTOR
nginx-deployment-56d9f665d7   3         3         3       4m2s   test-deployment   nginx:v1.9     app=nginx,pod-template-hash=56d9f665d7
nginx-deployment-75b96b6748   0         0         0       34m    test-deployment   nginx:latest   app=nginx,pod-template-hash=75b96b6748

查看此时deployment信息,如下:

[root@k8s-master-1 ~]# kubectl describe deployment nginx-deployment -n deployment 
Name:                   nginx-deployment
Namespace:              deployment
CreationTimestamp:      Sat, 20 Feb 2021 15:31:13 +0800
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision: 2
                        kubernetes.io/change-cause: kubectl set image deployment nginx-deployment test-deployment=nginx:v1.9 --record=true --namespace=deployment
Selector:               app=nginx
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge    ###此处可以看到默认值为25%
Pod Template:
  Labels:  app=nginx
  Containers:
   test-deployment:
    Image:        nginx:v1.9
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  
    Mounts:       
  Volumes:        
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  
NewReplicaSet:   nginx-deployment-56d9f665d7 (3/3 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  41m   deployment-controller  Scaled up replica set nginx-deployment-75b96b6748 to 3
  Normal  ScalingReplicaSet  11m   deployment-controller  Scaled up replica set nginx-deployment-56d9f665d7 to 1
  Normal  ScalingReplicaSet  11m   deployment-controller  Scaled down replica set nginx-deployment-75b96b6748 to 2
  Normal  ScalingReplicaSet  11m   deployment-controller  Scaled up replica set nginx-deployment-56d9f665d7 to 2
  Normal  ScalingReplicaSet  11m   deployment-controller  Scaled down replica set nginx-deployment-75b96b6748 to 1
  Normal  ScalingReplicaSet  11m   deployment-controller  Scaled up replica set nginx-deployment-56d9f665d7 to 3
  Normal  ScalingReplicaSet  11m   deployment-controller  Scaled down replica set nginx-deployment-75b96b6748 to 0
[root@k8s-master-1 ~]# 

从desribe中可以看出,第一次创建时deployment时创建了可以名为nginx-deployment-75b96b6748的rs,直接扩容副本数为3
更新deployment时,创建了一个名为nginx-deployment-56d9f665d7的rs,副本数扩展为1。
之前的rs副本数降为2,这样至少有2个pod可用,最多创建了4个pod,以此类推。
使用相同的滚动更新策略向上和向下扩展新旧rs,最后保证新的rs 3个 pod,旧的pod为0

回滚deployment

创建 Deployment 的时候使用了–recored 参数可以记录命令,我们可以很方便的查看每次 revison 的变化。
假设又进行了几次更新,具体过程和上述一直,查看回滚历史:

[root@k8s-master-1 ~]# kubectl rollout history deployment/nginx-deployment -n deployment
deployment.apps/nginx-deployment 
REVISION  CHANGE-CAUSE
1         kubectl set image deployment nginx-deployment test-deployment=nginx:v1.9 --record=true --namespace=deployment
2         kubectl set image deployment nginx-deployment test-deployment=google/cadvisor:latest --record=true --namespace=deployment
查看此时pod的image如下:
[root@k8s-master-1 ~]# kubectl get rs -n deployment  -o wide
NAME                          DESIRED   CURRENT   READY   AGE   CONTAINERS        IMAGES                   SELECTOR
nginx-deployment-56d9f665d7   0         0         0       35m   test-deployment   nginx:v1.9               app=nginx,pod-template-hash=56d9f665d7
nginx-deployment-75b96b6748   0         0         0       65m   test-deployment   nginx:latest             app=nginx,pod-template-hash=75b96b6748
nginx-deployment-b78df489c    3         3         3       44s   test-deployment   google/cadvisor:latest   app=nginx,pod-template-hash=b78df489c

将deployment回到上一次版本,如下:

[root@k8s-master-1 ~]# kubectl rollout undo deployment/nginx-deployment -n deployment
deployment.apps/nginx-deployment rolled back
[root@k8s-master-1 ~]# kubectl get rs -n deployment  -o wide
NAME                          DESIRED   CURRENT   READY   AGE    CONTAINERS        IMAGES                   SELECTOR
nginx-deployment-56d9f665d7   3         3         2       40m    test-deployment   nginx:v1.9               app=nginx,pod-template-hash=56d9f665d7
nginx-deployment-75b96b6748   0         0         0       71m    test-deployment   nginx:latest             app=nginx,pod-template-hash=75b96b6748
nginx-deployment-b78df489c    1         1         1       6m6s   test-deployment   google/cadvisor:latest   app=nginx,pod-template-hash=b78df489c
[root@k8s-master-1 ~]# kubectl get rs -n deployment  -o wide
NAME                          DESIRED   CURRENT   READY   AGE     CONTAINERS        IMAGES                   SELECTOR
nginx-deployment-56d9f665d7   3         3         3       40m     test-deployment   nginx:v1.9               app=nginx,pod-template-hash=56d9f665d7
nginx-deployment-75b96b6748   0         0         0       71m     test-deployment   nginx:latest             app=nginx,pod-template-hash=75b96b6748
nginx-deployment-b78df489c    0         0         0       6m12s   test-deployment   google/cadvisor:latest   app=nginx,pod-template-hash=b78df489c
[root@k8s-master-1 ~]# 
如果要回退到指定版本,可以指定以下参数,如下:
kubectl rollout undo deployment/nginx-deployment -n deployment --to-revision=2
--to-revision=2指定回退到哪一个版本。

扩展deployment

[root@k8s-master-1 ~]# 
[root@k8s-master-1 ~]# kubectl scale deployment/nginx-deployment --replicas=5 -n deployment 
deployment.apps/nginx-deployment scaled
[root@k8s-master-1 ~]# 
查看pod如下,pod数量已经变成5个
[root@k8s-master-1 ~]# kubectl get po -n deployment 
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-56d9f665d7-442m7   1/1     Running   0          8m41s
nginx-deployment-56d9f665d7-7xln2   1/1     Running   0          8m43s
nginx-deployment-56d9f665d7-npf5l   1/1     Running   0          8m38s
nginx-deployment-56d9f665d7-p4sqm   1/1     Running   0          3s
nginx-deployment-56d9f665d7-rqwl2   1/1     Running   0          2s
[root@k8s-master-1 ~]# kubectl get rs -n deployment 
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-56d9f665d7   5         5         5       49m
nginx-deployment-75b96b6748   0         0         0       79m
nginx-deployment-b78df489c    0         0         0       14m
也可以使用以下方式,效果一样,如下:
[root@k8s-master-1 ~]# kubectl edit deployment/nginx-deployment  -n deployment

暂停和恢复deployment更新

做操作之前需要了解一下金丝雀发布

比如有一批新的pod资源创建完成后立即暂停更新过程,此时,仅存在一部分新版本的应用,主体部分还是旧的版本。然后,再筛选一小部分的用户请求路由到新的pod应用,继续观察能否稳定地按期望的方式运行。确定没问题之后再继续完成余下的pod资源滚动更新,否则立即回滚更新操作。这就是所谓的金丝雀发布。
[root@k8s-master-1 ~]# kubectl rollout pause deployment/nginx-deployment -n deployment 
deployment.apps/nginx-deployment paused
对nginx-deployment进行更新,如下:
[root@k8s-master-1 ~]# kubectl set image  deployment/nginx-deployment -n deployment test-deployment=barnybug/elasticsearch:1.7.2 --record=true
deployment.apps/nginx-deployment image updated
[root@k8s-master-1 ~]# 
[root@k8s-master-1 ~]# kubectl get rs -n deployment  -o wide
NAME                          DESIRED   CURRENT   READY   AGE   CONTAINERS        IMAGES                   SELECTOR
nginx-deployment-56d9f665d7   5         5         5       67m   test-deployment   nginx:v1.9               app=nginx,pod-template-hash=56d9f665d7
nginx-deployment-75b96b6748   0         0         0       98m   test-deployment   nginx:latest             app=nginx,pod-template-hash=75b96b6748
nginx-deployment-b78df489c    0         0         0       33m   test-deployment   google/cadvisor:latest   app=nginx,pod-template-hash=b78df489c
[root@k8s-master-1 ~]# kubectl rollout history deployment/nginx-deployment -n deployment 
deployment.apps/nginx-deployment 
REVISION  CHANGE-CAUSE
1         kubectl set image deployment nginx-deployment nginx=nginx:v1.9 --record=true --namespace=deployment
3         kubectl set image deployment nginx-deployment test-deployment=google/cadvisor:latest --record=true --namespace=deployment
4         kubectl set image deployment nginx-deployment test-deployment=nginx:v1.9 --record=true --namespace=deployment
以上可以看到deployment并没有进行更新。
对deployment进行恢复,如下:
[root@k8s-master-1 ~]# kubectl rollout resume  deployment/nginx-deployment -n deployment 
deployment.apps/nginx-deployment resumed
再次查看rs看到pod的image已经变为pause时设置的镜像,如下:
[root@k8s-master-1 ~]# kubectl get rs -n deployment  -o wide
NAME                          DESIRED   CURRENT   READY   AGE    CONTAINERS        IMAGES                         SELECTOR
nginx-deployment-56d9f665d7   2         3         3       71m    test-deployment   nginx:v1.9                     app=nginx,pod-template-hash=56d9f665d7
nginx-deployment-75b96b6748   0         0         0       101m   test-deployment   nginx:latest                   app=nginx,pod-template-hash=75b96b6748
nginx-deployment-b78df489c    0         0         0       36m    test-deployment   google/cadvisor:latest         app=nginx,pod-template-hash=b78df489c
nginx-deployment-c8688cb9c    4         4         3       4s     test-deployment   barnybug/elasticsearch:1.7.2   app=nginx,pod-template-hash=c8688cb9c
再次查看deployment的rollout已经有了新的更新,如下:
[root@k8s-master-1 ~]# kubectl rollout history  deployment/nginx-deployment -n deployment 
deployment.apps/nginx-deployment 
REVISION  CHANGE-CAUSE
1         kubectl set image deployment nginx-deployment nginx=nginx:v1.9 --record=true --namespace=deployment
3         kubectl set image deployment nginx-deployment test-deployment=google/cadvisor:latest --record=true --namespace=deployment
4         kubectl set image deployment nginx-deployment test-deployment=nginx:v1.9 --record=true --namespace=deployment
5         kubectl set image deployment/nginx-deployment test-deployment=barnybug/elasticsearch:1.7.2 --namespace=deployment --record=true

你可能感兴趣的:(k8s,k8s)