其他分享
首页 > 其他分享> > kubernetes 之 Rolling Update 滚动升级

kubernetes 之 Rolling Update 滚动升级

作者:互联网

滚动升级

1、错误的yml文件

[machangwei@mcwk8s-master ~]$ cat mcwHttpd.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcw-httpd
spec:
  replicas: 3
  selector:
    matchLabels:
     run: mcw-httpd
  template:
    metadata:
      labels:
        run: mcw-httpd
    spec:
      containers:
        - name: mcw-httpd
          image: httpd
          ports:
          - containerPort: 80
[machangwei@mcwk8s-master ~]$ vim mcwHttpd.yml
[machangwei@mcwk8s-master ~]$ cat mcwHttpd.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcw-httpd
spec:
  replicas: 3
  selector:
    matchLabels:
     run: mcw-httpd
  template:
    metadata:
      labels:
        run: mcw-httpd
    spec:
      containers:
        - name: mcw-httpd
          image: :httpd:2.2.31
          ports:
          - containerPort: 80
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwHttpd.yml
deployment.apps/mcw-httpd created
[machangwei@mcwk8s-master ~]$ kubectl get deployment httpd -o wide
Error from server (NotFound): deployments.apps "httpd" not found
[machangwei@mcwk8s-master ~]$ kubectl get deployment -o wide
NAME        READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES          SELECTOR
mcw-httpd   0/3     3            0           33s   mcw-httpd    :httpd:2.2.31   run=mcw-httpd
[machangwei@mcwk8s-master ~]$ kubectl get pod
NAME                         READY   STATUS             RESTARTS   AGE
mcw-httpd-6644b55f8c-62dvc   0/1     InvalidImageName   0          98s
mcw-httpd-6644b55f8c-h8pfn   0/1     InvalidImageName   0          98s
mcw-httpd-6644b55f8c-xn259   0/1     InvalidImageName   0          98s
[machangwei@mcwk8s-master ~]$ 

2、修改为正确的yml文件,部署deployment

[machangwei@mcwk8s-master ~]$ vim mcwHttpd.yml 
[machangwei@mcwk8s-master ~]$ cat mcwHttpd.yml  #其它跟以前的没变,只是镜像后面加了冒号和数字,也就是添加了版本
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcw-httpd
spec:
  replicas: 3
  selector:
    matchLabels:
     run: mcw-httpd
  template:
    metadata:
      labels:
        run: mcw-httpd
    spec:
      containers:
        - name: mcw-httpd
          image: httpd:2.2.31
          ports:
          - containerPort: 80
[machangwei@mcwk8s-master ~]$ kubectl delete -f mcwHttpd.yml #把以前的删除
deployment.apps "mcw-httpd" deleted
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwHttpd.yml #部署deployment
deployment.apps/mcw-httpd created
[machangwei@mcwk8s-master ~]$ kubectl get deployment mcw-httpd -o wide  #查看镜像是31的镜像
NAME        READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
mcw-httpd   3/3     3            3           13s   mcw-httpd    httpd:2.2.31   run=mcw-httpd
[machangwei@mcwk8s-master ~]$ kubectl get replicaset  -o wide #查看副本集这里,也是31的镜像
NAME                   DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES         SELECTOR
mcw-httpd-6f64bbcfcf   3         3         3       40s   mcw-httpd    httpd:2.2.31   pod-template-hash=6f64bbcfcf,run=mcw-httpd
[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide  #查看pod部署情况
NAME                         READY   STATUS    RESTARTS   AGE   IP           NODE           NOMINATED NODE   READINESS GATES
mcw-httpd-6f64bbcfcf-5nhxc   1/1     Running   0          70s   10.244.2.9   mcwk8s-node2   <none>           <none>
mcw-httpd-6f64bbcfcf-s8hng   1/1     Running   0          70s   10.244.1.7   mcwk8s-node1   <none>           <none>
mcw-httpd-6f64bbcfcf-t6j2f   1/1     Running   0          70s   10.244.2.8   mcwk8s-node2   <none>           <none>

3、修改镜像版本号,重新部署,滚动升级

[machangwei@mcwk8s-master ~]$ vim mcwHttpd.yml 
[machangwei@mcwk8s-master ~]$ cat mcwHttpd.yml #修改版本号为:2.2.32
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcw-httpd
spec:
  replicas: 3
  selector:
    matchLabels:
     run: mcw-httpd
  template:
    metadata:
      labels:
        run: mcw-httpd
    spec:
      containers:
        - name: mcw-httpd
          image: httpd:2.2.32
          ports:
          - containerPort: 80
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwHttpd.yml #重新部署
deployment.apps/mcw-httpd configured
[machangwei@mcwk8s-master ~]$ kubectl get deployment mcw-httpd -o wide  #查看deployment镜像已经修改,更新了一个好像是
NAME        READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES         SELECTOR
mcw-httpd   3/3     1            3           6m58s   mcw-httpd    httpd:2.2.32   run=mcw-httpd
[machangwei@mcwk8s-master ~]$ kubectl get replicaset  -o wide #查看副本集,32是新建的副本集,镜像改为32了。这里还没有升级完,32镜像只有一个是ready,也就是刚刚升级完一个
NAME                   DESIRED   CURRENT   READY   AGE    CONTAINERS   IMAGES         SELECTOR
mcw-httpd-6f64bbcfcf   2         2         2       7m5s   mcw-httpd    httpd:2.2.31   pod-template-hash=6f64bbcfcf,run=mcw-httpd
mcw-httpd-7c977464db   2         2         1       10s    mcw-httpd    httpd:2.2.32   pod-template-hash=7c977464db,run=mcw-httpd
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide #查看pod。根据年龄查看,刚刚生成三个新的,旧的还有一个没有被删除
NAME                         READY   STATUS        RESTARTS   AGE     IP            NODE           NOMINATED NODE   READINESS GATES
mcw-httpd-6f64bbcfcf-s8hng   1/1     Terminating   0          7m16s   10.244.1.7    mcwk8s-node1   <none>           <none>
mcw-httpd-7c977464db-5ctkr   1/1     Running       0          21s     10.244.1.8    mcwk8s-node1   <none>           <none>
mcw-httpd-7c977464db-bfz6t   1/1     Running       0          10s     10.244.1.9    mcwk8s-node1   <none>           <none>
mcw-httpd-7c977464db-gp94c   1/1     Running       0          12s     10.244.2.10   mcwk8s-node2   <none>           <none>
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl get deployment mcw-httpd -o wide  #再次查看,已经升级完三个了
NAME        READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES         SELECTOR
mcw-httpd   3/3     3            3           7m36s   mcw-httpd    httpd:2.2.32   run=mcw-httpd
[machangwei@mcwk8s-master ~]$ kubectl get replicaset  -o wide  #再次查看副本集,三个副本都已经被新生成副本代替了,
#新生成的副本是 32的镜像。而且旧的副本集副本个数为0了,但是它不会删除过了九分钟都还没有删除,应该是不删的
NAME                   DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES         SELECTOR
mcw-httpd-6f64bbcfcf   0         0         0       7m47s   mcw-httpd    httpd:2.2.31   pod-template-hash=6f64bbcfcf,run=mcw-httpd
mcw-httpd-7c977464db   3         3         3       52s     mcw-httpd    httpd:2.2.32   pod-template-hash=7c977464db,run=mcw-httpd
[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide #查看pod,已经只有最新生成的镜像是32版本的pod了
NAME                         READY   STATUS    RESTARTS   AGE   IP            NODE           NOMINATED NODE   READINESS GATES
mcw-httpd-7c977464db-5ctkr   1/1     Running   0          57s   10.244.1.8    mcwk8s-node1   <none>           <none>
mcw-httpd-7c977464db-bfz6t   1/1     Running   0          46s   10.244.1.9    mcwk8s-node1   <none>           <none>
mcw-httpd-7c977464db-gp94c   1/1     Running   0          48s   10.244.2.10   mcwk8s-node2   <none>           <none>

4、查看滚动升级详情

[machangwei@mcwk8s-master ~]$ kubectl get replicaset  -o wide #查看副本集
NAME                   DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES         SELECTOR
mcw-httpd-6f64bbcfcf   0         0         0       21m   mcw-httpd    httpd:2.2.31   pod-template-hash=6f64bbcfcf,run=mcw-httpd
mcw-httpd-7c977464db   3         3         3       14m   mcw-httpd    httpd:2.2.32   pod-template-hash=7c977
[machangwei@mcwk8s-master ~]$ kubectl get deployment -o wide
NAME        READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
mcw-httpd   3/3     3            3           19m   mcw-httpd    httpd:2.2.32   run=mcw-httpd
[machangwei@mcwk8s-master ~]$ kubectl describe deployment mcw-httpd
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable #副本三个被更新的
 Image:        httpd:2.2.32  容器镜像是32

Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  19m   deployment-controller  Scaled up replica set mcw-httpd-6f64bbcfcf to 3
  Normal  ScalingReplicaSet  12m   deployment-controller  Scaled up replica set mcw-httpd-7c977464db to 1
  Normal  ScalingReplicaSet  12m   deployment-controller  Scaled down replica set mcw-httpd-6f64bbcfcf to 2
  Normal  ScalingReplicaSet  12m   deployment-controller  Scaled up replica set mcw-httpd-7c977464db to 2
  Normal  ScalingReplicaSet  12m   deployment-controller  Scaled down replica set mcw-httpd-6f64bbcfcf to 1
  Normal  ScalingReplicaSet  12m   deployment-controller  Scaled up replica set mcw-httpd-7c977464db to 3
  Normal  ScalingReplicaSet  12m   deployment-controller  Scaled down replica set mcw-httpd-6f64bbcfcf to 0

 每只更新替换一个pod:
mcw-httpd-6f64bbcfcf to 3  旧副本集是3个pod
mcw-httpd-7c977464db to 1  新副本集增加一个pod,总数为1
mcw-httpd-6f64bbcfcf to 2  旧副本集减少一个pod,总数为2
mcw-httpd-7c977464db to 2  新副本集增加一个pod,总数为2
mcw-httpd-6f64bbcfcf to 1  旧副本集减少一个pod,总数为1
mcw-httpd-7c977464db to 3  新副本集增加一个pod,总数为3
mcw-httpd-6f64bbcfcf to 0  旧副本集减少一个pod,总数为0


每次替换的POD数量是可以定制的。默认应该是1吧。定制的参数有两个,如:maxSurge ,maxUnavailable 来精细控制POD的替换数量

回滚

1、回滚前的模拟准备环境

模拟新建三个需要的版本。
[root@mcwk8s-node2 ~]$ docker tag httpd:latest  httpd:2.4.16
[root@mcwk8s-node2 ~]$ docker tag httpd:latest  httpd:2.4.17
[root@mcwk8s-node2 ~]$ docker tag httpd:latest  httpd:2.4.18
创文件文件,并修改版本
[machangwei@mcwk8s-master ~]$ cp mcwHttpd.yml mcw.httpd.v1.yml
[machangwei@mcwk8s-master ~]$ cp mcwHttpd.yml mcw.httpd.v2.yml
[machangwei@mcwk8s-master ~]$ cp mcwHttpd.yml mcw.httpd.v3.yml

修改一下文件,将镜像分别修改为  16,17,18
[machangwei@mcwk8s-master ~]$ ls mcw.httpd.v1
mcw.httpd.v16.yml  mcw.httpd.v17.yml  mcw.httpd.v18.yml  
[machangwei@mcwk8s-master ~]$ cat mcw.httpd.v16.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcw-httpd
spec:
  replicas: 3
  selector:
    matchLabels:
     run: mcw-httpd
  template:
    metadata:
      labels:
        run: mcw-httpd
    spec:
      containers:
        - name: mcw-httpd
          image: httpd:2.4.16
          ports:
          - containerPort: 80

2、部署并回滚

[machangwei@mcwk8s-master ~]$ ls mcw.httpd.v1 #查看所有yml配置文件
mcw.httpd.v16.yml  mcw.httpd.v17.yml  mcw.httpd.v18.yml  
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcw.httpd.v16.yml --record   #--record,将当前命令记录到revision记录中
Flag --record has been deprecated, --record will be removed in the future  #这样我们就能知道每个revison对应的是哪个配置文件了
deployment.apps/mcw-httpd created
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl get deployment mcw-httpd -o wide
NAME        READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS   IMAGES         SELECTOR
mcw-httpd   3/3     3            3           2m3s   mcw-httpd    httpd:2.4.16   run=mcw-httpd
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcw.httpd.v17.yml --record 
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/mcw-httpd configured
[machangwei@mcwk8s-master ~]$ kubectl get deployment mcw-httpd -o wide  #每次执行都被更新镜像版本了
NAME        READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES         SELECTOR
mcw-httpd   3/3     3            3           2m35s   mcw-httpd    httpd:2.4.17   run=mcw-httpd
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcw.httpd.v18.yml --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/mcw-httpd configured
[machangwei@mcwk8s-master ~]$ kubectl get deployment mcw-httpd -o wide
NAME        READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES         SELECTOR
mcw-httpd   3/3     2            3           2m54s   mcw-httpd    httpd:2.4.18   run=mcw-httpd
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl rollout history deployment mcw-httpd #--record,将当前命令记录到revision记录中
deployment.apps/mcw-httpd 
REVISION  CHANGE-CAUSE    #这样我们就能知道每个revison对应的是哪个配置文件了  #查看revison历史记录
1         kubectl apply --filename=mcw.httpd.v16.yml --record=true
2         kubectl apply --filename=mcw.httpd.v17.yml --record=true
3         kubectl apply --filename=mcw.httpd.v18.yml --record=true

[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl rollout undo deployment mcw-httpd --to-revision=1 #执行命令回滚到版本1。
deployment.apps/mcw-httpd rolled back
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl get deployment mcw-httpd -o wide #查看镜像已经改成版本1的了
NAME        READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES         SELECTOR
mcw-httpd   3/3     3            3           4m39s   mcw-httpd    httpd:2.4.16   run=mcw-httpd
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl rollout history deployment mcw-httpd #查看revison历史记录的变化

deployment.apps/mcw-httpd 
REVISION  CHANGE-CAUSE
2         kubectl apply --filename=mcw.httpd.v17.yml --record=true
3         kubectl apply --filename=mcw.httpd.v18.yml --record=true 
4         kubectl apply --filename=mcw.httpd.v16.yml --record=true #revison 1 变成了revison4,我们可以通过CHANG-CAUSE
#知道每个revison的具体含义,所以要在执行kubectl 加上参数 --record
[machangwei@mcwk8s-master ~]$ 

 

标签:httpd,kubernetes,kubectl,machangwei,Update,mcw,master,mcwk8s,Rolling
来源: https://www.cnblogs.com/machangwei-8/p/15832266.html