Day 34 Task: Working with Services in Kubernetes

Day 34 Task: Working with Services in Kubernetes

ยท

5 min read

Introduction ๐Ÿ“œ

Welcome back to our Kubernetes journey! Today, on Day 34, we'll dive into the fascinating world of Kubernetes Services. Services play a crucial role in ensuring seamless communication and accessibility within a Kubernetes cluster. In this task, we'll explore different types of services and apply them to our todo-app Deployment from Day 32.

Task 1: What are Services in K8s ๐Ÿ› ๏ธ

Before we jump into the tasks, let's understand what services are in Kubernetes. In simple terms, services act as an abstraction layer, providing a stable endpoint to interact with pods. They enable load balancing, service discovery, and help in connecting different parts of your application.

Task 1.1: Create a Service for your todo-app Deployment ๐Ÿšข

To get started, we'll create a basic Service for our todo-app Deployment. This service will act as a bridge, allowing other components to communicate with our application.

Task 1.2: Create a Service definition in YAML ๐Ÿ“„

In this step, we'll define our Service in a YAML file. YAML makes it easy to configure Kubernetes resources in a human-readable format.

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

Task 1.3: Apply the Service definition to your K8s cluster โš™๏ธ

Using the kubectl apply command, we'll deploy our Service to the Kubernetes cluster, specifically to the desired namespace.

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

now create namespace

kubectl create namespace django-app
kubectl create -f service.yml -n django-app

Task 1.4: Verify Service functionality ๐ŸŒ

Let's make sure everything is working smoothly by accessing our todo-app using the Service's IP and Port within our chosen namespace.

Task 2:ClusterIP Service

Purpose:

  • Internal Communication: ClusterIP is primarily used for communication within the Kubernetes cluster. It provides a stable internal IP address that other services or pods can use to communicate with the targeted pods.

Key Features:

  1. Internal Load Balancing: ClusterIP distributes traffic among the pods that belong to the service. This load balancing happens within the cluster.

  2. Stable Internal IP: A ClusterIP service gets assigned a stable internal IP address. This allows other services or pods within the cluster to reliably communicate with the pods associated with the service.

  3. Port Mapping: It enables pods or services to access the ClusterIP service using a specific port, abstracting away the complexity of dealing with individual pod IPs and ports.

Use Case:

  • Microservices Architecture: ClusterIP is well-suited for scenarios where different microservices within the same cluster need to communicate with each other without exposing their internal details.

Example YAML Definition:

apiVersion: v1
kind: Service
metadata:
  name: todo-service
  namespace: django-name
spec:
  type: NodePort
  selector:
    app: todo-app
  ports:
    - port: 80
      targetPort: 8000
  type: ClusterIP

Task 2.1: Create a ClusterIP Service ๐ŸŒ

Now, we'll create a ClusterIP Service, allowing internal communication within the cluster.

apiVersion: v1
kind: Service
metadata:
  name: todo-service
  namespace: django-name
spec:
  type: NodePort
  selector:
    app: todo-app
  ports:
    - port: 80
  type: ClusterIP

Task 2.2: Create a ClusterIP Service definition ๐Ÿ“„

Define the ClusterIP Service in another YAML file, making it clear and organized.

Task 2.3: Apply the ClusterIP Service definition โš™๏ธ

Deploy the ClusterIP Service to the Kubernetes cluster and namespace.

kubectl apply -f service.yaml

Task 2.4: Verify ClusterIP Service functionality ๐ŸŒ

Check if the ClusterIP Service is working by accessing the todo-app from another Pod within the cluster.

kubectl get svc -n django-name

Task 3: LoadBalancer Service

Purpose:

  • External Access: LoadBalancer Services are designed to expose applications to the external world. They allocate an external IP address, allowing external clients to access the services running inside the Kubernetes cluster.

Key Features:

  1. External IP Assignment: LoadBalancer services automatically obtain an external IP address. This IP is typically provided by the cloud provider's load balancer.

  2. External Load Balancing: It performs load balancing for external traffic, distributing incoming requests across the pods associated with the service.

  3. NodePort and ClusterIP Combination: Internally, LoadBalancer services use a combination of NodePort and ClusterIP services to expose applications externally.

Use Case:

  • Public-Facing Applications: LoadBalancer services are ideal for applications that need to be accessed by clients outside the Kubernetes cluster, such as web applications or APIs.

Example YAML Definition:

apiVersion: v1
kind: Service
metadata:
  name: my-loadbalancer-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer

Note:

  • It's important to mention that LoadBalancer services may not be available in every on-premise or self-hosted Kubernetes setup. In such cases, external load balancing solutions need to be considered.

Understanding and appropriately using these services is crucial for orchestrating communication within and outside the Kubernetes cluster. ClusterIP for internal communication and LoadBalancer for external accessibility contribute to creating a robust and well-connected Kubernetes environment.

Task 3.1: Create a LoadBalancer Service for external access ๐ŸŒ

In this exciting step, we'll make our todo-app accessible from outside the cluster using a LoadBalancer Service.

apiVersion: v1
kind: Service
metadata:
  name: todo-service
  namespace: django-name
spec:
  type: NodePort
  selector:
    app: todo-app
  ports:
    - port: 8000
      targetPort: 8000
  type: LoadBalancer

Task 3.2: Create a LoadBalancer Service definition ๐Ÿ“„

Define the LoadBalancer Service in a YAML file, specifying the necessary configuration.

Task 3.3: Apply the LoadBalancer Service definition โš™๏ธ

Deploy the LoadBalancer Service to the Kubernetes cluster and namespace, bringing our application one step closer to the world.

kubectl apply -f service.yml -n django-name

Task 3.4: Verify LoadBalancer Service functionality ๐ŸŒ

Finally, confirm that the LoadBalancer Service is working by accessing the todo-app from outside the cluster in our chosen namespace.

Conclusion ๐Ÿ’ฅ

Congratulations on completing the Day 34 tasks! Today, we explored the power of Kubernetes Services, connecting and enabling seamless communication within and outside the cluster. As you continue your Kubernetes journey, remember that mastering Services is key to building robust and scalable applications in the Kubernetes ecosystem. Keep exploring, keep learning, and stay tuned for more exciting Kubernetes adventures! ๐Ÿš€

ย