0
0
Kubernetesdevops~5 mins

Host-based routing in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Host-based routing lets you send web traffic to different services based on the website address visitors use. This helps run multiple websites on the same server or cluster without mixing their traffic.
When you want to run multiple websites on the same Kubernetes cluster using one IP address.
When you need to direct traffic to different backend services depending on the domain name users visit.
When you want to manage several apps under different domains but share the same load balancer.
When you want to separate traffic for testing and production environments using different hostnames.
When you want to simplify DNS management by routing multiple hosts through one Ingress.
Config File - ingress.yaml
ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  namespace: default
spec:
  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

This Ingress resource defines rules for routing traffic based on the host header.

rules: List of host-based routing rules.

host: The domain name to match in the request.

http.paths: Paths under the host to route.

backend.service.name: The Kubernetes Service to send traffic to.

backend.service.port.number: The port on the Service to use.

Commands
This command creates the Ingress resource in Kubernetes, setting up host-based routing rules.
Terminal
kubectl apply -f ingress.yaml
Expected OutputExpected
ingress.networking.k8s.io/example-ingress created
This command checks that the Ingress resource was created and shows its current status and assigned IP address.
Terminal
kubectl get ingress example-ingress
Expected OutputExpected
NAME CLASS HOSTS ADDRESS PORTS AGE example-ingress <none> app1.example.com,app2.example.com 192.168.1.100 80 10s
This command shows detailed information about the Ingress, including rules and backend services for troubleshooting.
Terminal
kubectl describe ingress example-ingress
Expected OutputExpected
Name: example-ingress Namespace: default Address: 192.168.1.100 Default backend: <none> Rules: Host Path Backends ---- ---- -------- app1.example.com / app1-service:80 (10.1.1.10:80) app2.example.com / app2-service:80 (10.1.2.10:80) Annotations: <none> Events: <none>
Key Concept

If you remember nothing else from this pattern, remember: host-based routing sends traffic to different services based on the website address users visit.

Common Mistakes
Not specifying the host field in the Ingress rules.
Without the host, the Ingress cannot route traffic based on domain names and will not separate traffic correctly.
Always include the host field in each rule to define which domain name the rule applies to.
Using the wrong service name or port in the backend configuration.
Traffic will fail to reach the intended service, causing errors or no response.
Double-check service names and ports match the actual Kubernetes Services deployed.
Not applying the Ingress resource after creating or editing the YAML file.
Changes won't take effect until the Ingress resource is applied to the cluster.
Run 'kubectl apply -f ingress.yaml' after any changes to update the Ingress.
Summary
Create an Ingress resource with host-based rules to route traffic by domain name.
Apply the Ingress YAML file to the Kubernetes cluster using kubectl apply.
Verify the Ingress is active and routing correctly with kubectl get and describe commands.