Flagger
Search…
App Mesh Canary Deployments
This guide shows you how to use App Mesh and Flagger to automate canary deployments. You'll need an EKS cluster (Kubernetes >= 1.16) configured with App Mesh, you can find the installation guide here.

Bootstrap

Flagger takes a Kubernetes deployment and optionally a horizontal pod autoscaler (HPA), then creates a series of objects (Kubernetes deployments, ClusterIP services, App Mesh virtual nodes and services). These objects expose the application on the mesh and drive the canary analysis and promotion. The only App Mesh object you need to create by yourself is the mesh resource.
Create a mesh called global:
1
cat << EOF | kubectl apply -f -
2
apiVersion: appmesh.k8s.aws/v1beta2
3
kind: Mesh
4
metadata:
5
name: global
6
spec:
7
namespaceSelector:
8
matchLabels:
9
appmesh.k8s.aws/sidecarInjectorWebhook: enabled
10
EOF
Copied!
Create a test namespace with App Mesh sidecar injection enabled:
1
cat << EOF | kubectl apply -f -
2
apiVersion: v1
3
kind: Namespace
4
metadata:
5
name: test
6
labels:
7
appmesh.k8s.aws/sidecarInjectorWebhook: enabled
8
EOF
Copied!
Create a deployment and a horizontal pod autoscaler:
1
kubectl apply -k https://github.com/fluxcd/flagger//kustomize/podinfo?ref=main
Copied!
Deploy the load testing service to generate traffic during the canary analysis:
1
helm upgrade -i flagger-loadtester flagger/loadtester \
2
--namespace=test \
3
--set appmesh.enabled=true \
4
--set "appmesh.backends[0]=podinfo" \
5
--set "appmesh.backends[1]=podinfo-canary"
Copied!
Create a canary definition:
1
apiVersion: flagger.app/v1beta1
2
kind: Canary
3
metadata:
4
name: podinfo
5
namespace: test
6
spec:
7
# App Mesh API reference
8
provider: appmesh:v1beta2
9
# deployment reference
10
targetRef:
11
apiVersion: apps/v1
12
kind: Deployment
13
name: podinfo
14
# the maximum time in seconds for the canary deployment
15
# to make progress before it is rollback (default 600s)
16
progressDeadlineSeconds: 60
17
# HPA reference (optional)
18
autoscalerRef:
19
apiVersion: autoscaling/v2beta2
20
kind: HorizontalPodAutoscaler
21
name: podinfo
22
service:
23
# container port
24
port: 9898
25
# App Mesh ingress timeout (optional)
26
timeout: 15s
27
# App Mesh retry policy (optional)
28
retries:
29
attempts: 3
30
perTryTimeout: 5s
31
retryOn: "gateway-error,client-error,stream-error"
32
# App Mesh URI settings
33
match:
34
- uri:
35
prefix: /
36
rewrite:
37
uri: /
38
# define the canary analysis timing and KPIs
39
analysis:
40
# schedule interval (default 60s)
41
interval: 1m
42
# max number of failed metric checks before rollback
43
threshold: 5
44
# max traffic percentage routed to canary
45
# percentage (0-100)
46
maxWeight: 50
47
# canary increment step
48
# percentage (0-100)
49
stepWeight: 5
50
# App Mesh Prometheus checks
51
metrics:
52
- name: request-success-rate
53
# minimum req success rate (non 5xx responses)
54
# percentage (0-100)
55
thresholdRange:
56
min: 99
57
interval: 1m
58
- name: request-duration
59
# maximum req duration P99
60
# milliseconds
61
thresholdRange:
62
max: 500
63
interval: 30s
64
# testing (optional)
65
webhooks:
66
- name: acceptance-test
67
type: pre-rollout
68
url: http://flagger-loadtester.test/
69
timeout: 30s
70
metadata:
71
type: bash
72
cmd: "curl -sd 'test' http://podinfo-canary.test:9898/token | grep token"
73
- name: load-test
74
url: http://flagger-loadtester.test/
75
timeout: 5s
76
metadata:
77
cmd: "hey -z 1m -q 10 -c 2 http://podinfo-canary.test:9898/"
Copied!
Save the above resource as podinfo-canary.yaml and then apply it:
1
kubectl apply -f ./podinfo-canary.yaml
Copied!
After a couple of seconds Flagger will create the canary objects:
1
# applied
2
deployment.apps/podinfo
3
horizontalpodautoscaler.autoscaling/podinfo
4
canary.flagger.app/podinfo
5
6
# generated Kubernetes objects
7
deployment.apps/podinfo-primary
8
horizontalpodautoscaler.autoscaling/podinfo-primary
9
service/podinfo
10
service/podinfo-canary
11
service/podinfo-primary
12
13
# generated App Mesh objects
14
virtualnode.appmesh.k8s.aws/podinfo-canary
15
virtualnode.appmesh.k8s.aws/podinfo-primary
16
virtualrouter.appmesh.k8s.aws/podinfo
17
virtualrouter.appmesh.k8s.aws/podinfo-canary
18
virtualservice.appmesh.k8s.aws/podinfo
19
virtualservice.appmesh.k8s.aws/podinfo-canary
Copied!
After the boostrap, the podinfo deployment will be scaled to zero and the traffic to podinfo.test will be routed to the primary pods. During the canary analysis, the podinfo-canary.test address can be used to target directly the canary pods.
App Mesh blocks all egress traffic by default. If your application needs to call another service, you have to create an App Mesh virtual service for it and add the virtual service name to the backend list.
1
service:
2
port: 9898
3
backends:
4
- backend1
5
- arn:aws:appmesh:eu-west-1:12345678910:mesh/my-mesh/virtualService/backend2
Copied!

Setup App Mesh Gateway (optional)

In order to expose the podinfo app outside the mesh you can use the App Mesh Gateway.
Deploy the App Mesh Gateway behind an AWS NLB:
1
helm upgrade -i appmesh-gateway eks/appmesh-gateway \
2
--namespace test
Copied!
Find the gateway public address:
1
export URL="http://$(kubectl -n test get svc/appmesh-gateway -ojson | jq -r ".status.loadBalancer.ingress[].hostname")"
2
echo $URL
Copied!
Wait for the NLB to become active:
1
watch curl -sS $URL
Copied!
Create a gateway route that points to the podinfo virtual service:
1
cat << EOF | kubectl apply -f -
2
apiVersion: appmesh.k8s.aws/v1beta2
3
kind: GatewayRoute
4
metadata:
5
name: podinfo
6
namespace: test
7
spec:
8
httpRoute:
9
match:
10
prefix: "/"
11
action:
12
target:
13
virtualService:
14
virtualServiceRef:
15
name: podinfo
16
EOF
Copied!
Open your browser and navigate to the ingress address to access podinfo UI.

Automated canary promotion

A canary deployment is triggered by changes in any of the following objects:
    Deployment PodSpec (container image, command, ports, env, resources, etc)
    ConfigMaps and Secrets mounted as volumes or mapped to environment variables
Trigger a canary deployment by updating the container image:
1
kubectl -n test set image deployment/podinfo \
2
podinfod=stefanprodan/podinfo:3.1.1
Copied!
Flagger detects that the deployment revision changed and starts a new rollout:
1
kubectl -n test describe canary/podinfo
2
3
Status:
4
Canary Weight: 0
5
Failed Checks: 0
6
Phase: Succeeded
7
Events:
8
New revision detected! Scaling up podinfo.test
9
Waiting for podinfo.test rollout to finish: 0 of 1 updated replicas are available
10
Pre-rollout check acceptance-test passed
11
Advance podinfo.test canary weight 5
12
Advance podinfo.test canary weight 10
13
Advance podinfo.test canary weight 15
14
Advance podinfo.test canary weight 20
15
Advance podinfo.test canary weight 25
16
Advance podinfo.test canary weight 30
17
Advance podinfo.test canary weight 35
18
Advance podinfo.test canary weight 40
19
Advance podinfo.test canary weight 45
20
Advance podinfo.test canary weight 50
21
Copying podinfo.test template spec to podinfo-primary.test
22
Waiting for podinfo-primary.test rollout to finish: 1 of 2 updated replicas are available
23
Routing all traffic to primary
24
Promotion completed! Scaling down podinfo.test
Copied!
When the canary analysis starts, Flagger will call the pre-rollout webhooks before routing traffic to the canary.
Note that if you apply new changes to the deployment during the canary analysis, Flagger will restart the analysis.
During the analysis the canary’s progress can be monitored with Grafana. The App Mesh dashboard URL is http://localhost:3000/d/flagger-appmesh/appmesh-canary?refresh=10s&orgId=1&var-namespace=test&var-primary=podinfo-primary&var-canary=podinfo.
App Mesh Canary Dashboard
You can monitor all canaries with:
1
watch kubectl get canaries --all-namespaces
2
3
NAMESPACE NAME STATUS WEIGHT
4
test podinfo Progressing 15
5
prod frontend Succeeded 0
6
prod backend Failed 0
Copied!
If you’ve enabled the Slack notifications, you should receive the following messages:
Flagger Slack Notifications

Automated rollback

During the canary analysis you can generate HTTP 500 errors or high latency to test if Flagger pauses the rollout.
Trigger a canary deployment:
1
kubectl -n test set image deployment/podinfo \
2
podinfod=stefanprodan/podinfo:3.1.2
Copied!
Exec into the load tester pod with:
1
kubectl -n test exec -it deploy/flagger-loadtester bash
Copied!
Generate HTTP 500 errors:
1
hey -z 1m -c 5 -q 5 http://podinfo-canary.test:9898/status/500
Copied!
Generate latency:
1
watch -n 1 curl http://podinfo-canary.test:9898/delay/1
Copied!
When the number of failed checks reaches the canary analysis threshold, the traffic is routed back to the primary, the canary is scaled to zero and the rollout is marked as failed.
1
kubectl -n appmesh-system logs deploy/flagger -f | jq .msg
2
3
New revision detected! progressing canary analysis for podinfo.test
4
Pre-rollout check acceptance-test passed
5
Advance podinfo.test canary weight 5
6
Advance podinfo.test canary weight 10
7
Advance podinfo.test canary weight 15
8
Halt podinfo.test advancement success rate 69.17% < 99%
9
Halt podinfo.test advancement success rate 61.39% < 99%
10
Halt podinfo.test advancement success rate 55.06% < 99%
11
Halt podinfo.test advancement request duration 1.20s > 0.5s
12
Halt podinfo.test advancement request duration 1.45s > 0.5s
13
Rolling back podinfo.test failed checks threshold reached 5
14
Canary failed! Scaling down podinfo.test
Copied!
If you’ve enabled the Slack notifications, you’ll receive a message if the progress deadline is exceeded, or if the analysis reached the maximum number of failed checks:
Flagger Slack Notifications

A/B Testing

Besides weighted routing, Flagger can be configured to route traffic to the canary based on HTTP match conditions. In an A/B testing scenario, you'll be using HTTP headers or cookies to target a certain segment of your users. This is particularly useful for frontend applications that require session affinity.
Flagger A/B Testing Stages
Edit the canary analysis, remove the max/step weight and add the match conditions and iterations:
1
analysis:
2
interval: 1m
3
threshold: 5
4
iterations: 10
5
match:
6
- headers:
7
x-canary:
8
exact: "insider"
9
webhooks:
10
- name: load-test
11
url: http://flagger-loadtester.test/
12
metadata:
13
cmd: "hey -z 1m -q 10 -c 2 -H 'X-Canary: insider' http://podinfo.test:9898/"
Copied!
The above configuration will run an analysis for ten minutes targeting users that have a X-Canary: insider header.
You can also use a HTTP cookie, to target all users with a canary cookie set to insider the match condition should be:
1
match:
2
- headers:
3
cookie:
4
regex: "^(.*?;)?(canary=insider)(;.*)?quot;
5
webhooks:
6
- name: load-test
7
url: http://flagger-loadtester.test/
8
metadata:
9
cmd: "hey -z 1m -q 10 -c 2 -H 'Cookie: canary=insider' http://podinfo.test:9898/"
Copied!
Trigger a canary deployment by updating the container image:
1
kubectl -n test set image deployment/podinfo \
2
podinfod=stefanprodan/podinfo:3.1.3
Copied!
Flagger detects that the deployment revision changed and starts the A/B test:
1
kubectl -n appmesh-system logs deploy/flagger -f | jq .msg
2
3
New revision detected! progressing canary analysis for podinfo.test
4
Advance podinfo.test canary iteration 1/10
5
Advance podinfo.test canary iteration 2/10
6
Advance podinfo.test canary iteration 3/10
7
Advance podinfo.test canary iteration 4/10
8
Advance podinfo.test canary iteration 5/10
9
Advance podinfo.test canary iteration 6/10
10
Advance podinfo.test canary iteration 7/10
11
Advance podinfo.test canary iteration 8/10
12
Advance podinfo.test canary iteration 9/10
13
Advance podinfo.test canary iteration 10/10
14
Copying podinfo.test template spec to podinfo-primary.test
15
Waiting for podinfo-primary.test rollout to finish: 1 of 2 updated replicas are available
16
Routing all traffic to primary
17
Promotion completed! Scaling down podinfo.test
Copied!
The above procedure can be extended with custom metrics checks, webhooks, manual promotion approval and Slack or MS Teams notifications.
Last modified 8mo ago