Working with Services in Kubernetes

Working with Services in Kubernetes

Day 34 Task

What are Services in K8s

In Kubernetes, a Service is a method for exposing a network application that is running as one or more Pods in your cluster.

OR

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.

Imagine you have a group of pods running your application, and you want other applications or users to be able to interact with it. Services provide a consistent and stable way for them to do that.

Think of a Service as a reception desk in a building. When someone wants to visit a specific department or office, they go to the reception desk, and the receptionist directs them to the right place. Similarly, a Service acts as a central point of access to your application.

Kubernetes: part 1 – architecture and main components overview

NodePort Service in Kubernetes is a way to make your application accessible from outside the cluster. It allows external traffic to reach your application by opening a specific port on all the nodes in the cluster.

Here's how it works:

  1. You create a NodePort Service and specify the port you want to use for external access.

  2. Kubernetes opens that specified port on each node in the cluster.

  3. When an external request arrives at any node's IP address on that port, Kubernetes routes the traffic to the Service.

  4. The Service, in turn, forwards the traffic to the pods running your application.

It's like having multiple doors to your application, with each door (node) listening on the same specified port. When someone accesses any of those doors from outside, the request is directed to your application running in the pods.

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.

apiVersion: v1
kind: Service
metadata:
  name: my-django-service
  namespace: my-todo-app
spec:
  type: NodePort
  selector:
    app: todo-app
  ports:
      # By default and for convenience, the `targetPort` is set to the same value as the `port` field.
    - port: 80
      targetPort: 8000
      # Optional field
      # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
      nodePort: 30015

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

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


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.

apiVersion: v1
kind: Service
metadata:
  name: my-django-app-cluster
  namespace: my-todo-app
spec:
  type: ClusterIP
  selector:
    app: todo-app
  ports:
    - name: http
      protocol: TCP
      port: 8000
      targetPort: 8000

  • Apply the ClusterIP Service definition to your K8s cluster using the kubectl apply -f cluster-ip-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.

Now we can say that ClusterIP Service is working by accessing the todo-app from another Pod in the cluster in your Namespace.

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.

  • Apply the LoadBalancer Service definition to your K8s cluster using the kubectl apply -f load-balancer-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.


Thank you for reading. I hope you were able to understand and learn something new from my blog.

Happy Learning!

Please follow me on Hashnode and do connect with me on LinkedIn ArnavSingh.

Did you find this article valuable?

Support Arnav Singh by becoming a sponsor. Any amount is appreciated!