Day 33 Task: Working with Namespaces and Services in Kubernetes

Day 33 Task: Working with Namespaces and Services in Kubernetes

"Managing Kubernetes Workloads with Namespaces and Services: Best Practices"

ยท

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/niluflip/node-todo-cicd.git
    
        sudo docker build . -t nileshsahare/node-todo-cicd:latest
    
  • Login to your Docker Hub by using your username and password and push your image to Docker Hub

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

        kubectl create namespace kubestarter
    

  • Update the deployment.yml file to include the Namespace

        apiVersion: apps/v1
        kind: Deployment
        metadata:
          name: todo-app-deployment
          namespace: kubestarter
        spec:
          replicas: 3  # Initial number of replicas
          selector:
            matchLabels:
              app: todo-app
          template:
            metadata:
              labels:
                app: todo-app
            spec:
              containers:
                - name: todo-app
                  image: nileshsahare/node-todo-cicd
                  ports:
                    - containerPort: 8000
    

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

        kubectl apply -f deployment.yml -n kubestarter
    

  • 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.

Happy Learning :)

If you find my blog valuable, I invite you to like, share, and join the discussion. Your feedback is immensely cherished as it fuels continuous improvement. Let's embark on this transformative DevOps adventure together! ๐Ÿš€ #devops #90daysofdevop #git&github #jenkins #k8s

ย