通过 Ingress 进行灰度发布

1、部署 Deployment V1 应用

创建如下 YAML 文件(app-v1.yaml)

apiVersion: v1
kind: Service
metadata:
  name: my-app-v1
  labels:
    app: my-app
spec:
  ports:
  - name: http
    port: 80
    targetPort: http
  selector:
    app: my-app
    version: v1.0.0
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-v1
  labels:
    app: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
      version: v1.0.0
  template:
    metadata:
      labels:
        app: my-app
        version: v1.0.0
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "9101"
    spec:
      containers:
      - name: my-app
        image: registry.cn-hangzhou.aliyuncs.com/containerdemo/containersol-k8s-deployment-strategies
        ports:
        - name: http
          containerPort: 8080
        - name: probe
          containerPort: 8086
        env:
        - name: VERSION
          value: v1.0.0
        livenessProbe:
          httpGet:
            path: /live
            port: probe
          initialDelaySeconds: 5
          periodSeconds: 5
        readinessProbe:
          httpGet:
            path: /ready
            port: probe
          periodSeconds: 5

执行如下命令部署 Deployement V1 应用:

kubectl apply -f app-v1.yaml

创建如下 Ingress YAML文件(ingress-v1.yaml)

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-app
  labels:
    app: my-app
spec:
  rules:
  - host: my-app-22e6541b324bd1dfe04a48beb40046ea.ca2ef983cc3a64042a32c1a423d32021e.cn-shanghai.alicontainer.com
    http:
      paths:
      - backend:
          serviceName: my-app-v1
          servicePort: 80
        path: /

执行如下命令部署 Ingress 资源

kubectl apply -f ingress-v1.yaml

部署完成后通过 curl 命令进行测试:

curl my-app-22e6541b324bd1dfe04a48beb40046ea.ca2ef983cc3a64042a32c1a423d32021e.cn-shanghai.alicontainer.com

会看到如下返回:

Host: my-app-v1-68bfcbb7cf-w4rpg, Version: v1.0.0

2、部署 Deployment V2 应用

创建如下 YAML 文件(app-v2.yaml)

apiVersion: v1
kind: Service
metadata:
  name: my-app-v2
  labels:
    app: my-app
spec:
  ports:
  - name: http
    port: 80
    targetPort: http
  selector:
    app: my-app
    version: v2.0.0
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-v2
  labels:
    app: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
      version: v2.0.0
  template:
    metadata:
      labels:
        app: my-app
        version: v2.0.0
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "9101"
    spec:
      containers:
      - name: my-app
        image: registry.cn-hangzhou.aliyuncs.com/containerdemo/containersol-k8s-deployment-strategies
        ports:
        - name: http
          containerPort: 8080
        - name: probe
          containerPort: 8086
        env:
        - name: VERSION
          value: v2.0.0
        livenessProbe:
          httpGet:
            path: /live
            port: probe
          initialDelaySeconds: 5
          periodSeconds: 5
        readinessProbe:
          httpGet:
            path: /ready
            port: probe
          periodSeconds: 5

执行如下命令部署 Deployement V2 应用:

kubectl apply -f app-v2.yaml

3、按照权重策略灰度到 Deployment V2 应用

创建如下 Ingress YAML文件(ingress-v2-canary-weigth.yaml)

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-app-canary
  labels:
    app: my-app
  annotations:
    # Enable canary and send 10% of traffic to version 2
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-weight: "10"
spec:
  rules:
  - host: my-app-22e6541b324bd1dfe04a48beb40046ea.ca2ef983cc3a64042a32c1a423d32021e.cn-shanghai.alicontainer.com
    http:
      paths:
      - backend:
          serviceName: my-app-v2
          servicePort: 80
        path: /

执行如下命令部署 Ingress 资源

kubectl apply -f ingress-v2-canary-weigth.yaml

执行如下命令进行测试:

while sleep 0.1;do curl my-app-22e6541b324bd1dfe04a48beb40046ea.ca2ef983cc3a64042a32c1a423d32021e.cn-shanghai.alicontainer.com; done

测试结果如下:

Host: my-app-v1-68bfcbb7cf-w4rpg, Version: v1.0.0
Host: my-app-v1-68bfcbb7cf-w4rpg, Version: v1.0.0
Host: my-app-v1-68bfcbb7cf-w4rpg, Version: v1.0.0
Host: my-app-v1-68bfcbb7cf-w4rpg, Version: v1.0.0
Host: my-app-v1-68bfcbb7cf-w4rpg, Version: v1.0.0
Host: my-app-v1-68bfcbb7cf-w4rpg, Version: v1.0.0
Host: my-app-v1-68bfcbb7cf-w4rpg, Version: v1.0.0
Host: my-app-v1-68bfcbb7cf-w4rpg, Version: v1.0.0
Host: my-app-v1-68bfcbb7cf-w4rpg, Version: v1.0.0
Host: my-app-v2-67c69b8857-g82gr, Version: v2.0.0
Host: my-app-v1-68bfcbb7cf-w4rpg, Version: v1.0.0

4、按照 Header 策略灰度到 Deployment V2 应用

创建如下 Ingress YAML文件(ingress-v2-canary-header.yaml)

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-app-canary
  labels:
    app: my-app
  annotations:
    # Enable canary and send traffic with headder x-app-canary to version 2
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-by-header: "x-app-canary"
    nginx.ingress.kubernetes.io/canary-by-header-value: "true"
spec:
  rules:
  - host: my-app-22e6541b324bd1dfe04a48beb40046ea.ca2ef983cc3a64042a32c1a423d32021e.cn-shanghai.alicontainer.com
    http:
      paths:
      - backend:
          serviceName: my-app-v2
          servicePort: 80

执行如下命令部署 Ingress 资源

kubectl apply -f ingress-v2-canary-header.yaml

通过 curl 命令对应用进行测试:

curl my-app-22e6541b324bd1dfe04a48beb40046ea.ca2ef983cc3a64042a32c1a423d32021e.cn-shanghai.alicontainer.com

结果如下:

目前还是环境在v1

Host: my-app-v1-68bfcbb7cf-w4rpg, Version: v1.0.0

执行如下命令环境全部灰度发布到v2

curl my-app-22e6541b324bd1dfe04a48beb40046ea.ca2ef983cc3a64042a32c1a423d32021e.cn-shanghai.alicontainer.com -H "x-app-canary: true"

结果如下

Host: my-app-v2-67c69b8857-g82gr, Version: v2.0.0

你可能感兴趣的:(阿里云,kubernetes,容器,docker)