Introduction
In this lab, you will learn how to use Ingress to route external traffic to services running in a Kubernetes cluster.
Kubernetes Ingress is a powerful tool for managing external access to services in a Kubernetes cluster. Ingress acts as a layer 7 load balancer, allowing you to route traffic to different services based on the incoming URL path or hostname.
In this lab, we will create a sample application and expose it to the outside world using Ingress. We will use nginx-ingress as the Ingress controller, which is a popular and widely-used solution for Kubernetes Ingress.
Start the Minikube Cluster
Before creating resources, you need a running Kubernetes cluster. Minikube is a lightweight Kubernetes environment that runs on your local machine.
Navigate to your working directory:
Open the terminal and navigate to the default project folder:
cd /home/labex/projectStart Minikube:
Start Minikube to initialize a Kubernetes cluster:
minikube start- This command sets up a single-node Kubernetes cluster on your local machine.
- Minikube may take a few minutes to start depending on your system's performance.
Verify Minikube is running:
Check the status of the Minikube cluster:
minikube status- Look for components like
kubeletandapiserverlisted asRunning. - If the cluster is not running, rerun
minikube start.
- Look for components like
If you encounter issues starting Minikube. Use minikube delete to reset the environment if needed.
Install the Nginx Ingress Controller
First, we need to install the nginx-ingress controller in our cluster. We can do this by creating a Deployment and a Service that will be responsible for running the Ingress controller.
Create a namespace for the Ingress controller:
kubectl create namespace ingress-nginx
Install the ingress-nginx chart using kubectl:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.7.0/deploy/static/provider/cloud/deploy.yaml
Verify that the ingress-nginx controller pods are running:
kubectl get pods -n ingress-nginx
NAME READY STATUS RESTARTS AGE
ingress-nginx-admission-create-zjfqx 0/1 ContainerCreating 0 2s
ingress-nginx-admission-patch-8rvzw 0/1 ContainerCreating 0 2s
ingress-nginx-controller-6bdb654777-qz8fb 0/1 ContainerCreating 0 2s
Create a Sample Application
Next, we will create a sample application that we will expose using Ingress.
Create a Deployment for a sample application:
apiVersion: apps/v1
kind: Deployment
metadata:
name: sample-app
spec:
replicas: 3
selector:
matchLabels:
app: sample-app
template:
metadata:
labels:
app: sample-app
spec:
containers:
- name: sample-app
image: nginx
ports:
- containerPort: 80
The YAML file called sample-app.yaml. Apply the deployment to your cluster with the following command:
kubectl apply -f sample-app.yaml
Create a Service for the sample application:
apiVersion: v1
kind: Service
metadata:
name: sample-app
spec:
selector:
app: sample-app
ports:
- name: http
port: 80
targetPort: 80
The YAML file called service-sample-app.yaml. Apply the deployment to your cluster with the following command:
kubectl apply -f service-sample-app.yaml
Create an Ingress Resource
Now that we have the Ingress controller set up and a backend service running, we can create the rules for the Ingress resource.
In this example, we will create a simple rule to route traffic for the test.local domain to our backend service:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
- host: test.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: sample-app
port:
name: http
The YAML file called ingress.yaml. Apply the Ingress resource to the cluster:
kubectl apply -f ingress.yaml
Test the Ingress Resource
Finally, we can test the Ingress resource to make sure everything is working correctly.
First, determine the IP address of the node:
kubectl get node -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
minikube Ready control-plane 93s v1.26.1 192.168.49.2 <none> Ubuntu 20.04.5 LTS 5.15.0-56-generic docker://20.10.23
This command will get the kubernetes node address, The IP address labeled as INTERNAL-IP.
Next, add an entry to your /etc/hosts file to map the test.local domain to the IP address of the node:
echo "<IP_ADDRESS> test.local" | sudo tee -a /etc/hosts
Replace <IP_ADDRESS> with the internal IP address of the node address. For example:
echo "192.168.49.2 test.local" | sudo tee -a /etc/hosts
Then, get service nodeport for ingress-nginx.
kubectl get services -n ingress-nginx
This command will dcurisplay a list of services in the ingress-nginx namespace. Look for the nginx-ingress-controller service and note its NodePort.
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller LoadBalancer 10.104.99.158 <pending> 80:32518/TCP,443:31620/TCP 2m45s
ingress-nginx-controller-admission ClusterIP 10.100.46.109 <none> 443/TCP 2m45s
Finally, use curl to make an HTTP request to the Ingress endpoint:
curl test.local:NodePort
For example:
curl test.local:32518
Replace <NodePort> with the NodePort of the nginx-ingress-controller service.
If everything is set up correctly, you should see the Nginx welcome page.
You can also test the Ingress by using a web browser to visit test.local:<NodePort>/nginx.
Congratulations, you have successfully set up an Ingress resource in Kubernetes and tested it to ensure that it is working correctly.
Summary
In this lab, we walked through the process of setting up and configuring an Nginx ingress controller in Kubernetes. We also created a sample application and used Ingress to route external traffic to our Nginx service. This is just the beginning of what you can do with Kubernetes networking, and we encourage you to explore further.


