Introduction to OpenTofu and Kubernetes
Kubernetes has been the de facto standard for container orchestration, revolutionizing how we deploy and manage applications. However, the Kubernetes ecosystem continues to evolve with tools that simplify and enhance its capabilities. One such tool is OpenTofu, a community-led open-source project that builds on existing container technologies, providing a user-friendly approach to managing Kubernetes clusters and deployments.
In this guide, we’ll explore how to leverage Kubernetes with OpenTofu and walk through a hands-on exercise to deploy an application seamlessly.
Why Choose OpenTofu?
- Simplicity: Easy to use and learn, with configurations that are intuitive.
- Flexibility: Can manage multiple cloud services along with Kubernetes, allowing for multi-cloud and hybrid cloud solutions.
- Open-Source and Community-Driven: Maintained by a diverse group of contributors, promoting transparency and trust.
Prerequisites
To follow along in this article, you would need the following:
- A working Kubernetes cluster: This cluster will serve as the foundation for deploying and managing containerized applications using OpenTofu. Minikube, and Kind are great for local development.
- Basic familiarity with Kubernetes and Terraform: A rudimentary understanding of Kubernetes concepts and Terraform syntax will be beneficial for comprehending the demo’s steps.
Installing OpenTofu
You can install OpenTofu on a wide range of operating systems. For installation instructions on your choice operating systems, refer to this section of the installation documentation.
To verify your installation, execute the following tofu command:
tofu --version
Creating OpenTofu Files
The main purpose of the OpenTofu language is declaring resources, which represent infrastructure objects. All other language features exist only to make the definition of resources more flexible and convenient.
OpenTofu language is almost indistinguishable from HCL, this essentially guarantees that your existing Terraform files would work.
Within a directory of your choice, create a file called main.tf, open it up in an editor of your choice, and follow along with the code below
terraform {
required_version = ">= 1.3.0"
required_providers {
kubernetes = {
source = "opentofu/kubernetes"
version = "~> 2.32.0"
}
}
}
provider "kubernetes" {
config_path = "~/.kube/config"
}
In the code above we declare two blocks one for the required providers and another to configure the Kubernetes provider, within the Kubernetes provider block we point OpenTofu to the default location of your kubeconfig using the config_path field.
As of the time of writing this code would pull in the providers from the Terraform registry, however there are efforts on going to make a dedicated registry for OpenTofu providers.
Initializing your OpenTofu Directory
With an OpenTofu file created, the next step is to initialize the directory. Initializing effectively instructs OpenTofu to download the required providers and create a dependency and state file.
To do this run the following command:
tofu init
Once this completes you should see something like this:
Creating a Namespace
Namespaces provide a mechanism for organizing Kubernetes resources within a cluster. They allow you to logically group resources, such as pods, deployments, and services, into separate units, making it easier to manage and isolate resources from each other.
To create a namespace, open up main.tf in your editor of choice and follow along with the code below:
resource "kubernetes_namespace" "opentofu_kubernetes" {
metadata {
name = "opentofu"
}
}
With the kubernetes_namespace resource added, we can get a preview of the resource OpenTofu would create with the plan sub-command. To do this, run the following command:
tofu plan
In a couple of seconds you should see the following:
If this looks good you can apply the configuration using:
tofu apply
OpenTofu would prompt you one more time to confirm the action, enter yes and in couple of minutes you should see the following output:
Creating a Deployment
With a namespace created, we can begin deploying resources using Kubernetes deployments. Deployments are responsible for creating and managing pods, the basic units of computation in Kubernetes.
To create a deployment using OpenTofu, you’ll need to define a resource declaration within main.tf the following code snippet deploys a simple Nginx web server to the tofu-experiments namespace:
resource "kubernetes_deployment" "nginx" {
metadata {
name = "nginx"
namespace = kubernetes_namespace.opentofu_kubernetes.metadata.0.name
}
spec {
replicas = 3
selector {
match_labels = {
app = "nginx"
}
}
template {
metadata {
labels = {
app = "nginx"
}
}
spec {
container {
name = "nginx"
image = "nginx:latest"
port {
container_port = 80
}
}
}
}
}
}
The code snippet defines a Kubernetes deployment resource named nginx that creates and manages three Nginx pods in the opentofu namespace. The deployment ensures that the desired number of replicas (pods) are running at all times.
The metadata block specifies the name of the deployment (nginx) and the namespace where it should be created (opentofu). The spec block defines the desired state of the deployment, including the number of replicas (replicas = 3) and the container configuration.
The selector block identifies the pods that belong to the deployment using a label selector (app = "nginx"). The template block defines the container configuration, including the container image (nginx:latest) and the container port (container_port = 80).
Once you have defined the deployment configuration. apply it to your Kubernetes cluster using the tofu apply command:
tofu apply
kubectl get pods -n opentofu
The output should include three pods named nginx-deployment-xxxxxx, indicating that the deployment was successful and the pods are running in the opentofu namespace.
Conclusion
Kubernetes is a powerful tool for modern application deployment, but managing its complexity can be daunting. OpenTofu bridges the gap by making infrastructure management more approachable and scalable. With a hands-on approach, as shown in this guide, engineers can confidently deploy and manage Kubernetes-based applications while benefiting from the flexibility and community-driven nature of OpenTofu.




