Using Nginx Ingress Controller in Kubernetes bare-metal setup

Jeganathan Swaminathan ( jegan@tektutor.org )
tektutor
Published in
4 min readMar 18, 2022

Jeganathan Swaminathan ( jegan@tektutor.org )

nginx ingress controller

This articles assumes you already have a Kubernetes setup on your local environment.

In case you would like to setup a local K8s cluster, check my other article https://medium.com/@jegan_50867/kubernetes-3-node-cluster-setup-50943378be41.

In a bare-metal on-prem Kubernetes setup, certain critical features of Kubernetes will not work by default.

Just to give an idea, things that won’t work out of the box in a bare-metal Kubernetes setup are

  1. Load Balancer Service
  2. Persistent Volume
  3. Ingress

Load Balancer Service

Kubernetes LoadBalancer service creates an external Load Balancer to manage the Pods traffic associated with a particular application service.

For example:-

  • AWS will spin-off a Network Load Balancer with a public IP.
  • Azure will spin-off an Outbound Azure Load Balancer with a public IP.

But in case of bare-metal, we need to setup a Load Balancer before we can successfuly create a LoadBalancer type of K8s service for our applications.

For more details, you may refer my article here https://medium.com/@jegan_50867/using-metal-lb-on-a-bare-metal-onprem-kubernetes-setup-6d036af1d20c.

Persistent Volume

Stateful applications that depends on database or that saves logs and or critical application data, requires external Persistent Volume to ensure the data isn’t lost when the Pod lifetime ends.

Kubernetes Application that need external storage can request for storage by way of PersistentVolumeClaims. But unless Kubernetes finds a Persistent Volume that can satisfy the requested claim, the Pod that requested the claim will be in Pending state.

The cloud providers, offers default storage classes to take care of this. But in bare-metal K8s setup, we need to do the heavy-lifting by creating necessary PersistentVolumes that match the storage class of the PersistentVolumeClaims requested by your applications.

For more details, you may refer my other article here https://medium.com/@jegan_50867/deploying-stateful-applications-in-kubernetes-8ffd46920b55.

Ingress

In Kubernetes, Ingress is used to configure routing rules to different services of your application. This also offers a public user-friendly URL for the end-users.

But the Ingress is just routing rules, Kubernetes requires an Ingress Controller that grabs these routing rules and configure the Load Balancer dynamically in your K8s cluster.

The cloud environments like AWS, AZure, GCP, etc., the Ingress Controller is supplied by the cloud provider.

In bare-metal K8s cluster, unless we deploy an Ingress Controller into our Kubernetes cluster, the Ingress will not work.

This articles focuses on this issue and demonstrates on how to use Nginx Ingress Controller in a bare-metal Kubernetes setup.

Let’s create a simple application deployment

kubectl create deploy nginx --image=nginx:1.18
kubectl scale deploy/nginx --replicas=6
kubectl expose deploy/nginx --type=NodePort --port=80

Installing HELM 3

curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3chmod 700 get_helm.sh./get_helm.sh

Setting up HELM Ingress repository

helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginxhelm repo updatehelm repo list

Installing Nginx Controller using HELM

helm upgrade --install myingress ingress-nginx/ingress-nginx --namespace ingress-nginx --create-namespace --values values.yml

Creating Ingress Rules

Let’s create an Ingress rule by creating a manifest file named ingress.yml.

ingress.yml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: tektutor-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: "tektutor.training.org"
http:
paths:
- pathType: Prefix
path: "/nginx"
backend:
service:
name: nginx
port:
number: 80

Let’s create the ingress using the ingress.yml file.

kubectl apply -f ingress.yml

Let’s list and describe the ingress resource

[jegan@master.tektutor.org ~]$ kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
tektutor-ingress <none> tektutor.training.org 80 4m20s
[jegan@master.tektutor.org ~]$ curl tektutor.training.org
[jegan@master.tektutor.org ~]$ kubectl describe ingress tektutor-ingress
Name: tektutor-ingress
Labels: <none>
Namespace: default
Address:
Default backend: default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
Host Path Backends
---- ---- --------
tektutor.training.org
/nginx nginx:80 (192.168.145.193:80,192.168.145.194:80,192.168.145.195:80 + 3 more...)
Annotations: kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /
Events: <none>

Before we attempt to access the ingress, we need add the ingress url with master node IP in the /etc/hosts file. This must be done on the machine where you wish to test the ingress.

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.167.151 master.tektutor.org
192.168.167.135 worker1.tektutor.org
192.168.167.136 worker2.tektutor.org
192.168.167.151 tektutor.training.org

Now you may test the ingress as shown below

curl tektutor.training.org

The expected output is

[jegan@master.tektutor.org ~]$ curl tektutor.training.org<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

You can follow the author to get notified when he publishes new articles.

If you found this post helpful, please click the clap 👏 button below a few times to show your support for the author 👇

Thank you !

My other articles

Setting up a 3 node Kubernetes on your local machine

https://medium.com/@jegan_50867/kubernetes-3-node-cluster-setup-50943378be41

Using Metal LB in a bare metal /onprem K8s setup

https://medium.com/@jegan_50867/using-metal-lb-on-a-bare-metal-onprem-kubernetes-setup-6d036af1d20c

--

--

Jeganathan Swaminathan ( jegan@tektutor.org )
tektutor

Freelance Software Consultant & Corporate Trainer.I deliver training & provide consulting — DevOps,K8s, OpenShift,TDD/BDD,CI/CD,Microservices etc.