Day 09/40 Days of K8s: Understanding Services - ClusterIP vs NodePort vs LoadBalancer vs External

·

4 min read

As we know Deployment manages Rps , Rps manages pods. Now, it's time to learn about service.

🤔 Why Need for Service?

Scenario: Consider we have no service...

By default, Kubernetes allows pod-to-pod communication using IP addresses within the same cluster. However, due to the ephemeral nature of pods, using IP addresses directly is not practical. This is where services come into play:

  1. Ephemeral Pod IPs: Each pod gets a unique IP address, and these addresses can change as pods are restarted or rescheduled.

  2. Inconsistent Exposure: It's impractical to expose applications using pod IP addresses directly because they are not static.

  3. Communication: Without services, it's challenging to maintain stable communication between pods and external users or even among pods themselves.

    Real World Example: When a user visits google.com, the request is directed to a load balancer, which then routes the traffic to the internal IPs of the target servers or pods. To ensure consistent traffic distribution across all backend pods, a static IP address is required. This setup helps manage and balance the load effectively.

User --> google.com --> Load Balancer --> Target Pods

🤔 How does Service function?

Svc acts as : Static IP, Load balancer, svc discovery, expose app to outside world

  1. Svc discovery(using selector and labels) :
    Services use selectors to identify and target pods that have specific labels. This way, services know which pods they need to route traffic to.

  2. Load balancing effectively :
    By default, Kubernetes services use round-robin load balancing technique to distribute traffic among the targeted pods.

  3. Expose applications to outside world:
    we have diff type of services: ClusterIP, NodePort, LoadBalancer,External

🔢 Types of services:

  1. ClusterIp: The default service type, accessible only within the cluster. It's used for internal communication between services.
    For example, communication between the front-end and back-end components of your application.

  2. NodePort: Exposes the service on each node's IP at a static port (range 30000-32767). Useful for accessing the service from outside the cluster. Basically, it opens a specific port on each node and route the traffic to the service.
    Example, When you want to enable external connectivity to your service. Using a NodePort gives you the freedom to set up your own load balancing solution.

    KIND cluster doesn’t expose NodePort outside default, create cluster again by exposing port by extraPortMappings.

    Create Kind cluster:

kind create cluster --config kind.yaml --name kind-cluster-2
Creating cluster "kind-cluster-2" ...

kind get clusters
cluster-1
kind-cluster-2

Kind.yaml:

# three node (two workers) cluster config
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  extraPortMappings:
  - containerPort: 30007
    hostPort: 30007
- role: worker
- role: worker

This will ensure the application is accessible on 30007 to host port(worker node) and container port using port mapping.

  1. LoadBalancer Service: Provides a stable, externally accessible IP address that remains consistent throughout the lifecycle of the service. Typically used in cloud environments that support external load balancers.Traffic from the external load balancer is directed at the backend Pods. The cloud provider decides how it is load balanced.

    NOTE: KIND cluster will not get external IP by default. We can configure using loadbalancer for kind cluster by Installing Cloud Provider KIND.

  2. External service: Maps a service to a DNS name. Internal services can refer to this DNS name to connect to external services.
    Example,This is commonly used to create a service within Kubernetes to represent an external datastore like a database that runs externally to Kubernetes.

     apiVersion: v1
     kind: Service
     metadata:
       name: my-service
       namespace: prod
     spec:
       type: ExternalName
       externalName: my.database.example.com
    

    This Service definition, maps the my-service Service in the prod namespace to my.database.example.com

Create Deployment & service type:

Declarative Way:

  1. Create Deployment:

  2. Create NodePort Service:

  3. Create ClusterIp and LoadBalancer service:

Imperative Way:

Endpoint: Endpoints are the IP addresses of the pods that a service routes traffic to. They are updated automatically when pods start or stop.

Troubleshooting Services:

To troubleshoot services, you can use commands like kubectl describe svc svcname, which will show the endpoints associated with the service,This helps in understanding which pods are being targeted by the service.

Kubernetes Services are essential for ensuring stable and efficient communication between different components of an application. They provide consistent endpoints, load balancing, and service discovery, making them crucial for both internal and external access to applications within a Kubernetes cluster.

By understanding and effectively utilizing services, you can ensure that our applications are accessible, scalable, and resilient within the Kubernetes environment.

#Kubernetes #Containerization #ClusterIP #NodePort #LoadBalancer #External #CKASeries #40DaysofKubernetes

Â