Canary Deployment
Requirements
Make sure you have Nginx Ingress deployed on your cluster:
https://kubernetes.github.io/ingress-nginx/deploy/
Deploy app v1
kubectl create -f - <<EOF
apiVersion: v1
kind: Service
metadata:
name: my-app-v1
labels:
app: my-app
spec:
ports:
- name: http
port: 80
targetPort: http
selector:
app: my-app
version: v1.0.0
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-v1
labels:
app: my-app
spec:
replicas: 1
selector:
matchLabels:
app: my-app
version: v1.0.0
template:
metadata:
labels:
app: my-app
version: v1.0.0
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "9101"
spec:
containers:
- name: my-app
image: containersol/k8s-deployment-strategies
ports:
- name: http
containerPort: 8080
- name: probe
containerPort: 8086
env:
- name: VERSION
value: v1.0.0
livenessProbe:
httpGet:
path: /live
port: probe
initialDelaySeconds: 5
periodSeconds: 5
readinessProbe:
httpGet:
path: /ready
port: probe
periodSeconds: 5
EOF
Deploy ingress for app v1
kubectl create -f - <<EOF
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: my-app
labels:
app: my-app
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: my-app.com
http:
paths:
- backend:
serviceName: my-app-v1
servicePort: 80
EOF
Deploy app v2
kubectl create -f - <<EOF
apiVersion: v1
kind: Service
metadata:
name: my-app-v2
labels:
app: my-app
spec:
ports:
- name: http
port: 80
targetPort: http
selector:
app: my-app
version: v2.0.0
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-v2
labels:
app: my-app
spec:
replicas: 1
selector:
matchLabels:
app: my-app
version: v2.0.0
template:
metadata:
labels:
app: my-app
version: v2.0.0
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "9101"
spec:
containers:
- name: my-app
image: containersol/k8s-deployment-strategies
ports:
- name: http
containerPort: 8080
- name: probe
containerPort: 8086
env:
- name: VERSION
value: v2.0.0
livenessProbe:
httpGet:
path: /live
port: probe
initialDelaySeconds: 5
periodSeconds: 5
readinessProbe:
httpGet:
path: /ready
port: probe
periodSeconds: 5
EOF
Deploy ingress canary for app v2
kubectl create -f - <<EOF
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: my-app-canary
labels:
app: my-app
annotations:
kubernetes.io/ingress.class: "nginx"
# Enable canary and send 10% of traffic to version 2
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-weight: "10"
spec:
rules:
- host: my-app.com
http:
paths:
- backend:
serviceName: my-app-v2
servicePort: 80
EOF
Note the following annotations:
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-weight: "10"
They ensure only 10% of the traffic will be redirected to this host.
To test run the following command:
while sleep 0.1; do curl "PUT-YOUR-NGINX-INGRESS-IP-HERE" -H "Host: my-app.com"; done
Roll out ingress for app v2
First, delete the ingress canary:
kubectl delete ingress my-app-canary
Then roll out the change.
kubectl create -f - <<EOF
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: my-app
labels:
app: my-app
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: my-app.com
http:
paths:
- backend:
serviceName: my-app-v2
servicePort: 80
EOF
References
https://medium.com/google-cloud/kubernetes-canary-deployments-for-mere-mortals-13728ce032fe
Last updated