Kubernetes 13 - Namespaces and Context

As applications grow and teams expand, organizing and isolating resources becomes crucial for maintaining clarity and control. Two key concepts in Kubernetes that address these needs are Namespaces and Context. These features allow administrators and developers to create logical partitions within a cluster and seamlessly switch between different environments.

Namespaces

Namespaces in Kubernetes are a way to organize and segregate resources within a cluster. They provide a scope for names, effectively creating “virtual clusters” within a physical cluster.

Key Points

  1. Purpose: Namespaces limit the scope of resources, allowing you to divide cluster resources between multiple users, teams, or projects.
  2. Distinction: Kubernetes namespaces are distinct from Docker or Linux namespaces. They serve a different purpose and operate at the cluster level rather than the container level.
  3. Usage: In small clusters or for personal projects, you might not need to use namespaces. They become more valuable as your cluster grows and accommodates multiple teams or applications.
  4. Built-in Namespaces: Kubernetes comes with several built-in namespaces to separate system components from user-created resources:
  • default: The default namespace for user-created resources
  • kube-system: Used for Kubernetes system components
  • kube-public: Readable by all users, used for public resources
  • kube-node-lease: Used for node lease objects

Useful commands

# List all namespaces
kubectl get namespaces

# List all resources across all namespaces
kubectl get all --all-namespaces

# Create a new namespace
kubectl create namespace my-namespace

# Set a namespace for a context
kubectl config set-context --current --namespace=my-namespace

Context

In Kubernetes, a context is a group of access parameters. Each context contains a Kubernetes cluster, a user, and a namespace. The current context is the cluster that kubectl communicates with by default.

Key Points

  1. Purpose: Contexts allow you to easily switch between different clusters, users, and namespaces without manually specifying these details in every command. 2.Configuration: Context information is stored in the ~/.kube/config file. This file contains details about clusters, users, and contexts.
  2. Usage: Contexts are particularly useful when working with multiple clusters or when switching between different namespaces within a cluster.

Useful Commands

# View current context
kubectl config current-context

# List all contexts
kubectl config get-contexts

# Switch to a different context
kubectl config use-context my-context

# Set a namespace for the current context
kubectl config set-context --current --namespace=my-namespace

# View the full kubectl configuration
kubectl config view

The ~/.kube/config File

The ~/.kube/config file contains the configuration details for kubectl. It stores information about clusters, users, and contexts, allowing you to manage multiple clusters and switch between them easily. It’s a YAML file that typically contains:

  • Clusters: Details about the Kubernetes clusters you have access to.
  • Users: User credentials for authentication to the clusters.
  • Contexts: Combinations of clusters and users, often with a specified namespace.
apiVersion: v1
kind: Config
preferences: {}
clusters:
  - name: my-cluster
    cluster:
      server: https://1.2.3.4:6443
      certificate-authority: /path/to/ca.crt
users:
  - name: my-user
    user:
      client-certificate: /path/to/cert.crt
      client-key: /path/to/key.key
contexts:
  - name: my-context
    context:
      cluster: my-cluster
      user: my-user
      namespace: my-namespace
current-context: my-context

Conclusion

Namespaces provide logical separation of resources, while contexts allow for easy switching between different cluster environments.




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • CPU Cache
  • Understanding Linear Blended Skinning in 3D Animation
  • Starvation in Operating Systems
  • Virtual Memory
  • What is Bytecode in Python?
  • Understanding Top P in Language Models
  • LDAP (Lightweight Directory Access Protocol)
  • Factory Method Pattern
  • Kubernetes 12 - Higher Deployment Abstractions in Kubernetes
  • Kubernetes 11 - CRD's and THe Operator Pattern