Foreword
Read little, type a lot. At some point nobody clicks through consoles anymore to create a server, a database, or a Keycloak realm – too slow, too error-prone, impossible to trace. Infrastructure as code means: what should exist is written in files, lives in Git, gets checked in review, and is put into place by a tool.
This book makes you confident with OpenTofu, the
free fork of Terraform. You learn the language and the lifecycle:
init, plan, apply, state,
variables, count and for_each, modules. Then
you manage more than Docker: Kubernetes, Helm charts, and a Keycloak
realm. After that it’s about running it as a team: remote state with
locking and encryption, environments, import and refactoring, secrets
that don’t end up in the state. At the end come tests, policies, and a
pipeline that shows plans in the pull request and finds drift.
The common thread is notes, a small notes API with Postgres and nginx in front. First it comes to life as Docker containers, then on Kubernetes with a database from an operator, with login through Keycloak, as a module in several environments – and finally managed by a CI pipeline.
The book is for admins, developers, and ops people who know Docker and the command line and no longer want to build infrastructure by hand.
The Training Rules
- Type, don’t copy. Every command once yourself.
- Compare with Expected. After each drill you’ll find what you should see. If it differs, that’s where the real lesson is.
- Read the plan. Before every
applythere is a plan. Skip it and you learn about replacement the hard way. - Pass the checkpoint. Only move on once the check commands show what is written there.
- Solve the challenge on your own. First the hint, then the solution.
- Lab values stay in the lab.
lab-db-pw-123,lab-s3-pw-123456, and the otherlab-…values are practice data.
Warning: All outputs come from real test runs. Times, IDs, and IP addresses will differ on your machine. What matters is what happens.
Structure of Each Training Day
| Section | Purpose |
|---|---|
| Briefing | The essentials in a few bullet points |
| Drills | Commands and configuration with expected output |
| Checkpoint | Individual check commands: did everything work? |
| Challenge | A task without instructions |
| Enterprise Note | What is different in production |
Boxes labeled Terraform show where Terraform differs from OpenTofu. Almost everything in this book applies to both.
The Training Plan
| Part | Day | Unit |
|---|---|---|
| 1 – Fundamentals | 0 | The training ground |
| 1 | First resources with the Docker provider | |
| 2 | Variables, outputs, locals | |
| 3 | Understanding state | |
| 4 | count, for_each, and data sources | |
| 5 | Modules | |
| 2 – More than Docker | 6 | notes on Kubernetes |
| 7 | Helm – Postgres and notes as a chart | |
| 8 | Keycloak – login as code | |
| 3 – Team operations | 9 | Remote state, locking, encryption |
| 10 | Environments – dev and prod | |
| 11 | Import, moved, removed | |
| 12 | Secrets | |
| 4 – Quality and CI | 13 | fmt, tflint, tofu test, Trivy, Conftest |
| 14 | CI/CD with Gitea Actions | |
| Competition | 15 | Disaster drills |
| Appendix | A | Cheat sheet |
| B | Final exam |
The Lab
You work over SSH on an Ubuntu 24.04 VM tf with 4 CPUs
and 8 GB of RAM, which you start with Multipass. Any other Linux VM with
sudo and internet access works too. Everything runs
locally: Docker, k3s as Kubernetes, S3-compatible storage for the state,
and Gitea for the pipeline – no cloud account needed.
The tested versions: OpenTofu 1.13.2, Docker 29.9, k3s 1.36.5, the providers kreuzwerker/docker 4.6, hashicorp/kubernetes 3.4, hashicorp/helm 3.3, keycloak/keycloak 5.10, and hashicorp/random 3.9, CloudNativePG 1.30, Keycloak 26.8, Versity S3 Gateway 1.10, tflint 0.65, Trivy 0.76, Conftest 0.71, Gitea 28.3 with act_runner 0.6.1 and tea 0.16.
You’ll find the companion code at
https://github.com/thomaszachmann/opentofu-zum-anfassen;
Day 0 fetches it with git clone. It contains notes, the
Helm chart, and the lab environment – you write the configurations
yourself.
Where this book only touches on an area, the sister books go deeper: “Kubernetes hands-on” for clusters and Helm, “Keycloak hands-on” for realms and tokens, “Docker Compose hands-on” for the notes stack.
Let’s go.