
Cluster IP is a fundamental concept in Kubernetes, and it's essential to understand how it works.
Cluster IP is a virtual IP address assigned to a Kubernetes Service. It's a unique IP address that allows communication between pods within the same cluster.
To create a Cluster IP, you need to define a Service in your Kubernetes configuration file. This Service is responsible for providing a stable IP address for your application.
A Cluster IP is not accessible from outside the cluster, it's only exposed within the cluster.
If this caught your attention, see: Zero-knowledge Service
What is Cluster IP
Cluster IP is a virtual, internal IP address assigned by Kubernetes to a Service with type: ClusterIP. It's only reachable from within the cluster's virtual network.
This internal reachability is a key characteristic of Cluster IP, where communication happens exclusively within the cluster. This means that traffic directed at the ClusterIP is routed to the correct backend Pods using mechanisms like iptables or IPVS.
Intriguing read: Virtual Ip in Fortigate
The Cluster IP remains constant, even if the underlying Pods change or get replaced. This stability is essential for services that need to be reachable consistently.
Here are the key characteristics of Cluster IP:
- Internal Reachability Only: Communication happens exclusively within the cluster.
- Stable IP: The Service IP remains constant, even if the underlying Pods change or get replaced.
- Built-In Load Balancing: Requests are round-robined or load-balanced to healthy Pods behind the Service.
- Service Discovery: By default, Kubernetes DNS automatically resolves the Service name to the ClusterIP, simplifying Pod-to-Service connections.
IP Allocation and Reservation
IP allocation in Kubernetes happens in one of two ways: across the whole cluster, every Service ClusterIP must be unique, or you can specify a specific ClusterIP.
Trying to create a Service with a specific ClusterIP that has already been allocated will return an error.
The best way to ensure you can create a Service with a specific ClusterIP is to reserve it in advance.
Kubernetes installers often reserve the 10th IP address from the Service IP range for the DNS Service, which is a good example of why you might want to reserve a specific IP address.
To reserve a specific IP address, you'd create a Service with that IP address specified, like 10.96.0.10, assuming your cluster is configured with Service IP range 10.96.0.0/16.
You might like: Golang Cidr to Ip Range
If other Services are created before or in parallel with dynamic allocation, there's a chance they can allocate the IP you want to reserve, resulting in a conflict error.
ClusterIP is a Kubernetes Service type that assigns an IP address from a reserved pool to help facilitate communication within the cluster.
The chosen IP should be valid within the service-cluster-ip-range CIDR, and Kubernetes can help avoid IP collisions to prevent conflicts with external network resources.
ClusterIPs provide load-balanced internal IP addresses to forward traffic inside the cluster.
Choosing an IP Address
To choose an IP address for your ClusterIP, you have two options: pick a specific IP address or let Kubernetes allocate one. If you choose to specify an IP address, it must be valid within the service-cluster-ip-range CIDR.
If you decide to let Kubernetes allocate an IP address, it will choose one from a reserved pool. However, this IP address must be unique across your whole cluster, so trying to create a Service with a specific IP address that has already been allocated will return an error.
Choose Your IP
You can choose your IP address when creating a Service in Kubernetes, which is a great way to have more control over your cluster's internal communication.
If you set .spec.clusterIP to “None,” Kubernetes won't assign an IP, giving you the freedom to pick one yourself.
The chosen IP should be valid within the service-cluster-ip-range CIDR, which is a reserved pool of IP addresses.
Trying to create a Service with a specific ClusterIP that has already been allocated will return an error, so make sure to check if the IP is available before proceeding.
Users can pick from a specific cluster IP or let Kubernetes allocate one, giving you options to suit your needs.
Arbitrary Ports
A client can call a Service at a specific IP address and port number, and the request will be forwarded to one of the member Pods. But did you know that the port number used to call the Service is arbitrary, meaning it can be any number?
The value of the port field in a Service manifest is arbitrary, but the value of targetPort is not. Each member Pod must have a container listening on targetPort.
You can have a Service with multiple ports, each with its own targetPort. This means each member Pod can have multiple containers listening on different ports.
If you have a Service with multiple ports, each port must have a unique name. This can be useful if you need to forward requests to different member Pods based on the port number used.
Here's an example of a Service with multiple ports:
In this example, if a client calls the Service at a specific IP address on port 50000, the request will be forwarded to a member Pod on port 8080. But if a client calls the Service at the same IP address on port 60000, the request will be forwarded to a member Pod on port 50000.
Simple Web
A simple web service can be created in a cluster, where Pods labeled app=my-web-app are load-balanced on port 80, forwarding to Pod port 8080.
This allows other Pods in the cluster to reach the Service at my-web-service.default.svc.cluster.local:80.
To access this Service, you can use the IP address of the Pod, which is the same as the Service's IP address.
Here's a breakdown of how this works:
- Service name: my-web-service
- Namespace: default
- Port: 80
- IP address: my-web-service.default.svc.cluster.local
Kubernetes and Cluster IP
Kubernetes assigns an IP address from a reserved pool for ClusterIP, a Kubernetes Service type that facilitates communication within the cluster. This IP address is only accessible within the cluster.
This Service type is the default in Kubernetes and is used for inter-service interactions within the cluster. It's also useful for services that don't require external access.
A ClusterIP can be either a specific IP address chosen by the user or an IP address allocated by Kubernetes. The chosen IP should be valid within the service-cluster-ip-range CIDR, and Kubernetes can help avoid IP collisions to prevent conflicts with external network resources.
Here are the key characteristics of ClusterIP:
Headless – Backend Tasks
A Headless Service is a Kubernetes service that doesn't allocate a ClusterIP, instead using DNS to expose the IP addresses of its pods.
This type of service is perfect for backend tasks without a user interface, allowing you to access services outside of the Kubernetes cluster.
The DNS setup in a headless service can be configured depending on whether selectors are defined, which adjust the DNS setup and create EndpointSlices through the Kubernetes API.
If selectors are not defined, the control plane doesn't create EndpointSlice objects, and the DNS will configure a CNAME record or another record for endpoints with the same name as the service.
A sample manifest for a Headless service looks like this: apiVersion: v1, kind: Service, metadata: name: nginx, spec: clusterIP: None, selector: app: nginx, ports: - name: http, port: 80, targetPort: 80.
This feature provides users with more control over communication with pods and can also be a solution for service discovery, as IP addresses can be added or removed in DNS once a new pod is added or removed.
A unique perspective: Ip Addresses in Computer Networks
Kubernetes Types Explained
Kubernetes offers four main types of services: ClusterIP, NodePort, LoadBalancer, and ExternalName.
Each type of service has its own unique characteristics and use cases.
ClusterIP is the default service type, accessible only within the cluster. It's used for inter-service interactions within the cluster.
NodePort exposes the service on a static port on each node for external traffic. It's used to enable users to design environments not entirely supported by Kubernetes.
LoadBalancer exposes the service using an external load balancer, typically used in cloud environments.
ExternalName maps a service to an external DNS name. It's used to map a service to a DNS name outside of the cluster, facilitating easy access to the service.
Here's a brief overview of each service type:
A Headless Service is another type of service that relies on individual pod IP addresses. It's used for content delivery, data retrieval, and integration between various platforms.
Creating and Configuring
Creating and Configuring a Cluster IP service in Kubernetes can be a straightforward process. A ClusterIP service is used to expose a set of pods and make them available to internal clients.
To create a ClusterIP service, you can use the `kubectl expose` command, which is a convenient way to create a service from an existing pod. This command allows you to specify the port and protocol you want to expose.
By default, a ClusterIP service is created with a ClusterIP type, which means it's only accessible from within the cluster. This is a good starting point for most use cases.
Example 1
In this example, the IP address range is 10.96.0.0/24, which is a common CIDR notation used for Services.
The range size is 254, a relatively small range that can be useful for a specific application or service.
To calculate the band offset, we use the formula: min(max(16, 256/16), 256). This gives us a band offset of 16.
The static band start is 10.96.0.1, which is the first IP address in the range.
The static band end is 10.96.0.16, which is the last IP address in the static band.
The range end is 10.96.0.254, which is the last IP address in the range.
This configuration can be useful for a small-scale application or service that requires a limited number of IP addresses.
Worth a look: Cgnat Ip Range
Multiple Ports
Creating a service in Kubernetes that exposes a set of pods and makes them available to internal or external clients requires attention to detail, especially when it comes to multiple ports.
A service can have multiple ports, each with its own protocol, port, target port, and node port. This is achieved through the use of ServicePort objects in the ports field of a Service.
Each ServicePort object has a unique name, which is essential if you have more than one ServicePort. The name field is used to identify each port uniquely.
The protocol field specifies the protocol used for each port, such as TCP or UDP. The port field is the port number used for the service, while the targetPort field specifies the port number on the pod where the request is forwarded.
The nodePort field is used for NodePort services, where the request is forwarded to a pod on a specific node.
Expand your knowledge: Internet Protocol Suite
Here are the fields of a ServicePort object in a concise table:
For example, if a client calls a service at 203.0.113.201 on TCP port 60000, the request is forwarded to a member pod on TCP port 50000.
On a similar theme: Ip Port 5060
Deployment Replica Set
In Kubernetes, a Replica Set is a resource that ensures a specified number of replicas, or copies, of a pod are running at any given time.
Replica Sets are often created in conjunction with Deployments, which allow for rolling updates and rollbacks of the pods.
A Replica Set can be created independently of a Deployment, but they are often used together.
A service can exist independently of a Replica Set and Deployment, and can simply route traffic to any pods with matching labels defined in the selector field.
You don't need to create a Replica Set or Deployment to create a service, but it's a common pattern in Kubernetes deployments.
Best Practices and Examples

A ClusterIP Service provides a stable, internal endpoint for your Pods in Kubernetes.
The IP address range for Services is 10.96.0.0/24, which gives us a range size of 254 and a Band Offset of 16. This means that the static band start is 10.96.0.1 and the static band end is 10.96.0.16.
To access a Service, it's best to use its DNS name instead of hardcoding IP addresses. This is a key best practice for working with ClusterIP Services.
Here are some key best practices to keep in mind:
- DNS over IP: Always access the Service by its DNS name rather than hardcoding IP addresses.
- Label Management: Ensure the Service selector and Pod labels match precisely.
- Network Policies: Combine ClusterIP with Kubernetes Network Policies to restrict or allow internal traffic.
- Resource Limits and Probes: Configure readiness and liveness probes so that Pods are only added to the Service when they’re healthy.
By following these best practices, you can ensure that your ClusterIP Services are running smoothly and securely.
Best Practices
Always access your Service by its DNS name, rather than hardcoding IP addresses, to ensure flexibility and scalability.
Using DNS over IP makes it easier to manage your Service, as you can easily update the DNS name without affecting the underlying IP address.
Label Management is crucial, ensuring that the Service selector and Pod labels match precisely to avoid confusion and miscommunication.
Intriguing read: Azure Dns Server Ip

This ensures that your Service can correctly identify and communicate with the right Pods, even as they're created, scaled, or terminated.
Combining ClusterIP with Kubernetes Network Policies allows you to restrict or allow internal traffic, giving you fine-grained control over your Service's access.
By configuring readiness and liveness probes, you can ensure that Pods are only added to the Service when they're healthy, reducing the risk of downtime and errors.
Here are the key best practices summarized:
- DNS over IP: Access your Service by its DNS name, not IP address.
- Label Management: Ensure precise matching of Service selector and Pod labels.
- Network Policies: Combine ClusterIP with Kubernetes Network Policies to control internal traffic.
- Resource Limits and Probes: Configure readiness and liveness probes for healthy Pods.
Examples
In CIDR notation, the IP address range can be quite large, with Example 1 showing a range of 4094, and Example 2 showing a range of 65534.
The Band Offset calculation is crucial to determine the static band start and end. For example, in Example 1, the Band Offset is 256, which is the minimum of the maximum of 16 and 4096/16.
The static band start is always 10.96.0.1, but the static band end can vary depending on the range size. For example, in Example 1, the static band end is 10.96.1.0.
If this caught your attention, see: Azure Data Factory Ip Address Range

In larger IP address ranges, like in Example 2, the range end is significantly larger, at 10.96.255.254.
The range size can be calculated by subtracting the CIDR prefix length from the total number of available addresses. For instance, in Example 1, the range size is 2 - 2 = 4094.
Avoiding Complexity
Cluster IP can get complicated, but it's worth simplifying the process to get the most out of it.
One way to avoid complexity is to use a load balancer, which can distribute traffic across multiple servers, making it easier to manage and maintain.
By using a load balancer, you can reduce the complexity of managing multiple servers and focus on other important tasks.
Avoiding Conflicts
Avoiding Conflicts is crucial in keeping your Kubernetes cluster organized and efficient. The allocation strategy implemented in Kubernetes reduces the risk of ClusterIP conflicts.
The formula used to divide the ClusterIP range is min(max(16, cidrSize / 16), 256). This ensures that the range is never less than 16 or more than 256 with a graduated step between them.
Dynamic IP assignment uses the upper band by default, which allows for a lower risk of collision. Once the upper band is exhausted, it will use the lower range.
You can use static allocations on the lower band, which minimizes the risk of collision.
Hardcoding

Hardcoding can be a limiting approach, especially when it comes to ClusterIPs in Kubernetes. Hardcoding a ClusterIP in the YAML configuration makes it difficult to update it in the future.
Any changes to the service YAML file won't affect the hardcoded IP address, which can lead to inconsistencies. This can cause issues down the line, especially if the ClusterIP needs to be updated.
Once a ClusterIP is hardcoded, it remains unchanged unless deleted and recreated. This can be a hassle, especially if the service is already in production.
Frequently Asked Questions
How to find the ClusterIP?
To find the ClusterIP, use the command `kubectl describe service
Featured Images: pexels.com

