部署一个新的应用用于新的发布,并通过 Ingress 能力实现灰度发布。

实现目的

  • 通过 Ingress 按权重进行灰度发布
  • 通过 Ingress 按 Header 进行灰度发布

灰度及蓝绿发布是为新版本创建一个与老版本完全一致的生产环境,在不影响老版本的前提下,按照一定的规则把部分流量切换到新版本,当新版本试运行一段时间没有问题后,将用户的全量流量从老版本迁移至新版本。

部署应用

##部署 Deployment V2 应用
创建如下 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 应用:

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

执行如下命令部署 Ingress 资源

kubectl apply -f app-v1.yaml

在这里插入图片描述

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

kubectl apply -f ingress-v1.yaml

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

curl my-app-3616b99d966d3ce142a285add342a06b.ca2ef983cc3a64042a32c1a423d32021e.cn-shanghai.alicontainer.com

返回如下
在这里插入图片描述

部署 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

执行如下命令部署 Ingress 资源

kubectl apply -f app-v2.yaml

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

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

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-app-canary
  labels:
    app: my-app
  annotations:
    # 启用金丝雀并将10%的流量发送到版本2
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-weight: "10"
spec:
  rules:
  - host: my-app-3616b99d966d3ce142a285add342a06b.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-3616b99d966d3ce142a285add342a06b.ca2ef983cc3a64042a32c1a423d32021e.cn-shanghai.alicontainer.com; done

在这里插入图片描述

按照 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-3616b99d966d3ce142a285add342a06b.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-3616b99d966d3ce142a285add342a06b.ca2ef983cc3a64042a32c1a423d32021e.cn-shanghai.alicontainer.com
curl my-app-3616b99d966d3ce142a285add342a06b.ca2ef983cc3a64042a32c1a423d32021e.cn-shanghai.alicontainer.com -H "x-app-canary: true"

在这里插入图片描述
在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐