Namespaces in Kubernetes provide a way to divide cluster resources between multiple users. They're useful in environments with many users spread across multiple teams, or when different projects share the same cluster. Think of namespaces as virtual clusters that sit on top of the same physical cluster.
Imagine two boys named Mark, each from a different family. To avoid confusion, they are called by their last names: Mark Smith and Mark Williams. Similarly, Kubernetes uses namespaces to organize resources within a cluster, ensuring there are no naming conflicts between objects in different namespaces.
Kubernetes automatically creates three namespaces when the cluster is initialized:
default
: This is where resources are created if no other namespace is specified.kube-system
: This namespace contains Kubernetes internal components like the DNS server and networking components.kube-public
: Resources in this namespace are available to all users.You can create your own namespaces to logically separate environments like dev, test, and production. Use the following command to create a namespace:
kubectl create namespace dev
Example output:
namespace/dev created
You can also define a namespace in a YAML configuration file:
apiVersion: v1
kind: Namespace
metadata:
name: dev
Apply the YAML file with the following command:
kubectl apply -f namespace.yaml
Example output:
namespace/dev created
To list all the namespaces in your cluster, use the following command:
kubectl get namespaces
Example output:
NAME STATUS AGE default Active 24d kube-system Active 24d kube-public Active 24d dev Active 1h
To view resources, such as pods, in a specific namespace, use:
kubectl get pods -n dev
Example output:
NAME READY STATUS RESTARTS AGE my-app-1-7d8cfdcbfd-pqj7r 1/1 Running 0 3m my-app-2-8476c9946b-vlqcp 1/1 Running 0 5m
If you frequently work within a specific namespace, you can set a default context for it:
kubectl config set-context --current --namespace=dev
Example output:
Context "minikube" modified.
Now, whenever you use kubectl
, it will automatically operate in the dev
namespace unless you specify otherwise.
To delete a namespace and all the resources within it, use the following command:
kubectl delete namespace dev
Example output:
namespace "dev" deleted
You can limit the resources a namespace can use by creating a resource quota. Here's an example YAML definition:
apiVersion: v1
kind: ResourceQuota
metadata:
name: dev-quota
namespace: dev
spec:
hard:
pods: "10"
requests.cpu: "4"
requests.memory: 8Gi
limits.cpu: "8"
limits.memory: 16Gi
Apply the quota with the following command:
kubectl apply -f resource-quota.yaml
Example output:
resourcequota/dev-quota created
To see the current usage of the resource quota in the dev
namespace:
kubectl get resourcequota dev-quota -n dev --output=yaml
Example output:
apiVersion: v1 kind: ResourceQuota metadata: name: dev-quota namespace: dev spec: hard: limits.cpu: "8" limits.memory: 16Gi pods: "10" requests.cpu: "4" requests.memory: 8Gi status: hard: limits.cpu: "8" limits.memory: 16Gi pods: "10" requests.cpu: "4" requests.memory: 8Gi used: limits.cpu: "2" limits.memory: 4Gi pods: "5" requests.cpu: "1" requests.memory: 2Gi