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 start2) Clone this article GIT repo :
cd applicationsEdit 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 init4) Plan
terraform plan5) Terraform apply
terraform applyEnter : Yes
6) Verify Namespaces get created
kubectl get nsShould 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 37d6) Verify Nginx pods are created
kubectl get po -n applicationsExpected 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 82s7) Access Nginx deployment via NodePort IP
kubectl get svc -n applicationsNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-example NodePort 10.100.32.98 <none> 80:30201/TCP 2m35sUse the port , from the output and “minikube ip”
➜ applications git:(main) minikube ip
192.168.64.2Taking into account my output :
Port : 30201
Minikube ip : 192.168.64.2
Nginx service will be accessible on :
192.168.64.2:302018) You will see “Welcome to nginx!” page