#90daysofdevops #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.

A Kubernetes service is a logical abstraction for a deployed group of pods in a cluster (which all perform the same function). Since pods are ephemeral, a service enables a group of pods, which provide specific functions (web services, image processing, etc.) to be assigned a name and unique IP address (clusterIP).

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.

No alt text provided for this image

In this illustration, a node port is defined as 30080, and the service type is set to NodePort. By using any cluster node's IP address and the nodePort specified in the service, will make the service available from outside the cluster.

apiVersion: v1

The API version to utilise, in this case v1, is specified in this section.

kind: Service

The kind of resource you are creating, in this case a service, is specified in this section.

metadata: 
  name: django-todo-service

The Service is given a name in this part; in this case, it is django-todo-service.

spec: 
  selector: 
    app: todo

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 todo.

 type: NodePort

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

ports: 
  - protocol: TCP 
    port: 8000 
    targetPort: 8000 
    nodePort: 30080

The port mapping for the Service is described in this section. In order to redirect traffic to the target port 8000 on the pods, the Service will listen on port 8000. A nodePort is also defined as 30080, making it possible to contact the Service from outside the cluster by using the IP addresses of any cluster nodes and the nodePort specified in the Service.

Use the command below to apply the Service definition to your K8s (minikube) cluster.

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

Services in a Kubernetes cluster can be listed using the kubectl get svc command. The namespace in which the Service is located is specified using the -n option.

kubectl get svc -n <namespace>

No alt text provided for this image

Use the IP address and port of the service in your namespace to access the to-do app to check if the service is operational.

To communicate with services in a Minikube cluster, use the minikube service command. The URL you can use to visit the Service in your browser will be returned by the --url option.

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

No alt text provided for this image

You can use the URL that is returned by this to visit the Service with a web browser. The URL won't work on your local system; it will only work within the Minikube cluster.

Use the Service's IP address to send a curl request:

Use the IP and Port of the Service to access the to-do app:

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

If the NodePort Service is functioning properly, you ought to see the todo-response.

No alt text provided for this image

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.

No alt text provided for this image

The service in the aforementioned example is called django-todo-service and is of the type ClusterIP.

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

No alt text provided for this image

Access the to-do app from another Pod in the cluster in your Namespace to ensure that the ClusterIP Service is operational.

Get the ClusterIP Service's IP address:

kubectl get services -n <namespace>

No alt text provided for this image

Make a note of the ClusterIP Service's IP address that you want to confirm.

Identify another pod in the same namespace by its name:

kubectl get pods -n <namespace>

No alt text provided for this image

Note the name of another Pod in the same Namespace.

  1. Exec into the Pod: go inside container
kubectl exec -it <pod-name> -n <namespace> -- bash

No alt text provided for this image

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

The todo-app should respond if the ClusterIP Service is functioning properly.

No alt text provided for this image

Task-3:

To allow access to the to-do app from outside the cluster, create a load-balancing service.

Create a YAML file with the LoadBalancer Service specification for your todo-app deployment.

No alt text provided for this image

The service in this illustration is of the type LoadBalancer and goes by the name django-todo-lb-service. To choose which Pods to link to the Service, utilise the selector app: todo.

Using the kubectl apply -f service.yml -n namespace-name> command, apply the LoadBalancer Service definition to your K8s (minikube) cluster.

By logging onto the to-do app from outside the cluster in your Namespace, you can confirm that the LoadBalancer Service is operational.

Get the load balancer service:

kubectl get services -n <namespace>

No alt text provided for this image

we can also use below command to get LoadBalancer URL:

minikube service list

No alt text provided for this image

Make a note of the load balancer service's IP address if you want to verify it.

Use the load balancer's IP address and the access port to access the to-do app from outside the cluster:

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

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

No alt text provided for this image