☑️Day 38: Exploring Namespaces in Kubernetes🚀

🔹Table of Contents :

  • Introduction

  • What are Namespaces?

  • Why Use Namespaces?

  • Hands-on Learning

  • Commands Used

  • Real-World Scenario

  • Key Learnings


✅Introduction:

What are Namespaces in Kubernetes?

In Kubernetes, namespaces provide a mechanism to divide a single Kubernetes cluster into multiple virtual clusters. They act as virtual boundaries, allowing you to logically separate resources, users, and services in a cluster. This is particularly useful for large environments or multi-tenant setups.

✅Why Use Namespaces?

  • Organizational Separation: Helps divide resources between different teams, projects, or environments (e.g., dev, test, production).

  • Resource Allocation: You can set resource limits per namespace, ensuring that one project or team does not consume all the resources.

  • Efficient Management: Simplifies managing large clusters by grouping similar workloads together.


✅How Namespaces Work in Kubernetes

  • Each resource in Kubernetes, such as Pods, Services, or Deployments, can be assigned to a specific namespace.

  • By default, Kubernetes comes with three pre-defined namespaces:

    • default: Where resources live when no specific namespace is defined.

    • kube-system: Contains all Kubernetes system components.

    • kube-public: Generally used for public access across the cluster.


✅Hands-On Tasks with Namespaces

Task 1: Creating a New Namespace and Deploying Resources

Scenario:
Imagine your organization has a production and development environment within the same cluster. You want to isolate these environments for easier management.

Steps:

  1. Create a new namespace:

    • Command:

        kubectl create namespace dev-environment
      
  2. Switch context to the new namespace:

    • Command:

        kubectl config set-context --current --namespace=dev-environment
      
  3. Deploy resources (e.g., NGINX Pod) in the new namespace:

    • Create a YAML file (nginx-pod.yaml) for deploying a pod.

        apiVersion: v1
        kind: Pod
        metadata:
          name: nginx-pod
          namespace: dev-environment
        spec:
          containers:
            - name: nginx
              image: nginx:latest
      
    • Apply the file:

        kubectl apply -f nginx-pod.yaml
      
  4. Verify the resources deployed in the namespace:

    • Command:

        kubectl get pods -n dev-environment
      
  5. Delete the namespace and its resources:

    • Command:

        kubectl delete namespace dev-environment
      

Task 2: Resource Quotas and Limits on Namespaces

Scenario:
You want to ensure that a particular team doesn’t consume more than a certain amount of resources (e.g., CPU, memory) in a namespace. This is especially important when multiple teams or departments share the same Kubernetes cluster.

Steps:

  1. Create a namespace with resource quotas:

    • Command:

        kubectl create namespace prod-environment
      
  2. Define resource quota YAML (resource-quota.yaml):

     apiVersion: v1
     kind: ResourceQuota
     metadata:
       name: prod-quota
       namespace: prod-environment
     spec:
       hard:
         pods: "10"
         requests.cpu: "4"
         requests.memory: 10Gi
         limits.cpu: "8"
         limits.memory: 20Gi
    
  3. Apply the resource quota to the namespace:

    • Command:

        kubectl apply -f resource-quota.yaml
      
  4. Check resource usage in the namespace:

    • Command:

        kubectl get resourcequota -n prod-environment
      
  5. Attempt to deploy a pod exceeding the resource limit:

    • If the resources exceed the defined quota, Kubernetes will deny the deployment, ensuring the namespace adheres to the constraints.

✅Real-World Scenario

Scenario:
A company has two development teams: Frontend and Backend. Both teams share the same Kubernetes cluster but need isolated environments to work independently. They create two separate namespaces: frontend-team and backend-team. Each team deploys resources and services within their namespace without interfering with each other. Additionally, resource quotas are set to avoid resource hogging by one team.


✅Key Commands for Namespaces

  1. List all namespaces:

     kubectl get namespaces
    
  2. Create a new namespace:

     kubectl create namespace <namespace-name>
    
  3. Set the default namespace for the current context:

     kubectl config set-context --current --namespace=<namespace-name>
    
  4. Deploy a pod in a specific namespace (YAML must specify the namespace):

     kubectl apply -f <pod-file>.yaml -n <namespace-name>
    
  5. View resources in a specific namespace:

     kubectl get pods -n <namespace-name>
    
  6. Delete a namespace:

     kubectl delete namespace <namespace-name>
    
  7. Apply resource quota:

     kubectl apply -f resource-quota.yaml -n <namespace-name>
    
  8. View resource quotas in a namespace:

     kubectl get resourcequota -n <namespace-name>
    

✅Key Learnings

Namespaces in Kubernetes provide an effective way to isolate and organize resources within a cluster. Whether you need separate environments for different teams or projects, or want to enforce resource limits, namespaces make managing a Kubernetes cluster simpler and more efficient. Combining namespaces with other Kubernetes features like resource quotas ensures scalability and optimal resource utilization in a multi-team, multi-environment setup.


🚀Thanks for joining me on Day 38! Let’s keep learning and growing together!

Happy Learning! 😊

#90DaysOfDevOps

💡
Follow for more updates on LinkedIn , Github and Twitter(X)
10
Subscribe to my newsletter

Read articles from Kedar Pattanshetti directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kedar Pattanshetti
Kedar Pattanshetti