Skip to content

Kubernetes Namespace

A Kubernetes Namespace is a mechanism used to divide cluster resources between multiple users, teams, or projects. It provides an abstraction of a virtual cluster atop the physical cluster, allowing for the isolation and grouping of resources.^[400-devops-06-kubernetes-k8s-ithelp-day22-readme.md]

Concept and Purpose

Kubernetes uses namespaces to partition a single physical cluster into multiple virtual clusters. This enables resources to be organized according to different projects, teams, or business considerations.^[400-devops-06-kubernetes-k8s-ithelp-day22-readme.md]

This feature is primarily beneficial in scenarios involving cross-team collaboration or multiple projects. For smaller clusters with only a few users, creating namespaces may not be necessary.^[400-devops-06-kubernetes-k8s-ithelp-day22-readme.md]

Initial Namespaces

Kubernetes automatically creates four initial namespaces upon cluster initialization^[400-devops-06-kubernetes-k8s-ithelp-day22-readme.md]:

  • default: The default namespace for objects that do not specify a namespace.
  • kube-system: The namespace for objects created by the Kubernetes system.
  • kube-public: A namespace automatically created to be readable by all users (including unauthenticated ones). It is primarily used for resources that should be visible across the entire cluster.
  • kube-node-lease: Contains Lease objects associated with each node, used by kubelet to send heartbeats and help the control plane detect node failures.

Usage and Management

Creating Namespaces

Namespaces can be created using the kubectl command-line tool^[400-devops-06-kubernetes-k8s-ithelp-day22-readme.md]:

[kubectl](<./kubectl.md>) create namespace <namespace-name>

Setting the Namespace

There are several ways to target a specific namespace:

  • Per Request: Use the --namespace flag with a kubectl command^[400-devops-06-kubernetes-k8s-ithelp-day22-readme.md].
    [kubectl run](<./kubectl-run.md>) nginx --image=nginx --namespace=<namespace-name>
    
  • Persistent Context: Change the default namespace for all subsequent commands in the current context^[400-devops-06-kubernetes-k8s-ithelp-day22-readme.md].
    [kubectl](<./kubectl.md>) config set-context --current --namespace=<namespace-name>
    
  • Configuration Files: Define the namespace within the metadata field of a resource manifest file (e.g., a Pod)^[400-devops-06-kubernetes-k8s-ithelp-day22-readme.md].

Key Characteristics

Namespaces possess several behaviors and properties that affect resource management^[400-devops-06-kubernetes-k8s-ithelp-day22-readme.md]:

  • Uniqueness: Resource names must be unique within the same namespace.
  • Reusability: Resources in different namespaces may share the same name.
  • Lifecycle: If a namespace is deleted, all resources contained within it are also deleted.
  • Resource Management: ResourceQuota and LimitRange policies can be applied to namespaces to allocate or limit system resources (such as CPU and memory).

Sources

  • 400-devops-06-kubernetes-k8s-ithelp-day22-readme.md