Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Kubernetes Namespace

Back


Namespace



Switch namespace in context


In DNS


Built-in Namespaces

kubectl run nginx --image=nginx
kubectl describe pod nginx | grep "Namespace"
# Namespace:        default


Declarative Method

apiVersion: v1
kind: Namespace
metadata:
  name: dev
kubectl create -f ns-dev.yaml
apiVersion: v1
kind: Pod
metadata:
  namespace: dev

Imperative Commands

Command Description
kubectl get ns List all namespaces
kubectl describe ns ns_name Show detailed information about a namespace.
kubectl create ns ns_name Create a new namespace.
kubectl delete ns ns_name Delete a namespace (removes all resources inside it).
Command Description
kubectl get all -n ns_name List all resources (pods, services, etc.) in a namespace.
kubectl get pods -n ns_name List all pods in a specific namespace.
kubectl get pods --all-namespaces List all pods in all namespaces.
kubectl config set-context --current --namespace=ns_name Set a default namespace for current kubectl context.
kubectl config view --minify \| grep namespace: Get the namespace set as default of the current context
kubectl get pod pod_name -n ns_name Get a specific pod in a namespace.
kubectl run test --image=nginx -n ns_name Run a test pod in a given namespace.
kubectl create -f pod.yaml --namespace=ns_name Run a test pod in a given namespace.

Lab: Built-in Namespace

# list built-in ns
kubectl get ns
# NAME              STATUS   AGE
# default           Active   4d12h
# kube-node-lease   Active   4d12h
# kube-public       Active   4d12h
# kube-system       Active   4d12h

default Namespace

kubectl describe ns default
# Name:         default
# Labels:       kubernetes.io/metadata.name=default
# Annotations:  <none>
# Status:       Active

kubectl get all -n default
# NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
# service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   4d12h

kube-system Namespace

kubectl describe ns kube-system
# Name:         kube-system
# Labels:       kubernetes.io/metadata.name=kube-system
# Annotations:  <none>
# Status:       Active

# No resource quota.

# No LimitRange resource.

kubectl get all -n kube-system
# NAME                                         READY   STATUS    RESTARTS       AGE
# pod/coredns-668d6bf9bc-2ztgc                 1/1     Running   4 (38m ago)    4d12h
# pod/coredns-668d6bf9bc-sb6nr                 1/1     Running   4 (38m ago)    4d12h
# pod/etcd-docker-desktop                      1/1     Running   4 (38m ago)    4d12h
# pod/kube-apiserver-docker-desktop            1/1     Running   4 (38m ago)    4d12h
# pod/kube-controller-manager-docker-desktop   1/1     Running   4 (38m ago)    4d12h
# pod/kube-proxy-l9pz4                         1/1     Running   4 (38m ago)    4d12h
# pod/kube-scheduler-docker-desktop            1/1     Running   7 (38m ago)    4d12h
# pod/storage-provisioner                      1/1     Running   11 (38m ago)   4d12h
# pod/vpnkit-controller                        1/1     Running   4 (38m ago)    4d12h

# NAME               TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
# service/kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   4d12h

# NAME                        DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR            AGE
# daemonset.apps/kube-proxy   1         1         1       1            1           kubernetes.io/os=linux   4d12h

# NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
# deployment.apps/coredns   2/2     2            2           4d12h

# NAME                                 DESIRED   CURRENT   READY   AGE
# replicaset.apps/coredns-668d6bf9bc   2         2         2       4d1

kube-public Namespace

kubectl describe ns kube-public
# Name:         kube-public
# Labels:       kubernetes.io/metadata.name=kube-public
# Annotations:  <none>
# Status:       Active

# No resource quota.

# No LimitRange resource.

kubectl get all -n kube-public
# No resources found in kube-public namespace.

kube-node-lease Namespace

kubectl describe ns kube-node-lease
# Name:         kube-node-lease
# Labels:       kubernetes.io/metadata.name=kube-node-lease
# Annotations:  <none>
# Status:       Active

# No resource quota.

# No LimitRange resource.

kubectl get all -n kube-node-lease
# No resources found in kube-node-lease namespace.

Lab: Create Namespace(Imperative Method)

kubectl create ns myns
# namespace/myns created
kubectl get ns | grep myns
# myns              Active   41m
kubectl describe ns myns
# Name:         myns
# Labels:       kubernetes.io/metadata.name=myns
# Annotations:  <none>
# Status:       Active

# No resource quota.

# No LimitRange resource.

kubectl run nginx --image=nginx -n myns
# pod/nginx created

kubectl get pod
# No resources found in default namespace.

kubectl get pod -n myns
# NAME    READY   STATUS    RESTARTS   AGE
# nginx   1/1     Running   0          45s

kubectl describe pod nginx -n myns
# Name:             nginx
# Namespace:        myns
# Priority:         0
# Service Account:  default
# Node:             docker-desktop/192.168.65.3
# Start Time:       Mon, 29 Sep 2025 12:04:36 -0400
# Labels:           run=nginx
# Annotations:      <none>
# Status:           Running
# IP:               10.1.2.175
# IPs:
#   IP:  10.1.2.175
# Containers:
#   nginx:

# all pods in all ns
kubectl get pod --all-namespaces
# NAMESPACE     NAME                                     READY   STATUS    RESTARTS        AGE
# kube-system   coredns-668d6bf9bc-2ztgc                 1/1     Running   4 (137m ago)    4d14h
# kube-system   coredns-668d6bf9bc-sb6nr                 1/1     Running   4 (137m ago)    4d14h
# kube-system   etcd-docker-desktop                      1/1     Running   4 (137m ago)    4d14h
# kube-system   kube-apiserver-docker-desktop            1/1     Running   4 (137m ago)    4d14h
# kube-system   kube-controller-manager-docker-desktop   1/1     Running   4 (137m ago)    4d14h
# kube-system   kube-proxy-l9pz4                         1/1     Running   4 (137m ago)    4d14h
# kube-system   kube-scheduler-docker-desktop            1/1     Running   7 (137m ago)    4d14h
# kube-system   storage-provisioner                      1/1     Running   11 (136m ago)   4d14h
# kube-system   vpnkit-controller                        1/1     Running   4 (137m ago)    4d14h
# myns          nginx                                    1/1     Running   0               31m

kubectl delete ns myns
# namespace "myns" deleted

# all pods in all ns
kubectl get pod --all-namespaces
# NAMESPACE     NAME                                     READY   STATUS    RESTARTS        AGE
# kube-system   coredns-668d6bf9bc-2ztgc                 1/1     Running   4 (139m ago)    4d14h
# kube-system   coredns-668d6bf9bc-sb6nr                 1/1     Running   4 (139m ago)    4d14h
# kube-system   etcd-docker-desktop                      1/1     Running   4 (139m ago)    4d14h
# kube-system   kube-apiserver-docker-desktop            1/1     Running   4 (139m ago)    4d14h
# kube-system   kube-controller-manager-docker-desktop   1/1     Running   4 (139m ago)    4d14h
# kube-system   kube-proxy-l9pz4                         1/1     Running   4 (139m ago)    4d14h
# kube-system   kube-scheduler-docker-desktop            1/1     Running   7 (139m ago)    4d14h
# kube-system   storage-provisioner                      1/1     Running   11 (138m ago)   4d14h
# kube-system   vpnkit-controller                        1/1     Running   4 (139m ago)    4d14h

Lab: Create namespace(Declarative Method)

apiVersion: v1
kind: Namespace
metadata:
  name: dev
apiVersion: v1
kind: Pod
metadata:
  name: pod-nginx
  namespace: dev
  labels:
    app: nginx
spec:
  containers:
    - image: nginx
      name: nginx-con
kubectl apply -f .
# namespace/dev created
# pod/pod-nginx created

kubectl get pod
# No resources found in default namespace.

kubectl get pod -n dev
# NAME        READY   STATUS    RESTARTS   AGE
# pod-nginx   1/1     Running   0          3m11s

kubectl delete -f .
# namespace "dev" deleted
# pod "pod-nginx" deleted

Lab: Set default for a context

kubectl config current-context
# docker-desktop

kubectl get pod
# No resources found in default namespace.

# set ns
kubectl config set-context docker-desktop --namespace=dev

kubectl get pod
# NAME        READY   STATUS    RESTARTS   AGE
# pod-nginx   1/1     Running   0          8m35s

kubectl config set-context docker-desktop --namespace=default

# get all pods in all ns
kubectl get pods --all-namespaces
# NAMESPACE     NAME                                     READY   STATUS    RESTARTS        AGE
# dev           pod-nginx                                1/1     Running   0               10m
# kube-system   coredns-668d6bf9bc-2ztgc                 1/1     Running   4 (133m ago)    4d14h
# kube-system   coredns-668d6bf9bc-sb6nr                 1/1     Running   4 (133m ago)    4d14h
# kube-system   etcd-docker-desktop                      1/1     Running   4 (133m ago)    4d14h
# kube-system   kube-apiserver-docker-desktop            1/1     Running   4 (133m ago)    4d14h
# kube-system   kube-controller-manager-docker-desktop   1/1     Running   4 (133m ago)    4d14h
# kube-system   kube-proxy-l9pz4                         1/1     Running   4 (133m ago)    4d14h
# kube-system   kube-scheduler-docker-desktop            1/1     Running   7 (133m ago)    4d14h
# kube-system   storage-provisioner                      1/1     Running   11 (132m ago)   4d14h
# kube-system   vpnkit-controller                        1/1     Running   4 (133m ago)    4d14h
# myns          nginx                                    1/1     Running   0               27m

TroubleShooting: Deleting NS get stuck

k get ns
# NAME                 STATUS        AGE
# kube-flannel         Terminating   3d23h

# !!! Patch doesn't work
kubectl patch namespace kube-flannel -p '{"metadata":{"finalizers":null}}'

# output ns as json
kubectl get ns kube-flannel -o json > temp.json

# update json
vi temp.json
# find:
#     "spec": {
#         "finalizers": [
#             "kubernetes"
#         ]
# change:
#     "spec": {
#         "finalizers": []

# proxy api server
kubectl proxy

# make request:
curl -k -H "Content-Type: application/json" -X PUT --data-binary @temp.json http://127.0.0.1:8001/api/v1/namespaces/kube-flannel/finalize

# confirm
k get ns kube-flannel