NameSpace
-
A Linux NameSpace implements kernel-level resource isolation
-
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)
-
The shortend command can be used is
ns
instead ofnamespace
Using NameSpace
Command | Description |
---|---|
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 namespace | Lists all the available namespaces in the cluster. |
kubectl get ... all --all-namespaces | Retrieves 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