• 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
    

    参考

  • 相关阅读:
    JS保留两位小数
    xml,json,html格式化工具
    SQL查询递归
    javascript验证价格输入
    类库说明文档生成器
    转换数据库时间字段格式函数
    mysql python 汉字乱码的解决方案
    Python list 按元素属性排序, key 的用法
    原以为会不同的
    libsvm 文本分类(分两类)(实验结果)直接调用Libsvm工具
  • 原文地址:https://www.cnblogs.com/woshimrf/p/13093212.html
Copyright © 2020-2023  润新知