Day 34- 90DaysOfDevOps
Working with Services in Kubernetes

Hey Learner! Welcome back. In the previous challenge understand the concept of Namespaces in K8s, and how to create, delete and use it in the manifest file. Moving forward in this challenge we'll look at how to use services. Let's start...
What are Services in K8s
In K8s, Services are used to expose your Pods and Deployments to the network. Pods are very dynamic i.e. they come and go on the K8s cluster and any of the available nodes and it would be difficult to access the Pods as the Pods IP changes once it's recreated.
A service object is a logical bridge between Pods and end users which provides virtual IP.
A service allows clients to reliably connect to the containers running in the Pod using the virtual IP. The virtual IP is not an actual IP connected to a network interface but its purpose is purely to forward traffic to one or more pods.
Service helps to expose the virtual IP mapped to the pods and allows applications to receive traffic.
Task 1- Create a Service for your todo-app Deployment from Day 32.
We created a deployment file for the todo-app without a namespace and service in DAY32. Moving forward in this task we will create a deployment with namespaces and services.
First, create a namespace called node-app for this task. kubectl create namespace node-app

Make changes in the deployment.yml file as shown below.
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo
namespace: node-app
labels:
app: todo-app
spec:
replicas: 2
selector:
matchLabels:
app: todo-app
template:
metadata:
namespace: node-app
labels:
app: todo-app
spec:
containers:
- name: todo-container
image: amitvpawar/node-app:v1
ports:
- containerPort: 5000
Create deployment using kubectl apply -f deployment.yml command.

Now create a service.yml file and apply with kubectl apply -f service.yml command.

To get details about services and deployments use the following commands as shown

To access your running application from your local machine you need a URL. For URL use minikube service <service-name> -n <namespace-name> --url command. After this command, you will get the URL. Browse on Chrome.
If you are in default namespace use minikube service <service-name> --url.


This is how we can use NodePort-type Service.
Task 2- Create a ClusterIP Service for accessing the todo-app from within the cluster
For this task first delete the service created in the above task. Use kubectl delete -f service.yml command. Copy the service.yml file in the same directory with any name and change it as shown.
Note:- ClusterIP is the default type for Service Object so no need to specify. In our case, we simply comment type line to avoid NodePort as a service type.
apiVersion: v1
kind: Service
metadata:
name: todo-service
namespace: node-app
spec:
selector:
app: todo-app
ports:
- port: 5000
targetPort: 5000
# type: NodePort
Create a service with the new file.

ClusterIP:- Exposes virtual IP only reachable from within a cluster. As we run this in Minikube cluster we have to enter into the Minikube container and access using curl <ClusterIP:PortNO>

It's reachable within the cluster.
Task 3- Create a LoadBalancer Service for accessing the todo-app from outside the cluster
Add a new service file as shown below
apiVersion: v1
kind: Service
metadata:
name: todo-service
namespace: node-app
spec:
selector:
app: todo-app
ports:
- port: 5000
targetPort: 5000
type: LoadBalancer
For this task also do the same procedure as do for task 2.

To access Get URL


This is all about the service types we use.
Thank you so much for taking the time to read till the end! Hope you found this blog informative.
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Find me on:- Hashnode LinkedIn Github
Happy Learning!




