TExcelTAD
Home › DevOps Capstone: Ship a Production Service › Deploy, Roll Back & Tear Down

Deploy, Roll Back & Tear Down

Everything is in place. This lesson runs the system the way you will operate it: the first deployment from nothing, a routine release, a bad release and its rollback, the runbook entry for the page, and the teardown. Cluster steps are sample sessions; the rollback itself is a Git operation, so it runs here for real.

First deployment, from nothing (local track, sample)

bash
# 1. infrastructure
tofu -chdir=linkly-infra/local/cluster apply
tofu -chdir=linkly-infra/local/platform apply

# 2. image and chart (normally CI does this on the v1.4.0 tag)
docker build --build-arg VERSION=v1.4.0 -t localhost:5001/linkly:v1.4.0 linkly
docker push localhost:5001/linkly:v1.4.0
helm package linkly/chart/linkly --version 1.4.0 --app-version v1.4.0
helm push linkly-1.4.0.tgz oci://localhost:5001/charts --plain-http

# 3. hand the cluster to Git
kubectl apply -n argocd -f linkly-deploy/argocd/root.yaml

# 4. watch it converge, then smoke-test through the Gateway
kubectl -n linkly-staging rollout status deployment/linkly --timeout=5m
kubectl -n gateway-system port-forward svc/envoy-gateway-system-public 8080:80 &   # the Service name depends on the controller
curl -s -H 'Host: lnk.staging.example.com' -X POST localhost:8080/links -d '{"url": "https://go.dev/doc/", "code": "smoke"}'
curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' -H 'Host: lnk.staging.example.com' localhost:8080/smoke

For the local track the image repository in linkly-deploy's values points at the local registry, and the Argo CD Applications at the local chart registry; on the cloud track they point at GHCR. That is the only difference in the deploy repository.

A routine release

text
developer merges to main ─► git tag v1.5.0 ─► CI: check, image, scan, push, chart push
                                                       │
                     bot commit "staging: linkly v1.5.0" in linkly-deploy
                                                       │
                     Argo CD syncs staging ─► rollout (maxUnavailable 0) ─► smoke test, dashboards
                                                       │ looks good for a day
                     ./promote.sh staging production on a branch ─► PR ─► review ─► merge
                                                       │
                     Argo CD syncs production ─► watch "pods per version" and the SLO panel

Two habits make this boring in the good way: never promote what you haven't watched in staging, and one change per promotion (a chart change and an image change together make a rollback ambiguous).

A bad release, rolled back

v1.5.0 reached production and the error budget alert fired. The fastest safe fix is to put back what worked, and in a GitOps setup that means a revert commit in the deploy repository. The session builds a short deploy-repo history, then reverts the promotion and checks what production will run:

bash · try it yourself
git init -q linkly-deploy && cd linkly-deploy && mkdir -p apps/linkly/production
printf 'image:\n  tag: v1.4.0\n  digest: sha256:4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945\nautoscaling: {enabled: true, minReplicas: 3, maxReplicas: 12}\n' > apps/linkly/production/values.yaml
git add . && git commit -qm "production: linkly v1.4.0"
yq -i '.autoscaling.maxReplicas = 16' apps/linkly/production/values.yaml
git commit -qam "production: allow 16 replicas"
yq -i '.image.tag = "v1.5.0" | .image.digest = "sha256:9b2f3c1e8d7a6b5c4d3e2f1a0b9c8d7e6f5a4b3c2d1e0f9a8b7c6d5e4f3a2b1c"' apps/linkly/production/values.yaml
git commit -qam "production: promote linkly v1.5.0 from staging"
echo "== history"
git log --oneline
echo "== incident: revert only the promotion"
bad=$(git log --format=%h --grep='promote linkly v1.5.0' -1)
git revert --no-edit "$bad" | head -1
git log --oneline -1
echo "== production now runs"
yq -o json -I0 '{"tag": .image.tag, "maxReplicas": .autoscaling.maxReplicas}' apps/linkly/production/values.yaml
git diff --quiet HEAD~2 && echo "identical to the state just before the promotion (HEAD~2)"
echo "== compared with the original v1.4.0 commit (HEAD~3):"
git diff HEAD~3 | grep '^[-+][^-+]'
== history
8e8259b production: promote linkly v1.5.0 from staging
4c4114c production: allow 16 replicas
303d99e production: linkly v1.4.0
== incident: revert only the promotion
[main f61be3d] Revert "production: promote linkly v1.5.0 from staging"
f61be3d Revert "production: promote linkly v1.5.0 from staging"
== production now runs
{"tag":"v1.4.0","maxReplicas":16}
identical to the state just before the promotion (HEAD~2)
== compared with the original v1.4.0 commit (HEAD~3):
-autoscaling: {enabled: true, minReplicas: 3, maxReplicas: 12}
+autoscaling: {enabled: true, minReplicas: 3, maxReplicas: 16}

The revert undid exactly one commit: the image went back to v1.4.0 (by digest), while the unrelated maxReplicas: 16 change made after v1.4.0 stayed. Merge it, and Argo CD rolls production back like any other change, with a record of who did it and why. The last two checks confirm it: the files are identical to the state just before the promotion, and compared with the original v1.4.0 commit the only difference is the replica change.

The alternatives, and why they are second choices with GitOps:

Rollback Effect Catch
git revert in linkly-deploy Git and cluster agree; reviewed; audited takes a merge (keep the PR process fast for reverts)
Argo CD UI or argocd app rollback redeploys an earlier synced revision immediately auto-sync must be off for it, and Git still says v1.5.0: re-enable auto-sync and the bad version returns
kubectl rollout undo the Deployment goes back a ReplicaSet self-heal reverts it within minutes; only useful with auto-sync paused
helm rollback — the release is managed by Argo CD, not Helm; don't

If the bad release included a database migration, a revert of the image is only safe if the migration was backwards compatible (expand/contract, see Production Hardening); otherwise roll forward with a fix.

The runbook entry

Every alert's runbook_url points at a section of linkly/docs/runbook.md. Short, ordered, copy-pasteable:

md
## Error budget burn

**Meaning:** more than 1.44% of requests failed with 5xx over the last hour and the last 5 minutes.
At this rate the monthly budget is gone in about two days.

1. Scope it. Dashboard "linkly", row Errors: which route? Since when? Did "pods per version" change
   at the same time? (yes: likely the release, go to 4)
2. Dependencies. Row Dependencies: PostgreSQL healthy? `kubectl -n linkly-production get cluster linkly-db`
   Readiness: `kubectl -n linkly-production get pods -l app.kubernetes.io/name=linkly`
3. Logs. Grafana Explore, Loki:
   {namespace="linkly-production", app="linkly"} | json | level="ERROR"
   Open a trace_id from an error line in Tempo.
4. Recent release? Revert the promotion commit in linkly-deploy (see "Rollback"), merge, watch the
   SLO panel recover within 10 minutes.
5. Not a release and not a dependency: escalate to the linkly team channel, keep notes for the postmortem.

After the incident, write the blameless postmortem the SRE course describes, and add the test or alert that would have caught it earlier.

Teardown

Local track: one command removes everything, since all of it lives in Docker containers.

bash
kind delete cluster --name linkly          # or: tofu -chdir=linkly-infra/local/cluster destroy
docker rm -f kind-registry 2>/dev/null     # the local registry, if you created one

Cloud track, in order, the same day:

bash
argocd app delete root --cascade --yes                            # deletes the Applications; their finalizers prune what they deployed
kubectl delete gateway --all -A                                  # the load balancer goes with the Gateway
tofu -chdir=linkly-infra/cloud/platform destroy
tofu -chdir=linkly-infra/cloud/data destroy -var environment=staging -var-file=../from-cluster.tfvars
tofu -chdir=linkly-infra/cloud/network-cluster destroy
aws resourcegroupstaggingapi get-resources --tag-filters Key=project,Values=linkly \
  --query 'ResourceTagMappingList[].ResourceARN'                 # expect []

Check the billing console the next day as well: some resources (final snapshots, CloudWatch log groups, unattached EBS volumes from PVCs whose reclaim policy was Retain) are not tagged by default or outlive the stack.

Test yourself

Why is git revert of the promotion commit the preferred rollback with Argo CD?

  1. It is faster than any other method
  2. Git and the cluster stay in agreement, the change is reviewed and recorded, and auto-sync keeps working
  3. Argo CD can't roll back otherwise
  4. It also reverts database migrations

The revert removed v1.5.0 but kept maxReplicas: 16. Why?

  1. yq merges values
  2. git revert undoes only the changes of the reverted commit; the replica change was a separate commit
  3. Argo CD restored it
  4. It was in another file

What should be deleted before tofu destroy on the cloud track, and why?

  1. Nothing; OpenTofu deletes everything
  2. Gateways and Argo CD apps, so that controller-created load balancers and volumes are released before the infrastructure disappears
  3. The Git repositories
  4. The Terraform state