惜风不起、唯有努力!
ingress-nginx实现灰度发布

ingress-nginx实现灰度发布

PS:我们都知道发布方案有很多种如A/B,灰度等,具体细节原理可自行网上查阅资料,我不做过多描述。在k8s里有一种资源叫ingress,我们就通过ingress去实现灰度发布,流量切割。话不多说,入正题。

1、我分别用nginx和tomcat来模拟一个服务2个版本,先基于权重方法,下面是yaml文件。

nginx_all.yaml

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-dep
  namespace: web
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      terminationGracePeriodSeconds: 40

      containers:
      - name: nginx-images
        image: nginx
        imagePullPolicy: IfNotPresent
        env:
        - name: DEMO_GREETING
          value: "test"
        ports:
        - containerPort: 80
          name: app-port
          protocol: TCP
        resources:
          limits:
            cpu: 200m
            memory: 500Mi
          requests:
            cpu: 100m
            memory: 200Mi
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
  namespace: web
  labels:
    app: nginx
spec:
  selector:
    app: nginx
  #type: NodePort
  ports:
  - name: web
    port: 80
    targetPort: app-port
    #nodePort: 30002
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx
  namespace: web
spec:
  ingressClassName: nginx
  tls:
  - hosts:
    - nginx.yys.cn
    secretName: nginx-test-tls    #我这是https
  rules:
  - host: nginx.yys.cn
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-svc
            port:
              number: 80

tomcat_all.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-dep
  namespace: web
  labels:
    app: tomcat
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tomcat
  template:
    metadata:
      labels:
        app: tomcat
    spec:
      containers:
      - name: tomcat-images
        image: tomcat
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
          name: app-port
          protocol: TCP
        resources:
          limits:
            cpu: 200m
            memory: 500Mi
          requests:
            cpu: 50m
            memory: 50Mi
---
apiVersion: v1
kind: Service
metadata:
  name: tomcat-svc
  namespace: web
  labels:
    app: tomcat
spec:
  selector:
    app: tomcat
  #type: NodePort
  ports:
  - name: web
    port: 8080
    targetPort: app-port
    #nodePort: 30003
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: tomcat
  namespace: web
  annotations:
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-weight: "10"   #10%的请求转发到tomcat版本
spec:
  ingressClassName: nginx
  tls:
  - hosts:
    - nginx.yys.cn
    secretName: nginx-test-tls
  rules:
  - host: nginx.yys.cn
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: tomcat-svc
            port:
              number: 8080

测试

for i in {1..100}; do  curl https://nginx.yys.cn -k; done

2、基于用户请求方法,在新版本修改如下配置

  annotations:
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-by-header: "region"
    nginx.ingress.kubernetes.io/canary-by-header-value: "zhejiang"

下面是测试结果

发表回复

您的电子邮箱地址不会被公开。