实战K8S滚动更新和回滚

发布相应的服务

kind: Deployment
metadata:
  name: omni-slice-adapter-test
  namespace: sit-srmp
spec:
  selector:
    matchLabels:
      app: omni-slice-adapter-test
  replicas: 1
  revisionHistoryLimit: 1
  minReadySeconds: 10
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: omni-slice-adapter-test
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/yayaw/slice-adapter:v0.1
        envFrom:
        - configMapRef:
            name: sit-srmp
        name: omni-slice-adapter-test
        imagePullPolicy: IfNotPresent
        command: ["java"]
        args: ["-jar", "-Dfile.encoding=UTF-8", "slice-adapter.jar", "--server.port=8081", "--spring.profiles.active=sit"]
        ports:
        - containerPort: 8081

minReadySeconds: 10  # 这里需要估一个比较合理的值,从容器启动到应用正常提供服务
strategy:  # k8s 默认的 strategy 就是 RollingUpdate, 这里写明出来可以调节细节参数
maxSurge: 1  # 更新时允许最大激增的容器数,默认 replicas 的 1/4 向上取整
maxUnavailable: 0  # 更新时允许最大 unavailable 容器数,默认 replicas 的 1/4 向下取整

回滚服务

获取要回滚的历史版本
kubectl rollout history  deployment/omni-slice-adapter-test -n sit-srmp

查看版本的详细信息
kubectl rollout history  deployment/omni-slice-adapter-test -n sit-srmp --revision=3
回滚指定版本
kubectl rollout undo deployment/omni-slice-adapter-test -n sit-srmp --to-revision=2

回滚上一个版本
kubectl rollout undo deployment/omni-slice-adapter-test -n sit-srmp