滚动升级回滚

滚动升级回滚

ReplicationController

资源文件

apiVersion: v1
kind: ReplicationController
metadata:
  name: kubia-v1
  labels:
    app: kubia
spec:
  replicas: 3
  template:
    metadata:
      name: kubia
      labels:
        app: kubia
    spec:
      containers:
        - image: luksa/kubia:v1
          name: nodejes
---
apiVersion: v1
kind: Service
metadata:
  name: kubia
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 8080
  selector:
    app: kubia

apply资源到K8S

kubectl create namespace update-rollback

kubectl apply -f kubia-v1.yaml -n update-rollback

通过暴露端口访问资源

curl http://暴露地址

执行滚动升级指令

kubectl rolling-update kubia-v1 kubia-v2 --image=luksa/kubia:v2 -n update-rollback

Deployment

资源文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kubia
  labels:
    app: kubia
spec:
  replicas: 3
  selector:
    matchLabels:
      app: kubia
  template:
    metadata:
      name: kubia
      labels:
        app: kubia
    spec:
      containers:
        - image: luksa/kubia:v1
          name: nodejes
---
apiVersion: v1
kind: Service
metadata:
  name: kubia
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 8080
  selector:
    app: kubia

apply资源到K8S

kubectl create namespace update-rollback

kubectl apply -f kubia.yaml -n update-rollback

通过暴露端口访问资源

curl http://暴露地址

修改镜像版本触发滚动升级到v2

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kubia
  labels:
    app: kubia
spec:
  replicas: 3
  minReadySeconds: 10
  selector:
    matchLabels:
      app: kubia
  template:
    metadata:
      name: kubia
      labels:
        app: kubia
    spec:
      containers:
        - image: luksa/kubia:v2
          name: nodejes
---
apiVersion: v1
kind: Service
metadata:
  name: kubia
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 8080
  selector:
    app: kubia
  • minReadySeconds:表示新创建pod至少要成功运行多久之后才能视为可用

  • 通过strategy属性可以设置升级的方式。默认为滚动升级(RollingUpdate),另一种为删除所有旧Pod再创建新pod(Recreate)

    • maxSurge:允许超出期望副本数的百分比的pod数量。也可以直接指定个数,默认值为1

    • maxUnavailable:允许不可用pod占期望副本数的百分比。也可以直接指定个数,默认值为1

spec:
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate

修改镜像版本触发滚动升级到v3

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kubia
  labels:
    app: kubia
spec:
  replicas: 3
  minReadySeconds: 10
  selector:
    matchLabels:
      app: kubia
  template:
    metadata:
      name: kubia
      labels:
        app: kubia
    spec:
      containers:
        - image: luksa/kubia:v3
          name: nodejes
---
apiVersion: v1
kind: Service
metadata:
  name: kubia
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 8080
  selector:
    app: kubia

查看历史版本

kubectl rollout history deployment kubia -n update-rollback

查历史版本指定版本pod template信息

kubectl rollout history deployment kubia --revision=2 -n update-rollback

回滚到上一个版本

kubectl rollout undo deployment kubia -n update-rollback
  • rollout命令适用于deployments、daemonsets、statefulsets

回滚到指定版本

kubectl rollout undo deployment kubia --to-revision=1 -n update-rollback

限制历史版本数量

通过revisionHistoryLimit可以限制历史版本的数量,默认为10

spec:
  revisionHistoryLimit: 10

暂停滚动升级

kubectl rollout pause deployment kubia -n update-rollback

恢复滚动升级

kubectl rollout resume deployment kubia -n update-rollback

使用就绪探针阻止问题版本滚动升级

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kubia
  labels:
    app: kubia
spec:
  replicas: 3
  minReadySeconds: 10
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  selector:
    matchLabels:
      app: kubia
  template:
    metadata:
      name: kubia
      labels:
        app: kubia
    spec:
      containers:
        - image: luksa/kubia:v3
          name: nodejes
          readinessProbe:
            periodSeconds: 1
            httpGet:
              path: /
              port: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: kubia
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 8080
  selector:
    app: kubia
  • maxUnavailable为0,maxSurge为1:保证pod一个一个被替换
  • 就绪探针检测pod是否正真可访问

查看升级状态

查看升级状态可以发现升级流程一直没有继续下去

kubectl rollout status deployment kubia -n update-rollback

查看pod情况

查看pod情况可以看到pod一直没到就绪状态,应该就绪探针检测到异常

kubectl get pods -n update-rollback

取消出错版本滚动升级

kubectl rollout undo deployment kubia -n update-rollback

Helm

查看Release列表

helm list -n aim-upgrade-test2

查看Release历史版本

helm history devops-wuvreb-register -n aim-upgrade-test2

回滚Release到历史版本

回滚到指定版本

helm rollback devops-wuvreb-register 2 -n aim-upgrade-test2

在这里插入图片描述

查看release状态

helm status devops-wuvreb-register --show-desc -n aim-upgrade-test2

你可能感兴趣的:(Kubernetes实践,Kubernetes实践)