https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#canary

k8s环境中,解决灰度等流量过滤的需求

规划

项目操作备注
green一般域名默认后端模拟正在使用的版本
blue新部署的项目模拟新版本版本,本实验切50%流量到它上面

本实验按照流量切换,随机流量;后续补充按照客户端/区域/ip段等来区分

1、green

cat nginx1.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-green
spec:
  replicas: 4
  selector:
    matchLabels:
      app: nginx-green
  template:
    metadata:
      labels:
        app: nginx-green
    spec:
      containers:
      - name: nginx-green
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
      volumes:
        - name: www
          configMap:
            name: nginx-green
---
apiVersion: v1
kind: ConfigMap
metadata:
    name: nginx-green
data:
  index.html: |
        green green green  -->  If you see  this message  --> The  route to green,bye ~~~;
 
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-green
  labels:
    app: nginx-green
spec:
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    name: http
  selector:
    app: nginx-green
---
apiVersion:  networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-green
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - host: nginx.yemaodiyi.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-green
            port:
              number: 80
2、blue

cat nginx2.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-blue
spec:
  replicas: 4
  selector:
    matchLabels:
      app: nginx-blue
  template:
    metadata:
      labels:
        app: nginx-blue
    spec:
      containers:
      - name: nginx-blue
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
      volumes:
        - name: www
          configMap:
            name: nginx-blue
---
apiVersion: v1
kind: ConfigMap
metadata:
    name: nginx-blue
data:
  index.html: |
        blue blue blue  --> If you see  this message  --> The  route to blue,bye ~~~;
 
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-blue
  labels:
    app: nginx-blue
spec:
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    name: http
  selector:
    app: nginx-blue
---
apiVersion:  networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-blue
  annotations:
    kubernetes.io/ingress.class: nginx     #指定ingress服务,无意义,可不写
    nginx.ingress.kubernetes.io/canary: "true"    #打开金丝雀开关
    nginx.ingress.kubernetes.io/canary-weight: "50"   #权重,按照50%的流量区分
spec:
  rules:
  - host: nginx.yemaodiyi.com    #和上面一样的域名
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-blue     #请求转到什么后端
            port:
              number: 80
3、验证,请求不是很平均,因为访问次数不够,这里不纠结
for i in `seq 20`;do curl nginx.yemaodiyi.com;done 


green green green  -->  If you see  this message  --> The  route to green,bye ~~~;
blue blue blue  --> If you see  this message  --> The  route to blue,bye ~~~;
blue blue blue  --> If you see  this message  --> The  route to blue,bye ~~~;
green green green  -->  If you see  this message  --> The  route to green,bye ~~~;
green green green  -->  If you see  this message  --> The  route to green,bye ~~~;
green green green  -->  If you see  this message  --> The  route to green,bye ~~~;
blue blue blue  --> If you see  this message  --> The  route to blue,bye ~~~;
green green green  -->  If you see  this message  --> The  route to green,bye ~~~;
green green green  -->  If you see  this message  --> The  route to green,bye ~~~;
blue blue blue  --> If you see  this message  --> The  route to blue,bye ~~~;
blue blue blue  --> If you see  this message  --> The  route to blue,bye ~~~;
blue blue blue  --> If you see  this message  --> The  route to blue,bye ~~~;
green green green  -->  If you see  this message  --> The  route to green,bye ~~~;
green green green  -->  If you see  this message  --> The  route to green,bye ~~~;
green green green  -->  If you see  this message  --> The  route to green,bye ~~~;
green green green  -->  If you see  this message  --> The  route to green,bye ~~~;
blue blue blue  --> If you see  this message  --> The  route to blue,bye ~~~;
blue blue blue  --> If you see  this message  --> The  route to blue,bye ~~~;
green green green  -->  If you see  this message  --> The  route to green,bye ~~~;
green green green  -->  If you see  this message  --> The  route to green,bye ~~~;

注意这里权重不是一个精确的百分比,使用过程当中,只是会看到一个近似分布。

Logo

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

更多推荐