Before configuring an ingress service, we need our backend services to provide connectivity within the server. We will use the following deployment to create two Pods that run NGINX and Apache, and two services that provide connectivity to the Pods.
backend.yml
# NGINX Pod
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-webserver
spec:
selector:
matchLabels:
run: nginx-webserver
replicas: 1
template:
metadata:
labels:
run: nginx-webserver
spec:
containers:
- name: nginx-webserver
image: nginx
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
---
# Apache Pod
apiVersion: apps/v1
kind: Deployment
metadata:
name: apache-webserver
spec:
selector:
matchLabels:
run: apache-webserver
replicas: 1
template:
metadata:
labels:
run: apache-webserver
spec:
containers:
- name: apache-webserver
image: httpd
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
---
# NGINX Service
apiVersion: v1
kind: Service
metadata:
name: nginx-webserver
labels:
run: nginx-webserver
spec:
ports:
- port: 80
protocol: TCP
selector:
run: nginx-webserver
---
# Apache Service
apiVersion: v1
kind: Service
metadata:
name: apache-webserver
labels:
run: apache-webserver
spec:
ports:
- port: 80
protocol: TCP
selector:
run: apache-webserver
Apply the configuration by using the “kubectl apply” command.
Verify that the resource was created by running the “kubectl get all” command.
NGINX Ingress Configuration
The Minikube ingress add-on automatically configures the NGINX Ingress controller within the local Kubernetes cluster. First, enable the addon using the command below.
minikube addons enable ingress.
We will now create a name-based virtual host ingress, pointing to our previously created services, as shown below.
nginx-ingress.yml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
# Point to NGINX Service
- host: nginx.k8s.localhost.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-webserver
port:
number: 80
# Point to Apache Service
- host: apache.k8s.localhost.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: apache-webserver
port:
number: 80
Finally, apply the ingress configuration above by using the following commands.
kubectl apply -f .\nginx-ingress.yml
kubectl get ingress
When you follow the configured URLs, you will be directed to the relevant Pods.
Figure 2 - NGINX Ingress Controller routing result.
Traefik Ingress Configuration
The easiest way to install Traefik in Kubernetes is via a Helm Chart. Just run the following commands.
helm repo add traefik https://helm.traefik.io/traefik
helm repo update
helm install traefik traefik/traefik.fffff
Next, verify that the Helm chart creates the required Pods and services as expected.
The next step is to create the ingress configuration for Traefik. The "router.entrypoint" annotation ensures that the Ingress only listens to the Traefik service created by the Helm chart.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: traefik-ingress
annotations:
traefik.ingress.kubernetes.io/router.entrypoints: web
spec:
rules:
# Point to NGINX Service
- host: nginx.k8s.localhost.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-webserver
port:
number: 80
# Point to Apache Service
- host: apache.k8s.localhost.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: apache-webserver
port:
number: 80
The following command exposes the Traefik web UI.
kubectl port-forward $(kubectl get pods --selector "app.kubernetes.io/name=traefik" --output=name) 9588:9000
By looking at the HTTP services within the dashboard, you can see that there are two ingress routes. These routes are configured for two services in Kubernetes, indicating that the Ingress configuration is now complete.
Figure 4 - Traefik Interface with Kubernetes Routing