Skip to Content
This project is a work in progress. If you have any questions or suggestions, feel free to contact me.

NameSpace

  • A Linux NameSpace implements kernel-level resource isolation

  • A namespace in Kubernetes is a logical partition inside a cluster. Think of it like a virtual cluster within your actual cluster. It helps you organize, isolate, and manage resources.

  • Kubernetes offers NameSpace resources that provide the same functionality

  • Different NameSpaces can be used to strictly separate between customer resources

  • NameSpaces are used to apply different security-related settings -Role-Based Access Control (RBAC)

  • Separate environments (e.g., dev, test, prod)

  • Isolate teams or projects

  • The shortend command can be used is ns instead of namespace

Using NameSpace

CommandDescription
kubectl create namespace <namespace_name>Creates a new Kubernetes namespace (e.g., mynamespace).
kubectl ... -n <namespace_name>Runs a command in a specific namespace by specifying the -n flag followed by the namespace name.
kubectl get namespaceLists all the available namespaces in the cluster.
kubectl get ... all --all-namespacesRetrieves all resources across all namespaces using the --all-namespaces or -A flag.

Demo: Using NameSpace

myNameSpace.yml
apiVersion: v1 kind: Pod metadata: labels: run: nginx name: nginx namespace: secret spec: containers: - image: nginx name: nginx

Create namespace with a custom name

kubectl create ns secret
Output
controlplane $ kubectl create ns secret namespace/secret created

Create the Pod

kubectl create -f myNameSpace.yml
Output
controlplane $ kubectl apply -f myNameSpace.yaml pod/mynginx created

List all the running Pods

kubectl get pods
Output
controlplane $ kubectl get pods NAME READY STATUS RESTARTS AGE init-demo 0/1 Init:0/1 0 12m

You don’t see your pod running that’s beacuse it’s running in a different namespace, and by default the kubectl get pods display the running pods in default namespace. To view the pods from a specific namespace -

kubectl get pods -n secret
Output
controlplane $ kubectl get pods -n secret NAME READY STATUS RESTARTS AGE mynginx 1/1 Running 0 18s
Last updated on