Kubernetes Service Types Explained
Tags: kubernetes, devops, community
Published: 2022-06-20 14:33:06

Kubernetes has emerged as a powerful tool to manage and scale cloud-native applications. Organizations need to deploy their software quickly, leveraging highly scalable and always available capabilities to maintain zero downtime. As more applications are containerized and deployed, it becomes increasingly complex for any organization to manage these containers. Hence, scaling becomes an issue. This is where Kubernetes shines. With Kubernetes, you can easily automate, deploy, scale, and monitor your applications. 

If you’ve read any documentation about Kubernetes services and networking, you’ve probably come across the terms ClusterIP, NodePort, LoadBalancer, and Ingress. There seems to be a lot of confusion around these terms, and you must understand the difference before you start building your next Kubernetes-based application. 

This tutorial will explain the difference between these four Kubernetes service types, and how you should choose the best one for your application.

### Understanding Networking Requirements for Your Application
Kubernetes networking and services are a complex topic. You need to understand the needs of your application in order to successfully deploy it on Kubernetes. This means understanding the type of service you want to provide, the size and location of your cluster, and what kind of traffic you expect your application to receive. 

There are four types of services that Kubernetes supports: ClusterIP, NodePort, LoadBalancer, and Ingress. Each has their own set of requirements to enable them for your application, so you must understand which one you need before deploying. 

For example, NodePort allows pods within the same node to communicate with each other without having an IP address assigned. Your Kubernetes cluster must have at least two nodes for this type of network communication to work correctly. Also, NodePort only works when accessed from inside the cluster, as opposed to LoadBalancers or Ingress, which allow external access. Let’s go through each to understand how they work.

## ClusterIP
![cluster ip](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/em4qw5xak2qzcjh482v4.png)
ClusterIP is the default service that enables the communication of multiple pods within the cluster. By default, your service will be exposed on a ClusterIP if you don't manually define it. ClusterIP can’t be accessed from the outside world. But, a Kubernetes proxy can be used to access your services. This service type is used for internal networking between your workloads, while debugging your services, displaying internal dashboards, etc. 

## NodePort
![nodeport](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/97a8uff9xrpurjjacssn.png)
A NodePort is the simplest networking type of all. It requires no configuration, and it simply routes traffic on a random port on the host to a random port on the container. This is suitable for most cases, but it does have some disadvantages: 

- You may need to use a reverse proxy (like Nginx) to ensure that web requests are routed correctly. 
- You can only expose one single service per port.
- Container IPs will be different each time the pod starts, making DNS resolution impossible. 
- The container cannot access localhost from outside of the pod, as there is no IP configured. 

Nevertheless, you can use NodePort during experimentation and for temporary use cases, such as demos, POCs, and internal training to show how traffic routing works. It is recommended not to use NodePort in production to expose services. 

## LoadBalancer
![load balancer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l2p5ioln6c15g56by9rq.png)
LoadBalancer is the most commonly used service type for Kubernetes networking. It is a standard load balancer service that runs on each pod and establishes a connection to the outside world, either to networks like the Internet, or within your datacenter. 

The LoadBalancer will keep connections open to pods that are up, and close connections to those that are down. This is similar to what you have on AWS with ELBs, or Azure with Application Gateway. Upstreams provide Layer 4 routing for HTTP(S) traffic, whereas Downstreams provide Layer 7 routing for HTTP(S) traffic. 

You can route traffic on destination port number, protocol and hostname, or use application labels. You can send almost any kind of traffic to this service type, such as HTTP, TCP, UDP, Grpc, and more. Use this approach to expose your services directly. 

## Ingress
![ingress](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pmy5ghh6r4du81o4dd9n.png)
Ingress is not considered an official Kubernetes service, but it can be used to expose services. You can configure an Ingress service by creating rules to define which inbound connections should reach which services.

An Ingress is a Kubernetes object that sits in front of multiple services and acts as an intelligent router. It defines how external traffic can reach the cluster services, and it configures a set of rules to allow inbound connections to reach the services on the cluster. 

Ingress rules are typically defined by annotations. The Ingress controller reads these annotations and configures iptables or NGINX accordingly. There are many types of Ingress controllers that possess different capabilities. You can find the list of ingress controllers here. 

Ingress is the most powerful service type used to expose services, and it only requires you to maintain one load balancer, which can be cheaper than using a LoadBalancer service type. 

### Here is a simple comparison table to help you understand the service types at a quick glance.

![comparison image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kf4ij8gqo60j7j3w9njt.png)Image credits and source: [StackOverflow](https://stackoverflow.com/questions/41509439/whats-the-difference-between-clusterip-nodeport-and-loadbalancer-service-types)

#### Conclusion: Kubernetes is a Must in the Cloud-Native World
Kubernetes is a powerful tool for automating and managing your IT infrastructure. It gives you the ability to group related parts of your infrastructure, giving them their own “node” in the cluster, making them easier to manage, monitor, and update. 

As Kubernetes adoption is skyrocketing, it has become a must-know platform for developers and enterprises to be competitive in the cloud-native space. 

The [Harness Platform](https://harness.io/products/platform) is built with all the capabilities to supercharge your Kubernetes deployments with ease. Harness has an intuitive dashboard where you can easily configure your deployment stage, target infrastructure, and execution strategy.

We would love to be a part of your journey, as we use Kubernetes intensively at Harness. Looking to get started? We have a quickstart guide on Kubernetes that will help you [deploy your first application using Harness CD](https://docs.harness.io/article/7in9z2boh6-kubernetes-quickstart).