In modern microservice architecture, application update and release is a high-frequency and critical operation. How to safely and smoothly push new version applications to the production environment without affecting the user experience is a challenge that every developer and operation and maintenance team must face. Gray Release, as a progressive release strategy, can effectively reduce release risks, and the Ingress annotation function of Kubernetes provides us with a simple and powerful implementation method.
This article will give you an in-depth understanding of how to passIngress annotationImplement grayscale publishing in Kubernetes, and gradually master core skills such as traffic segmentation and weight control. Whether you are new to Kubernetes or an experienced user, you can get practical knowledge and guidance from this article.
What is grayscale publishing?
Grayscale release, also known as Canary Release, is a progressive application release strategy. Its core idea is:Gradually push the new version of the application to a small number of users, observe its running status, and then gradually expand the scope after confirmation, and finally complete the full release。
Compared with full release, grayscale release has the following advantages:
- Reduce risk: Through small-scale verification, avoid global failures caused by new version problems.
- Quick rollback: If there is a problem with the new version, you can quickly switch back to the old version.
- User experience optimization: Gradual rollout can reduce the impact on users.
Grayscale publishing implementation in Kubernetes
In Kubernetes, grayscale release can be implemented in many ways, such as:
- Deployment + Service: Manually control flow switching.
- Istio: Advanced traffic management through service mesh.
- Ingress annotation: Traffic segmentation is achieved through the annotation function of Nginx Ingress Controller.
This article will focus onIngress annotationimplementation because it is simple and easy to use and does not require the introduction of additional components.
Implement grayscale publishing through Ingress annotations
Nginx Ingress Controller provides rich annotations (Annotations), which can easily implement grayscale publishing. Here are the specific steps:
1. Deploy new and old versions of applications
First, we need to deploy two versions of the application:
- Old version (v1): The currently running production version.
- New version (v2): New version to be released.
1.1 Create v1 version Deployment and Service
# v1 version Deployment
apiVersion: apps/v1
Kind: Deployment
metadata:
name: my-app-v1
spec:
replicas: 3
template:
metadata:
labels:
app: my-app
version: v1
spec:
containers:
- name: my-app
image: my-app:v1
ports:
- containerPort: 80
# v1 version Service
apiVersion: v1
Kind: Service
metadata:
name: my-app-v1
spec:
selector:
app: my-app
version: v1
ports:
- port: 80
targetPort: 80
1.2 Create v2 version Deployment and Service
# v2 version Deployment
apiVersion: apps/v1
Kind: Deployment
metadata:
name: my-app-v2
spec:
replicas: 3
template:
metadata:
labels:
app: my-app
version: v2
spec:
containers:
- name: my-app
image: my-app:v2
ports:
- containerPort: 80
# v2 version Service
apiVersion: v1
Kind: Service
metadata:
name: my-app-v2
spec:
selector:
app: my-app
version: v2
ports:
- port: 80
targetPort: 80
2. Configure Ingress to implement grayscale publishing
Via Nginx Ingress Controllercanary
Note, we can easily implement traffic segmentation.
2.1 Create Ingress resource
Here is an example configuration:
apiVersion: networking./v1
Kind: Ingress
metadata:
name: my-app-ingress
annotations:
/canary: "true" # Enable grayscale publishing
/canary-weight: "10" # 10% traffic to new version
spec:
rules:
-host:
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-v2 # New version service
port:
number: 80
-host:
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-v1 #Old version service
port:
number: 80
2.2 Key annotations
-
/canary: "true"
: Enable grayscale publishing function. -
/canary-weight: "10"
: Allocate 10% of the traffic to the new version (v2) and the remaining 90% of the traffic to the old version (v1).
3. Gradually adjust traffic weights
During the grayscale release process, the traffic proportion of the new version can be gradually increased. For example:
- Initial phase: 10% traffic to v2.
- After verification: Adjust the weight to 50%.
- Final stage: Adjust the weight to 100% and complete full release.
Just modifycanary-weight
The value of the annotation can be:
/canary-weight: "50" # 50% of traffic goes to the new version
4. Monitoring and rollback
During the grayscale release process, it is important to monitor the running status of the new version, including:
- Application log: Check for errors or exceptions.
- Performance indicators: Such as response time, error rate, etc.
- User feedback: Collect users’ experience.
If problems are found, you can adjust thecanary-weight
Annotation switches traffic back to the old version:
/canary-weight: "0" # Switch all traffic back to the old version
Advanced usage of grayscale publishing
In addition to weight-based traffic segmentation, Nginx Ingress Controller also supports the following grayscale publishing strategies:
1. Traffic segmentation based on request headers
pass/canary-by-header
Annotation to route traffic for specific request headers to the new version.
Example configuration
apiVersion: networking./v1
kind: Ingress
metadata:
name: my-app-ingress
annotations:
/canary: "true"
/canary-by-header: "X-Canary"
/canary-by-header-value: "true"
spec:
rules:
- host:
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-v2
port:
number: 80
- host:
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-v1
port:
number: 80
illustrate
- When the request header contains
X-Canary: true
, traffic will be routed to the new version (v2). - Other requests continue to use the old version (v1).
2. Cookie-based traffic segmentation
pass/canary-by-cookie
Annotation to route traffic for specific cookies to the new version.
Example configuration
apiVersion: networking./v1
kind: Ingress
metadata:
name: my-app-ingress
annotations:
/canary: "true"
/canary-by-cookie: "canary"
spec:
rules:
- host:
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-v2
port:
number: 80
- host:
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-v1
port:
number: 80
illustrate
- When the request contains
canary=true
cookies, traffic is routed to the new version (v2). - Other requests continue to use the old version (v1).
3. Use in combination
You can use weights, request headers, and cookies simultaneously to implement more complex grayscale publishing strategies.
Example configuration
apiVersion: networking./v1
kind: Ingress
metadata:
name: my-app-ingress
annotations:
/canary: "true"
/canary-weight: "10"
/canary-by-header: "X-Canary"
/canary-by-header-value: "true"
/canary-by-cookie: "canary"
spec:
rules:
- host:
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-v2
port:
number: 80
- host:
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-v1
port:
number: 80
illustrate
- 10% of traffic will be allocated to the new version (v2).
- If the request header contains
X-Canary: true
or cookies contained incanary=true
, traffic will also be routed to the new version.
Summarize
Through the Ingress annotation of Kubernetes, we can easily implement grayscale release, gradually push new versions of applications to users, and reduce release risks. Whether it is traffic segmentation based on weight or fine-grained control based on request headers or cookies, Nginx Ingress Controller provides powerful support.
Grayscale release is not only a technical optimization, but also a respect for user experience. I hope this article can help you master this important skill and make your publishing process smoother and more reliable!
Try it now: Deploy a grayscale release example in your Kubernetes cluster and experience the charm of progressive release! If you have any questions or ideas, please leave a message in the comment area to discuss!