【学习笔记07】每天5分钟,玩转kubernetes-07(Rolling Update)

第七章: Rolling Update

7.1 实践

修改deployment中app image版本

vim  httpd.yml 
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: httpd
spec:
  replicas: 3
  template:
    metadata:
      labels:
        run: httpd
    spec:
      containers:
      - name: httpd
        image: httpd:2.2.31
        ports:
        - containerPort: 80

查看效果
kubectl describe deployment httpd

$kubectl apply -f httpd.yml 

$kubectl get pods -o wide
NAME                                  READY   STATUS              RESTARTS   AGE    IP             NODE                 NOMINATED NODE   READINESS GATES
httpd-69cb5b9fdd-crtfm                1/1     Running             0          101m   10.244.3.118   k8s-node-122132073              
httpd-69cb5b9fdd-k9xbv                1/1     Running             0          101m   10.244.4.35    k8s-node-122132072              
httpd-6fdbf8b54-cnhdj                 1/1     Running             0          34s    10.244.4.39    k8s-node-122132072              
httpd-6fdbf8b54-kgv2h                 0/1     ContainerCreating   0          19s             k8s-node-122132073              

$ kubectl describe deployment httpd
NewReplicaSet:   httpd-6fdbf8b54 (3/3 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  97s   deployment-controller  Scaled up replica set httpd-6fdbf8b54 to 1
  Normal  ScalingReplicaSet  82s   deployment-controller  Scaled down replica set httpd-69cb5b9fdd to 2
  Normal  ScalingReplicaSet  82s   deployment-controller  Scaled up replica set httpd-6fdbf8b54 to 2
  Normal  ScalingReplicaSet  55s   deployment-controller  Scaled down replica set httpd-69cb5b9fdd to 1
  Normal  ScalingReplicaSet  55s   deployment-controller  Scaled up replica set httpd-6fdbf8b54 to 3
  Normal  ScalingReplicaSet  52s   deployment-controller  Scaled down replica set httpd-69cb5b9fdd to 0

7.2 回滚

布署时使用--record,然后就可以用--to-revision进行回滚。

httpd.v1.yml ~ httpd.v3.yml
三个文件的app image版本不同

vim httpd.v1.yml 
kind: Deployment
metadata:
  name: httpd
spec:
  revisionHistoryLimit: 10
  replicas: 3
  template:
    metadata:
      labels:
        run: httpd
    spec:
      containers:
      - name: httpd
        image: httpd:2.4.16
        ports:
        - containerPort: 80

布署服务

kubectl apply -f httpd.v1.yml --record       # record的作用是将命令记录到revision记录中。
deployment.apps/httpd configured

$kubectl get deployment httpd -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS   IMAGES         SELECTOR
httpd   3/3     1            3           113m   httpd        httpd:2.4.16   run=httpd

$kubectl apply -f httpd.v2.yml --record    
deployment.apps/httpd configured
$kubectl get deployment httpd -o wide     
NAME    READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS   IMAGES         SELECTOR
httpd   3/3     1            3           113m   httpd        httpd:2.4.17   run=httpd

$ kubectl apply -f httpd.v3.yml --record 
$ kubectl get deployment httpd -o wide     
NAME    READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS   IMAGES         SELECTOR
httpd   3/3     1            3           116m   httpd        httpd:2.4.18   run=httpd

测试回滚

# 查看版本
$kubectl rollout history deployment 
kubectl rollout history deployment 
deployment.extensions/httpd 
REVISION  CHANGE-CAUSE
3         kubectl apply --filename=httpd.v1.yml --record=true
4         kubectl apply --filename=httpd.v2.yml --record=true
5         kubectl apply --filename=httpd.v3.yml --record=true

deployment.extensions/kubernetes-bootcamp 
REVISION  CHANGE-CAUSE
1         
2         

$kubectl describe deployment httpd
    Image:        httpd:2.4.18

#回滚
$kubectl rollout undo deployment httpd --to-revision=3 
deployment.extensions/httpd rolled back

#检查效果 
$kubectl describe deployment httpd
    Image:        httpd:2.4.16

参考:

  • 【目录】每天5分钟,玩转kubernetes
  • https://kubernetes.io/zh/docs/tutorials/
  • https://www.kubernetes.org.cn/k8s
  • https://jimmysong.io/kubernetes-handbook/practice/service-rolling-update.html

你可能感兴趣的:(【学习笔记07】每天5分钟,玩转kubernetes-07(Rolling Update))