Kubernetes(k8s)的滚动更新和回滚

时间秒杀一切 提交于 2020-02-06 00:50:44

1.部署httpd:2.2.31的版本

# vi httpd.yml

apiVersion: apps/v1
kind: Deployment
metadata: 
 name: httpd
spec:
 selector:
  matchLabels:
   run: httpd_web
 replicas: 2
 template:
  metadata:
   labels:
    run: httpd_web
  spec:
   containers:
   - name: httpd
     image: httpd:2.2.31
     ports:
     - containerPort: 80


# kubectl apply -f httpd.yml

2.查看httpd:2.2.31的部署信息

# kubectl get deployment -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
httpd   2/2     2            2           66m   httpd        httpd:2.2.31   run=httpd_web


# kubectl get replicaset -o wide
NAME               DESIRED   CURRENT   READY   AGE    CONTAINERS   IMAGES         SELECTOR
httpd-656f47889c   2         2         2       3h5m   httpd        httpd:2.2.31   pod-template-hash=656f47889c,run=httpd_web


# kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE    IP            NODE       NOMINATED NODE   READINESS GATES
httpd-656f47889c-bpjrv   1/1     Running   0          3h6m   10.244.1.70   computer   <none>           <none>
httpd-656f47889c-jqlwd   1/1     Running   0          3h6m   10.244.2.41   storage    <none>           <none>

 3.滚动更新,从httpd:2.2.31到部署httpd:2.2.32的版本

# vi httpdv2.yml

apiVersion: apps/v1
kind: Deployment
metadata: 
 name: httpd
spec:
 selector:
  matchLabels:
   run: httpd_web
 replicas: 2
 template:
  metadata:
   labels:
    run: httpd_web
  spec:
   containers:
   - name: httpd
     image: httpd:2.2.32
     ports:
     - containerPort: 80


# kubectl apply -f httpdv2.yml 
deployment.apps/httpd configured

4. 查看httpd:2.2.32的部署信息

# kubectl get deployment -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES         SELECTOR
httpd   2/2     1            2           3h27m   httpd        httpd:2.2.32   run=httpd_web


# # kubectl get replicaset -o wide
NAME               DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES         SELECTOR
httpd-58966f4976   2         2         2       29m     httpd        httpd:2.2.32   pod-template-hash=58966f4976,run=httpd_web
httpd-656f47889c   0         0         0       3h55m   httpd        httpd:2.2.31   pod-template-hash=656f47889c,run=httpd_web

# kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP            NODE       NOMINATED NODE   READINESS GATES
httpd-58966f4976-b74h4   1/1     Running   0          29m   10.244.1.71   computer   <none>           <none>
httpd-58966f4976-c5qdn   1/1     Running   0          15m   10.244.2.42   storage    <none>           <none>

5.回滚

5.1默认配置下,kubernetes只会保留最近几个revision,可以通过revisionHistoryLimit属性增加revision的数量

5.2部署httpd2.4.16、httpd2.4.17、httpd2.4.18

# kubectl delete deployment --all     #删除之前部署的httpd
deployment.apps "httpd" deleted


# vi httpd.v1.yml

apiVersion: apps/v1
kind: Deployment
metadata: 
 name: httpd
spec:
 selector:
  matchLabels:
   run: httpd_web
 revisionHistoryLimit: 10
 replicas: 2
 template:
  metadata:
   labels:
    run: httpd_web
  spec:
   containers:
   - name: httpd
     image: httpd:2.4.16
     ports:
     - containerPort: 80


#以下是部署并将当前命令记录到revision的记录中
# kubectl apply -f httpd.v1.yml --record
deployment.apps/httpd created
# vi httpd.v2.yml

apiVersion: apps/v1
kind: Deployment
metadata: 
 name: httpd
spec:
 selector:
  matchLabels:
   run: httpd_web
 revisionHistoryLimit: 10
 replicas: 2
 template:
  metadata:
   labels:
    run: httpd_web
  spec:
   containers:
   - name: httpd
     image: httpd:2.4.17
     ports:
     - containerPort: 80


#以下是部署并将当前命令记录到revision的记录中
# kubectl apply -f httpd.v2.yml --record
deployment.apps/httpd configured
# vi httpd.v3.yml

apiVersion: apps/v1
kind: Deployment
metadata: 
 name: httpd
spec:
 selector:
  matchLabels:
   run: httpd_web
 revisionHistoryLimit: 10
 replicas: 2
 template:
  metadata:
   labels:
    run: httpd_web
  spec:
   containers:
   - name: httpd
     image: httpd:2.4.18
     ports:
     - containerPort: 80


#以下是部署并将当前命令记录到revision的记录中
# kubectl apply -f httpd.v3.yml --record
deployment.apps/httpd configured
#以下是查看部署的历史记录
# kubectl rollout history deployment 
deployment.apps/httpd 
REVISION  CHANGE-CAUSE
1         kubectl apply --filename=httpd.v1.yml --record=true
2         kubectl apply --filename=httpd.v2.yml --record=true
3         kubectl apply --filename=httpd.v3.yml --record=true

5.3回滚到httpd:2.4.17版本

#以下是查看现在的版本
# kubectl get deployment -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
httpd   2/2     2            2           17m   httpd        httpd:2.4.18   run=httpd_web

#以下是回滚版本到httpd:2.4.17
# kubectl rollout undo deployment httpd --to-revision=2
deployment.apps/httpd rolled back

#以下是查看回滚后的版本
# kubectl get deployment -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
httpd   2/2     1            2           20m   httpd        httpd:2.4.17   run=httpd_web

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!