peter (admin) -

Kubernetes ClusterIP Service
Here we will learn how a ClusterIP Service works inside the Kubernetes networking system and how it allows Pods to communicate with an application through a stable internal IP address.
We will also learn how traffic is forwarded from the Service to the backend Pods running in the cluster.
Let's get started.
For this, we are already logged into both the Master node and the Worker node.
Create the Web Application Deployment
Let's first create a deployment named web-app using the NGINX container image, which will run a web server inside the Kubernetes cluster.
The purpose of creating this deployment is to provide a running application that we can later expose through a ClusterIP Service, so other Pods inside the cluster can access it through the Kubernetes networking system.
After pressing Enter, the output confirms that the deployment has been successfully created, which means Kubernetes has accepted the configuration and started creating the required Pods for this application.
deployment.apps/webapp created
peter@k8s-master:~$
Let's verify the deployment.
NAME READY UP-TO-DATE AVAILABLE AGE
health-demo 2/2 2 2 4d2h
webapp 0/1 1 1 4s
peter@k8s-master:~$
Here you can see that it is not running yet because we checked the status immediately after creating it.
Let's wait about 10 to 15 seconds and check again.
NAME READY UP-TO-DATE AVAILABLE AGE
health-demo 2/2 2 2 4d2h
webapp 1/1 1 1 4s
peter@k8s-master:~$
This time, you can see that the output shows one ready and one available, which confirms that the Pod has now been successfully created and the application is running properly inside the cluster.
Create the ClusterIP Service
Next, we expose the deployment as a Service inside the cluster so that other Pods can access the application through the Kubernetes networking system.
service/webapp exposed
peter@k8s-master:~$
Here Kubernetes creates a ClusterIP Service for the web application deployment.
The Service provides a stable internal address that routes traffic to the Pods running the application.
The Service listens on port 80, which means other Pods will send requests to this port when accessing the Service.
However, we intentionally configure the Service to forward the traffic to port 9999.
The purpose of doing this is to intentionally create a configuration problem so that we can observe how the Service behaves when the target port is incorrect and then troubleshoot and fix the issue.
Verify the ClusterIP Service
Let's verify the Service we created.
webapp ClusterIP 10.111.140.169 <none> 80/TCP 4m59s
peter@k8s-master:~$
Here you can see that the web application Service now exists inside the cluster.
The Service type is ClusterIP, which means it is only accessible from inside the Kubernetes cluster network.
You can also see the ClusterIP address assigned to the Service.
This acts as a stable internal address that other Pods can use to reach the application.
The output also shows that the Service is listening on port 80.
This means any request sent to this Service IP on port 80 will be forwarded to the backend Pods that belong to the web application deployment.
Describe the Service
If we need more detailed information about the Service, we can check the Service description.
kubectl describe service web-app
Here you can see several important networking details.
The output shows that the Service belongs to the deep namespace and selects Pods with:
app=webapp
peter@k8s-master:~$ kubectl describe svc webapp
Name: webapp
Namespace: deep
Labels: app=webapp
Annotations: <none>
Selector: app=webapp
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.111.140.169
IPs: 10.111.140.169
Port: <unset> 80/TCP
TargetPort: 9999/TCP
Endpoints: 10.244.1.82:9999
Session Affinity: None
Internal Traffic Policy: Cluster
Events: <none>
peter@k8s-master:~$This means traffic sent to the Service will be forwarded to those Pods.
You can also see the ClusterIP address, which is the internal address other Pods will use to reach the application.
The Service is listening on port 80, but the traffic is being forwarded to target port 9999.
This is the intentional configuration mistake we created.
At the bottom, you can also see the endpoint IP, which represents the actual Pod receiving the traffic.
However, because the target port does not match the port where NGINX is listening, the request will not work correctly.
Check the Service Endpoints
We can also check only the endpoints of the Service to see which Pod is actually receiving the traffic.
Warning: v1 Endpoints is deprecated in v1.33+; use discovery.k8s.io/v1 EndpointSlice
NAME ENDPOINTS AGE
webapp 10.244.1.82:9999 55m
peter@k8s-master:~$
The output shows the Pod IP address along with the port number.
Here you can see the endpoint showing the Pod IP with port 9999, which confirms that the Service is sending traffic to the wrong port.
Create a Client Pod
Next, we create a client Pod using the BusyBox container so we have a temporary system inside the cluster that can send requests to the web application Service.
pod/client created
peter@k8s-master:~$
The purpose of creating this Pod is to simulate another application running in the cluster that tries to access the ClusterIP Service.
The container is kept running for some time so we can enter it and test the connection to the Service IP.
Let's verify the Pods in the cluster.
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
client 1/1 Running 0 3m19s 10.244.1.83 k8s-worker1 <none> <none>
pod-a 0/1 Completed 0 143m 10.244.1.79 k8s-worker1 <none> <none>
pod-b 0/1 Completed 0 142m 10.244.1.80 k8s-worker1 <none> <none>
webapp-645b5b46cd-5w65l 1/1 Running 0 95m 10.244.1.82 k8s-worker1 <none> <none>
peter@k8s-master:~$
Here you can see that the client Pod is now successfully running along with the web application Pod.
It also shows the Pod IP addresses and the node where they are running, which confirms that all these Pods are active and connected to the cluster network.
Now the client Pod is ready to send a request to the web application Service.
Test the ClusterIP Service
Let's move on and test the ClusterIP Service from inside the cluster by entering the client Pod and sending an HTTP request to the Service IP address.
For this, we use the wget tool because it allows us to request the web page from the Service just like one application inside the cluster would contact another application.
After pressing Enter, you can see the message:
Connection refused
peter@k8s-master:~$ kubectl exec -it client -- wget -q http://10.244.1.82
wget: can't connect to remote host (10.244.1.83): Connection refused
command terminated with exit code 1
peter@k8s-master:~$ This means the request reached the Service but the backend application did not accept it.
This happens because the Service is forwarding traffic to port 9999, while the NGINX application is actually listening on port 80.
The request is therefore being sent to the wrong port.
Fix the Service Configuration
Let's move on and correct this Service configuration so the traffic is forwarded to the proper port.
For this, we update the Service by patching its configuration and change the target port from 9999 to 80, which is the correct port.
For example:
kubectl patch service web-app -n deep -p '{"spec":{"ports":[{"port":80,"targetPort":80}]}}'
After pressing Enter, it confirms that the Service has been successfully updated.
Verify the Corrected Service
Let's verify the Service again.
Warning: v1 Endpoints is deprecated in v1.33+; use discovery.k8s.io/v1 EndpointSlice
NAME ENDPOINTS AGE
broken-svc <none> 2d12h
web-app <none> 7d9h
webapp 10.244.1.82:80 82m
peter@k8s-master:~$
Here you can see that the target port is now set to 80, which means the Service is forwarding traffic to the correct application port.
The endpoint also shows the Pod IP with port 80, confirming that the Service is now properly connected to the backend Pod.
Test the Service Again
Now we test the Service again from the client Pod.
Connecting to 10.244.1.82 (10.244.1.82:80)
saving to 'index.html'
index.html 100% |**********************************************************************************************************************************************| 896 0:00:00 ETA
'index.html' saved
peter@k8s-master:~$ kubectl exec -it client -- cat index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, nginx is successfully installed and working.
Further configuration is required for the web server, reverse proxy,
API gateway, load balancer, content cache, or other features.</p>
<a href="https://nginx.org/">nginx.org</a>.<br/>
To engage with the community please visit
<a href="https://community.nginx.org/">community.nginx.org</a>.<br/>
For enterprise grade support, professional services, additional
security features and capabilities please refer to
<a href="https://f5.com/nginx">f5.com/nginx</a>.</p>
</body>
</html>
This time, you can see that the request is successfully reaching the application.
The output shows the default NGINX web page, which confirms that the request sent to the ClusterIP Service is now correctly forwarded to the backend Pod.
This means the communication between the client Pod and the web application is now working properly.
How a ClusterIP Service Works
A ClusterIP Service provides a stable internal IP address that Pods inside the Kubernetes cluster can use to communicate with an application.
The basic traffic flow is:
Client Pod
↓
ClusterIP Service
↓
Service Port 80
↓
Target Port 80
↓
Backend Pod
↓
NGINX
In this example, the initial configuration used an incorrect target port:
ClusterIP Service
↓
Port 80
↓
TargetPort 9999
↓
Connection refused
After correcting the target port:
ClusterIP Service
↓
Port 80
↓
TargetPort 80
↓
Backend Pod
↓
NGINX
↓
HTTP Response
This is a simple explanation of how a ClusterIP Service works in Kubernetes, how Pods communicate with an application through a stable internal IP, and how we can troubleshoot and fix configuration issues when the Service is not working correctly.
Comments (0)
There are no comments yet. Be the first to comment.
Log in or register to leave a comment. all comments are subject to admin approval