Learnitweb

Kubernetes Object Names, Labels, Selectors, and Namespaces

Kubernetes relies on four core mechanisms to identify, organize, group, and isolate its objects: Names, Labels, Selectors, and Namespaces. These are foundational concepts you must understand before working with Kubernetes objects effectively.

1. Object Name

Every Kubernetes object, such as a Pod, Deployment, or Service, must have a unique name within its namespace. The object name helps Kubernetes (and you) identify and manage specific resources.

Key Points:

  • The name must be unique for each object type within a namespace.
    • You can have a Pod and a Deployment both named mynginx in the same namespace.
    • You cannot have two Pods named mynginx in the same namespace.
  • Names are DNS subdomain compliant, which means:
    • Maximum length: 253 characters
    • Allowed characters: lowercase alphanumeric (a–z, 0–9), hyphen (-), and period (.)
    • Must start and end with an alphanumeric character

Example:

apiVersion: v1
kind: Pod
metadata:
  name: mynginx
spec:
  containers:
    - name: nginx
      image: nginx

2. Labels

Labels are key-value pairs attached to Kubernetes objects such as Pods, Deployments, and Services. They act as identifiers and metadata to organize and select subsets of objects.

Why Labels Are Important:

  • Used by other Kubernetes components (e.g., Services, Deployments) to select and group objects.
  • Allow flexible querying and grouping of resources.
  • Help define application tiers, environments (dev/prod), or versions.

Label Rules:

  • Each label has a key and an optional value.
  • A Kubernetes object can have multiple labels.
  • Label keys must be unique per object.
  • Label Key and Value:
    • Maximum length: 63 characters
    • Allowed characters: alphanumeric, hyphen (-), underscore (_), and period (.)

Attaching Labels (Declarative Way):

apiVersion: v1
kind: Pod
metadata:
  name: pod-label-demo
  labels:
    environment: production
    app: nginx
spec:
  containers:
    - name: nginx
      image: nginx:1.14.2
      ports:
        - containerPort: 80
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-label-demo
  labels:
    environment: production
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.14.2

Working with Labels (Imperative Way):

  • View all labels on Pods: kubectl get pods --show-labels
  • View all labels on Deployments: kubectl get deployments --show-labels
  • Filter Pods by label: kubectl get pods -l app=nginx
  • Add label to running Pod: kubectl label pod pod-label-demo tier=frontend
  • Delete Pods with a specific label: kubectl delete pod -l app=nginx

3. Selectors

Selectors are used to select Kubernetes objects based on their labels. They are essential for grouping and managing related resources.

Where Selectors Are Used:

  • Deployments, ReplicaSets, DaemonSets, and Jobs use matchLabels or matchExpressions to manage their associated Pods.
  • Services use selectors (but only support matchLabels) to find and expose Pods.

Deployment Selector Example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.7.9
          ports:
            - containerPort: 80
  • The selector matchLabels: app: nginx matches the Pods created from the template (which also has app: nginx label).

Service Selector Example:

apiVersion: v1
kind: Service
metadata:
  name: my-nginx-service
spec:
  type: LoadBalancer
  ports:
    - port: 80
  selector:
    app: nginx
  • The service exposes all Pods with the label app: nginx.

Note: matchLabels is only supported in Deployments, ReplicaSets, DaemonSets, and Jobs. Services use a simpler label selector.

4. Namespace

Namespaces are virtual clusters within a Kubernetes cluster. They allow you to isolate and manage resources for different teams, environments, or projects.

Key Concepts:

  • Object names must be unique only within a namespace, not across all namespaces.
  • A resource can only belong to one namespace.
  • Namespaces cannot be nested.
  • Namespaces that start with kube- are reserved for Kubernetes system components.
  • If no namespace is specified, resources are created in the default namespace.

Creating Namespace (Imperative):

kubectl create namespace my-namespace

Creating Namespace (Declarative):

apiVersion: v1
kind: Namespace
metadata:
  name: my-namespace
kubectl apply -f my-namespace.yaml

Using Namespaces:

  • Create Pod in a specific namespace:
apiVersion: v1
kind: Pod
metadata:
  name: my-nginx-pod
  namespace: my-namespace
spec:
  containers:
    - name: nginx
      image: nginx
  • Get all namespaces: kubectl get namespaces
  • Get resources within a namespace: kubectl get pods --namespace=my-namespace
  • Delete a namespace: kubectl delete namespace my-namespace
  • Set default namespace for your session: kubectl config set-context --current --namespace=my-namespace

Summary Table

ConceptPurposeScope/Usage
NameUnique identifier within a namespaceOne per object per type per namespace
LabelKey-value pair metadataCan be assigned to multiple objects
SelectorSelects objects based on label matchUsed in Deployments, Services, etc.
NamespaceVirtual cluster within a clusterIsolates resources and configurations