Day 33 Task: Working with Namespaces and Services in Kubernetes

Day 33 Task: Working with Namespaces and Services in Kubernetes

Efficient Namespace and Service Management in Kubernetes

ยท

3 min read

๐Ÿ”ถ 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. Read more about Namespace Here

๐Ÿ”ถ Task 1: Create a Namespace for your Deployment

  • Prerequisites Make instances of t2.medium.

  •     # First all update system and install Docker 
        sudo apt-get update
        sudo apt-get install docker.io -y
        sudo usermod -aG docker $USER && newgrp docker
    
        # To install Minikube & Kubectl
        curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
        sudo install minikube-linux-amd64 /usr/local/bin/minikube 
    
        sudo snap install kubectl --classic
        minikube start --driver=docker
    
  • Clone this repository to your local machine:

  •     git clone https://github.com/Chandreshpatle28/django-todo-cicd-Jenkins.git
    
  •   sudo docker build . -t chandreshpatle28/django-todo:latest
    
  • Login to your Docker Hub by using your username and password and push your image to Docker Hub as we used it for day 32 of 90 days of DevOps.

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

  •   kubectl create namespace kube-name
    

  • Update the deployment.yml file to include the Namespace

  •   apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: todo-app-deployment
        namespace: kube-name
      spec:
        replicas: 3  # Initial number of replicas
        selector:
          matchLabels:
            app: todo-app
        template:
          metadata:
            labels:
              app: todo-app
          spec:
            containers:
              - name: todo-app
                image: chandreshpatle28/django-todo
                ports:
                  - containerPort: 8000
    

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

  •   kubectl apply -f deployment.yml -n kube-name
    

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

  •    kubectl get namespaces
    

๐Ÿ”ถ Task 2: Read about Services, Load Balancing, and Networking in Kubernetes.

  • Services: Kubernetes Services provide an abstraction layer for pods, allowing them to communicate without needing to know their IP addresses. Services group pods based on labels, enabling dynamic scaling and failover. There are several types of services:

    • ClusterIP: Exposes the service only within the cluster. It's the default type and is used for internal service-to-service communication.

    • NodePort: Exposes the service on a static port across all nodes. It's suitable for scenarios where external access is required.

    • LoadBalancer: Integrates with external cloud load balancers to expose the service externally. It's ideal for applications requiring high availability.

    • Headless: Disables load balancing and allows direct access to individual pods by DNS.

  • Load Balancing: Kubernetes Services inherently provide load balancing. When multiple pods belong to a service, incoming traffic is automatically distributed among these pods. This ensures even distribution of requests, fault tolerance, and scalability.

  • Networking in Kubernetes is a fundamental aspect that enables communication between containers (pods), exposes services to the external world, and controls network traffic within the cluster. It plays a crucial role in maintaining the functionality, scalability, and security of containerized applications.

  • Refer to the official documentation of Kubernetes Link

Need help with Namespaces? Check out this video for assistance.

In conclusion, Kubernetes simplifies service management, load balancing, and networking for containerized applications. Services abstract the complexities of networking, allowing for seamless communication between pods. Kubernetes provides various types of services and advanced networking features to cater to diverse application requirements. Understanding these concepts is essential for deploying scalable and reliable containerized applications in Kubernetes.


Happy Learning! :)

Stay in the loop with my latest insights and articles on cloud โ˜๏ธ and DevOps โ™พ๏ธ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).

Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.

#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker #Jenkins #Kubernetes

Did you find this article valuable?

Support Chandresh Patle's Blog by becoming a sponsor. Any amount is appreciated!

ย