K8s实现最简单灰度发布

2020-06-13 09:42:32 浏览数 (2)

灰度发布又名金丝雀部署,是让部分用户访问到新版本应用,在 Kubernetes 中,可以使用两个具有相同 Pod 标签的 Deployment 来实现金丝雀部署。新版本的副本和旧版本的一起发布。在一段时间后如果没有检测到错误,则可以扩展新版本的副本数量并删除旧版本的应用。

v1版本

代码语言:javascript复制
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: tomcat-test
  name: tomcat-test-v1
  namespace: test
spec:
  minReadySeconds: 100
  replicas: 4
  revisionHistoryLimit: 5
  selector:
    matchLabels:
      app: tomcat-test
      version: v1
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      annotations:
        initializer.kubernetes.io/lxcfs: 'true'
      labels:
        app: tomcat-test
        version: v1
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - tomcat-test
            topologyKey: kubernetes.io/hostname
      containers:
      - env:
        - name: HOST_IP
          valueFrom:
            fieldRef:
              fieldPath: status.hostIP
        - name: POD_NAME
          value: tomcat-test
        - name: version
          value: v1
        image: vnet01-harbor.sy.cn/test/tomcat-test:r-20200214_175535
        imagePullPolicy: Always
        livenessProbe:
          httpGet:
            path: /abc/check_health.jsp
            port: 6080
          initialDelaySeconds: 80
          timeoutSeconds: 20
        name: tomcat-test
        readinessProbe:
          httpGet:
            path: /abc/check_health.jsp
            port: 6080
          initialDelaySeconds: 80
          timeoutSeconds: 20
        resources:
          limits:
            cpu: '2'
            memory: 4096M
          requests:
            cpu: '1'
            memory: 2048M
        volumeMounts:
        - mountPath: /data/logs
          name: app-log
          readOnly: false
      nodeSelector:
        apptype: memnode
      terminationGracePeriodSeconds: 60
      volumes:
      - hostPath:
          path: /var/lib/docker/logs/tomcat-test
        name: app-log

v2版本

代码语言:javascript复制
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: tomcat-test
  name: tomcat-test-v2
  namespace: test
spec:
  minReadySeconds: 100
  replicas: 1
  revisionHistoryLimit: 5
  selector:
    matchLabels:
      app: tomcat-test
      version: v2
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      annotations:
        initializer.kubernetes.io/lxcfs: 'true'
      labels:
        app: tomcat-test
        version: v2
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - tomcat-test
            topologyKey: kubernetes.io/hostname
      containers:
      - env:
        - name: HOST_IP
          valueFrom:
            fieldRef:
              fieldPath: status.hostIP
        - name: POD_NAME
          value: tomcat-test
        - name: version
          value: v2
        image: vnet01-harbor.sy.cn/test/tomcat-test:r-20200319_101302
        imagePullPolicy: Always
        livenessProbe:
          httpGet:
            path: /abc/check_health.jsp
            port: 6080
          initialDelaySeconds: 80
          timeoutSeconds: 20
        name: tomcat-test
        readinessProbe:
          httpGet:
            path: /abc/check_health.jsp
            port: 6080
          initialDelaySeconds: 80
          timeoutSeconds: 20
        resources:
          limits:
            cpu: '2'
            memory: 4096M
          requests:
            cpu: '1'
            memory: 2048M
        volumeMounts:
        - mountPath: /data/logs
          name: app-log
          readOnly: false
      nodeSelector:
        apptype: memnode
      terminationGracePeriodSeconds: 60
      volumes:
      - hostPath:
          path: /var/lib/docker/logs/tomcat-test
        name: app-log

流量比就是4:1

0 人点赞