Skip to main content

Lab 8.4 - Multi-domains and Virtual Hosts

Lab Objectives

By the end of this lab, you will be able to:

  • Configure multiple domains in a single Ingress.
  • Route traffic to different applications based on the domain.
  • Understand virtual hosts.
  • Configure TLS for multiple domains.
  • Test access to multiple domains.

Estimated Duration

45-60 minutes

Prerequisites

  • kubectl installed and configured.
  • Kubernetes cluster with Ingress Controller.
  • Knowledge of Ingress (previous labs).

Part 1: Deploying Multiple Applications

Let's deploy three different applications for three domains.

Step 1.1: Application for Domain1

Create 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: nginx
image: nginx:1.20
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: app1-service
spec:
selector:
app: app1
ports:
- port: 80
targetPort: 80

Step 1.2: Application for Domain2

Create 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: httpd
image: httpd:2.4
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: app2-service
spec:
selector:
app: app2
ports:
- port: 80
targetPort: 80

Step 1.3: Application for Domain3

Create app3-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
name: app3
spec:
replicas: 2
selector:
matchLabels:
app: app3
template:
metadata:
labels:
app: app3
spec:
containers:
- name: nginx
image: nginx:1.20
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: app3-service
spec:
selector:
app: app3
ports:
- port: 80
targetPort: 80

Apply all deployments:

kubectl apply -f app1-deployment.yaml
kubectl apply -f app2-deployment.yaml
kubectl apply -f app3-deployment.yaml

Part 2: Multi-domain Ingress Configuration

Step 2.1: Create the Ingress with Multiple Domains

Create ingress-multi-domain.yaml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: multi-domain-ingress
spec:
ingressClassName: nginx
rules:
# First domain
- host: app1.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: app1-service
port:
number: 80
# Second domain
- host: app2.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: app2-service
port:
number: 80
# Third domain
- host: app3.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: app3-service
port:
number: 80

Apply the Ingress:

kubectl apply -f ingress-multi-domain.yaml

Part 3: Configuring Hostnames

Step 3.1: Add Entries to /etc/hosts

Add the three domains to your /etc/hosts file (or Windows equivalent):

<INGRESS_IP> app1.local
<INGRESS_IP> app2.local
<INGRESS_IP> app3.local

Replace <INGRESS_IP> with your Ingress Controller IP.


Part 4: Testing the Domains

Step 4.1: Test Each Domain

Test access to each domain:

# Test app1.local
curl http://app1.local

# Test app2.local
curl http://app2.local

# Test app3.local
curl http://app3.local

You should see different responses depending on the domain.

Step 4.2: Verify with the Host Header

You can also test with the Host header:

curl -H "Host: app1.local" http://<INGRESS_IP>
curl -H "Host: app2.local" http://<INGRESS_IP>
curl -H "Host: app3.local" http://<INGRESS_IP>

Part 5: TLS for Multiple Domains

Step 5.1: Create Certificates for Each Domain

If you are using cert-manager, create certificates for each domain:

apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: app1-tls-cert
spec:
secretName: app1-tls-secret
issuerRef:
name: selfsigned-issuer
kind: ClusterIssuer
dnsNames:
- app1.local
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: app2-tls-cert
spec:
secretName: app2-tls-secret
issuerRef:
name: selfsigned-issuer
kind: ClusterIssuer
dnsNames:
- app2.local
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: app3-tls-cert
spec:
secretName: app3-tls-secret
issuerRef:
name: selfsigned-issuer
kind: ClusterIssuer
dnsNames:
- app3.local

Step 5.2: Configure TLS in the Ingress

Modify the Ingress to include TLS:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: multi-domain-ingress-tls
spec:
ingressClassName: nginx
tls:
- hosts:
- app1.local
secretName: app1-tls-secret
- hosts:
- app2.local
secretName: app2-tls-secret
- hosts:
- app3.local
secretName: app3-tls-secret
rules:
- host: app1.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: app1-service
port:
number: 80
- host: app2.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: app2-service
port:
number: 80
- host: app3.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: app3-service
port:
number: 80

Part 6: Cleanup

Delete the resources:

kubectl delete ingress multi-domain-ingress
kubectl delete -f app1-deployment.yaml
kubectl delete -f app2-deployment.yaml
kubectl delete -f app3-deployment.yaml

Lab Summary

In this lab, you configured an Ingress with multiple domains (virtual hosts). You learned how to route traffic to different applications based on the domain, configure TLS for multiple domains, and test access to each domain.


Next Steps

This module on Ingress and Load Balancing is now complete. You can proceed to Module 9 on Security and RBAC.

Module 9: Security and RBAC


Lab created: December 2024