一、方案:使用ingress实现应用的灰度发布
说明:此灰度发布方法不适用于利用网关转发服务的架构,利用网关转发的架构,需在网关实现灰度。
灰度流程:
1、服务端:版本v1正常运行,发布版本v2
2、客户端:带有请求头Headers标识key=version,value=v2的请求访问服务端版本v2,其他的请求访问服务端版本v1
3、待服务端版本v2稳定后,所有请求切换至服务端版本v2,停止服务端版本v1
二、操作步骤
1、创建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.2 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
kubectl apply -f nginx-v1.yml
2、创建nginx服务v1版本ingress
nginx-v1-ingress.yml
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
kubectl apply -f nginx-v1-ingress.yml
3、postman调用test.nginx.com
返回信息v1,说明所有请求都正常访问v1版本
4、创建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.14.2 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
kubectl apply -f nginx-v2.yml
5、创建nginx服务v2版本ingress,添加请求头version=2访问
nginx-v2-ingress.yml
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
kubectl apply -f nginx-v2-ingress.yml
6、postman调用test.nginx.com
加了请求头version=v2的请求,返回信息v2,说明请求访问的是v2版本
没加请求头version=v2的请求,返回v1,说明其他请求访问的还是v1版本
7、切换版本,使所有请求都访问v2版本
编辑v1版本的ingress,name: nginx-v1-svc改为nginx: nginx-v2-svc
kubectl edit ingres nginx-v1
8、验证:postman调用test.nginx.com,返回信息v2,说明所有请求访问的都是版本v2
没加请求头信息
加请求头信息:version=v2
9、删除v1版本服务deployment,service,v2版本ingress
kubectl delete deploy nginx-v1 kubectl delete svc nginx-v1-svc kubectl delete ingress nginx-v2
标签:ingress,name,v1,nginx,v2,灰度,版本,k8s From: https://www.cnblogs.com/chenlin2022/p/16742604.html