K8S Nginx Ingress实现金丝雀发布

一、部署蓝环境版本服务

1、ConfigMap

kind: ConfigMap
apiVersion: v1
metadata:
  name: nginx-blue-config
data:
  nginx.conf: |-
    worker_processes  1;

    events {
        accept_mutex on;
        multi_accept on;
        use epoll;
        worker_connections  1024;
    }

    http {
        ignore_invalid_headers off;
        server {
            listen 80;
            location / {
                access_by_lua '
                    local header_str = ngx.say("blue")
                ';
            }
        }
    }

2、Deployment

kind: Deployment
apiVersion: apps/v1
metadata:
  name: nginx-blue
  labels:
    dce.daocloud.io/app: nginx-blue
  annotations:
    dce.daocloud.io/last-replicas: '1'
    deployment.kubernetes.io/revision: '3'
    kubernetes.io/change-cause: update YAML
spec:
  replicas: 1
  selector:
    matchLabels:
      dce.daocloud.io/component: nginx-blue
  template:
    metadata:
      name: nginx-blue
      labels:
        dce.daocloud.io/app: nginx-blue
        dce.daocloud.io/component: nginx-blue
      annotations:
        dce.daocloud.io/parcel.egress.burst: '0'
        dce.daocloud.io/parcel.egress.rate: '0'
        dce.daocloud.io/parcel.ingress.burst: '0'
        dce.daocloud.io/parcel.ingress.rate: '0'
        dce.daocloud.io/parcel.net.type: calico
    spec:
      volumes:
        - name: nginx-blue-config
          configMap:
            name: nginx-blue-config
            defaultMode: 420
      containers:
        - name: nginx-blue
          image: 'x.x.x.x/library/openresty:1.19.9.1-sw-r4'
          resources:
            limits:
              cpu: 500m
              memory: '314572800'
            requests:
              cpu: 200m
              memory: '314572800'
          volumeMounts:
            - name: nginx-blue-config
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf

3、Service

kind: Service
apiVersion: v1
metadata:
  name: nginx-blue-default
  labels:
    dce.daocloud.io/app: nginx-blue
  annotations:
    io.daocloud.dce.serviceSelectorType: service
spec:
  ports:
    - name: nginx-nginx-default-80680-80
      protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 31046
  selector:
    dce.daocloud.io/component: nginx-blue
  clusterIP: 172.31.69.137
  type: NodePort
  sessionAffinity: None
  externalTrafficPolicy: Cluster

二、部署绿环境版本服务

1、ConfigMap

kind: ConfigMap
apiVersion: v1
metadata:
  name: nginx-green-config
data:
  nginx.conf: |-
    worker_processes  1;

    events {
        accept_mutex on;
        multi_accept on;
        use epoll;
        worker_connections  1024;
    }

    http {
        ignore_invalid_headers off;
        server {
            listen 80;
            location / {
                access_by_lua '
                    local header_str = ngx.say("green")
                ';
            }
        }
    }

2、Deployment

kind: Deployment
apiVersion: apps/v1
metadata:
  name: nginx-green
  labels:
    dce.daocloud.io/app: nginx-green
  annotations:
    deployment.kubernetes.io/revision: '5'
    kubernetes.io/change-cause: update YAML
spec:
  replicas: 1
  selector:
    matchLabels:
      dce.daocloud.io/component: nginx-green
  template:
    metadata:
      name: nginx-green
      labels:
        dce.daocloud.io/app: nginx-green
        dce.daocloud.io/component: nginx-green
        env: green
      annotations:
        dce.daocloud.io/parcel.egress.burst: '0'
        dce.daocloud.io/parcel.egress.rate: '0'
        dce.daocloud.io/parcel.ingress.burst: '0'
        dce.daocloud.io/parcel.ingress.rate: '0'
        dce.daocloud.io/parcel.net.type: calico
        dce.daocloud.io/parcel.net.value: default-ipv4-ippool
    spec:
      volumes:
        - name: nginx-green-config
          configMap:
            name: nginx-green-config
            defaultMode: 420
      containers:
        - name: nginx-green
          image: 'x.x.x.x/library/openresty:1.19.9.1-sw-r4'
          resources:
            limits:
              cpu: 500m
              memory: '314572800'
            requests:
              cpu: 200m
              memory: '314572800'
          volumeMounts:
            - name: nginx-green-config
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf

3、Service

kind: Service
apiVersion: v1
metadata:
  name: nginx-green-default
  labels:
    dce.daocloud.io/app: nginx-green
  annotations:
    io.daocloud.dce.serviceSelectorType: service
spec:
  ports:
    - name: nginx-nginx-default-15833-80
      protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 35218
  selector:
    dce.daocloud.io/component: nginx-green
  clusterIP: 172.31.207.22
  type: NodePort
  sessionAffinity: None
  externalTrafficPolicy: Cluster

三、设置Ingress

1、blue环境Ingress

kind: Ingress
apiVersion: networking.k8s.io/v1beta1
metadata:
  name: nginx-blue-ingress
  labels:
    dce.daocloud.io/app: nginx-blue
  annotations:
    nginx.ingress.kubernetes.io/use-port-in-redirects: 'true'
spec:
  rules:
    - host: nginx.ms-sit.xxxxxx.net
      http:
        paths:
          - path: /
            pathType: ImplementationSpecific
            backend:
              serviceName: nginx-blue-default
              servicePort: 80

2、green环境Ingress

kind: Ingress
apiVersion: networking.k8s.io/v1beta1
metadata:
  name: nginx-green-ingress
  labels:
    dce.daocloud.io/app: nginx-green
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/canary: 'true'
    nginx.ingress.kubernetes.io/canary-by-header: env
    nginx.ingress.kubernetes.io/canary-by-header-pattern: green
spec:
  rules:
    - host: nginx.ms-sit.xxxxxx.net
      http:
        paths:
          - path: /
            pathType: ImplementationSpecific
            backend:
              serviceName: nginx-green-default
              servicePort: 80

四、测试

K8S Nginx Ingress实现金丝雀发布_第1张图片

 

K8S Nginx Ingress实现金丝雀发布_第2张图片

 

你可能感兴趣的:(K8S,kubernetes,nginx)