This repository contains all necessary configurations for provisioning the infrastructure of the Voting application (with NATS) using Terraform. It supports setting up and configuring a Kubernetes cluster, either locally via K3d or on a remote environment with K0s. This project uses Flux to manage the Kubernetes resources.
Before starting, ensure you have the following tools installed on your machine:
- Terraform (v0.12+)
- kubectl (matching your Kubernetes cluster version)
K3d is a lightweight wrapper to run K3s (Rancher Lab's minimal Kubernetes distribution) in Docker.
To create a local Kubernetes cluster with K3d:
-
Move to the
local-cluster
directory:cd terraform/local-cluster
-
Copy
example.tfvars
tolocal.tfvars
and fill in the necessary values. -
Initialize Terraform:
terraform init
-
Create a plan to preview the changes:
terraform plan -out plan.plan -var-file=local.tfvars
-
If everything looks good. Apply the configuration:
terraform apply plan.plan
Note: To destroy the local cluster, run
terraform destroy -var-file=local.tfvars
. It will remove the K3d cluster and the associated resources. If it fails to remove the cluster, you can manually delete it by runningk3d cluster delete votingapp
or by destroying the docker containers (starting withk3d-votingapp
:docker ps -a | grep k3d-votingapp | awk '{print $1}' | xargs docker rm -f
).
K0s is a minimal Kubernetes distribution that is designed to be secure, lightweight, and easy to install. K0s is now production-ready and can be used to deploy a remote Kubernetes cluster.
To deploy a remote Kubernetes cluster with k0s:
-
Move to the
remote-cluster
directory:cd terraform/remote-cluster
-
The Terraform state is stored securely in Terraform Cloud. So first you need to login to Terraform Cloud:
terraform login
-
Copy the
example.tfvars
file toproduction.tfvars
and fill in the necessary values. -
Initialize Terraform:
terraform init
-
Create a plan to preview the changes:
terraform plan -out plan.plan -var-file=production.tfvars
-
If everything looks good. Apply the configuration:
terraform apply plan.plan
To access the Chaos Mesh dashboard, you need to port-forward the service to your local machine.
-
Get the Chaos Mesh dashboard service name:
kubectl get svc -n chaos-mesh
-
Port-forward the service to your local machine:
kubectl port-forward svc/chaos-dashboard -n chaos-mesh 2333:2333
-
Generate a token to access the dashboard:
kubectl create token account-cluster-manager -n chaos-mesh
-
Access the dashboard by visiting
http://localhost:2333
in your browser.
This project uses Flux to manage the Kubernetes resources. Flux watches the Git repository for changes and applies the changes to the cluster.
You just have to push the changes to the repository, and Flux will automatically apply the changes to the cluster.