灰度nginx怎么搭建 nginx 灰度测试规则_java

nginx.ingress.kubernetes.io/canary-by-header
基1于Header的流量切分,适用于灰度发布。如果请求头中包含指定的header名称,并且值为“always”,就将该请求转发给Canary Ingress定义的对应后端服务。如果值为“never”则不转发,可用于回滚到旧版本。如果为其他值则忽略该annotation,并通过优先级将请求流量分配到其他规则。

nginx.ingress.kubernetes.io/canary-by-header-value
必须与canary-by-header一起使用,可自定义请求头的取值,包含但不限于“always”或“never”。当请求头的值命中指定的自定义值时,请求将会转发给Canary Ingress定义的对应后端服务,如果是其他值则忽略该annotation,并通过优先级将请求流量分配到其他规则。

nginx.ingress.kubernetes.io/canary-by-header-pattern
与8canary-by-header-value类似,唯一区别是该annotation用正则表达式匹配请求头的值,而不是某一个固定值。如果该annotation与canary-by-header-value同时存在,该annotation将被忽略。

nginx.ingress.kubernetes.io/canary-by-cookie
基于Cookie的流量切分,适用于灰度发布。与canary-by-header类似,该annotation用于cookie,仅支持“always”和“never”,无法自定义取值。

nginx.ingress.kubernetes.io/canary-weight
基于服务权重的流量切分,适用于蓝绿部署。表示Canary Ingress所分配流量的百分比,取值范围[0-100]。例如,设置为100,表示所有流量都将转发给Canary Ingress对应的后端服务。
金丝雀规则按优先顺序进行如下排序:canary-by-header - > canary-by-cookie - > canary-weight
本次演示的为基于权重灰度
cat <<END>old.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: weball-v2
  labels:
    app: weball
spec:
  replicas: 1
  selector:
    matchLabels:
      app: weball
  template:
    metadata:
      labels:
        app: weball
    spec:
      containers:
      - name: weball
        image: registry.cn-shenzhen.aliyuncs.com/lolos/loloss:weball-00
        ports:
        - containerPort: 8282
---
apiVersion: v1
kind: Service
metadata:
  name: weball-v2
  labels:
    app: weball
spec:
  type: NodePort
  ports:
    - name: new
      port: 8282
      targetPort: 8282
      nodePort: 28282
  selector:
    app: weball
END
cat <<END> new.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: weball-v1
  labels:
    app: weball
spec:
  replicas: 1
  selector:
    matchLabels:
      app: weball
  template:
    metadata:
      labels:
        app: weball
    spec:
      containers:
      - name: weball
        image: registry.cn-shenzhen.aliyuncs.com/lolos/loloss:weball-00
        ports:
        - containerPort: 8181
---
apiVersion: v1
kind: Service
metadata:
  name: weball-v1
  labels:
    app: weball
spec:
  type: NodePort
  ports:
    - name: new
      port: 8181
      targetPort: 8181
      nodePort: 28181
  selector:
    app: weball
END
列如部署一个老的服务
cat <<END>old-release.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress 
metadata: 
  name: old-release 
  annotations: 
    kubernetes.io/ingress.class: nginx 
    kubernetes.io/elb.port: '80' 
spec: 
  rules: 
    - host: jb.jbjb.com 
      http: 
        paths: 
          - path: '/' 
            pathType: Prefix
            backend: 

              service:
                name: weball-v2  
                port: 
                  number: 8282
END
访问
curl -s jb.jbjb.com | grep Server
Old Old Old Server<<2222222
仅允许20%的流量被转发到新版本服务中,实现灰度发布
cat <<END>new-canary.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress 
metadata: 
  name: new-canary-release
  annotations: 
    kubernetes.io/ingress.class: nginx 
    nginx.ingress.kubernetes.io/canary: "true"      # 启用Canary   
    nginx.ingress.kubernetes.io/canary-weight: "20"   # 将20%的流量转发到Canary Ingress 
    kubernetes.io/elb.port: '80' 
spec: 
  rules: 
    - host: jb.jbjb.com
      http: 
        paths: 
          - path: '/' 
            pathType: Prefix
            backend: 
              service:
                name: weball-v1    
                port:
                  number: 8181
END

灰度nginx怎么搭建 nginx 灰度测试规则_java_02

只有少量请求进入new的服务

灰度nginx怎么搭建 nginx 灰度测试规则_nginx_03


灰度nginx怎么搭建 nginx 灰度测试规则_java_04

再尝试将所有流量打入新服务

nginx.ingress.kubernetes.io/canary-weight: "100"    # 所有流量均转发到Canary Ingress

灰度nginx怎么搭建 nginx 灰度测试规则_nginx_05

所有的访问均由新版本服务响应,成功实现了蓝绿发布

监控可以使用官网提供的方案我的是用yaml安装所以添加几个参数即可

apiVersion: v1
kind: Service
metadata:
 annotations:
   prometheus.io/scrape: "true"
   prometheus.io/port: "10254"
..
spec:
  ports:
    - name: prometheus
      port: 10254
      targetPort: prometheus
      ..
部署以下两个服务就不废话了
[root@node66 test-go]# kubectl get pods -n istio-system | grep -E "grafana|prometheus"
grafana-6ccd56f4b6-wg7lt               1/1     Running   0          118m
prometheus-6555cf8b7d-8kggv            2/2     Running   0          51m

灰度nginx怎么搭建 nginx 灰度测试规则_运维_06


官网链接

https://kubernetes.github.io/ingress-nginx/user-guide/monitoring/