Day33 of #90DaysOfDevOps

Working with Namespaces and Services in Kubernetes

What are Namespaces in Kubernetes?

Namespaces in Kubernetes are used to create isolated environments for different resources, like Pods, Services, and Deployments. Think of each namespace as a separate cluster within the same Kubernetes environment. This helps you avoid conflicts when you have multiple teams or applications running in the same cluster.

Namespaces are particularly useful when:

  • You want to organize resources by environment (e.g., dev, staging, prod).

  • You need to limit the resources available to specific teams or projects.

  • You want to enforce access control and resource quotas across different teams.


What are Services in Kubernetes?

Services in Kubernetes are responsible for exposing your Pods and Deployments to the network, allowing them to communicate with each other or external users. Services ensure that Pods are discoverable and accessible, even as the underlying Pods scale up or down.

Kubernetes offers several types of services:

  • ClusterIP: Exposes the service internally within the cluster.

  • NodePort: Exposes the service on a static port on each node in the cluster.

  • LoadBalancer: Exposes the service externally using a cloud provider’s load balancer.

Services abstract away the complexity of how Pods are created and destroyed, allowing you to reliably connect to them even when their IP addresses change.


Task 1:Create a Namespace for Your Deployment

  • 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:Learn About Services, Load Balancing, and Networking in Kubernetes.

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.