DEV Community

Emmanuel Ogar
Emmanuel Ogar

Posted on

Deploying a Nodejs app using Terraform to kind Kubernetes cluster.

This article explains how terraform is used to deploy the kubectl manifest of a simple Node.js app to the kind cluster installed locally and set up monitoring and observability using the kubectl terraform provider.

Prerequisites

  • Docker to run kind cluster locally.
  • Kubectl to perform basic Kubernetes functions on our cluster
  • Terraform CLI.
  • Helm to setup monitoring and observability for the prometheus cluster.

Install Docker

Run the below command to install Docker

sudo apt update -y && sudo apt install docker.io -y && sudo systemctl enable --now docker
Enter fullscreen mode Exit fullscreen mode

Verify Docker Installation

docker --version
Enter fullscreen mode Exit fullscreen mode

Install Kubectl

sudo apt update && sudo snap install kubectl --classic
Enter fullscreen mode Exit fullscreen mode

Verify Kubectl Installation

kubectl version --output=yaml
Enter fullscreen mode Exit fullscreen mode

Install Terraform

Install Terraform from APT repository

sudo apt install software-properties-common gnupg2 curl -y
Enter fullscreen mode Exit fullscreen mode
curl https://apt.releases.hashicorp.com/gpg | gpg --dearmor > hashicorp.gpg
sudo install -o root -g root -m 644 hashicorp.gpg /etc/apt/trusted.gpg.d/
Enter fullscreen mode Exit fullscreen mode
sudo apt-add-repository "deb [arch=$(dpkg --print-architecture)] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
Enter fullscreen mode Exit fullscreen mode

Now install terraform on your Ubuntu Linux system:

sudo apt install terraform
Enter fullscreen mode Exit fullscreen mode

Verify Terraform Installation

$ terraform --version
Terraform v1.5.7
on linux_amd64
Enter fullscreen mode Exit fullscreen mode

Install Helm

curl https://baltocdn.com/helm/signing.asc | gpg --dearmor | sudo tee /usr/share/keyrings/helm.gpg > /dev/null
sudo apt-get install apt-transport-https --yes
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/helm.gpg] https://baltocdn.com/helm/stable/debian/ all main" | sudo tee /etc/apt/sources.list.d/helm-stable-debian.list
sudo apt-get update
sudo apt-get install helm
Enter fullscreen mode Exit fullscreen mode

Lets Begin

  • Clone this repo:
git clone https://github.com/emmanuelogar/Node.js_app_using_terraform.git && cd Node.js_app_using_terraform
Enter fullscreen mode Exit fullscreen mode

run

./install_kind.sh

to install kind and create a local kind cluster with the specified name, provides cluster information, downloads the kubeconfig for the cluster and store in a file.

Run the following terraform commands to:

  • Deploy the kubernetes manifest to the running kind cluster using the kubectl terraform provider.
  • setup monitoring and observability for the prometheus cluster using the kube-prometheus stack with terraform helm provider.
terraform init
terraform plan -out=tfplan
terraform apply "tfplan"
Enter fullscreen mode Exit fullscreen mode
  • #### Connecting to Prometheus: Port Forward to Prometheus
kubectl port-forward -n monitoring svc/kube-prometheus-kube-prome-prometheus 9090
Enter fullscreen mode Exit fullscreen mode

This command forwards local port 9090 to the Prometheus service's port within the monitoring namespace.

Access Prometheus Web UI:
Open your web browser and navigate to


. You should see the Prometheus web user interface, where you can query metrics and create custom dashboards.

- #### Connecting to Grafana:
Port Forward to Grafana:


Enter fullscreen mode Exit fullscreen mode

kubectl port-forward -n monitoring svc/kube-prometheus-grafana 3000:80



This command forwards local port 3000 to the Grafana service's port within the monitoring namespace.

**Access Grafana Web UI:**
Open your web browser and go to

 ``` http://localhost:3000```

. Grafana's default login credentials are typically:

Username: admin
Password: You might need to obtain the password from your Grafana configuration or secret.

To retrieve the Grafana admin password from the secret


Enter fullscreen mode Exit fullscreen mode

kubectl get secrets -n monitoring
kubectl describe secret kube-prometheus-grafana -n monitoring
kubectl get secret kube-prometheus-grafana -n monitoring -o jsonpath="{.data.admin-password}" | base64 --decode



After logging in, you can set up dashboards and visualize your Kubernetes cluster's metrics.

Enter fullscreen mode Exit fullscreen mode

Top comments (0)