Day 34- Mastering Kubernetes Services: Elevate Your Todo-App  (Dec 30, 2023)

Day 34- Mastering Kubernetes Services: Elevate Your Todo-App (Dec 30, 2023)

What are Services in K8s?

A Kubernetes Service is a mechanism to expose applications both internally and externally. Every service will create an everlasting IP address that can be used as a connector

Additionally, it will open a port that will be linked with a targetPort. Some services can create ports in every Node, and even external IPs to create connectors outside the cluster.

With the combination of both IP and Port, we can create a way to uniquely identify an application.

Services come in several types:

  • ClusterIP (default):

    This is the default type for service in Kubernetes.

    As indicated by its name, this is just an address that can be used inside the cluster. This creates a connection using an internal Cluster IP address and a Port. But, what if we need to use this connector from outside the Cluster? This IP is internal and won’t work outside.

    This is where the rest of the services come in…

  • Nodeport:

    A NodePort differs from the ClusterIP in the sense that it exposes a port in each Node. When a NodePort is created, kube-proxy exposes a port in the range 30000-32767.

    The problem with using a NodePort is that you still need to access each of the Nodes separately.

  • LoadBalancer:

    A LoadBalancer is a Kubernetes service that:

    • Creates a service like ClusterIP

    • Opens a port in every node like NodePort

    • Uses a LoadBalancer implementation from your cloud provider (your cloud provider needs to support this for LoadBalancers to work).

    • LoadBalancer services are typically used in cloud environments where they provision a load balancer to distribute traffic across multiple pods. This allows for external access to services.

  • ExternalName:

    This service type maps a service to an external DNS name. It's used for accessing external services from within the cluster.

Services provide an essential layer of networking abstraction in Kubernetes, allowing applications to communicate reliably and consistently, regardless of where pods are located or how they are scaled.

Today Task — 01:

Create a Service for your todo-app Deployment From Day-32

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

apiVersion: v1
kind: Service
metadata:
  name: todo-service
  namespace: django-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: 30007

What-are-Kubernetes-Services?

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: todo-service

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

spec: 
  selector: 
    app: todo-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 todo.

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

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 30007, 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 the kubectl apply -f service.yml -n <namespace-name> 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>

What-are-Kubernetes-Services?

- 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

What-are-Kubernetes-Services?

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:

  1. Access the todo-app using the Service’s IP and Port:
curl -L <service-ip>:<service-port>
curl -L http://192.168.49.2:30007

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