kubectl更新镜像和回滚命令

使用kubernetes 进行升级的时候并不需要停止业务,kubectl 支持滚动升级的方式,每次更新一个pod,而不是同时删除整个服务。

准备实验素材

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-nginx
spec:
  selector:
    matchLabels:
      name: nginx
  replicas: 3
  template:
    metadata:
      labels:
        name: nginx
    spec:
      containers:
        - name: hello-nginx
          image: nginx:1.7.9
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
kubectl apply -f hello-nginx.yml --record

更新镜像的语法

可以不用使用yml配置文件, 直接替换镜像版本

# kubectl set image deployment  <deploymentName> <containerName>=<image>

 kubectl --kubeconfig config-lego-test  set image deployment/hello-nginx hello-nginx=nginx:1.9.2

我们可以通过设置docker:lastTag的方式, 回滚到上一个镜像. 但k8s本身也支持版本记录和回滚.

查看发布历史

[root@localhost .kube]# kubectl --kubeconfig config-my  rollout history deployment hello-nginx
deployment.extensions/hello-nginx 
REVISION  CHANGE-CAUSE
1         kubectl apply --kubeconfig=config-lego-test --filename=hello-nginx.yml --record=true
2         kubectl apply --kubeconfig=config-lego-test --filename=hello-nginx.yml --record=true
3         kubectl apply --kubeconfig=config-lego-test --filename=hello-nginx.yml --record=true
4         kubectl apply --kubeconfig=config-lego-test --filename=hello-nginx.yml --record=true

[root@localhost .kube]# kubectl --kubeconfig config-my  rollout history deployment hello-nginx --revision=4
deployment.extensions/hello-nginx with revision #4
Pod Template:
  Labels:       app=hello-nginx
        pod-template-hash=6d5c95fcc5
  Annotations:  kubernetes.io/change-cause: kubectl apply --kubeconfig=config-lego-test --filename=hello-nginx.yml --record=true
  Containers:
   hello-nginx:
    Image:      nginx:1.9.2
    Port:       8080/TCP
    Host Port:  0/TCP
    Requests:
      cpu:      250m
      memory:   512Mi
    Environment:        <none>
    Mounts:     <none>
  Volumes:      <none>


回滚到上一个版本

比如, 刚才查看了最近的几条发布历史记录, 现在回滚到上一个版本, 即revision=3.

[root@localhost .kube]# kubectl --kubeconfig config-lego-test  rollout undo deployment hello-nginx
deployment.extensions/hello-nginx rolled back

[root@localhost .kube]# 
[root@localhost .kube]# kubectl --kubeconfig config-lego-test  rollout history deployment hello-nginx
deployment.extensions/hello-nginx 
REVISION  CHANGE-CAUSE
1         kubectl apply --kubeconfig=config-lego-test --filename=hello-nginx.yml --record=true
2         kubectl apply --kubeconfig=config-lego-test --filename=hello-nginx.yml --record=true
4         kubectl apply --kubeconfig=config-lego-test --filename=hello-nginx.yml --record=true
5         kubectl apply --kubeconfig=config-lego-test --filename=hello-nginx.yml --record=true

可以发现, revision=3没了, 多个了一个revison=5. describe一下, 可以看到, 确实是第3个版本的镜像.

回滚指定版本

除了上面直接回滚到上一次, 也可以指定具体某个版本. 比如回滚到revision=1.

 kubectl rollout undo deployment/hello-nginx --to-revision=1

ps: deployment/hello-nginx 表示 deployment hello-nginx

查看回滚状态

[root@localhost .kube]# kubectl --kubeconfig config-lego-test  rollout status deployment hello-nginx       
deployment "hello-nginx" successfully rolled out

参考

posted @ 2020-06-11 14:49  Ryan.Miao  阅读(8004)  评论(0编辑  收藏  举报