Chapter 00: Your Lab Environment
Every hands-on chapter from 02 onwards runs against one local cluster that you build here once. Nothing in the core track needs a cloud account, a domain, or an API key: the cluster runs in Docker with kind, and Flux reconciles the local profile of the SafeOps platform - the production GitOps tree minus the parts that only make sense with a cloud provider.
Do this chapter first; Chapter 01, which has almost no commands, comes once your cluster is green.
Is This Course for You?
This course teaches production guardrails, not the basics underneath them. You should already be comfortable with:
- Docker - running containers, reading
docker psanddocker logs - A terminal and Git - branches, commits, pull requests
- Kubernetes basics - pods, deployments, services, namespaces,
kubectl get/describe/logs
and have a machine where Docker can get at least 4 CPUs and 8 GB of memory (12 GB recommended - with everything up to Chapter 14 running, the two kind nodes use about 5 GB at rest, and later labs start extra workloads such as a restored database). If a local kind cluster is new territory, learn Docker and Kubernetes fundamentals first and come back; the chapters will make much more sense.
Learning Objectives
By the end of this chapter, you will be able to:
- Provision the SafeOps kind cluster with Terraform and prove that Flux is green
- Explain which platform components the local profile leaves out, and why
- Generate your own age key and register it for the secrets you will encrypt in Chapter 04
- Run a test pod in an environment namespace that enforces Pod Security
restricted
1. Install the Tools
- Docker - the cluster nodes run as containers. Give it at least 4 CPUs and 8 GB of memory (12 GB recommended).
- Install: OrbStack (macOS, recommended), Docker Desktop or Docker Engine (Linux). Colima works too, but its default VM is too small:
colima start --cpu 4 --memory 8 --disk 60. - Check:
docker info
- Install: OrbStack (macOS, recommended), Docker Desktop or Docker Engine (Linux). Colima works too, but its default VM is too small:
- Terraform ≥ 1.11 - provisions the cluster and the Flux bootstrap (the modules use write-only attributes, new in 1.11).
- Install: developer.hashicorp.com/terraform/install
- Check:
terraform version
- kind ≥ 0.30 - local Kubernetes in Docker.
- Install: kind.sigs.k8s.io
- Check:
kind version
- kubectl ≥ 1.35 and the flux CLI ≥ 2.4 - you will use both constantly.
- Install: kubernetes.io/docs/tasks/tools and fluxcd.io/flux/installation
- Check:
kubectl version --clientandflux version --client
- age and sops - encrypt secrets (Chapter 04).
- Install: age and sops releases
- Check:
age-keygen --versionandsops --version
- pre-commit, make and git - local guardrails and the repository workflow (Chapters 02 and 05).
- Install: pre-commit.com; make and git come with the Xcode command line tools (macOS) or
build-essential(Linux). - Check:
pre-commit --version
- Install: pre-commit.com; make and git come with the Xcode command line tools (macOS) or
- checkov - scans the Terraform code for insecure settings; the pre-commit hook for
.tffiles fails without it (Chapter 02). It must not be older than the version the repository’s CI runs: an older Checkov knows fewer checks, so a scan that passes on your laptop can fail in CI. The latest release always meets that; Homebrew’s formula often lags behind.- Install:
uv tool install checkov(uv;pipx install checkovorpip install checkovwork too) - checkov.io. Update it later withuv tool upgrade checkov. - Check:
make check-tools(below) -TOO OLDnames the version CI needs.
- Install:
- jq - reads and builds JSON: the hook for your AI agent in Chapter 01 and
scripts/lab-pod.sh(section 7) use it.- Install:
brew install jq(macOS),sudo apt install jq(Debian, Ubuntu),sudo dnf install jq(Fedora, RHEL) - other systems: jqlang.org - Check:
jq --version
- Install:
- yq v4 - reads the Kubernetes and Flux manifests in the pre-commit guardrails (Chapter 03 on). It must be the Go
yqby Mike Farah - the Pythonyqthataptinstalls is a different tool.- Install:
brew install yq(macOS); on Linux the release binary from github.com/mikefarah/yq - Check:
yq --version(4.x)
- Install:
- The test tools of the pre-commit hooks - every hook runs on
pre-commit run --all-files(Chapter 05), so each needs its tool from the start, even before its chapter:- kyverno CLI ≥ 1.19 (the policy tests, Chapter 16), cosign ≥ 3.0 (image signatures, Chapter 17), promtool ≥ 3.0 (the alert tests; it comes with Prometheus), helm ≥ 3.0 (the tool-versions test, Chapter 20), kubeconform (the manifests against a Kubernetes version, Chapter 20).
- Install:
brew install kyverno cosign prometheus helm kubeconform(macOS); on Linux each project’s release binary -make check-toolsprints the link for anything missing. - Check:
make check-tools(below).
- docker buildx - compares image digests in the registry (Chapter 10). It comes with Docker Desktop and OrbStack; otherwise
brew install docker-buildxor Docker’s own packages.- Check:
docker buildx version
- Check:
On macOS every tool except Docker is one brew install away (terraform via hashicorp/tap, flux via fluxcd/tap). You do not have to check them one by one: the platform repository ships a checker - run it right after you clone in the next section.
2. Get the Platform Repository
The course is taught from the SafeOps reference implementation, safeops-course/sre. Fork it on GitHub, then clone your fork - from Chapter 03 on you will commit to it and Flux will deploy from it. You also push to it (section 4), and GitHub no longer accepts your account password for git push. Clone it the way you sign in to GitHub - one of the two. In this and every later command, replace YOUR_GITHUB_USER with your GitHub username:
# A) over SSH, with an SSH key added to your GitHub account (ssh -T [email protected] greets you by name)
git clone [email protected]:YOUR_GITHUB_USER/sre.git
# B) over HTTPS, signed in with the GitHub CLI (install: cli.github.com; check: gh --version)
gh auth login
git clone https://github.com/YOUR_GITHUB_USER/sre.git
Then:
cd sre
make check-tools
make check-tools prints one line per tool - OK with the version, or MISSING / TOO OLD with the install command for your OS - and checks that the Docker daemon is running with at least 4 CPUs and 8 GB. Fix whatever it reports before going on; everything below assumes a clean run.
The upstream repository is public, and so is your fork - GitHub does not let you make a fork of a public repository private. Flux reads it anonymously. If you want a private copy instead, create a new private repository and push the code there; then Flux needs a token: create a fine-grained GitHub personal access token for that one repository with
Contents: Read-onlyand pass it asTF_VAR_flux_git_token(a classic token’sreposcope would also allow writing).
3. Build the Cluster
cd infra/terraform/kind_cluster
cp terraform.tfvars.example terraform.tfvars
# edit terraform.tfvars: flux_git_repository_url = "https://github.com/YOUR_GITHUB_USER/sre.git" - your fork
# export TF_VAR_flux_git_token="<fine-grained PAT, Contents: Read-only>" # only for a private copy
terraform init
terraform plan -out=tfplan # read it: 2 nodes, Cilium, metrics-server, Flux, generated secrets
terraform apply tfplan
rm tfplan # applied, so it is done - and it holds the generated secrets (Chapter 02)
Without flux_git_repository_url, Flux reads the upstream course repository instead of your fork: everything turns green, but nothing you commit to your fork ever reaches your cluster. It goes into terraform.tfvars (Git ignores the file), not into an export: an exported variable lives in one shell, and a later plan from another terminal would point Flux back to the course repository - replacing the Flux installation to do it.
The cluster takes three ports on your machine, all on 127.0.0.1: 6443 for the Kubernetes API, 8080 and 8443 for the ingress. If another program already listens on one of them, the apply fails with address already in use - see If Something Goes Wrong below.
Two to three minutes later the cluster exists and Flux starts pulling images. Terraform also:
- merges the kubeconfig into
~/.kube/configas contextkind-sre-control-plane - generates the runtime secrets the applications need (
backend-secrets,app-postgres-app, MinIO credentials) - never committed, unique to your cluster - generates
age.agekeyin the repository root (git-ignored) and stores it in the cluster assops-age
age.agekey is the private half of your encryption key: whoever has it can read every secret encrypted for you. That is why it stays on your machine, is git-ignored, and is never committed. The cluster gets a copy so it can decrypt your secrets; the public half is what you register in the next step. Chapter 04 explains SOPS and age properly.
4. Register Your Key for SOPS
cd ../../.. # back to the repository root
scripts/sops-setup.sh --local
git add .sops.yaml && git commit -m "sops: register my local key" && git push
Repeating this after Chapter 05? From then on the pre-commit hooks refuse a commit on main, and - if you set up the ruleset on your fork in Chapter 05 - GitHub refuses a direct push to it. Commit on a branch, open a pull request, let its checks pass and merge it.
This writes your public key into the flux/secrets/local/ rule of .sops.yaml. Only files under that path are encrypted for you; the platform’s own secrets stay encrypted for the platform key, which you do not have and do not need.
5. Prove It Is Green
Wait for the images to arrive (five minutes on a normal connection), then:
flux --context kind-sre-control-plane get kustomizations -A # every row READY=True and SUSPENDED=False
kubectl --context kind-sre-control-plane get pods -A --no-headers | grep -vE 'Running|Completed' # prints nothing
make smoke-test # last line: # Tests: 15, Pass: 15, Fail: 0 - later chapters write it as Fail: 0
Each command names its cluster (--context kind-sre-control-plane) instead of switching the current context first, and make smoke-test tests kind-sre-control-plane unless you set KUBE_CONTEXT. The current context is one line in a kubeconfig file that every terminal using it shares - Chapter 01 shows how that sends a correct command to the wrong cluster. Every lab in this course names the cluster the same way.
What these commands prove
You have not learned GitOps yet - Chapter 03 does that. For now, enough to read the output:
- Flux is a controller running inside the cluster that keeps the cluster equal to your Git repository. You do not
kubectl applyplatform manifests; you commit them, and Flux applies them. - A Kustomization is one piece of the platform that Flux applies from one folder of the repository - the network policies of
develop, the backend instaging, MinIO, and so on.flux get kustomizations -Alists all of them. SUSPENDED=Falsemeans Flux still follows Git for that piece. A suspended one keeps showing its lastREADY=Truewhile it ignores every new commit - Chapter 03’s incident - so the baseline reads both columns.READY=Truemeans Flux’s last reconciliation of that folder succeeded: it applied what Git says, and the health checks it waits for passed. It does not prove that every part of the application works (Chapter 01).FalseorUnknownmeans that piece is not there yet or is broken; theMESSAGEcolumn says why. Right after the first apply this is usually just images still downloading.
The pod check catches workloads that are stuck (Pending, ImagePullBackOff, CrashLoopBackOff), and make smoke-test checks the core of the platform: Flux, the applications, a request to the backend in develop, that every frontend and backend pod answers on each of its addresses (IPv4 and IPv6) in all three environments, the generated secrets, the databases. The number of checks grows with the platform; what matters is Fail: 0. It does not test every environment or a restore from backup - later chapters do that.
One more check, once: that Flux really follows your fork, not the upstream repository:
kubectl --context kind-sre-control-plane -n flux-system get gitrepository flux-system -o jsonpath='{.spec.url}{"\n"}' # https://github.com/YOUR_GITHUB_USER/sre.git
These three commands are your baseline check - make smoke-test also fails on a suspended Kustomization, and when it finds no Kustomizations or no databases at all.
See it answer
The platform you just built serves an application. Ask it through the local ingress on port 8080 - the ingress routes by host name, so the request names the develop site in its Host header. -f makes an HTTP error a failure, -sS stays quiet but still prints why a request failed:
curl -fsS -H 'Host: develop.safeops.work' http://localhost:8080/api/healthz # {"status":"ok"}
curl -fsS -H 'Host: develop.safeops.work' http://localhost:8080/api/version # the build that runs: version, commit, build time
That is the backend in develop, reached the way a user would reach it - through Traefik, the Service and the NetworkPolicies - not through kubectl. They finish this chapter’s check; if they are not clean, fix the environment before you start the exercise, not after.
6. What the Local Profile Contains
On your kind cluster, out of the box
- namespaces
develop,stagingandproductionwith NetworkPolicies, quotas and Pod Security enforced;labwith a quota and Pod Security that only warns - backend and frontend in all three environments, Traefik ingress on
localhost:8080 - the OpenTelemetry collectors in
observability- a DaemonSetotel-collectorfor pod logs and a Deploymentotel-eventsfor Kubernetes Events; on kind they write what they collect to their own log (the debug exporter), not to Uptrace as on the cloud cluster - Prometheus and Grafana (
kubectl --context kind-sre-control-plane -n observability port-forward svc/kube-prometheus-stack-grafana 3000:80, then http://localhost:3000, useradmin, passwordadmin- a lab default, reachable only through your own port-forward) - CloudNativePG with one Postgres cluster per environment, backed up to MinIO
- Kyverno with both policy packs - they refuse in
develop,stagingandproductionand only report elsewhere (Chapters 16 and 17), Flagger (the develop canaries are switched on in Chapter 19), Chaos Monkey
On kind too, if you bring your own account (optional)
- Uptrace tracing - set
TF_VAR_uptrace_dsnto the DSN of your Uptrace project beforeterraform apply; backend and frontend send traces straight to it (Chapter 11). Uptrace Cloud has a free plan - the easy way. Uptrace can also be self-hosted for free (Community edition) with databases of its own - ClickHouse and PostgreSQL among them; this course has not tested it, so take the full list for your version from Uptrace’s install guide (uptrace.dev/get/hosted/install) - An LLM key for the Guardian - the Guardian (
k8s-ai-monitor) runs on kind and works without one; with a key interraform.tfvarsit also analyses incidents (Chapter 14). The Gemini API has a free tier - a project without a billing account. On it, outside the EEA, Switzerland and the UK, Google may use what you send to improve its products; inside them, and on any project with an active billing account (a paid tier), it does not. Either way, a model provider is a third party: send lab data, never a real cluster’s
Left out of the local profile - they run in the Cloud track, and their chapters say what they need
- Flux image automation - needs write access to your fork, because it commits new image tags back to Git (Chapter 10)
- Alertmanager’s dead man’s switch - needs an external heartbeat service (Chapter 11)
Cloud track only - things a laptop cannot provide
- real servers, a cloud load balancer, persistent volumes - on Hetzner, the worked example, k3s on MicroOS; node autoscaling and automatic OS updates with reboots are in the module but off: upgrades and reboots happen when you run them (Chapter 20)
- a public address: Cloudflare DNS records managed by external-dns and origin certificates from Cloudflare Origin CA - both only call the Cloudflare API, so they need outbound internet access - plus the sites and the OIDC login (Dex, Headlamp - kubectl accepts the sign-in on Hetzner only so far) that visitors reach through the Cloudflare proxy, which need the cluster reachable from the internet (appendix)
When a chapter says “Cloud track”, that step needs something from the last list. Read it, understand it, and move on - the core exercise works on the first list alone.
7. Running Test Pods
The environment namespaces enforce Pod Security restricted. A plain kubectl run busybox is rejected before it starts - that is a guardrail, not a bug.
How it works: each namespace carries the label pod-security.kubernetes.io/enforce: restricted. Kubernetes’ built-in Pod Security admission reads that label and checks every new pod against the restricted profile before the pod is created. The profile requires a pod that cannot easily hurt the node: it runs as a non-root user, cannot gain more privileges (allowPrivilegeEscalation: false), drops every Linux capability and uses the runtime’s default seccomp filter. A plain kubectl run sets none of that, so the API server refuses it. The principle is secure by default: the safe settings are required, not recommended (Chapter 07 goes deeper).
The course uses a wrapper that adds the required security context (user 65532, a read-only root filesystem with a writable /tmp):
scripts/lab-pod.sh -n develop -i curlimages/curl:8.22.0 -l app=frontend -- curl -sf http://backend/healthz
scripts/lab-pod.sh -n develop --daemon np-debug # long-lived pod named np-debug
kubectl --context kind-sre-control-plane -n develop exec np-debug -- id # uid=65532 gid=65532 - not root
kubectl --context kind-sre-control-plane -n develop delete pod np-debug # a --daemon pod stays until you delete it
The lab namespace is the exception: Pod Security only warns there, so drills that must observe a violation (Chapter 16) can run.
8. Tear Down and Rebuild
From the root of the repository, tear down:
cd infra/terraform/kind_cluster
make destroy # cluster and state gone - it stops on the first error and keeps the state then
Then build again. In a new terminal, start from the root of the repository too:
cd infra/terraform/kind_cluster
# terraform.tfvars still names your fork - no export needed
# only for a private copy (section 2) - without it the plan removes the Git credentials Flux needs:
# printf 'GitHub PAT: '; IFS= read -rs TF_VAR_flux_git_token; echo; export TF_VAR_flux_git_token
make plan # read it, as in section 3 - do not go on until you have
make apply # applies exactly that plan, then deletes it
Run them one at a time: make apply belongs after you have read the plan, not in the same breath. In a new terminal the variable from section 3 is gone - without it the rebuilt cluster follows the upstream repository, not your fork. make plan and make apply are section 3 with a guard: the apply runs only the plan you saved and read, refuses one older than an hour, and deletes the plan file afterwards (Chapter 02 explains why).
What you lose: everything that lived inside the cluster - the databases in every environment, anything you created by hand. What stays is in Git, in age.agekey and in infra/terraform/kind_cluster/.minio-data - MinIO’s data, the database backups, which live on your machine (delete that directory too for a fully clean start). The key is reused, so the files you encrypted still open; the data is not.
How it works: make destroy does not ask Terraform to delete the objects one by one. It deletes the kind cluster itself - the Docker containers that are the nodes, and with them everything inside - and then removes the entries from the local Terraform state, so the next apply starts from nothing. That is fast and cannot get stuck on an object that refuses to go - and if deleting the cluster fails (Docker not running, for example), it stops before it touches the state, so Terraform never forgets a cluster that still runs. Your age.agekey stays in the repository root; the module generates a key only when that file is missing, so the rebuilt cluster gets the same key and the files you encrypted still open.
Rebuilding is cheap. When a chapter leaves your cluster in a state you do not understand, rebuild rather than debug the debugging.
If Something Goes Wrong
In the commands below, replace NAMESPACE and POD with the namespace and pod name that kubectl --context kind-sre-control-plane get pods -A shows.
make check-toolsreportsdocker daemon ... not running, or the apply cannot reach Docker - start Docker Desktop / OrbStack / Colima and run it again.- The apply fails with
address already in use- another program listens on127.0.0.1:6443,8080or8443. Find it (lsof -nP -iTCP:6443 -iTCP:8080 -iTCP:8443 -sTCP:LISTENon macOS,ss -ltnpon Linux) and stop it, or stop the container that holds the port, then apply again. - Pods
Pendingfor minutes - read why first:kubectl --context kind-sre-control-plane -n NAMESPACE describe pod PODand look atEventsat the end. If they sayInsufficient cpuorInsufficient memory, Docker has too little for the two nodes -make check-toolsshows what it has; raise it in the Docker settings and rebuild. Any other reason (a missing volume, a node taint) is not fixed by more resources - follow what the event says. ImagePullBackOff- in the first minutes it is usually a slow connection: wait and look again. If it stays,kubectl --context kind-sre-control-plane -n NAMESPACE describe pod PODshows the image and the error.git pushasks for a password, or is refused - Git is not signed in to GitHub:gh auth login, or clone over SSH (section 2).kubectlfails with a certificate error right after a rebuild - an old entry for the kind cluster survived in~/.kube/config. The current module replaces it each time it creates the cluster, not on every apply. With an older clone, pull the latest code and rebuild (Tear Down and Rebuild), or merge the new kubeconfig once by hand - frominfra/terraform/kind_cluster, where the rebuild left you:./scripts/merge-kubeconfig.sh kubeconfig.yaml.- A Kustomization stays
Falsefor more than ten minutes - read itsMESSAGEcolumn. If it does not say something you can fix, rebuild (section 8).
Knowledge Check
Complete the Quiz before Chapter 01 - it checks that you know what you set up, what the automation does for you, and what to do when the environment is not clean.
Next
Your cluster is green and your key is registered - you are ready for Chapter 01: Blast Radius & the Shape of Safety. It has almost no commands - only a hook for your AI agent to set up; it gives you the rules and the vocabulary that every later chapter uses. The hands-on work on this cluster starts in Chapter 02.