A development team needs to deploy a new multi-tier application in the cluster. The application consists of a public-facing web frontend and a backend database service. As the Kubernetes administrator, you are responsible for deploying these components and setting up the networking to allow them to communicate correctly and to expose the frontend to the internet.
To accomplish this, you will create a ClusterIP
service for the database, and both a NodePort
and a LoadBalancer
service for the frontend.
Create the Backend Database Deployment:
database-deployment
.postgres:16-alpine
image. (Note: You can verify this is current at https://hub.docker.com/_/postgres, but for this exercise, use the specified version for consistency.)app: database
.Expose the Database Internally:
ClusterIP
Service named database-svc
.database-deployment
on port 5432
.Create the Frontend Web Application Deployment:
frontend-deployment
.nginx:1.25-alpine
image.app: frontend
.Expose the Frontend for Testing (NodePort):
NodePort
Service named frontend-nodeport-svc
.frontend-deployment
on port 80
.30007
on each node in the cluster.Expose the Frontend to the Internet (LoadBalancer):
LoadBalancer
Service named frontend-lb-svc
.frontend-deployment
on port 80
.database-deployment
and frontend-deployment
are running with the correct number of replicas.database-svc
is a ClusterIP
service and correctly selects the database pod.frontend-nodeport-svc
is a NodePort
service, is accessible on port 30007
on the nodes, and correctly selects the frontend pods.frontend-lb-svc
is a LoadBalancer
service, receives an external IP (in a cloud environment), and correctly selects the frontend pods.database-svc
(verified by DNS resolution and network connectivity tests).For Creating Deployments:
kubectl create deployment
with --image
and --replicas
. Add labels afterward using kubectl label deployment
.kubectl apply -f
.For Creating Services:
kubectl expose deployment
with the --type
flag (ClusterIP
, NodePort
, or LoadBalancer
).nodePort
, you may need to create the service declaratively using a YAML file.ClusterIP
, you can omit the --type
flag as it's the default.For Verification:
kubectl get deployments,services,pods -o wide
to get a comprehensive overview.kubectl describe service <service-name>
to inspect the service's selector and endpoints.kubectl exec
into a pod and use curl
or another tool to access the service DNS name.Understanding Kubernetes Service types is fundamental to controlling network traffic in your cluster.
ClusterIP
is the workhorse for internal, service-to-service communication, forming the backbone of a microservices architecture.NodePort
is an essential tool for development, testing, or exposing services in on-premise environments where a cloud load balancer isn't available.LoadBalancer
is the standard, production-grade method for exposing applications to the internet, providing a stable, publicly accessible endpoint that distributes traffic across your pods.A CKA must be able to choose the correct Service type for a given scenario and configure it correctly to ensure application availability, security, and scalability.