CI/CD Pipeline Step 2: Deploying to Kubernetes with Terraform
Automating Your Kubernetes Deployments with Infrastructure as Code
With a versioned Docker image in your registry, the next step is to deploy it. The ‘Ubuntu System Administration Guide’ introduces Terraform, the industry-standard tool for Infrastructure as Code (IaC), to automate the deployment of your application to a Kubernetes cluster.
What is Terraform?
Terraform allows you to define your infrastructure (servers, load balancers, Kubernetes deployments, etc.) in declarative configuration files. Terraform then reads these files and makes the necessary API calls to create, update, or delete resources to match your desired state.
Deploying to Kubernetes
The book shows how to write a Terraform configuration file (`deployments.tf`) that defines a `kubernetes_deployment` resource. This resource specifies:
- The name of the deployment.
- The number of replicas (Pods) to run.
- The Docker image to use for the containers (e.g., `mattiashem/ubuntu-static:$VERSION`).
- Resource requests and limits (CPU and memory).
- Health checks (liveness probes).
This approach allows you to manage your Kubernetes applications in a repeatable and version-controlled way.
This post is based from content of the book Ubuntu System adminstration guide. And the book can be found here https://www.amazon.com/stores/Mattias-Hemmingsson/author/B0FF5CQX13