4 Kubernetes Services and How to Use them
Kubernetes services allow organizations to map different entities together using IP addresses and selectors. For example, you may have a database, a backend application, and a web application — and the only way to link them together is via services.
Today, we are going to discuss the four different services K8S (Kubernetes) provides: ClusterIP, LoadBalancing, NodePort, and ExternalName.
Want to Learn Kubernetes?
If you’re a network administrator or engineer, it’s worth learning about — and how to use — Kubernetes. If you’re new to Kubernetes, CBT Nuggets can help you get up to speed. You can choose from a variety of Kubernetes training from Trevor Sullivan.
His Kubernetes courses are designed to help learners build the foundation they need to really leverage Kubernetes for their organizations. Start your free week today.
1. What is Kubernetes ClusterIP Service?
ClusterIP provides internal communication between nodes within a particular cluster.
Whenever a ClusterIP service is created, a corresponding endpoint is created. For example, a service can be created to match all pods with the selector, name=MyApp. That means every pod that has that selector will have an endpoint exposed on the service.
Let’s say you are hosting a full stack application on K8S. It has three pods replicated for the frontend, three for the backend, and three for the database. Remember, each one of these pods has an IP address… but pods are ephemeral. So those IP addresses will disappear. That means it’s a bad idea to use them to communicate with each other. Instead, use a cluster IP service labeled “backend” or “db”. Now it will reliably communicate with the intended pods. Remember, ClusterIP does is not used to communicate with the internet.
2. What is Kubernetes NodePort Service?
The NodePort service exposes a single port on each node in the Kubernetes cluster. Remember, a cluster is just a group of available nodes, so that means when you create this service, you should be able to ping the IP address of the node. NodePorts can be used to make your application accessible on the internet.
As an alternative to using NodePort, you can use something called an Ingress. An Ingress opens up your cluster and routes traffic to specific servers, thus traveling to different pods. Remember, if you are using NodePorts in a production environment and it is not working, remember to check firewall rules.
3. What is Kubernetes Load Balancer Service?
If you are on the cloud, LoadBalancer Service is a great way to expose your app to the internet. The LoadBalancer service routes traffic between multiple nodes. That way your site does not crash when it experiences a lot of traffic. Let’s go over a quick example of why this could be useful.
Let’s say you own a clothing store website and it’s hosted on a cloud provider. The web application has been placed into a Docker container. Then, that docker container is put into a Kubernetes pod. Lastly, a NodePort and ClusterIP service is created to expose it to the internet.
However, as your website becomes more popular, it slows down and sometimes even crashes when hundreds of thousands of people arrive to buy clothes. This is where the LoadBalancing Service comes in. By replacing the NodePort service with LoadBalancing Service, all of that traffic can be distributed between multiple pods that hold the exact same website. So if you have three pods, each one will only get around 33% of the traffic instead of one getting the full brunt of it.
LoadBalancing Service is an excellent tool, but remember, it is an extension of existing cloud infrastructure — meaning you have to have AWS, GC, or Azure.
4. What is Kubernetes ExternalName Service?
The ExternalName service maps a particular service to a DNS name. The ExternalName Service acts as a proxy, allowing a user to redirect requests to a service sitting outside (or inside) the cluster. Essentially it creates a CNAME record that connects the DNS name to some cluster-local name—that way your pods can leverage that service. This all may feel a little vague at first, so let’s look at a real-life example.
Let’s say you are migrating all of your applications to Kubernetes. However, that is a hard job, and it would be easier to do it piecemeal. So you retain an external database that your new k8s containers can retrieve data from. However, that database resides outside the cluster—so your pods have no idea what it is. We get around this problem by using ExternalName Service.
Below is a code snippet to create an ExternalName Service.
kind: Service
apiVersion: v1
metadata:
name: database-service
spec:
type: ExternalName
externalName:
production.database.acme.org
Now, whenever your pod tries to access the database on database-service, it redirects to the DNS of our production database. Voila!
Final Thoughts
These four services are a lot to take in, so don’t feel bad if you don’t understand it on your first pass. The best thing to do is download a local Kubernetes server like MiniKube and practice these concepts until they are second nature. It takes a lot of work, but as a Certified Kubernetes Application Developer myself, I can guarantee that K8S expertise will make you very marketable.
delivered to your inbox.
By submitting this form you agree to receive marketing emails from CBT Nuggets and that you have read, understood and are able to consent to our privacy policy.