How to Expose an Application Internally Using a ClusterIP Service
The DevOpscube team is deploying a microservice that should be accessible only by other applications within the Kubernetes cluster. The application must therefore be exposed through an internal ClusterIP Service.
Your task is to create a Pod named backend-app using the nginx image.
Expose the application internally using a ClusterIP Service named backend-svc. The Service must forward TCP traffic from port 80 to the application's port 80.
Verify that the Pod and Service are functioning correctly and that the Service has a valid ClusterIP.
Save the ClusterIP assigned to backend-svc to:
/tmp/ip.txtFinally, verify that /tmp/ip.txt contains the ClusterIP of the Service.
Create the Backend Pod
Create the backend-app Pod using the nginx image:
$ kubectl run backend-app --image=nginx
pod/backend-app createdVerify that the Pod is running:
$ kubectl get pod backend-app
NAME READY STATUS RESTARTS AGE
backend-app 1/1 Running 0 18sThe Pod should be in the Running state.
Create the ClusterIP Service
Expose the backend-app Pod using a ClusterIP Service:
$ kubectl expose pod backend-app \
--name=backend-svc \
--port=80 \
--target-port=80The Service listens on port 80 and forwards traffic to port 80 on the Pod. Since the default Service type is ClusterIP, it is accessible only from within the cluster.
A Service created without specifying another Service type is a ClusterIP Service by default, so it is only accessible from within the cluster.
Verify the Service:
kubectl get service backend-svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
backend-svc ClusterIP 10.104.1.72 <none> 80/TCP 11sYou should see this, which confirms that backend-svc is a ClusterIP Service exposing port 80.
Verify the Service Endpoint
Check whether the Service has correctly selected the backend-app Pod:
$ kubectl describe service backend-svc
Name: backend-svc
Namespace: default
Labels: run=backend-app
Annotations: <none>
Selector: run=backend-app
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.104.1.72
IPs: 10.104.1.72
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.140.111:80
Session Affinity: None
Internal Traffic Policy: Cluster
Events: <none>Look for the Endpoints field. It should contain the Pod's IP address on port 80.
You can also check it directly:
$ kubectl get endpoints backend-svc
NAME ENDPOINTS AGE
backend-svc 10.244.140.111:80 3m9sIf an endpoint is listed, the Service has successfully selected the backend-app Pod.
Test the Service
First, get the Service's ClusterIP:
$ kubectl get service backend-svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
backend-svc ClusterIP 10.104.1.72 <none> 80/TCP 5m3sUse the ClusterIP to test the Service from the backend-app Pod:
kubectl exec backend-app -- curl http://10.104.1.72 $ kubectl exec backend-app -- curl http://10.104.1.72
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 <!DOCTYPE html> 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
<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 iYou should receive the NGINX welcome page HTML. This confirms that the ClusterIP Service is correctly routing traffic to the backend-app Pod.
Save the ClusterIP
Retrieve the Service ClusterIP directly and save it to /tmp/ip.txt:
Instead of manually copying the IP, you can retrieve it directly with kubectl:
kubectl get service backend-svc \
-o jsonpath='{.spec.clusterIP}' > /tmp/ip.txtVerify the file:
cat /tmp/ip.txt
10.104.1.72The file should contain only the Service ClusterIP.
CKA Exam Tips
These tips will help you quickly identify the important Kubernetes concepts and avoid common mistakes during the exam.
1. ClusterIP Is the Default Service Type
You don't need to specify --type=ClusterIP unless the task explicitly requires it.
2. Remember the Difference Between port and targetPort
The Service's port receives traffic, while targetPort forwards it to the Pod:
Service port 80 → Pod port 80
3. Check Service Endpoints
If a Service has no endpoints, check whether its selector matches the Pod labels:
kubectl describe service backend-svc
kubectl get pod --show-labels
4. Test Service Connectivity Quickly
For a quick Service connectivity test, you can use:
kubectl exec <pod> -- curl http://<cluster-ip>
5. Use JSONPath to Save Specific Values
When a task asks you to save Kubernetes output to a file, using JSONPath can help retrieve the required value directly without manually copying it.