This topic describes how to release the latest version of a service by using the Ingress function provided by Alibaba Cloud Container Service for Kubernetes.

Prerequisites

Procedure

  1. Log on to the Container Service console.
  2. In the left-side navigation pane under Kubernetes, choose Application > Deployment.
  3. In the upper-right corner, click Create by Template.


  4. Select the target cluster and namespace, select a sample template or customize a template, and then click DEPLOY.


    Deploy an Nginx application of the latest version that contains the required deployment, the target service, and an Ingress. The orchestration template that contains the deployment and service is as follows:
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: new-nginx
    spec:
      replicas: 1
      selector:
        matchLabels:
          run: new-nginx
      template:
        metadata:
          labels:
            run: new-nginx
        spec:
          containers:
          - image: registry.cn-hangzhou.aliyuncs.com/xianlu/new-nginx
            imagePullPolicy: Always
            name: new-nginx
            ports:
            - containerPort: 80
              protocol: TCP
          restartPolicy: Always
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: new-nginx
    spec:
      ports:
      - port: 80
        protocol: TCP
        targetPort: 80
      selector:
        run: new-nginx
      sessionAffinity: None
      type: NodePort
    The following are Ingress orchestration templates of different annotation settings:
    Note If you do not set service-match or service-weight in the annotations field of an Ingress template, the Ingress controller forwards client requests evenly to the latest and earlier services in a random manner.
    • Ingress template used to specify only the client requests that meet the requirement of the regular expression foo=bar to be routed to the latest version of the service

      apiVersion: extensions/v1beta1
      kind: Ingress
      metadata:
        name: gray-release
        annotations:
            nginx.ingress.kubernetes.io/service-match: |      # Only if the request header of a request meets the requirements of the regular expression foo=bar, can the request be routed to the new-nginx service.
                new-nginx: header("foo", /^bar$/)
      spec:
        rules:
        - host: www.example.com
          http:
            paths:
            # Earlier version of the service
            - path: /
              backend:
                serviceName: old-nginx
                servicePort: 80
            # Latest version of the service
            - path: /
              backend:
                serviceName: new-nginx
                servicePort: 80
    • Ingress template used to specify the proportion of requests that can be routed to the latest version of the service
      Note In this example, the latest version of the service and the earlier version version of the service are weighted at 50% each.
      apiVersion: extensions/v1beta1
      kind: Ingress
      metadata:
        name: gray-release
        annotations:
            nginx.ingress.kubernetes.io/service-weight: |    # Set 50% of traffic to be routed to the new-nginx service.
                new-nginx: 50, old-nginx: 50
      spec:
        rules:
        - host: www.example.com
          http:
            paths:
            # Earlier version of the service
            - path: /
              backend:
                serviceName: old-nginx
                servicePort: 80
            # Latest version of the service
            - path: /
              backend:
                serviceName: new-nginx
                servicePort: 80
    • Ingress template used to specify that only 50% of the client request traffic that meets the requirements of foo=bar will be routed the latest version of the service

      apiVersion: extensions/v1beta1
      kind: Ingress
      metadata:
        name: gray-release
        annotations:
          nginx.ingress.kubernetes.io/service-match: |   # Only if the request header of a request meets the requirements of the regular expression foo=bar, can the request be routed to the new-nginx service.
              new-nginx: header("foo", /^bar$/)
          nginx.ingress.kubernetes.io/service-weight: |  # Only 50% of the client request traffic that meets the requirements of the preceding matching rule can be routed to the new-nginx service.
              new-nginx: 50, old-nginx: 50
      spec:
        rules:
        - host: www.example.com
          http:
            paths:
            # Earlier version of the service
            - path: /
              backend:
                serviceName: old-nginx
                servicePort: 80
            # Latest version of the service
            - path: /
              backend:
                serviceName: new-nginx
                servicePort: 80
  5. In the left-side navigation pane, choose Application > Ingress.

    You can see that the virtual host name points to old-nginx.



  6. Log on to the Master node and run the following curl commands to view the Ingress access of the following settings:
    • Only the client requests that meet the requirements of the regular expression foo=bar can be routed to the latest version of the service.

      # curl -H "Host: www.example.com" -H "foo: bar" http://<EXTERNAL_IP> 


    • Requests of a specified proportion can be routed to the latest version of the service.

      # curl -H "Host: www.example.com" http://<EXTERNAL_IP> 


    • Only 50% of the client request traffic that meets the requirements of the regular express foo=bar can be routed to the latest version of the service.

      # curl -H "Host: www.example.com" -H "foo: bar" http://<EXTERNAL_IP>