Deployment strategies
Roll out a service with a rolling update, a blue-green cutover and a canary, and roll each of them back.
A Deployment replaces pods. A strategy decides how much of the new version your users see while that happens, and how fast you can undo it. This lab runs the three strategies you will actually choose between on OpenShift: rolling, blue-green and canary. All three use the same image from GitOps. The difference is the Service and the Route.
Prerequisites
- An OpenShift project
paymentswith permission to create Deployments, Services and Routes - Two image tags of the same application,
payments:v1andpayments:v2, both passing a readiness probe ocandcurl
The probe matters more than the strategy. Every strategy below sends traffic only to pods that are Ready. Without a readiness probe, a rolling update sends users to a process that is still starting.
Rolling
A rolling update is the default, and the right choice when one version at a time is enough and the new pods can run beside the old ones.
apiVersion: apps/v1
kind: Deployment
metadata:
name: payments
namespace: payments
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
selector:
matchLabels:
app: payments
template:
metadata:
labels:
app: payments
spec:
containers:
- name: app
image: image-registry.openshift-image-registry.svc:5000/payments/payments:v2
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /health/ready
port: 8080
maxUnavailable: 0 keeps the current replica count serving throughout. maxSurge: 1 starts one new pod before stopping an old one. Roll back to the previous ReplicaSet:
oc apply -f deployment.yaml
oc rollout status deployment/payments -n payments
oc rollout history deployment/payments -n payments
oc rollout undo deployment/payments -n payments
oc rollout undo restores the previous pod template. It does not restore a database migration. That constraint is shared by every strategy on this page.
Blue-green
Blue-green runs two complete Deployments. The Route points at one of them. Switching is a single object change, and switching back is the same change in reverse. You pay for two copies while both exist.
apiVersion: route.openshift.io/v1
kind: Route
metadata:
name: payments
namespace: payments
spec:
to:
kind: Service
name: payments-blue
port:
targetPort: http
tls:
termination: edge
Deploy v2 as payments-green and check it on its own Route before anyone else sees it.
oc patch route/payments -n payments --type=merge \
-p '{"spec":{"to":{"name":"payments-green"}}}'
# back again
oc patch route/payments -n payments --type=merge \
-p '{"spec":{"to":{"name":"payments-blue"}}}'
Leave the idle Deployment in place until you trust the live one. Deleting it removes the fast path back.
Canary
A canary sends a slice of traffic to the new version. OpenShift Routes do this with weights. There is no second controller to install.
apiVersion: route.openshift.io/v1
kind: Route
metadata:
name: payments
namespace: payments
spec:
to:
kind: Service
name: payments-stable
weight: 90
alternateBackends:
- kind: Service
name: payments-canary
weight: 10
port:
targetPort: http
tls:
termination: edge
payments-stable is v1. payments-canary is v2. Raise the canary weight as you gain confidence, then point to at the new Service and drop the alternate backend. Abort by setting the canary weight to 0.
oc patch route/payments -n payments --type=json \
-p '[{"op":"replace","path":"/spec/alternateBackends/0/weight","value":0}]'
Weights are per request, not per user. A caller that hits the route twice can see both versions. That is acceptable for a stateless read, and it is a bug for a flow that must stay on one version. Use blue-green for those flows.
Which one, and the rollback you can actually do
| Strategy | Users see | Rollback | Use when |
|---|---|---|---|
| Rolling | A mix of old and new pods during the update | oc rollout undo |
The change is backwards compatible and a minute of mixed versions is fine |
| Blue-green | One version, then the other | Patch the Route back | You want a dress rehearsal on a full copy, and you can afford the extra pods |
| Canary | A small share on the new version | Set the canary weight to 0 | You can judge the new version from a fraction of real traffic |
For all three, the schema has to work for both versions at once. Add a column before you deploy the code that writes it, and drop the old column only after the previous version is gone. This is the expand-and-contract pattern. A migration that rewrites a column in place makes oc rollout undo restore pods that cannot read the data.
OpenShift GitOps should hold the Route and the weights. A canary you only clicked in the console will be healed back to Git, or forgotten.
What's next
- Put the Deployment and the Route under OpenShift GitOps so the cutover is a commit.
- Stop a bad image reaching the canary at all with the admission rule in DevSecOps.
- Watch error rate and latency while the canary weight is above zero. The signals are the same ones in Measuring DevOps.