Kubernetes - Creating Namespaces

Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
Search for a command to run...

Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
No comments yet. Be the first to comment.
1. Difference between Docker and Kubernetes Docker → Builds and runs containers.Kubernetes → Orchestrates containers across multiple nodes. Key points: Docker = container runtime. Kubernetes = container orchestration tool. Kubernetes provides auto...
In this session, we learn how to monitor a Kubernetes cluster using Prometheus and Grafana.This is not just theory — there is a GitHub repository containing all installation commands and demo steps.The repo will also be enhanced later with advanced K...
1. What is a ConfigMap in Kubernetes? A ConfigMap is used to store non-sensitive configuration data that your application needs — such as: Database port Connection type Any general configuration values In normal applications (non-Kubernetes), de...
Kubernetes normally supports built-in resources like: Deployment Service Pod ConfigMap Secret Ingress These are called native resources. Sometimes companies (Istio, ArgoCD, Prometheus Operator, Kyverno, etc.) want to add new features that Kub...
1. Why Kubernetes Services Are Needed When a Pod is created in Kubernetes, it receives a dynamic IP address.If the Pod dies and restarts, its IP changes.So other Pods (like checkout → payments) cannot rely on Pod IP because it changes, creating issue...
Kubernetes namespaces provide a way to divide cluster resources between multiple users, teams, or applications. By isolating different environments (like dev, test, and prod) within the same cluster, namespaces help in organizing and managing resources efficiently.
By default, Kubernetes comes with four namespaces: default,kube-public, kube-system and kube-node-lease
Understanding Kubernetes Namespaces
When you first deploy a Kubernetes cluster, it comes with four default namespaces:
default: The default namespace for resources with no other specified namespace.
kube-public: Used for resources that should be publicly accessible across the cluster. Typically contains cluster-wide information.
kube-system: Reserved for Kubernetes system components, such as the API server, scheduler, and other core processes.
kube-node-lease: Used for managing node lease objects, which help in determining the availability of nodes in the cluster.
These namespaces allow you to categorize and manage resources efficiently, especially as your cluster grows in complexity.
Creating a Kubernetes Namespace
Creating a namespace in Kubernetes is a straight forward process. You can use the kubectl command-line tool or a YAML file.
1. Verifying Existing Namespaces
Before creating a new namespace, it's helpful to verify the namespaces already running in your cluster:
kubectl get namespaces

kubectl create Command (Imperative Approach)The simplest way to create a namespace is by using the kubectl create command:
kubectl create namespace <namespace-name>
For example, to create a namespace called dev:
kubectl create namespace dev

You can verify the creation by listing all namespaces:
kubectl get namespaces

Create a YAML file named namespaces.yaml:
apiVersion: v1
kind: Namespace
metadata:
name: test

Apply the YAML file to create the namespace:
kubectl apply -f namespaces.yaml

This declarative method is ideal for maintaining version control and automating your Kubernetes infrastructure.
Verifying the Namespace
After creating a namespace, you can verify its existence with:
kubectl get namespaces
This command will list all namespaces in your cluster, including the newly created one.

Deleting a Kubernetes Namespace
Deleting a namespace is just as simple as creating one, but you need to be cautious. When you delete a namespace, all resources (like pods, services, and deployments) within that namespace are also deleted.
To delete a namespace, use the following command:
kubectl delete namespace <namespace-name>
For example, to delete the dev namespace:
kubectl delete namespace dev
This will remove the namespace and all its resources from your cluster.
Namespaces are a powerful feature in Kubernetes that allows for efficient resource management and isolation within a cluster. By understanding how to create and delete namespaces, you can better organize your workloads and maintain a clean and manageable cluster environment.