# Ingress in bare metal Kubernetes cluster
You are working in enterprise that have one cluster with one public IP address and one Domain Name. In our Example justk8s.k8s
.
That Enterprise want to deploy two web apps for these new service (gym and restaurant) with that single cluster,IP and Domain Name !!
Your task is to deploy these two apps! So you decide to use Ingress
because it’s the best solution for this kind of problems!
# What is Ingress
Kubernetes Ingress is an API object that provides routing rules to manage external users’ access to the services in a Kubernetes cluster, typically via HTTPS/HTTP. With Ingress, you can easily set up rules for routing traffic without creating a bunch of Load Balancers or exposing each service on the node
# Ingress Controller
An Ingress Controller is a Kubernetes controller that is deployed manually to the cluster, most often as a DaemonSet or a Deployment object that runs dedicated Pods for handling incoming traffic load balancing and smart routing. It is responsible for processing the Ingress objects (which specify that they especially want to use the Ingress Controller) and dynamically configuring real routing rules
The most common used Ingress controllerfor Kubernetes is Ingress Nginx
# Ingress in cloud Vs Ingress in bare metal cluster
Deploying Ingress pn managed Kubernetes cluster provided in cloud easier than in bare metal cluster. Because the Ingress Controller must be exposed as LoadBlancer!
In traditional cloud environments, where network load balancers are available on-demand, a single Kubernetes manifest suffices to provide a single point of contact to the NGINX Ingress controller to external clients and, indirectly, to any application running inside the cluster.
Bare-metal environments lack this commodity, requiring a slightly different setup to offer the same kind of access to external consumers. So we need an aproach to solve this problem !
# Create Load Balancer / Proxy for bare metal cluster
There is a lot of method to set up a Load Balancer or Proxy to get Ingress work on our cluster.
- We can expose our ingress controller using NodePort service and set up a proxy that forward to this service using a nginx proxy or HA proxy
- We can also install a software solution called
metalLB
. This solution allows to create Load Balancer in local cluster without any problem ! So we can expose our ingress controller using Load Balancer service like in the cloud environment!
# Create Load Balancer using metalLB:
In this demo we will deploy one of the 2 apps and expose it with Load Balancer service for the test purposes. So Let’s start deploying the restaurant app and expose it !
1 | apiVersion: apps/v1 |
Create the gym app deployment
1 | raf²@4n6nk8s-master$ kubectl apply -f kitchen-deployment.yaml |
Let’s now expose now the deployment:
1 | raf²@4n6nk8s-master$ kubectl expose deploy/4n6nk8s-kitchen--type=LoadBalacner --port=80 |
Let’s check this service
1 | raf²@4n6nk8s-master$ kubectl get svc |
As we see the LoadBalancer service still pending since we don’t have any Load Balancer! Now it’s time to talk about the metalLB !
MetalLB provides a network load-balancer implementation for Kubernetes clusters that do not run on a supported cloud provider, effectively allowing the usage of LoadBalancer Services within any cluster.
It’s time to install metalLB. Installing metalLB is so easy we just need to apply two manifest from the official site of metalLB
1 | kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.12.1/manifests/namespace.yaml |
After running the previous commands, a new namespace will be created with a deployment, daesmonset and lot of RBAC authorization and rules!. So let’s check this namespace!
1 | raf²@4n6nk8s-master$ kubectl get all -n metallb-system |
# Configure the address pool of the Load Balancer
Now we must configure the metalLB
using configmap object. In this configmap we give the address pool that will be used as LoadBalancer IP! Also the type of the Load Balancer.
metalLB supports two LoadBalancing modes : Layer 2 mode and BGP mode. In our case we will use the Layer 2 mode
Now Let’s move to the configmap manifest!
1 | apiVersion: v1 |
After Applying this recent configmap. we can say that the metalLB installtion is finished! So Let’s check the services again in our cluster !
1 | raf²@4n6nk8s-master$ kubectl get svc |
Bingo! our Load Balancer service has an external IP address. So our deployment is accessable through this IP! Let’s check it:
ٍ
Everything is OK! now we can say that we have a Load Balancer on our bare metal cluster!
# Install the nginx Ingress Controller:
Installing the nginx ingress controller can be done either by using Helm or the yaml manifest provided in the nginx controller website
# Install the controller using Helm
If you have Helm, you can deploy the ingress controller with the following command:
1 | raf²@4n6nk8s-master$ helm upgrade --install ingress-nginx ingress-nginx \ |
# Install the controller using yaml manifest:
If you don’t have Helm or if you prefer to use a YAML manifest, you can run the following command instead:
1 | raf²@4n6nk8s-master$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.2.0/deploy/static/provider/cloud/deploy.yaml |
# Check the Nginx Ingress Controller:
Let’s check the pods created in the namespace of the ingress-nginx
1 | raf²@4n6nk8s-master$ kubectl get pods --namespace=ingress-nginx |
After waiting a few minute until the controller is ready, we must check all the services and deployments of the ingress-nginx namespace again ! You must find an output similar to the following
1 | raf²@4n6nk8s-master$ kubectl get all -n ingress-nginx |
Bingo! the ingress controller is ready and on running state! also is exposed with Load Balancer and it have a Extenal-IP 192.168.1.240
Now we can deploy an ingress object without any problem ! It’s time to deploy our 2 apps with ingress in one single domain name and one IP address !
# Deploy The two Apps using Ingress:
Let’s start by deploying the apps and expose it with Cluster IP service !
1 | apiVersion: apps/v1 |
Create the deployment and service of the gym app
1 | raf²@4n6nk8s-master$ kubectl apply -f gym-deployment.yaml |
The manifest of the restaurant app and the service:
1 | apiVersion: apps/v1 |
Create the restaurant deployment and the service:
1 | raf²@4n6nk8s-master$ kubectl apply -f kitchen-deployment.yaml |
Check the deployments and the services! :
1 | raf²@4n6nk8s-master$ kubectl get all |
Let’s now create the ingress manifest:
1 |
|
The ingressClassName
must be “nginx” since we have nginx ingress controller. The host
field in the rules
define the domain name of the request. So if a HTTP request come to the cluster with the host name justk8s.k8s
this rull will be applied!
The the path
int the paths
will be the route ! The ingress give you the service in this route!
In our Example if we enter http://justk8s.k8s/kitchen
the ingress will forward the restaurant app!
in the path
field we specify the service
name and the port number
Now Let’s Create the ingress object
1 | raf²@4n6nk8s-master$ kubectl apply -f ingress.yaml |
Check the ingerss object !
1 | raf²@4n6nk8s-master$ kubectl get ingress |
Display more details about the ingress object with kubectl describe ingress
:
1 | raf²@4n6nk8s-master$ kubectl describe ingress ingress-4n6nk8s |
We see that the Default backend display (<error: endpoints "default-http-backend" not found>)
.
This error occur because we don’t specify a default backend (route) so when you enter http://justk8s.k8s/
we will find the nginx 404 not found page !
# Check the Deployments!
After Installing the LoadBalancer and Ingress Controller. Let’s try to access to our 2 apps. But before access to this apps let’s create an /etc/hosts
entry ( C:\Windows\System32\Drivers\etc\hosts
for Windows):
1 | 192.168.1.240 justk8s.k8s # The IP address of the Load Balancer of the Ingress Controller ! |
Now Let’s go to our browser! I add this entry in my master node! So I can access to these apps from my master node !:
Bingo! Everything is Ok !