Introduction

Namespaces in Kubernetes are a way to divide cluster resources between multiple users. They provide a mechanism for isolating groups of resources within a single cluster. Namespaces are intended for use in environments with many users spread across multiple teams, or projects.

Key Concepts

  • Isolation: Namespaces provide a scope for names. Resources in different namespaces can have the same name.
  • Resource Quotas: You can set resource quotas for each namespace to limit the amount of resources that can be consumed.
  • Access Control: Role-Based Access Control (RBAC) can be applied to namespaces to control access to resources.

Creating a Namespace

To create a namespace, you can use the kubectl command or a YAML configuration file.

Using kubectl Command

kubectl create namespace my-namespace

Using a YAML Configuration File

apiVersion: v1
kind: Namespace
metadata:
  name: my-namespace

Apply the configuration:

kubectl apply -f namespace.yaml

Listing Namespaces

To list all namespaces in your cluster, use the following command:

kubectl get namespaces

Switching Between Namespaces

To switch between namespaces, you can use the kubectl config set-context command:

kubectl config set-context --current --namespace=my-namespace

Using Namespaces in Resource Definitions

When defining resources, you can specify the namespace in the metadata section of the YAML file:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  namespace: my-namespace
spec:
  containers:
  - name: my-container
    image: nginx

Practical Example

Let's create a namespace and deploy a pod within that namespace.

Step 1: Create a Namespace

Create a file named namespace.yaml:

apiVersion: v1
kind: Namespace
metadata:
  name: dev

Apply the configuration:

kubectl apply -f namespace.yaml

Step 2: Deploy a Pod in the Namespace

Create a file named pod.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  namespace: dev
spec:
  containers:
  - name: nginx
    image: nginx

Apply the configuration:

kubectl apply -f pod.yaml

Step 3: Verify the Pod is Running in the Namespace

kubectl get pods -n dev

Exercises

Exercise 1: Create and Use a Namespace

  1. Create a namespace named test.
  2. Deploy a pod named test-pod using the nginx image in the test namespace.
  3. Verify that the pod is running in the test namespace.

Solution

  1. Create the namespace:
    kubectl create namespace test
    
  2. Create a file named test-pod.yaml:
    apiVersion: v1
    kind: Pod
    metadata:
      name: test-pod
      namespace: test
    spec:
      containers:
      - name: nginx
        image: nginx
    
    Apply the configuration:
    kubectl apply -f test-pod.yaml
    
  3. Verify the pod:
    kubectl get pods -n test
    

Exercise 2: Set a Resource Quota for a Namespace

  1. Create a namespace named quota-ns.
  2. Set a resource quota in the quota-ns namespace to limit the number of pods to 5.

Solution

  1. Create the namespace:
    kubectl create namespace quota-ns
    
  2. Create a file named resource-quota.yaml:
    apiVersion: v1
    kind: ResourceQuota
    metadata:
      name: pod-quota
      namespace: quota-ns
    spec:
      hard:
        pods: "5"
    
    Apply the configuration:
    kubectl apply -f resource-quota.yaml
    

Conclusion

Namespaces are a powerful feature in Kubernetes that help manage and isolate resources within a cluster. They are essential for organizing resources, applying resource quotas, and implementing access control. By understanding and utilizing namespaces, you can better manage your Kubernetes environment, especially in multi-user and multi-team scenarios.

Kubernetes Course

Module 1: Introduction to Kubernetes

Module 2: Core Kubernetes Components

Module 3: Configuration and Secrets Management

Module 4: Networking in Kubernetes

Module 5: Storage in Kubernetes

Module 6: Advanced Kubernetes Concepts

Module 7: Monitoring and Logging

Module 8: Security in Kubernetes

Module 9: Scaling and Performance

Module 10: Kubernetes Ecosystem and Tools

Module 11: Case Studies and Real-World Applications

Module 12: Preparing for Kubernetes Certification

© Copyright 2024. All rights reserved