Skip to content

Ingress Resource Creation with Hostname Routing

Ingress Resource Creation with Hostname Routing is the practice of defining Kubernetes Ingress rules that route external HTTP traffic to specific Services within the cluster based on the Host header^[400-devops__06-Kubernetes__k8s-learning__06.ingress__README.md]. This allows a single IP address to serve multiple applications or domains.

Prerequisites

Before hostname routing can be utilized, an Ingress Controller must be installed and running in the cluster^[400-devops__06-Kubernetes__k8s-learning__06.ingress__README.md]. This controller is responsible for reading the Ingress resource definitions and implementing the routing rules, typically by configuring a load balancer like Nginx^[400-devops__06-Kubernetes__k8s-learning__06.ingress__README.md].

Configuration

Routing is achieved by creating an Ingress resource that specifies rules mapping a hostname to a backend Service^[400-devops__06-Kubernetes__k8s-learning__06.ingress__README.md].

Command Line Creation

The simplest way to create a basic Ingress with hostname routing is using the kubectl create ingress command^[400-devops__06-Kubernetes__k8s-learning__06.ingress__README.md]. The following command creates a rule that directs traffic for a specific hostname to a designated Service on port 80:

[kubectl](<./kubectl.md>) create [Ingress](<./ingress.md>) demo-localhost --class=nginx \
  --rule=demo.localdev.me/*=demo:80

In this example: * --class=nginx specifies the IngressClass. * --rule defines the routing logic: traffic for demo.localdev.me is routed to the Service named demo^[400-devops__06-Kubernetes__k8s-learning__06.ingress__README.md].

Traffic Routing

In Kubernetes environments that do not natively support a cloud LoadBalancer (where the EXTERNAL-IP remains <pending>), hostname routing is typically tested locally using port forwarding^[400-devops__06-Kubernetes__k8s-learning__06.ingress__README.md].

To verify the routing, the Ingress Controller service can be forwarded to a local port:

[kubectl port-forward](<./kubectl-port-forward.md>) --namespace=ingress-nginx service/ingress-nginx-controller 8080:80

Once active, accessing the URL http://demo.localdev.me:8080/ will direct the request through the Ingress Controller to the backend service defined in the rule^[400-devops__06-Kubernetes__k8s-learning__06.ingress__README.md].

  • Ingress
  • [[Service]]
  • [[LoadBalancer]]

Sources

^[400-devops__06-Kubernetes__k8s-learning__06.ingress__README.md]