📖 Scenario: You are managing a Kubernetes cluster and want to expose a set of pods internally using a ClusterIP service. This service type allows communication only within the cluster, which is useful for internal components like databases or backend APIs.
🎯 Goal: Build a Kubernetes YAML configuration that defines a ClusterIP service exposing pods labeled app: myapp on port 80.
📋 What You'll Learn
Create a YAML file with a Service resource named myapp-service.
Set the service type to ClusterIP.
Select pods with the label app: myapp.
Expose port 80 on the service and map it to target port 80 on the pods.
💡 Why This Matters
🌍 Real World
ClusterIP services are commonly used to expose backend applications or databases inside a Kubernetes cluster without exposing them to the outside world.
💼 Career
Understanding how to configure ClusterIP services is essential for Kubernetes administrators and DevOps engineers managing internal application communication.
Progress0 / 4 steps
1
Create the basic Service YAML skeleton
Create a YAML file with apiVersion set to v1, kind set to Service, and metadata.name set to myapp-service.
Kubernetes
Hint
Start by defining the API version, kind, and metadata name for the service.
2
Add the service type and selector
Add a spec section with type set to ClusterIP and a selector that matches pods with label app: myapp.
Kubernetes
Hint
The type field controls how the service is exposed. Use ClusterIP for internal access only.
3
Define the ports for the service
Under spec, add a ports list with one entry that sets port to 80 and targetPort to 80.
Kubernetes
Hint
The ports section tells Kubernetes which port the service listens on and which port to forward to on the pods.
4
Display the complete YAML configuration
Print the complete YAML configuration for the myapp-service ClusterIP service.
Kubernetes
Hint
Show the full YAML content as the final output.
Practice
(1/5)
1. What is the primary purpose of a ClusterIP service in Kubernetes?
easy
A. To create a load balancer outside the cluster
B. To expose the service to the internet with a public IP
C. To assign a static external IP to the service
D. To provide an internal IP for pods to communicate within the cluster
Solution
Step 1: Understand ClusterIP service role
ClusterIP is designed to create an internal IP address accessible only inside the Kubernetes cluster.
Step 2: Compare with other service types
Other types like NodePort or LoadBalancer expose services externally, but ClusterIP does not.
Final Answer:
To provide an internal IP for pods to communicate within the cluster -> Option D
Quick Check:
ClusterIP = internal communication [OK]
Hint: ClusterIP is for internal pod communication only [OK]
Common Mistakes:
Confusing ClusterIP with LoadBalancer or NodePort
Thinking ClusterIP exposes service externally
Assuming ClusterIP assigns static external IP
2. Which of the following YAML snippets correctly defines a ClusterIP service?
easy
A. apiVersion: v1
kind: Pod
metadata:
name: my-service
spec:
containers:
- name: myapp
image: nginx
Hint: ClusterIP service YAML must specify type: ClusterIP [OK]
Common Mistakes:
Using wrong service type like NodePort
Confusing Pod definition with Service
Omitting service type field
3. Given this service YAML with type ClusterIP, what will be the output of kubectl get svc my-service?
medium
A. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-service ClusterIP 10.96.0.1 <none> 80/TCP 5m
B. NAME TYPE EXTERNAL-IP PORT(S) AGE
my-service LoadBalancer 192.168.1.1 80/TCP 5m
C. Error: service not found
D. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-service NodePort 10.96.0.1 <none> 80:30080/TCP 5m
Solution
Step 1: Understand ClusterIP service output
ClusterIP services show a cluster internal IP and no external IP.
Step 2: Match output format
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-service ClusterIP 10.96.0.1 <none> 80/TCP 5m shows ClusterIP type with a cluster IP and <none> external IP, matching expected output.
Final Answer:
Service listed with ClusterIP and no external IP -> Option A
Quick Check:
ClusterIP service output = NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-service ClusterIP 10.96.0.1 <none> 80/TCP 5m [OK]
Hint: ClusterIP services have no external IP in kubectl output [OK]
Common Mistakes:
Expecting external IP for ClusterIP service
Confusing LoadBalancer or NodePort output
Assuming service not found error
4. You created a ClusterIP service but pods cannot reach it. Which YAML mistake could cause this?
medium
A. Using type: LoadBalancer instead of ClusterIP
B. Missing or incorrect selector labels in the service spec
C. Specifying targetPort as a string instead of a number
D. Omitting the metadata section
Solution
Step 1: Identify service selector role
The selector connects the service to the correct pods. If missing or wrong, no pods match.
Step 2: Evaluate other options
Type LoadBalancer changes exposure but doesn't block pod access. targetPort as string is accepted. metadata omission causes other errors but not pod reachability.
Final Answer:
Missing or incorrect selector labels -> Option B
Quick Check:
Selector connects pods = Missing or incorrect selector labels in the service spec [OK]
Hint: Check selector labels to link service and pods [OK]
Common Mistakes:
Ignoring selector labels importance
Blaming service type for pod connectivity
Confusing metadata omission with connectivity issues
5. You want to expose a web app only inside your Kubernetes cluster using a ClusterIP service. Which combination of YAML fields ensures this?