Access Pods Without Kubernetes Service¶
Consider a deployment with 2 replicas of a pod. Now, let's assume there is no Kubernetes Service. How does other pods in the cluster access these pods?
They do so through IP addresses of these pods. Let's see this in action!
Docker Images¶
Here are the Docker Images used in this tutorial:
Note
reyanshkharga/nodeapp:v1 runs on port 5000
and has the following routes:
GET /
Returns host info and app versionGET /health
Returns health status of the appGET /random
Returns a randomly generated number between 1 and 10
Step 1: Create Pods Using Deployments¶
Let's create two deployments as follows:
Here's what your folder structure should look like:
Apply the manifest to create the deployments:
# Create frontend deployment
kubectl apply -f frontend-deployment.yml
# Create backend deployment
kubectl apply -f backend-deployment.yml
Or, you can also apply them together as follows:
Step 2: Verify Deployments and Pods¶
Also, let's list out the pods with wide option to view the IP addresses of the pods.
Step 3: Access Backend Pod From a Frontend Pod¶
Let's try to access one of the backend pods from a frontend pod.
-
Start a shell session inside the conainer of one of the frontend pods:
-
Access backend pods:
You can verify the same with other backend pod.
Problems With This Approach of Accessing Pods¶
-
What happens if let's say one of the pods go down? The Kubernetes deployment controller brings up another pod. Now the IP address list of these pods change and all the other pods need to keep track of the same.
-
The same is the case when there is auto scaling enabled. The number of the pods increases or decreases based on demand.
-
What if you want to enable load balancing for these pods? How do you do that? Implementing load balancing could be a tedious job.
And that's where kubernetes Service
comes into the picture. It solves all of the above problems.
Clean Up¶
Delete the deployments:
# Delete frontend deployment
kubectl delete -f frontend-deployment.yml
# Delete backend deployment
kubectl delete -f backend-deployment.yml
Or, you can also delete them together as follows: