🌐

Understanding Kubernetes Namespaces

Apr 13, 2025

Kubernetes Namespaces and Best Practices

What is a Namespace?

  • Namespace in Kubernetes: A virtual cluster within a Kubernetes cluster to organize resources.
  • Allows multiple namespaces in a single cluster.
  • Default namespaces are provided by Kubernetes out-of-the-box.

Default Namespaces

  1. kube-system
    • Contains system processes and components.
    • Not meant for user modification.
  2. kube-public
    • Contains publicly accessible data and cluster information config map.
  3. kube-node-lease
    • Holds information about node heartbeats and availability.
  4. default
    • Used initially to create resources unless others are defined.

Creating Namespaces

  • Use kubectl create namespace <name> to create a new namespace.
  • Recommended to use namespace configuration files for history tracking.

Why Use Namespaces?

  • Organization: Group resources logically, e.g., database, monitoring, etc.
  • Multiple Teams: Avoid conflicts by assigning each team its own namespace.
  • Environment Separation: Host staging and development in the same cluster.
  • Blue-Green Deployment: Manage different production versions in one cluster.
  • Resource Limitation: Control resource usage and access within namespaces.

Namespace Characteristics

  • Resources are largely isolated and cannot be accessed cross-namespace, except for services.
  • Some components, like Persistent Volumes, are global and not namespace-bound.

Creating Components in a Namespace

  • By default, components are created in the default namespace unless specified.
  • Use kubectl apply -n <namespace> or include namespace attribute in config files.
  • Configuration file approach is recommended for clarity and automation.

Switching Active Namespace

  • By default, kubectl uses the default namespace.
  • Use kubens tool to switch active namespace for convenience.

Tools and Commands

  • kubectl: Command-line tool for managing Kubernetes.
  • kubens: Tool to switch default namespaces easily.

Conclusion

  • Consider isolation, resource sharing, and organizational needs when using namespaces.
  • Namespaces enhance manageability in multi-team, multi-environment projects.
  • Always document namespace usage in configuration files for better clarity and management.

Helpful Tips

  • Use namespaces even for smaller projects to maintain order.
  • Regularly review and adjust namespaces as project needs evolve.
  • Utilize shared resources effectively across namespaces.