Kubernetes Services Explained

Understanding Cluster IP, Node IP, and Load Balancer IP in Kubernetes Services

When it comes to managing and orchestrating containerized applications, Kubernetes has become the go-to tool for developers and DevOps engineers alike. One of the critical components of Kubernetes is the service, which allows applications to communicate with each other within the cluster. Kubernetes service can have different types of IP addresses, including Cluster IP, Node IP, and Load Balancer IP. In this blog post, we’ll explain the differences between these IP types and provide YAML code examples.

Cluster IP

A Cluster IP is an IP address assigned to a Kubernetes service. It’s a virtual IP address that is only accessible within the Kubernetes cluster. Cluster IP is used to expose a service on a specific port within the cluster. When a pod wants to communicate with a service, it uses the Cluster IP address and the service port. The Cluster IP is automatically assigned by Kubernetes and can be a static or dynamic IP address.

Here’s an example of a Kubernetes service with a Cluster IP:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP

In the example above, the service my-service is assigned a Cluster IP address, and it’s configured to expose port 80.

Node IP

A Node IP is the IP address of a Kubernetes node. Each node in a Kubernetes cluster has a unique IP address, which can be used to access services running on that node directly. Node IP is practical when you need to expose a service outside the cluster, but don’t want to use a Load Balancer. For example, if you have a database running on a node, you can use the Node IP to access it from outside the cluster.

Here’s an example of a Kubernetes service with a Node IP:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80
  type: NodePort

In the example above, the service my-service is configured to use the NodePort type, which means that Kubernetes will assign a port on each node to access the service. You can then use the Node IP and the assigned port to access the service directly.

Load Balancer IP

A Load Balancer IP is an IP address assigned to a Kubernetes service that’s exposed outside the cluster. Load Balancer IP is useful when you want to distribute traffic across multiple nodes running the service. It’s commonly used when you have a web application that needs to handle a high volume of traffic. Kubernetes can automatically provision a Load Balancer in cloud providers like AWS, Google Cloud Platform, and Azure.

Here’s an example of a Kubernetes service with a Load Balancer IP:

apiVersion: v1
kind: Service
metadata:
  name: my-service
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-type: nlb
spec:
  selector:
    app: my-app
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

In this example, the service my-service is configured to use the LoadBalancer type and the service.beta.kubernetes.io/aws-load-balancer-type: nlb annotation is used to specify that an AWS Network Load Balancer should be provisioned. The Load Balancer IP will be automatically assigned by the cloud provider and can be used to access the service from outside the cluster.

Provisioning a Load Balancer in GCP, AWS, and Microsoft Azure

The process of provisioning a Load Balancer in cloud providers can differ based on the provider. Here’s how it’s done in some of the most popular cloud providers:

Amazon Web Services (AWS)

To provision a Load Balancer in AWS, you need to use the LoadBalancer type in your Kubernetes service manifest. Once you deploy the service, Kubernetes will automatically provision an AWS Load Balancer for the service. You can then use the DNS name associated with the Load Balancer to access the service.

Google Cloud Platform (GCP)

To provision a Load Balancer in GCP, you need to use the LoadBalancer type in your Kubernetes service manifest. Once you deploy the service, Kubernetes will automatically provision a GCP Load Balancer for the service. You can then use the IP address associated with the Load Balancer to access the service.

Microsoft Azure

To provision a Load Balancer in Azure, you need to use the LoadBalancer type in your Kubernetes service manifest. Once you deploy the service, Kubernetes will automatically provision an Azure Load Balancer for the service. You can then use the IP address associated with the Load Balancer to access the service.

Conclusion

Understanding the differences between Cluster IP, Node IP, and Load Balancer IP is essential when working with Kubernetes services. Cluster IP is used to expose services within the cluster, while Node IP can be used to expose services outside the cluster without using a Load Balancer. Load Balancer IP is used to distribute traffic across multiple nodes running the service and can be automatically provisioned by cloud providers. By using the examples provided in this post, you can start building more robust and scalable Kubernetes services.

External resources for further reading

  1. Kubernetes Service Documentation: https://kubernetes.io/docs/concepts/services-networking/service/
  2. Kubernetes Load Balancer Documentation: https://kubernetes.io/docs/tasks/access-application-cluster/create-external-load-balancer/