How to add NGINX Ingress controller in your local k8s
After for the umpteenth time type the command port-forward I noticed that I had to use Ingress 😜 and avoid the famous DRY (Don’t Repeat Yourself).
Disclaimer → Is not wrong use port-forward command, but if you can don’t remember it, looks great.
Assumptions → You’re familiar with docker and has installed the softwares below.
For this tutorial I’m using the following softwares
In terms of deploy, I’m using an application built with Quarkus, with 2 endpoints, /resteasy-jackson/quarks and /hello-resteasy. The image is available on my Docker hub.
This is an overview of those piece together, and let’s analyse how configure them.
- Create a cluster using kind, such as the content below
cat <<EOF | kind create cluster --name=16-bits --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
extraPortMappings:
- containerPort: 80
hostPort: 80
protocol: TCP
- containerPort: 443
hostPort: 443
protocol: TCP
EOF
A screen like that is expected
2. Check the localhost using curl and nothing should be returned
curl localhost
3. Create the Ingress. The Ingress will be the key piece to connect our service to external environment without use the port-forward command.
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/kind/deploy.yaml
4. Check the k8s namespaces, and the new namespace has been created, ingress-nginx.
kubectl get namespaces
5. Check if the service has been deployed and execute a request to localhost. Now a NGINX error is returned, and it’s the expected result.
kubectl wait --namespace ingress-nginx \
--for=condition=ready pod \
--selector=app.kubernetes.io/component=controller \
--timeout=90scurl localhost
6. Create a namespace to deploy the service, for it type the command below
kubectl create namespace 16-bits
7. Using the namespace 16-bits, deploy the service
kubectl -n 16-bits apply -f https://github.com/luizgustavocosta/16-bits-quarkus/blob/main/service-4-k8s/src/main/resources/k8s/kubernetes.yml
8. Check wether the service is running. First, you need to know the pod, follow the instructions below
kubectl get pods -n 16-bits
kubectl logs -n bits <pod name>
9. Ingress NGINX 🖥 is on fire 🔥
You can use cURL or your browser.
curl http://localhost/hello-resteasy
Coming up → k8s dashboard
Conclusion
Is more easy and more professional has a Ingress instead of manage the ports by myself, since this decrease the error prone.
References