Canary Deployment with NGINX Ingress
From: https://medium.com/@muppedaanvesh/implementing-canary-deployment-in-kubernetes-0be4bc1e1aca
A Canary Deployment is a deployment strategy in which a new version of an application is introduced to a small percentage of users before rolling it out to the broader user base.
In Kubernetes, canary deployments are achieved by running multiple versions of an application simultaneously and routing a portion of the traffic to the newer version.
Base Application (Older Version) and Service:
apiVersion: apps/v1
kind: Deployment
metadata:
name: base-app
spec:
replicas: 1
selector:
matchLabels:
app: base-app
template:
metadata:
labels:
app: base-app
spec:
containers:
- name: base-app
image: anvesh35/echo-pod-name
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: base-svc
labels:
app: base-app
spec:
type: ClusterIP
selector:
app: base-app
ports:
- protocol: TCP
port: 80
targetPort: 80
Canary Application (New Version) and Service:
apiVersion: apps/v1
kind: Deployment
metadata:
name: canary-app
spec:
replicas: 1
selector:
matchLabels:
app: canary-app
template:
metadata:
labels:
app: canary-app
spec:
containers:
- name: canary-app
image: anvesh35/echo-pod-name
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: canary-svc
labels:
app: canary-app
spec:
type: ClusterIP
selector:
app: canary-app
ports:
- protocol: TCP
port: 80
targetPort: 80
Expose Base Application with Ingress:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: base-ingress
spec:
ingressClassName: nginx
rules:
- host: canary.echo.pod.name.com
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: base-svc
port:
number: 80
Expose Canary Application with Ingress:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: canary-ingress
annotations:
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-weight: "30"
spec:
ingressClassName: nginx
rules:
- host: canary.echo.pod.name.com
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: canary-svc
port:
number: 80
Pay attention to the following:
- The
host
name should be identical to the main ingresshost
name. - The annotation
nginx.ingress.kubernetes.io/canary: "true"
is required to mark this Ingress as a canary deployment. - The annotation
nginx.ingress.kubernetes.io/canary-weight: "30"
determines the routing weight.