Day 34: Working with Services in Kubernetes

Day 34: Working with Services in Kubernetes

Day 34: #90DaysOfDevOpsChallenge

What are Services in K8s

In Kubernetes, Services are objects that provide stable network identities to Pods and abstract away the details of Pod IP addresses. Services allow Pods to receive traffic from other Pods, Services, and external clients.

Task-1:

Create a Service for your todo-app Deployment from Day-32.

Create a Service definition for your todo-app Deployment in a YAML file.

In this example, the Service type is set to NodePort, and a nodePort is defined as 30010. This will make the Service accessible from outside the cluster by using the IP address of any node in the cluster and the nodePort defined in the Service.

**apiVersion: v1:**This section defines the API version to use, in this case, v1.

Kind: Service: This section specifies the type of resource you are creating, in this case a Service.

metadata: name: django-todo-service: This section gives the Service a name, in this case django-todo-service

spec: selector: app: django-app: This section defines the selector for the pods that the Service should target. In this case, it will target pods with the app label set to django-app.

type: NodePort: This section specifies the type of Service you want to create, in this case NodePort.

ports: - protocol: TCP port: 80 targetPort: 8000 nodePort: 30010:

This section defines the port mapping for the Service. The Service will listen on port 80 and forward traffic to the target port 8000 on the pods. Additionally, a nodePort is defined as 30010, which will make the Service accessible from outside the cluster by using the IP address of any node in the cluster and the nodePort defined in the Service.

Apply the Service definition to your K8s (minikube) cluster using below command

 kubectl apply -f service.yml -n <namespace-name>

The kubectl get svc command is used to list Services in a Kubernetes cluster. The -n option is used to specify the namespace where the Service is located.

kubectl get svc -n <namespace>

Verify that the Service is working by accessing the todo-app using the Service’s IP and Port in your Namespace.

The minikube service command is used to interact with services in a Minikube cluster. The --url option will return the URL that you can use to access the Service in your browser.

 minikube service <service_name> -n=<namespace> --url

This will return the URL for the Service, which you can use to access the Service in a web browser. the URL will be accessible only from within the Minikube cluster, not from your local machine.

Make a curl request to the Service using its IP address:

Access the todo-app using the Service’s IP and Port:

curl -L <service-ip>:<service-port>
curl -L http://192.168.49.2:30010

If the NodePort Service is working correctly, you should see the response from the todo-app.

To test Kubernetes application on local browser we use following steps:

sudo vim /etc/hosts

In the local host file do following changes

Edit our local IP address in below format

192.168.49.2:30010 to 192.168.49.2 todo-app.com

add this line 192.168.49.2 todo-app.com in below localhost file.

We will get this command curl -L todo-app.com:30010

So we can able to deploy our application in localhost 192.168.49.2 todo-app.com

To test Kubernetes application on browser we use tool - ngrok

ngrok is a cross-platform application that enables developers to expose a local development server to the Internet with minimal effort. It creates a secure tunnel from the public internet to a local web server on your computer, making it possible to access the local web server from anywhere in the world.

Go to ngrok site, In that right click on Linux and copy link address

Create a new folder for ngrok setup, inside folder use command wget <copied_linux_address> this command will download ngrok.

unzip downloaded file using tar -xvzf <file_name>

Add authentication - Running below command will add your authtoken to the default ngrok.yml configuration file

ngrok config add-authtoken 2T4FX1oE7L3iKQEBkktrbaCUHOL_2NDXMVRBgJK8N2zynmxWB

Use command "ngrok http 192.168.49.2:30010" to make a tunnel. Here

http 192.168.49.2:30010 is url of a service given by minikube service.

above command creates a tunnel and gives IP address which is ended with ngrok.io

Copy e917-44-203-36-5.ngrok-free.app and paste it in your browser.

Task-2:

Create a ClusterIP Service for accessing the todo-app from within the cluster

Create a ClusterIP Service definition for your todo-app Deployment in a YAML file.

In above example, the Service is named django-todo-service and is of type ClusterIP. The selector app: django-todo is used to determine which Pods to associate with the Service. The Service has a single port, 8000, which is mapped to the target port 8000 on the Pods.

Apply the ClusterIP Service definition to your K8s (minikube) cluster using the kubectl apply -f service.yaml -n <namespace-name> command.

Verify that the ClusterIP Service is working by accessing the todo-app from another Pod in the cluster in your Namespace.

Get the IP address of the ClusterIP Service:

kubectl get services -n <namespace>

Note the IP address of the ClusterIP Service you want to verify.

Get the name of another Pod in the same Namespace:

kubectl get pods -n <namespace>

Note the name of another Pod in the same Namespace.

Exec into the Pod: go inside container

kubectl exec -it <pod-name> -n <namespace> -- bash

  1. Make a curl request to the ClusterIP Service using its IP address:
curl -L <cluster-ip>:<service-port> 
curl -L http://10.96.248.1:8000

If the ClusterIP Service is working correctly, you should see the response from the django-app.

Task-3:

Create a LoadBalancer Service for accessing the todo-app from outside the cluster

Create a LoadBalancer Service definition for your todo-app Deployment in a YAML file.

In this example, the Service is named django-todo-lb-service and is of type LoadBalancer. The selector app: django-todo is used to determine which Pods to associate with the Service.

Apply the LoadBalancer Service definition to your K8s (minikube) cluster using the kubectl apply -f service.yaml -n <namespace-name> command.

Verify that the LoadBalancer Service is working by accessing the todo-app from outside the cluster in your Namespace.

Get the LoadBalancer service:

kubectl get services -n <namespace>

Get the name of another Pod in the same Namespace:

kubectl get pods -n <namespace>

Get the LoadBalancer IP service:

Note the name of another Pod in the same Namespace.

Exec into the Pod: go inside container

The minikube service command is used to interact with services in a Minikube cluster. The --url option will return the URL that you can use to access the Service in your browser.

 minikube service <service_name> -n=<namespace> --url

we can also use below command to get LoadBalancer URL:

minikube service list

Note the LoadBalancer IP address of the LoadBalancer Service you want to verify.

Access the todo-app from outside the cluster using the LoadBalancer IP and the exposed port:

curl -L <load-balancer-ip>:<service-port>
curl -L http://192.168.49.2:30613

If the LoadBalancer Service is working correctly, you should see the response from the django-app.

Thank you for reading!! Hope you find this helpful.

#day34challenge#90daysofdevops

Always open to suggestions..!!

~ Manoj Bhamidipati 🙂