Containers and Kubernetes · Advanced · 28 min
Release a new Snip version safely, roll back and clean up
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · Kubernetes 1.37 on kind 0.33 · Kustomize 5.8.3 · kubeconform 0.8.0 · Docker Engine 29 · distribution registry 3.1.2
You will release Snip 0.2.0 to your kind cluster through a rollout gate with the prod image pinned by digest, watch a deliberately broken release stall while the old pods keep serving, roll it back from the revision history, and then remove everything the series created.
On this page
Snip now runs on your kind cluster with a database, probes, a Gateway, autoscaling and locked-down networking. What it does not have yet is a safe way to change. Every earlier part applied manifests and hoped. A release process has to answer three questions before anyone types kubectl apply: which exact image is going out, how will you know it worked, and what happens to users if it did not.
In this last part you answer all three. You tell the Deployment to add new pods before it removes old ones, so a bad version can never take capacity away. You pin production to an image digest, which cannot be moved the way a tag can. You wrap the apply in a short script whose exit status is the release gate. Then you ship a version that fails on purpose, watch the gate catch it while the old pods carry on, roll it back, and read what happened in the revision history. The part ends with a release checklist that draws on parts 1 to 7, and the clean-up for the whole series.
Step 1 · Set the rollout rules in the base
A Deployment replaces pods according to its strategy. You have relied on the defaults so far: maxSurge and maxUnavailable both at 25%. With four pods, 25% of unavailability rounds down to one, so Kubernetes may stop an old pod before its replacement is ready. If the new version is broken, you lose a quarter of your capacity for as long as the rollout is stuck.
Write the rules down yourself instead, in a patch in the base so every environment gets them:
cd ~/snip-demo/k8s/base
cat > rollout.yaml <<'YAML'
apiVersion: apps/v1
kind: Deployment
metadata:
name: snip
spec:
minReadySeconds: 10
progressDeadlineSeconds: 120
revisionHistoryLimit: 5
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
YAML
kustomize edit add patch --path rollout.yamlWhat each field buys you:
maxUnavailable: 0means the number of available pods never drops below the desired count during a rollout. An old pod is removed only after a new one has become available. A release that never becomes ready therefore costs nothing but one pending pod.maxSurge: 1allows one extra pod above the desired count, so with the prod HPA's floor of four, the cluster runs at most five Snip pods during a release. Each release then replaces one pod at a time. It is slower than the default, and it needs room for one more pod's requests on some node. That headroom is the price ofmaxUnavailable: 0.minReadySeconds: 10makes a new pod stay ready for 10 seconds before it counts as available. A version that passes its first readiness check and then falls over a few seconds later is caught here instead of replacing the next old pod.progressDeadlineSeconds: 120is how long the Deployment may go without progress before it marks itself failed. The startup probe from part 4 allows 60 seconds, andminReadySecondsadds 10, so a healthy pod always makes progress well inside 120 seconds. Kubernetes does not roll back on its own when the deadline passes. It records the failure in the Deployment'sProgressingcondition, and your gate acts on it.revisionHistoryLimit: 5keeps the five previous ReplicaSets, instead of the default ten. Each one is a version you can roll back to. Part 2 explained why each also keeps its own hashed ConfigMap alive, so a rollback restores old settings as well as the old image.
The PodDisruptionBudget from part 6 does not take part in any of this. It limits evictions, such as a node drain, and Kubernetes does not consult it when a Deployment replaces its own pods. Only the strategy above protects a rollout.
Check it worked
cd ~/snip-demo/k8s
kustomize build overlays/prod | grep -E 'maxSurge|maxUnavailable|minReadySeconds|progressDeadlineSeconds|revisionHistoryLimit'You see the five values: maxSurge: 1, maxUnavailable: 0, minReadySeconds: 10, progressDeadlineSeconds: 120 and revisionHistoryLimit: 5. Kustomize prints them in alphabetical order within their sections, so the order on your screen may differ from the patch.
Step 2 · Write the release gate and record a baseline
A release gate is a check whose result decides whether a release counts as done. Here it is kubectl rollout status with a timeout: it waits until every new pod is available and exits with 0, or exits with an error when the progress deadline passes. Wrap it in a script so that every release runs the same steps in the same order, from your laptop today and from CI later.
From this part on the kind cluster plays production, so releases use the prod overlay. Part 5 added the routes to the dev overlay only. Add them to prod, so the Gateway still reaches Snip once prod is applied:
cd ~/snip-demo/k8s/overlays/prod
grep -q '\.\./\.\./routes' kustomization.yaml || kustomize edit add resource ../../routesEvery release also says why it exists. Kubernetes shows the Deployment's kubernetes.io/change-cause annotation in the revision history, so put it in a patch in the prod overlay. You rewrite this one small file for each release:
cd ~/snip-demo/k8s/overlays/prod
cat > release.yaml <<'YAML'
apiVersion: apps/v1
kind: Deployment
metadata:
name: snip
annotations:
kubernetes.io/change-cause: "snip 0.1.0: baseline with rollout rules"
YAML
kustomize edit add patch --path release.yamlNow the script. It renders the overlay once, validates exactly what it rendered, refuses to run without a change-cause, applies, and then waits. Every kubectl call names the kind-snip context, so a release can never land on whichever cluster your shell happens to point at. The schema catalog line is the one part 5 used for the Gateway API kinds in routes/:
cat > ~/snip-demo/release.sh <<'SH'
#!/usr/bin/env bash
# Release one Snip environment: render, validate, apply, then wait for the rollout.
# Usage: ./release.sh prod
set -euo pipefail
env="${1:?usage: ./release.sh dev|prod}"
context="${KUBE_CONTEXT:-kind-snip}"
ns="snip"
timeout="${ROLLOUT_TIMEOUT:-180s}"
catalog='https://raw.githubusercontent.com/datreeio/CRDs-catalog/30499112dbaf5d3760c0843d4841e52e62dd0233/{{.Group}}/{{.ResourceKind}}_{{.ResourceAPIVersion}}.json'
k() { kubectl --context "$context" "$@"; }
cd "$(dirname "$0")"
rendered="$(mktemp)"
trap 'rm -f "$rendered"' EXIT
kustomize build "k8s/overlays/$env" > "$rendered"
kubeconform -strict -summary -kubernetes-version 1.37.3 \
-schema-location default -schema-location "$catalog" < "$rendered"
if ! grep -q 'kubernetes.io/change-cause:' "$rendered"; then
echo "No change-cause in k8s/overlays/$env/release.yaml. Stopping before apply."
exit 1
fi
k apply -f "$rendered"
if ! k -n "$ns" rollout status deployment/snip --timeout="$timeout"; then
echo "RELEASE FAILED: the new pods did not become available. The old pods are still serving."
k -n "$ns" get pods -l app.kubernetes.io/name=snip
echo "Roll back with: kubectl --context $context -n $ns rollout undo deployment/snip"
exit 1
fi
echo "Release finished. Latest revisions:"
k -n "$ns" rollout history deployment/snip | tail -n 3
SH
chmod 755 ~/snip-demo/release.sh
bash -n ~/snip-demo/release.sh--timeout=180s is longer than the 120-second progress deadline on purpose. A stuck rollout then fails because the Deployment itself declared it failed, which you can see afterwards in kubectl describe, and not only because the script stopped waiting. The exit 1 lines are inside the script, so they end the script and not your terminal.
Before the first release, ask the cluster what will change:
cd ~/snip-demo
kustomize build k8s/overlays/prod | kubectl --context kind-snip diff -f -The diff shows the strategy fields, the annotation, and whatever differs between your dev and prod overlays, such as the HPA's floor of four. Now release the baseline:
cd ~/snip-demo
./release.sh prodCheck it worked
The kubeconform summary ends in Invalid: 0, Errors: 0, rollout status ends with deployment "snip" successfully rolled out, and the script prints Release finished. with the last lines of the history. History lists revisions oldest first, so the newest is the last (bottom) line, and it has your change-cause, snip 0.1.0: baseline with rollout rules. Older revisions above it, from parts 2 to 7, show <none>, because nothing recorded why they happened.
Step 3 · Give the cluster a registry
A digest belongs to an image in a registry: it is the SHA-256 hash of the manifest the registry serves. Images copied in with kind load docker-image, as in part 1, never pass through one, so there is nothing stable to pin. Run a small local registry next to the cluster, as the kind documentation describes. It listens on 127.0.0.1:5001 on your machine only:
docker run -d --restart=always --name kind-registry -p 127.0.0.1:5001:5000 registry:3.1.2
docker network connect kind kind-registryThe second command puts the registry on kind's Docker network, where the nodes can reach it by the name kind-registry. Each node's containerd also has to know that an image named localhost:5001/... should be fetched from there. Write that mapping into every node of the cluster, the control plane and the two workers from part 6:
for node in $(kind get nodes --name snip); do
docker exec "$node" mkdir -p /etc/containerd/certs.d/localhost:5001
printf '[host."http://kind-registry:5000"]\n' | docker exec -i "$node" cp /dev/stdin /etc/containerd/certs.d/localhost:5001/hosts.toml
donekind node images from v0.27.0 onwards already read this folder, so no cluster restart is needed. The registry is plain HTTP, which is acceptable for a registry bound to your own loopback address and nothing else. A shared registry needs TLS and logins.
Check it worked
curl -fsS http://127.0.0.1:5001/v2/; echoThe registry answers {}, the empty reply of a working registry API. docker exec snip-worker cat /etc/containerd/certs.d/localhost:5001/hosts.toml shows the one-line mapping on a worker node.
Step 4 · Build and push Snip 0.2.0
A release needs a change, and a version you can see from outside the image. Add a file that logs Snip's version at start-up. The value is a placeholder that the build replaces, so the version lives in one place, the build command, and not in the source:
cd ~/snip-demo
cat > version.go <<'GO'
package main
import "log"
// version is replaced at build time with -ldflags "-X main.version=...".
var version = "dev"
func init() {
log.Printf("snip %s starting", version)
}
GORewrite the Dockerfile from part 1 so it takes the version as a build argument. Two things change in the build stage, the ARG lines and the -X main.version flag, and the runtime stage gets the standard OCI version label. ARG sits just before the build command, so changing the version reuses the cached go mod download layer:
cd ~/snip-demo
cat > Dockerfile <<'DOCKER'
# Build stage: the Go toolchain. Never shipped.
FROM golang:1.27.4-trixie AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY *.go ./
ARG VERSION=dev
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w -X main.version=${VERSION}" -o /out/snip .
# Runtime stage: Snip, CA certificates and tzdata. No shell.
FROM gcr.io/distroless/static-debian13:nonroot
ARG VERSION=dev
LABEL org.opencontainers.image.version="${VERSION}"
COPY --from=build /out/snip /snip
USER 65532:65532
EXPOSE 8080
ENTRYPOINT ["/snip"]
DOCKER
grep -c 'VERSION' DockerfileAn ARG only exists in the stage that declares it, which is why the runtime stage declares it again for the label. Build the image under the registry's name, push it, and read back the digest the registry assigned:
cd ~/snip-demo
docker build --build-arg VERSION=0.2.0 -t localhost:5001/snip:0.2.0 .
docker push localhost:5001/snip:0.2.0
REF="$(docker image inspect --format '{{index .RepoDigests 0}}' localhost:5001/snip:0.2.0)"
echo "$REF"Docker on Linux and Docker Desktop both treat registries on localhost as allowed over plain HTTP, so the push needs no extra settings. The push, and each node's first pull of the image, stay on your machine and use no internet data; only docker build downloads the base images.
Check it worked
curl -fsS http://127.0.0.1:5001/v2/snip/tags/list; echo
echo "$REF" | grep -E '^localhost:5001/snip@sha256:[0-9a-f]{64}$'The registry lists {"name":"snip","tags":["0.2.0"]}, and REF has the form localhost:5001/snip@sha256: followed by 64 hex characters. Keep this terminal open: the next step uses REF. If you lose it, run the docker image inspect line again. To check the version is baked in, run docker run --rm localhost:5001/snip:0.2.0 and stop it with Ctrl+C; the first log line reads snip 0.2.0 starting.
Step 5 · Pin prod to the digest and release 0.2.0
A tag such as 0.2.0 is a name someone can point at a different image tomorrow, by pushing again, by accident or otherwise. A node that already has the old 0.2.0 keeps running it, while a new node pulls the new one, and your pods quietly run two different builds. A digest cannot move: it names the content itself. Pin prod to it. kustomize edit set image replaces the images: entry for snip, so the old newTag goes away:
cd ~/snip-demo/k8s/overlays/prod
kustomize edit set image "snip=$REF"
cat > release.yaml <<'YAML'
apiVersion: apps/v1
kind: Deployment
metadata:
name: snip
annotations:
kubernetes.io/change-cause: "snip 0.2.0: version in the startup log, image pinned by digest"
YAMLDev keeps using tags, which is what you want while you are iterating: the pin is for the environment where a surprise costs most. Look at what will run before releasing it:
cd ~/snip-demo/k8s
grep -A3 '^images:' overlays/prod/kustomization.yaml
kustomize build overlays/prod | grep -E '^ +image: 'The kustomization shows digest: sha256:..., name: snip and newName: localhost:5001/snip, and the rendered Deployment has a single line image: localhost:5001/snip@sha256:... with your digest. Release it through the gate:
cd ~/snip-demo
./release.sh prodIn a second terminal, watch the pods change one at a time while it runs:
kubectl --context kind-snip -n snip get pods -l app.kubernetes.io/name=snip -wCheck it worked
The script ends with Release finished., and the last history line reads snip 0.2.0: version in the startup log, image pinned by digest. In the watch, a new pod appears, becomes 1/1, and only after about 10 more seconds does one old pod start terminating. There is never more than one pod above your desired count. Confirm what the pods are running:
kubectl --context kind-snip -n snip get pods -l app.kubernetes.io/name=snip \
-o jsonpath='{range .items[*]}{.status.containerStatuses[0].imageID}{"\n"}{end}' | sort | uniq -c
kubectl --context kind-snip -n snip logs deployment/snip | head -n 1Every pod reports the same image ID, ending in your digest, and the first log line is snip 0.2.0 starting.
Step 6 · Ship a broken release and watch the gate hold
Now break a release on purpose, the way it often happens for real: a configuration change in the same release as working code. Here Snip is told to listen on port 9090, but the container port and the probes still say 8080. Put the mistake in its own patch file so that undoing it later is one command, and say what it is in the change-cause:
cd ~/snip-demo/k8s/overlays/prod
cat > broken-port.yaml <<'YAML'
apiVersion: apps/v1
kind: Deployment
metadata:
name: snip
spec:
template:
spec:
containers:
- name: snip
env:
- name: ADDR
value: ":9090"
YAML
kustomize edit add patch --path broken-port.yaml
cat > release.yaml <<'YAML'
apiVersion: apps/v1
kind: Deployment
metadata:
name: snip
annotations:
kubernetes.io/change-cause: "snip 0.2.0: listen on 9090 (broken on purpose)"
YAML
kustomize build . | grep -B1 -A1 'name: ADDR'The rendered Deployment has name: ADDR with value: :9090. Nothing in kustomize or kubeconform objects, because the YAML is perfectly valid. Only a running pod can show that it is wrong, which is why the gate waits for one. Keep the pod watch from the last step running, and release:
cd ~/snip-demo
if ./release.sh prod; then
echo "unexpected: the broken release passed the gate"; false
else
echo "the gate stopped the broken release"
fiThe if keeps your shell open whichever way the script exits. While it waits, about two minutes, look at the Deployment from a third terminal:
kubectl --context kind-snip -n snip get deployment snip
kubectl --context kind-snip -n snip get endpointslices -l kubernetes.io/service-name=snip \
-o jsonpath='{range .items[*].endpoints[*]}{.targetRef.name}{" ready="}{.conditions.ready}{"\n"}{end}'Check it worked
get deployment shows READY 4/4, UP-TO-DATE 1 and AVAILABLE 4: one new pod exists, and all four old pods are still available. The EndpointSlice lists the four old pods with ready=true and the new pod with ready=false, so the Gateway and the Service send it no traffic. In the watch, the new pod stays at 0/1, and its RESTARTS count climbs every minute or so. kubectl --context kind-snip -n snip describe pod <new-pod-name> (replace the name with the one from your watch) shows events like Startup probe failed: ... connect: connection refused and Container snip failed startup probe, will be restarted.
After 120 seconds without progress, rollout status prints error: deployment "snip" exceeded its progress deadline, and the script prints RELEASE FAILED with the pod list and the undo command, then the gate stopped the broken release. Users saw nothing. With the default 25% settings, one of the four old pods could have been removed at the start, a quarter of capacity gone for as long as nobody noticed.
Step 7 · Roll back, read the history and fix the files
The cluster is still half-way: the Deployment wants the broken template, and only maxUnavailable: 0 keeps it from going further. Roll it back to the previous revision:
kubectl --context kind-snip -n snip rollout undo deployment/snip
kubectl --context kind-snip -n snip rollout status deployment/snip --timeout=180s
kubectl --context kind-snip -n snip rollout history deployment/sniprollout undo copies the previous ReplicaSet's pod template back into the Deployment. The broken pod is deleted, nothing else has to start, and rollout status succeeds almost at once. To go further back, list the history and name a revision: kubectl --context kind-snip -n snip rollout history deployment/snip --revision=<number> shows that revision's template, and rollout undo deployment/snip --to-revision=<number> restores it. With revisionHistoryLimit: 5, five old revisions are kept to choose from.
The history has a detail worth knowing. An undo does not move backwards through the numbers. The 0.2.0 revision you returned to is renumbered as the newest, and its old number disappears from the list. In the list, the second-to-last line is the broken release and the last line is snip 0.2.0: version in the startup log, image pinned by digest, under a new, higher revision number.
An undo fixes the cluster, not your files. The prod overlay still says port 9090, and the next ./release.sh prod would ship the mistake again. Remove the patch and restore the change-cause, so the files describe what is running:
cd ~/snip-demo/k8s/overlays/prod
kustomize edit remove patch --path broken-port.yaml
rm broken-port.yaml
cat > release.yaml <<'YAML'
apiVersion: apps/v1
kind: Deployment
metadata:
name: snip
annotations:
kubernetes.io/change-cause: "snip 0.2.0: version in the startup log, image pinned by digest"
YAML
kustomize build . | grep -c 'name: ADDR' || trueThe count is 0. If you keep ~/snip-demo in Git, a revert commit does the same job and leaves a record of the mistake.
Check it worked
Compare the files with the cluster:
kustomize build ~/snip-demo/k8s/overlays/prod | kubectl --context kind-snip diff -f -The diff shows no change to the Deployment's pod template, because the files and the running revision now agree. kubectl --context kind-snip -n snip get deployment snip shows UP-TO-DATE equal to READY again.
cd ~/snip-demo
./release.sh prodRunning the gate once more applies the restored files and passes straight away, since there is nothing to replace.
Step 8 · Keep a release checklist
The script checks what a machine can check. The rest of a safe release is things a person confirms, and most of them come from earlier parts of this series. Keep them next to the script:
cat > ~/snip-demo/RELEASE.md <<'MD'
# Releasing Snip
Before:
- [ ] The image builds from a pinned base and runs as UID 65532 with no shell (part 1).
- [ ] The new version still answers /healthz and /readyz, and drains on SIGTERM (parts 1 and 4).
- [ ] Both overlays render and pass kubeconform -strict (part 2; release.sh repeats it).
- [ ] Any schema change is in `snip migrate`, is safe to run twice, and the old
version still works against the new schema (part 3).
- [ ] The cluster has room for one extra pod (maxSurge 1; part 6 sets the HPA floor).
- [ ] New pods pass the restricted Pod Security level and need no new network paths (part 7).
- [ ] Prod points at a digest, not a tag, and release.yaml has a new change-cause (part 8).
During:
- [ ] kustomize build k8s/overlays/prod | kubectl diff -f - shows only the intended change.
- [ ] ./release.sh prod exits 0. On failure: rollout undo, then fix the files.
After:
- [ ] The route answers over HTTPS through the Gateway (part 5).
- [ ] The newest (last) revision in rollout history shows the new change-cause.
MD
grep -c '\[ \]' ~/snip-demo/RELEASE.mdThe migration line deserves a moment. During a rolling update, old and new pods run side by side against one database, so a schema change has to suit both. Add a column in one release and start using it in the next; drop the old one only once nothing reads it. A rollback is only safe if the old version still works with the database as the new version left it.
Check it worked
The grep counts 11 open boxes. Read each line against what you built in parts 1 to 7, and change any that do not match your setup: if you never added the Gateway, for example, replace the HTTPS check with the way you reach Snip.
Step 9 · Clean up the series
This removes everything the eight parts created. Copy anything you want to keep out of ~/snip-demo first, such as release.sh, RELEASE.md or the manifests: the last command deletes the folder, including the .secrets folder with the database password from part 3, and cannot be undone.
Delete the kind cluster. Its nodes are Docker containers, so this removes every pod, volume and controller in it at once, including the PostgreSQL data, Envoy Gateway and cert-manager from part 5:
kind delete cluster --name snip
kind get clustersRemove the registry and the images you built or pulled for the series. Leave out any image name you also use for other work:
docker rm -f kind-registry
docker image rm localhost:5001/snip:0.2.0 snip:0.1.0 registry:3.1.2
docker image rm golang:1.27.4-trixie gcr.io/distroless/static-debian13:nonroot
docker image ls --filter reference='kindest/node'The last command lists the kind node image, which is several hundred megabytes. Remove it with docker image rm kindest/node:v1.37.0 if you have no other kind clusters that use it. docker builder prune clears the build cache from parts 1 and 8 as well; it asks before deleting.
Finally, the project folder and the kubeconform schema cache:
rm -rf ~/snip-demo ~/.cache/kubeconformkustomize and kubeconform stay in /usr/local/bin. If you installed them only for this series, remove them with sudo rm /usr/local/bin/kustomize /usr/local/bin/kubeconform. They are useful for any Kubernetes work, though, and the next tutorials use them.
Check it worked
test ! -e ~/snip-demo && echo "snip-demo removed"kind get clusters no longer lists snip (it prints No kind clusters found. if it was your only one), docker ps -a --filter name=kind-registry lists nothing, and the last command prints snip-demo removed.
Next steps
- Choose a tagging scheme and keep digest pins fresh with automated updates in /tutorials/pin-container-images-by-digest.
- Revisit how readiness, startup probes and the preStop pause make the gate meaningful in /tutorials/keep-snip-healthy-with-probes-and-graceful-shutdown.
- Compare this release process with the first, simpler rollback in /tutorials/deploy-and-expose-an-app-on-kubernetes.
- Run
release.shfrom a pipeline instead of your laptop, starting with /tutorials/github-actions-first-workflow.
Related tutorials
- Validate Kubernetes manifests before they reach a clusterYou will have one script that checks plain, Kustomize, Helm, generated and custom-resource manifests against the Kubernetes 1.37 schemas and a set of lint rules, with no cluster, and a GitHub Actions job that runs it on every pull request.
- Use secrets in Docker builds without leaking them into the imageYou will find a token and a private key leaking through a Docker build with Trivy 0.76, then fix the build with secret and SSH mounts, carry the fix into Compose and GitHub Actions, and prove with a rescan that the build context is clean.
- Store data in Kubernetes with persistent volumes and claimsYou will claim storage on a Kubernetes 1.37 kind cluster, prove the data outlives its pod, see which access modes and reclaim policies keep or lose data, and know how expansion and snapshots work on a cloud CSI driver.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials