Exposing Kubernetes Pods Using Services
In this simple hands on lab, we'll be practising exposing Kubernetes Pods using Services.
Kubernetes Services provides a way to expose an application running as a set of Pods. Service acts as an abstraction layer that allows Client to communicate with the service rather than communicate with the individual Pods.
Service Types:
ClusterIP: ClusterIP Services expose applications inside the cluster network.
NodePort: NodePort Services expose applications outside the cluster network.
LoadBalancer: LoadBalancer Services also exposes application outside the cluster network, but they use an external cloud load balancer to do so.
For this lab, we'll use Kubernetes Services to expose existing Kubernetes Pods. We've been asked to set up Kubernetes Services for two existing Deployments (user-db and web-frontend) running 2 Pod replicas each.
The user database is a backend service that should only be accessible by other components within the cluster. The web frontend needs to be accessible by users outside the cluster.
We'll locate the existing deployments and create the necessary Services to expose them. There is an existing Pod called busybox
which we'll use to test Services.
To achieve this, we'll:
Expose the Pods from the user-db deployment as an internal service
Expose the Pods from the web-frontend as an external service
Confirm you can reach the user-db Pods internally
Confirm you can reach the web-frontend Pods externally
Steps:
Expose the Pods from the user-db deployment as an internal service:
Confirm the properties of the user-db deployment. The deployment selector label will be needed to create the Service.
Also, confirm the deployment "containerPort" to map to it using the Service
Create the user-db service yaml file for the service. We'll be creating a "ClusterIP" service type because we are creating an internal service
Expose the Pods from the web-frontend as an external service:
Confirm the properties of the web-frontend deployment. The deployment selector label will be needed to create the Service.
Also, confirm the deployment "containerPort" to map to it using the Service
Create the web-frontend service yaml file for the service. We'll be creating a "NodePort" service type because we are creating an external service
Confirm you can reach the user-db Pods internally:
Execute a command inside the busybox Pod to test reachability to the "user-db" pods through the created "user-db-svc" service
Confirm you can reach the web-frontend Pods externally:
Go to your web browser and navigate to the Public IP address of your Cluster Node and the "nodePort" number configured in the "web-frontend-svc" service file.
Voila! QED!
Subscribe to my newsletter
Read articles from Jubril Edun directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Jubril Edun
Jubril Edun
Hi there, My name is Jubril Edun, a tech enthusiast who is on a learning journey into the world of Cloud Engineering, Cybersecurity and beyond. Join me as I document my learning journey.