Step-by-Step Guide to Kubernetes Ingress and Traffic Management
Ingress in Kubernetes is a powerful tool that manages external access to the services in a cluster, typically HTTP. It allows you to define rules for routing traffic without creating a bunch of LoadBalancers or exposing each service on the node.
Hands-On Project: Exploring Kubernetes Ingress
1. Pre-requisites
A running Kubernetes cluster (you've got this set up on DigitalOcean).
kubectl
configured to communicate with your cluster.Helm, a package manager for Kubernetes, to install some tools.
2. Install an Ingress Controller
First, you need an Ingress Controller. The most commonly used is the Nginx Ingress Controller.
# Add the ingress-nginx repository
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
# Install the ingress-nginx controller
helm install ingress-nginx ingress-nginx/ingress-nginx --create-namespace --namespace ingress-nginx
3. Deploy Sample Applications
Deploy two simple web applications to test Ingress rules:
# app1-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: app1
spec:
replicas: 2
selector:
matchLabels:
app: app1
template:
metadata:
labels:
app: app1
spec:
containers:
- name: app1
image: hashicorp/http-echo
args:
- "-text=app1"
---
# app1-service.yaml
apiVersion: v1
kind: Service
metadata:
name: app1-service
spec:
selector:
app: app1
ports:
- protocol: TCP
port: 80
targetPort: 5678
# app2-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: app2
spec:
replicas: 2
selector:
matchLabels:
app: app2
template:
metadata:
labels:
app: app2
spec:
containers:
- name: app2
image: hashicorp/http-echo
args:
- "-text=app2"
---
# app2-service.yaml
apiVersion: v1
kind: Service
metadata:
name: app2-service
spec:
selector:
app: app2
ports:
- protocol: TCP
port: 80
targetPort: 5678
Deploy these using kubectl apply -f app1-deployment.yaml -f app1-service.yaml -f app2-deployment.yaml -f app2-service.yaml
.
4. Configure Ingress
Create an Ingress resource to route traffic based on the host or path:
# ingress-resource.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
spec:
ingressClassName: nginx
rules:
- host: app1.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: app1-service
port:
number: 80
- host: app2.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: app2-service
port:
number: 80
Deploy this with kubectl apply -f ingress-resource.yaml
.
5. Test Your Setup
Modify your
/etc/hosts
or DNS settings to pointapp1.example.com
andapp2.example.com
to the external IP of your Ingress Controller.Access
http://app1.example.com
andhttp://app2.example.com
in your browser.Modify
/etc/hosts
On your local machine, you need to edit the
/etc/hosts
file to point the domains (app1.example.com
andapp2.example.com
) to the external IP of the Ingress Controller. This file is used by your operating system to resolve hostnames before querying DNS servers.Open your
/etc/hosts
file: You'll need administrative or root privileges to do this.On Linux or macOS, you can open this file in a text editor with sudo permissions. For example:
sudo nano /etc/hosts
On Windows, the file is located at
C:\Windows\System32\drivers\etc\hosts
. You might need to open your text editor as an Administrator to modify this file.
Add entries for your applications:
Suppose your Ingress Controller's external IP is
203.0.113.123
. You would add these lines to your/etc/hosts
:203.0.113.123 app1.example.com 203.0.113.123 app2.example.com
Save and close the file.
Test Your Setup
After updating your /etc/hosts
file, open a web browser and access:
Your browser should display the responses from
app1
andapp2
respectively, routed through your Kubernetes Ingress. This setup mimics how DNS resolution will direct traffic in a production environment, allowing you to fully test the behavior of your Ingress under conditions similar to live deployment.If you encounter any issues or the web pages do not load as expected, ensure that the Ingress resources are configured correctly and that your services are running. You can also check the Ingress Controller logs for any errors:
kubectl logs -n ingress-nginx -l app.kubernetes.io/name=ingress-nginx
This setup allows you to explore basic routing between different applications based on the domain or path. From here, you can explore more advanced use cases like SSL/TLS termination, fanout to multiple services, or even use annotations to modify behavior specific to Nginx.
Subscribe to my newsletter
Read articles from Sundaram Kumar Jha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Sundaram Kumar Jha
Sundaram Kumar Jha
I Like Building Cloud Native Stuff , the microservices, backends, distributed systemsand cloud native tools using Golang