Name-based routing – Managing Advanced Kubernetes Resources

Name-based or FQDN-based routing relies on the host header we pass while making an HTTP request. The Ingress resource can route based on the header. For example, if we want to access the v1 Service resource, we can use v1.example.com, and for the v2 Service resource, we can use the v2.example.com URL.

Let’s now have a look at the nginx-app-host-ingress.yaml manifest to understand this concept further:


spec:
rules:
host: v1.example.com http:
paths:
path: “/”
pathType: Prefix backend:
service:
name: nginx-v1
port:
number: 80
host: v2.example.com http:
paths:
path: “/”
pathType: Prefix backend:
service:
name: nginx-v2 port:
number: 80

The manifest now contains multiple hosts—v1.example.com routing to nginx-v1, and v2.example.com routing to nginx-v2.

Now, let’s apply this manifest and get the Ingress using the following commands:

$kubectlapply -f nginx-app-host-ingress.yaml 
$kubectlget ingress  
NAMEHOSTSADDRESSPORTS
nginx-appv1.example.com,v2.example.com34.120.27.3480

This time, we can see thattwo hosts are defined, v1.example.com and v2.example.com, running on the same address. Before we hit those endpoints, we need to make an entry on the / etc/hosts file to allow our machine to resolve v1.example.com and v2.example.com to the Ingress address.

Edit the /etc/hosts file and add the following entry at the end:

v1.example.com v2.example.com
Now, let’s curl both endpoints and see what we get:
$ curl v1.example.com
This is version 1
$ curl v2.example.com
This is version 2

And, as we can see, the name-based routing is working correctly! You can create a more dynamic setup by combining multiple hosts and path-based routing.

Service, Ingress, Pod, Deployment, and ReplicaSet resources help us to maintain a set number of replicas within Kubernetes and help to serve them under a single endpoint. As you may have noticed, they are linked together using a combination of labels and matchLabels attributes. The following diagram will help you visualize this:

Figure 6.7 – Linking Deployment, Service, and Ingress

Till now, we have been scaling our pods manually, but a better way would be to autoscale the replicas based on resource utilization and traffic. Kubernetes provides a resource calledHorizontalPodAutoscaler to handle that requirement.

Related Posts

Static provisioning – Managing Advanced Kubernetes Resources-2

As the Service resource is created, we can create a StatefulSet resource that uses the created PersistentVolume and Service resources. The StatefulSet resource manifest, nginx-manual-statefulset.yaml, looks like…

Static provisioning – Managing Advanced Kubernetes Resources-1

Static provisioning is the traditional method of provisioning volumes. It requires someone (typically an administrator) to manually provision a disk and create a PersistentVolume resource using the…

StatefulSet resources – Managing Advanced Kubernetes Resources

StatefulSet resources help manage stateful applications. They are similar to Deployment resources, but unlike a Deployment resource, they also keep track of state and require Volume and…

Managing stateful applications – Managing Advanced Kubernetes Resources

Imagine you’re a librarian in a magical library. You have a bunch of enchanted books that store valuable knowledge. Each book has a unique story and is…

Horizontal Pod autoscaling – Managing Advanced Kubernetes Resources-2

Now, let’s autoscale this deployment. The Deployment resource needs at least 1 pod replica and can have a maximum of 5 pod replicas while maintaining an average…

Horizontal Pod autoscaling – Managing Advanced Kubernetes Resources-1

Imagine you’re the manager of a snack bar at a park. On a sunny day, lots of people come to enjoy the park, and they all want…

Leave a Reply

Your email address will not be published. Required fields are marked *