k8s使用ingress实现应用的灰度发布升级

v1是1.14.0版本nginx ,实操时候升级到v2是1.20.0版本nginx,来测试灰度发布实现过程

一、方案:使用ingress实现应用的灰度发布

1、服务端:正常版本v1,灰度升级版本v2

2、客户端:带有请求头version=v2标识的请求访问版本v2,其他的请求访问版本v1

3、待版本v2稳定后,所有请求切换至版本v2,停止版本v1(删除原deployment,service,ingress)

二、操作步骤

1、创建版本v1的deployment、service、ingress

nginx服务版本v1的deployment和service

nginx-v1.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-v1
spec:
  selector:
    matchLabels:
      app: nginx-v1
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-v1
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.0
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: file
      volumes:
      - name: file
        hostPath:
          path: /data/nginx-v1
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-v1-svc  
  labels:
    app: nginx-v1   
spec:
  type: ClusterIP  
  selector:
    app: nginx-v1
  ports:
  - port: 80
    targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-v1
spec:
  ingressClassName: nginx
  rules:
  - host: test.nginx.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-v1-svc 
            port:
              number: 80

验证:apifox 调用test.nginx.com,当前所有请求都正常访问版本v1,即1.14版本nginx

k8s使用ingress实现应用的灰度发布升级_第1张图片

2、创建版本v2的deployment、service、ingress

nginx服务版本v2的deployment、service

nginx-v2.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-v2
spec:
  selector:
    matchLabels:
      app: nginx-v2
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-v2
    spec:
      containers:
      - name: nginx
        image: nginx:1.20.0
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: file
      volumes:
      - name: file
        hostPath:
          path: /data/nginx-v2
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-v2-svc  
  labels:
    app: nginx-v2   
spec:
  type: ClusterIP  
  selector:
    app: nginx-v2
  ports:
  - port: 80
    targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-v2
  annotations:
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-by-header: "version"  #请求头key=version
    nginx.ingress.kubernetes.io/canary-by-header-value: "v2"  #请求头value=v2
spec:
  ingressClassName: nginx
  rules:
  - host: test.nginx.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-v2-svc 
            port:
              number: 80

nginx服务版本v2的ingress,匹配请求头version=2访问

验证:postman调用test.nginx.com,加了请求头version=v2的请求访问版本v2,即1.20版nginx,其他请求访问版本v1

k8s使用ingress实现应用的灰度发布升级_第2张图片

k8s使用ingress实现应用的灰度发布升级_第3张图片k8s使用ingress实现应用的灰度发布升级_第4张图片k8s使用ingress实现应用的灰度发布升级_第5张图片k8s使用ingress实现应用的灰度发布升级_第6张图片

你可能感兴趣的:(kubernetes,容器,云原生)