Terraform kubernetes provider on minikube. Creating namespaces and running nginx deployment

2021, May 04    

How to runn Nginx deployment in Minikube with Terraform in own namespace

Prerequisites :

  • terraform
  • minikube

What we will complete by the end of this exercise :

  • Create 2 namespaces
  • Deploy Nginx deployment
  • Create a service with type NodePort to access it

Nodeport exposes the service on every node on a fixed port. In our case, will expose it on our minikube ip and fixed , dedicated port

Which makes it easy to access in combination with minikube ip

1) Start minikube :

minikube start

2) Clone this article GIT repo :

Clone

cd applications

Edit main.tf file, and edit appropiately to point to your kubeconfig file location :

provider "kubernetes" {
  config_context_cluster   = "minikube"
  config_path = "~/.kube/config"
}

3) Initialize terraform

terraform init

4) Plan

terraform plan

5) Terraform apply

terraform apply

Enter : Yes

6) Verify Namespaces get created

kubectl get ns

Should show :

NAME                     STATUS   AGE
applications-namespace   Active   6s
monitoring-namespace     Active   6s
default                  Active   37d
kube-node-lease          Active   37d
kube-public              Active   37d
kube-system              Active   37d

6) Verify Nginx pods are created

kubectl get po -n applications

Expected output :

  applications git:(main) 
NAME                                      READY   STATUS    RESTARTS   AGE
scalable-nginx-example-5fbb9989bf-9mz9w   1/1     Running   0          82s
scalable-nginx-example-5fbb9989bf-mbhvd   1/1     Running   0          82s

7) Access Nginx deployment via NodePort IP

kubectl get svc -n applications
NAME            TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
nginx-example   NodePort   10.100.32.98   <none>        80:30201/TCP   2m35s

Use the port , from the output and “minikube ip”

  applications git:(main) minikube ip
192.168.64.2

Taking into account my output :

Port : 30201

Minikube ip : 192.168.64.2

Nginx service will be accessible on :

192.168.64.2:30201

8) You will see “Welcome to nginx!” page