Here are a few CKA (Certified Kubernetes Administrator) Exam questions related to Kubernetes services:
Understanding Kubernetes Service Types
In Kubernetes, there are 4 different types of Services:
ClusterIP
- This is the default service type in Kubernetes
- Assigns a cluster internal IP to the service
- Only serves cluster internal traffic, with no exposure to outside networks
- Enables simple load balancing for pods via Podselectors
NodePort
- Enables external traffic through the Nodes’ IP
- Exposes the same port on each node of the Kubernetes cluster
- Assigns a cluster internal IP to the service
- Routes traffic from port on notes to internal IP
- Enables load balancing to pods, just like a ClusterIP service
LoadBalancer
- Provisions an external Load Balancer in a data center
- Requires a Load Balancer Provisioner in the cluster that abstracts data center APIs
- Exposes the same port on each node of the Kubernetes cluster
- Configures the external LoadBalancer to route traffic to these ports
- Assigns a cluster internal IP to the service
- Enables load balancing to pods, just like a ClusterIP service
ExternalName
- No IP addresses or ports assigned
- Acts as DNS alias for cluster external services
- Enables dependencies to other services without configuring external URLs
Create a ClusterIP Service for a deployment
Create a Deployment called webserver in namespace moon using image nginx:1.25.1. Set 3 replicas for the deployment. Next, create a service in namespace moon
called webserver
. The service should route cluster internal traffic to the pods of the deployment using port 80
.
Solution
Start with creating the deployment:
kubectl create deploy webserver --image nginx:1.25.1 --replicas 3 -n moon
Since the tasks mentioned the service should only route cluster internal traffic, we need a ClusterIP service. We can create this service in two ways:
- Using
kubectl expose
kubectl expose deployment webserver --port=80 --protocol=TCP --name webserver -n moon
- Using
kubectl create service
kubectl create service clusterip webserver -n moon --tcp=80:80
We can verify the service is working like expected by sending an HTTP request:
kubectl run test -n moon --rm -i --tty --image nginx:1.25.1 -- sh curl http://webserver # Nginx default response
Create a NodePort service for a deployment
Create a Deployment called webserver
in namespace sun
using image nginx:1.25.1
. Set 3 replicas for the deployment. Next, create a service in namespace sun
called webserver
. The service should expose a port on each node of the cluster. Configure it to route traffic to port 80
of the deployment Pods.
Solution
Start with creating the deployment:
kubectl create deploy webserver --image nginx:1.25.1 --replicas 3 -n sun
The task mentions you should expose a port on each node. This indicates you should use a service of type NodePort. Let’s create the service:
kubectl expose deployment webserver -n sun --port=80 --type=NodePort # You can also you kubectl create service if your more comfortable with that command kubectl create svc nodeport webserver --tcp=80 -n sun
Verify the service is working as expected by sending an HTTP request to a node on the node port:
# Get the nodePort assigned to the service kubectl get svc webserver -n sun -o yaml | grep nodePort nodePort: 32152 # Get the IP address of a node k get nodes -o wide NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME minikube Ready control-plane 22d v1.26.3 192.168.49.2 <none> Ubuntu 20.04.5 LTS 4.19.128-microsoft-standard docker://23.0.2 minikube-m02 Ready <none> 5m33s v1.26.3 192.168.49.3 <none> Ubuntu 20.04.5 LTS 4.19.128-microsoft-standard docker://23.0.2 minikube-m03 Ready <none> 5m21s v1.26.3 192.168.49.4 <none> Ubuntu 20.04.5 LTS 4.19.128-microsoft-standard docker://23.0.2 # Send the HTTP request with curl curl 192.168.49.2:32152 # Should return the default nginx page
ExternalName and LoadBalancer in the CKA exam
I did not cover CKA exam questions for service types ExternalName and LoadBalancer. It is unlikely that you have to create those services in the exam. Those services depend on external services like Cloud Provider APIs or DNS records of existing domains.
I can’t guarantee there are no questions regarding those service types, but I’ve never seen one.
Conclusion
In this article, you learned:
- To understand the different service types in Kubernetes
- To create a ClusterIP service for a sample CKA exam question
- To create a NodePort service for a sample CKA exam question
Don’t want to miss the next post in the Certified Kubernetes Administrator (CKA) series? Follow me on LinkedIn!
To support my efforts use my affiliate link to buy your courses and exams from the Linux Foundation.
Previous post in the CKA series: CKA Exam Task: DaemonSet