PracticeVideosBlogFlashcards

3.3 Namespaces, Labels, Selectors & Annotations

Key Takeaways

  • Namespaces provide virtual cluster isolation within a single physical cluster, isolating resources, names, and access control boundaries across multi-tenant environments.

  • Kubernetes initializes four standard default namespaces: default, kube-system, kube-public, and kube-node-lease.

  • ResourceQuota objects enforce aggregate resource consumption limits per namespace, while LimitRange objects set container-level minimum, maximum, and default request/limit constraints.

  • Labels are identifying key-value pairs attached to objects, enabling loose coupling and dynamic grouping via equality-based and set-based label selectors.

  • Annotations store non-identifying metadata (such as build information, commit hashes, or ingress controller configuration) that cannot be queried via label selectors.

Last updated: August 2026

3.3 Namespaces, Labels, Selectors & Annotations

As Kubernetes clusters expand to accommodate dozens of applications and multi-tenant engineering teams, managing resource organization, isolation, and metadata becomes vital. Kubernetes satisfies these management challenges using Namespaces, ResourceQuotas, LimitRanges, Labels, Selectors, and Annotations.


1. Virtual Isolation with Namespaces

A Namespace is a logical abstraction that divides a single physical Kubernetes cluster into multiple virtual clusters. Namespaces provide scoped naming boundaries, resource quota boundaries, and access control scopes (via Role-Based Access Control / RBAC).

Default Kubernetes Namespaces

Every fresh Kubernetes cluster starts with four standard system namespaces:

  1. default: The fallback namespace for any object created without an explicit .metadata.namespace specification.
  2. kube-system: The system namespace reserved for Kubernetes control plane components, system add-ons, CoreDNS, and ingress controllers.
  3. kube-public: A readable namespace created automatically for resources that should be publicly discoverable across the whole cluster (e.g., cluster-info ConfigMap).
  4. kube-node-lease: A specialized namespace holding Lease objects for worker nodes. Node heartbeats are updated here to reduce control plane traffic overhead.

Note

Namespaces scope cluster names (you cannot have two Pods named frontend in the default namespace, but you can have one frontend Pod in development and another frontend Pod in production). However, Namespaces do not provide network isolation by default; NetworkPolicies must be configured to block cross-namespace traffic.


2. Namespace Resource Management: ResourceQuota & LimitRange

To prevent one tenant or buggy microservice from exhausting all cluster resources in a shared cluster, administrators apply resource controls at the namespace level.

ResourceQuota

A ResourceQuota enforces aggregate resource consumption caps across an entire namespace. If a namespace reaches its quota, the API server rejects any further creation of objects that would exceed the defined caps.

ResourceQuotas can restrict:

  • Compute Resources: Total CPU requests/limits (e.g., requests.cpu: "10", limits.cpu: "20") and total memory requests/limits.
  • Storage Resources: Total PersistentVolumeClaim storage requests (e.g., requests.storage: 500Gi).
  • Object Count Limits: Maximum total count of Pods, Services, ConfigMaps, Secrets, or Deployments.

LimitRange

While a ResourceQuota sets namespace-wide aggregate limits, a LimitRange enforces resource constraints at the individual Pod or container level within a namespace.

A LimitRange can:

  • Enforce minimum and maximum CPU and memory requests/limits for individual containers.
  • Define default resource requests and limits automatically injected into any container submitted without resource specifications.
apiVersion: v1
kind: ResourceQuota
metadata:
  name: dev-quota
  namespace: development
spec:
  hard:
    requests.cpu: "4"
    requests.memory: 8Gi
    limits.cpu: "8"
    limits.memory: 16Gi
    pods: "10"
---
apiVersion: v1
kind: LimitRange
metadata:
  name: dev-limit-range
  namespace: development
spec:
  limits:
  - default:
      cpu: 500m
      memory: 512Mi
    defaultRequest:
      cpu: 250m
      memory: 256Mi
    type: Container

3. Metadata Identification: Labels vs. Annotations

Kubernetes objects can be tagged with key-value metadata pairs. However, Kubernetes makes a fundamental architectural distinction between Labels and Annotations.

Labels and Label Selectors

Labels are identifying key-value pairs attached to API objects (such as Pods, Nodes, and Services). They are intended to specify identifying attributes of objects that are meaningful and relevant to users, without implying semantic changes to the core system.

Labels are queried using Label Selectors to group and bind resources dynamically. There are two types of selectors:

  1. Equality-Based Selectors: Filter resources using =, ==, or != operators.
    • Example: environment = production, tier != frontend
  2. Set-Based Selectors: Filter resources using set operations (in, notin, exists).
    • Example: environment in (production, staging), tier notin (legacy)

Labels power core Kubernetes operations:

  • Services use label selectors to route network traffic to matching Pod endpoints.
  • Deployments use label selectors to discover and manage ReplicaSet Pods.
  • kubectl CLI commands use selectors to target specific resources (kubectl get pods -l env=prod).

Annotations

Annotations are key-value pairs used to attach non-identifying, arbitrary metadata to objects. Unlike Labels, Annotations cannot be queried by label selectors and are not used by Kubernetes to identify or group objects.

Common annotation use cases include:

  • Storing build info, git commit hashes, or release timestamps.
  • Ingress controller configuration directives (e.g., nginx.ingress.kubernetes.io/rewrite-target: /).
  • Storage provisioner options or container runtime parameters.
  • Pointer fields for external management and orchestration tools.

4. Metadata Feature Comparison Matrix

AttributeLabelsAnnotationsNamespaces
Primary PurposeResource identification & groupingNon-identifying metadata storageVirtual cluster resource isolation
Queryable via Selectors?Yes (Equality & Set selectors)NoN/A (Scoped target)
Used by Kubernetes Core?Yes (Service routing, Deployments)Yes (Ingress, tool plugins)Yes (RBAC, Quotas, Object scope)
Example Key-Valueapp: payment-apibuild.info/commit: 8f3a12bnamespace: staging
apiVersion: v1
kind: Pod
metadata:
  name: order-service
  namespace: production
  labels:
    app.kubernetes.io/name: order-service
    app.kubernetes.io/tier: backend
    environment: production
  annotations:
    build.info/commit: "a1b2c3d4e5f6"
    prometheus.io/scrape: "true"
    prometheus.io/port: "8080"
spec:
  containers:
  - name: app
    image: order-service:v2.1.0
Test Your Knowledge

What is the primary operational difference between Kubernetes Labels and Annotations?

A

Labels store encrypted secret data, whereas Annotations store unencrypted config values.

B

Labels are restricted to system namespaces, while Annotations can be attached to user objects.

C

Labels are identifying key-value pairs used by selectors to query and group objects, whereas Annotations hold non-identifying metadata not used for selection.

D

Labels apply only to Pods, while Annotations apply exclusively to Nodes and Services.

Test Your Knowledge

Which default Kubernetes namespace holds lease objects used by nodes to send periodic heartbeats to the control plane?

A

default

B

kube-system

C

kube-public

D

kube-node-lease

Test Your Knowledge

How do ResourceQuotas and LimitRanges work together within a Kubernetes namespace?

A

ResourceQuota enforces aggregate resource consumption limits across the namespace, while LimitRange defines container-level default, minimum, and maximum resource constraints.

B

ResourceQuota manages external ingress bandwidth, while LimitRange controls internal pod-to-pod network throughput.

C

ResourceQuota specifies cluster-wide node capacity, while LimitRange restricts RBAC permissions per service account.

D

ResourceQuota applies to storage volumes only, while LimitRange applies exclusively to CPU and RAM requests.

Sections you finish are checked off in the contents.