Day 34: Working with Services in Kubernetes

Day 34: Working with Services in Kubernetes

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 30080. 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: 8001 targetPort: 8001 nodePort: 30080:

This section defines the port mapping for the Service. The Service will listen on port 8001 and forward traffic to the target port 8001 on the pods. Additionally, a nodePort is defined as 30080, 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:30080

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

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 2LreFXIv6VCMj1n3z2HctGuyYnV_2qjHyH5hY9H3S1yYz2zEe

Use command “./ngrok http 192.168.49.2:30080 to make a tunnel. Here http 192.168.49.2:30080 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 0860-34-228-158-184.ngrok.io 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-app is used to determine which Pods to associate with the Service. The Service has a single port, 8001, which is mapped to the target port 8001 on the Pods.

Apply the ClusterIP Service definition to your K8s (minikube) cluster using the kubectl apply -f service.yml -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 10.106.107.66:8001

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-app 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.yml -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 LoadBalancer IP service:

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.

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:32059

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

Thank you for reading! I hope you find this article helpful!!

Happy Learning!!

Sayali ✨

Did you find this article valuable?

Support Sayali Jadhav by becoming a sponsor. Any amount is appreciated!