Day 33 Task: Working with Namespaces and Services in Kubernetes

What are Namespaces and Services in k8s

In Kubernetes, Namespaces are used to create isolated environments for resources. Each Namespace is like a separate cluster within the same physical cluster. Services are used to expose your Pods and Deployments to the network.

Today's task:

Task 1:

  • Create a Namespace for your Deployment

  • Use the command kubectl create namespace <namespace-name> to create a Namespace

  • Update the deployment.yml file to include the Namespace

  • Apply the updated deployment using the command: kubectl apply -f deployment.yml -n <namespace-name>

  • Verify that the Namespace has been created by checking the status of the Namespaces in your cluster.

apiVersion: apps/v1

kind: Deployment

metadata:

  name: todo-deployment
  namespace: django-app

  labels:

    app: todo-app

spec:

  replicas: 3

  selector:

    matchLabels:

      app: todo-app

  template:

    metadata:

      labels:

        app: todo-app

    spec:

      containers:

      - name: todo-app

        image: pooja-bhavani/django-todo-cicd

        ports:

        - containerPort: 8000
kubectl create namespace django-app
kubectl delete pods <your pods names>
kubectl apply -f deployment.yaml -n django-app
kubectl get namespace

Task 2:

Services:

  • Services provide a consistent way to access and connect to groups of Pods (instances of an application) within a Kubernetes cluster.

  • Types of Services:

    • ClusterIP: Exposes the service only within the cluster.

    • NodePort: Exposes the service on each node's IP at a specific port.

    • LoadBalancer: Creates an external load balancer in the cloud provider, routing external traffic to the service.

    • ExternalName: Maps the service to a DNS name.

Load Balancing:

  • Role: Load balancing ensures even distribution of network traffic across multiple Pods or instances of an application.

  • How It Works: When multiple instances of an application (Pods) exist, Kubernetes' built-in load balancer intelligently distributes incoming traffic among these instances, optimizing performance and preventing overload on any single Pod.

Networking:

  • Kubernetes Network Model: Each Pod in Kubernetes has a unique IP address within the cluster, allowing direct communication between Pods regardless of the node they're on.

  • Container-to-Container Communication: Pods within the same node can communicate via localhost, while Pods on different nodes communicate using the Pod's IP address.