Enabling Blue-Green Release with Alibaba Cloud Container Service for Kubernetes

Prerequisites

Since we need blue-green releases, there should already be an old service that is serving normally. Here we will take Nginx as an example. Assume that there is already an nginx deployment exposing ports through NodePort and an ingress is providing services. The template we will use is as follows:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
run: old-nginx
name: old-nginx
spec:
replicas: 1
selector:
matchLabels:
run: old-nginx
template:
metadata:
labels:
run: old-nginx
spec:
containers:
- image: registry.cn-hangzhou.aliyuncs.com/xianlu/old-nginx
imagePullPolicy: Always
name: old-nginx
ports:
- containerPort: 80
protocol: TCP
restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
labels:
run: old-nginx
name: old-nginx
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
run: old-nginx
sessionAffinity: None
type: NodePort
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: echo
spec:
backend:
serviceName: default-http-backend
servicePort: 80
rules:
- host: mini-echo.io
http:
paths:
- path: /
backend:
serviceName: old-nginx
servicePort: 80
[root@iZwz91e2au5xvyw5jdpqp7Z manifests]# kubectl get ing
NAME HOSTS ADDRESS PORTS AGE
echo mini-echo.io 47.106.45.47 80 3m
~ curl -H "Host: mini-echo.io" http://47.106.45.47
old

Performing Blue-Green Release

Creating New Deployment and Service

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
run: new-nginx
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:
labels:
run: new-nginx
name: new-nginx
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
run: new-nginx
sessionAffinity: None
type: NodePort

Modifying Ingress

  1. annotations is added, and the tag ingress.aliyun.weight/new-nginx: "50" indicates that the content after the slash (/) is the name of the new service, i.e., new service name. The following "50" represents the percentage (%), and 50 indicates that the new service occupies 50% of the traffic. The complete meaning of this tag is that 50% the traffic is switched into the pod of the new service.
  2. The second addition is to specify the corresponding new serviceName. This is in parallel with the above old service, that is to say, two different services exist in the same path and these services correspond to the new and old applications.

Result

~ curl -H "Host: mini-echo.io" http://47.106.45.47
old
~ curl -H "Host: mini-echo.io" http://47.106.45.47
new
~ curl -H "Host: mini-echo.io" http://47.106.45.47
old
~ curl -H "Host: mini-echo.io" http://47.106.45.47
new
~ curl -H "Host: mini-echo.io" http://47.106.45.47
old
~ curl -H "Host: mini-echo.io" http://47.106.45.47
new

Switching Traffic to the New Service Completely

~ curl -H "Host: mini-echo.io" http://47.106.45.47
new
~ curl -H "Host: mini-echo.io" http://47.106.45.47
new
~ curl -H "Host: mini-echo.io" http://47.106.45.47
new
~ curl -H "Host: mini-echo.io" http://47.106.45.47
new
~ curl -H "Host: mini-echo.io" http://47.106.45.47
new

Finalizing the Blue-Green Release

~ curl -H "Host: mini-echo.io" http://47.106.45.47
new
~ curl -H "Host: mini-echo.io" http://47.106.45.47
new
~ curl -H "Host: mini-echo.io" http://47.106.45.47
new
~ curl -H "Host: mini-echo.io" http://47.106.45.47
new
~ curl -H "Host: mini-echo.io" http://47.106.45.47
new

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com