1.Deployment的升级

    1.1 我们创建nginx-deployment的pod


#1.创建yaml文件
[root@k8s-master k8s-yaml]# cat nginx-deployment.yaml 
apiVersion : apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

#2.创建pod
[root@k8s-master k8s-yaml]# kubectl create -f nginx-deployment.yaml 
deployment.apps/nginx-deployment created
#3.查看pod
[root@k8s-master k8s-yaml]# kubectl get po -o wide
NAME                              READY   STATUS    RESTARTS   AGE    IP              NODE         NOMINATED NODE   READINESS GATES
nginx-deployment-f7599d4c-2lj8v   1/1     Running   0          81s    172.18.58.194   k8s-node02   <none>           <none>
nginx-deployment-f7599d4c-z2d9d   1/1     Running   0          7m1s   172.18.85.194   k8s-node01   <none>           <none>

1.2 升级nginx至1.9.1

    kubectl set image deployment/nginx- deployment nginx=nginx:1.9.1

    或者kubectl edit deployment/nginx-deployment   将Nginx:1.7.9更改为nginx:1.9.1
   我们采取第一种方法来演示:

[root@k8s-master k8s-yaml]# kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
deployment.apps/nginx-deployment image updated
[root@k8s-master k8s-yaml]# kubectl get po 
NAME                               READY   STATUS              RESTARTS   AGE
nginx-deployment-95df64745-dpdn7   0/1     ContainerCreating   0          7s
nginx-deployment-f7599d4c-rwf58    1/1     Running             0          106m
nginx-deployment-f7599d4c-s4mfb    1/1     Running             0          106m
[root@k8s-master k8s-yaml]# kubectl describe po nginx-deployment-95df64745-dpdn7 |grep Image
    Image:          nginx:1.9.1
    Image ID:       
[root@k8s-master k8s-yaml]# kubectl get po -o wide
NAME                               READY   STATUS        RESTARTS   AGE    IP              NODE         NOMINATED NODE   READINESS GATES
nginx-deployment-95df64745-dpdn7   1/1     Running       0          119s   172.18.58.195   k8s-node02   <none>           <none>
nginx-deployment-95df64745-xs26x   1/1     Running       0          61s    172.18.85.197   k8s-node01   <none>           <none>
nginx-deployment-f7599d4c-s4mfb    1/1     Terminating   0          108m   172.18.85.196   k8s-node01   <none>           <none>
#
[root@k8s-master k8s-yaml]# kubectl get replicasets.apps 
NAME                         DESIRED   CURRENT   READY   AGE
nginx-deployment-95df64745   2         2         2       12m
nginx-deployment-f7599d4c    0         0         0       144m

我们可以看到nginx的镜像已经升级为1.9.1了

初始创建deployment的时候, 系统创建了一个ReplicaSet:nginx-deployment-f7599d4c,并按照用户的需求创建了2个副本,更新deployment的时候,系统创建新的ReplicaSet:nginx-deployment-95df64745,并将其副本数量扩充到1,旧的ReplicaSet副本缩减到1,在将新的扩充到2,旧的缩减到0

2.deployment的回滚

        kubectl rollout undo deployment/nginx-deployment

[root@k8s-master k8s-yaml]# kubectl rollout undo deployment/nginx-deployment 
deployment.apps/nginx-deployment rolled back
[root@k8s-master k8s-yaml]# kubectl get po 
NAME                               READY   STATUS        RESTARTS   AGE
nginx-deployment-95df64745-dpdn7   1/1     Terminating   0          35m
nginx-deployment-f7599d4c-2wlxc    1/1     Running       0          9s
nginx-deployment-f7599d4c-kzdwp    1/1     Running       0          7s
[root@k8s-master k8s-yaml]# kubectl describe po nginx-deployment-f7599d4c-2wlxc |grep Image
    Image:          nginx:1.7.9
    Image ID:       sha256:35d28df486f6150fa3174367499d1eb01f22f5a410afe4b9581ac0e0e58b3eaf

目前来说Deployment中通过spec.strategy指定pod的更新策略有两种:Rereate和RollingUpdate,默认值为RollingUpdate,Rereate(重建)指的是先杀掉pod,在创建新pod。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐