What is a Namespace?
A Namespace is used to isolate resources within a single cluster. Each namespace can have its own policies, permissions (RBAC), and resource controls. In other words, it is used to isolate users’ accessibility. By default, Kubernetes creates 4 namespaces:| Namespace | Description |
|---|---|
default | The namespace you can deploy to without creating a new namespace when starting a new cluster. |
kube-system | Namespace for objects created by the Kubernetes system (kube-dns, kube-proxy, kubernetes-dashboard, etc.). |
kube-public | Contains resources readable publicly by all users without authentication. Mainly reserved for cluster usage. |
kube-node-lease | Holds Lease objects associated with each node. Node leases allow kubelet to send heartbeats so the control plane can detect node failure. |
Commands
Switch Namespace Permanently
To switch to another namespace permanently (so you don’t have to specify it each time):A context is a set of access parameters that define a cluster, namespace, and user in Kubernetes. Contexts are stored in the
kubeconfig YAML file and are used to manage multiple clusters or environments from the same system.Namespace YAML
Using a Namespace in Resource YAML
Connecting to Services in Other Namespaces
To connect to a service in another namespace, reference its DNS entry (automatically added when the service is created):cluster.localis the default domain name of the Kubernetes cluster.
Resource Quota for a Namespace
You can limit the total resources used within a namespace.- Scope: Applies to an entire namespace
- Purpose: Enforces overall resource usage limits for all pods
- Usage: Ensures total consumption (CPU, memory, number of pods) does not exceed specified limits
resource-quota.yaml
Limit Range for a Namespace
You can set default minimum and maximum limits for CPU and memory for pods in a namespace. If you create or change a LimitRange, it will not affect existing pods.- Scope: Applies to individual containers or pods within a namespace
- Purpose: Sets default, minimum, and maximum resource usage limits
- Usage: Ensures each container or pod has resource requests and limits within specified bounds
cpu-limit-range.yaml
| Configuration | Result |
|---|---|
| No resources specified | Container gets 500m CPU and 512Mi memory (defaults). |
| Explicitly set 200m CPU and 300Mi memory | Valid — within min/max range. |
| Explicitly set 50m CPU and 200Mi memory | Fails — below the min values. |
| Explicitly set 2 CPU and 2Gi memory | Fails — exceeds the max values. |