Efficient Namespace Management in Kubernetes | create | delete | add resources| get list of all resource

Manage the Namespace in Kubernetes

What is Namespace?

In Kubernetes, namespaces help separate groups of resources within a single cluster. They function like virtual clusters, improving organization, resource management, and access control. Namespaces are especially beneficial in environments with many users from different teams or projects.

Get a list of all namespaces in Cluster environment

kubectl get namespaces

Get a list of all resources in the namespace.

kubectl api-resources --namespaced=true | head

Get a list of all resources not part of the namespace.

kubectl api-resources --namespaced=false | head

Namespace have active, state, and terminating

kubectl describe namespaces

Describe the details of individual namespace

kubectl describe namespaces kube=system

Get all the pods in our cluster in all our namespaces

kubectl get pods --all-namespaces

Get all the resources across all our namespaces

kubectl get all --all-namespaces

Get a list of pod in individual kube system

kubectl get pods --namespace kube-system

Create a namespace

kubectl create namespace my-namespace

Create resources in the namespace

kubectl run hello-pod \
-- image= nginx:latest \
-- generator=run-pod/v1 \
-- namespace my-namespace

Or you can create a yaml file and mentioned namespace name in metadata of yaml file

apiVersion: v1
kind: Pod
metadata:
name: my-pod
namespace: my-namespace
labels:
app: my-app
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80

Deploy this with following command from yaml file

kubectl apply -f file.yaml -n my-namespace

List all the pods in the namespace

kubectl get pods -n my-namespace

Get all list of resource in the namespace

kubectl get all -n my-namespace

Delete the namespace : But our pod restarted and deploy new pods because we are not deleted deployment

kubectl delete pods -all --namespace my-namespace
-- all pods again rebuilt by kubernets because its deployment is not deleted

For actual delete use this command, this will delete all resource in it

kubectl delete namespaces my-namespace