Establish Kubernetes Service and setup tags on Nodes using labels

Tola Ore-Aruwaji
FAUN — Developer Community 🐾
5 min readAug 12, 2021

--

Photo by Andrew Coop on Unsplash

In this tutorial, you are going to learn how to set up Kubernetes Service and create tags on your Kubernetes Nodes and Resources.

Pods are used as the unit of replication in Kubernetes. If your application becomes too popular and a single pod instance can’t carry the load, Kubernetes can be configured to deploy new replicas of your pod to the cluster as necessary. Even when not under heavy load, it is standard to have multiple copies of a pod running at any time in a production system to allow load balancing and failure resistance.

Pods are very dynamic, they come and go on the Kubernetes Cluster. When using a Replication Controller, pods are terminated and created during scaling operations.

When using Deployments, when updating the image version, pods are terminated and new pods take the place of older pods.

  • That’s why Pods should never be accessed directly, but always through a Service.
  • A Service is the logical bridge between the “mortal” pods and other services or end-users.
  • When using the “kubectl expose” command in our previous post, we created a new Service for your pod, so it could be accessed externally.
  • Creating a new service will create an endpoint for your pod(s):

You can use:

  • A ClusterIP: a virtual IP address that is only reachable within the cluster (which is the default) or
  • A NordPort: A port that is the same on each node and also reachable externally.
  • A LoadBalancer: This is used for production applications on the cloud. A LoadBalancer created by the cloud provider that will route external traffic to every node on the NodePort (ELB on AWS)

These options only allow you to create virtual IPs or ports.

  • There is also a possibility to use DNS names for the service. If you use an external name in the service definition, you can provide a DNS name for the service. For instance, for service discovery using DNS. This only works when the DNS add-on is enabled.

From the example above, the name of our service is helloworld-service , In the specification, we are defining the ports. The port we are using is 31001, and it’s a nodePort , the targetPort is the python-port , the protocol is TCP and the selector is app: hello-world .

You don’t have to specify the NodePort, if you don’t specify it, then it will be a random port, but if you specify it, the port will be used, but this service can only be created if the port is still available. So you have to manage port collisions yourself.

Note: By default, services can only run ports 30000-32767 , but you could change this behavior by adding the --service-node-port-range= argument to the kube-apiserver (in the init scripts) and you also have to make sure that you don’t have collisions with ports that are already used on the node.

  • Check to see if your pods are still running.
  • Run the service you created earlier which will open the python port 31001 From your Kubernetes service, you will be able to access your Pod.
  • Get the specific info from your service. kubectl describe svc helloworld-service you will see the cluster IP that is reachable within the cluster. The endpoint can also be used within the cluster. There are multiple ways to access your app the nodePort is going to be the static way to access your cluster.
  • You can get the list of all services using kubectl get svc
  • When you delete it and create it again, the IP address changes. The Nodeport number is the static port number and is the one we can use to configure external services to access our application in this pod.

Labels

Labels are key/value pairs that can be attached to objects.

  • Labels are like tags in AWS or other cloud providers, used to tag resources.
  • You can label your objects, for instance, your pods following an organizational structure.
  • You can tag your pods using:

key: environmentvalue: dev/staging/qa/prod

Another typical example is, you can have multiple departments and you can tag each individual pod for each department.

key:departmentvalue: engineering/finance/marketing

Labels are not unique, and multiple labels can be added to one object.

  • Once labels are attached to an object, you can use filters to narrow down the results. These are called Label Selectors.
  • Using Label selectors, you can use matching expressions to match labels.
  • For instance, a particular port can only run on a node labeled with “environment” equals “development”
  • In our previous lecture, we used Node Labels for the services. The service has had a label selector to select the correct pod that the service was going to be for.
  • You can also use labels to tag nodes.
  • Once nodes are tagged, you can use label selectors to let pods only run on specific nodes.

There are 2 steps required to run a pod on a specific set of nodes:

  1. First, you tag the node itself.
  2. Add a nodeselector to your pod configuration. It can be to a single pod or a pod template within a deployment.

STEPS

  • Add a label or multiple labels to your nodes:

Specify the node name and the label you want to give it using key=value

kubectl label nodes node1 hardware=high-speckubectl label nodes node2 hardware=low-spec
  • Confirm if you can see your nodes by entering

kubectl get nodes --show-labels

  • Secondly, add a pod that uses those labels.
apiVersion: v1
kind: Pod
metadata:
name: helloworld-service
spec:
contianers:
- name: docker-demo
image: docker/docker-demo
ports:
-containerPort: 5000
-nodeselector:
hardware: high-spec

This pod 👆🏽 will only run on nodes that have the labels high-spec.

gracias

Enjoyed the read? Leave some ‘claps’ below so others can find this post 🙂

Check out my other posts :)

https://thecraftman.medium.com/

Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

--

--