liveBook · Manning

Label

As explained in Learn Docker in a Month of Lunches, Second Edition

A Kubernetes label is a key-value metadata pair attached to a resource for organization and selection.

A label is a key-value metadata pair attached to a Kubernetes resource. Labels organize objects and loosely connect application components without tightly coupling their definitions. section 15.2

Label a Pod and select it from a Service

Add labels such as app: numbers and component: api under metadata. Then select the matching Pod with kubectl get po --selector app=numbers,component=api. A Service can use the same selector to route traffic to that Pod. section 15.2

Listing 15.2 A Pod spec for the random number REST API
apiVersion: v1
kind: Pod
metadata:
  name: numbers-api
  labels:
    app: numbers
    component: api
spec:
  containers:
    - name: api
      image: kiamol/ch03-numbers-api
      ports:
        - containerPort: 80
          name: http

Inspect a complete Pod manifest showing labels alongside the container and port configuration. section 15.2

Verify and use labels with kubectl:

  • Run kubectl get pods --show-labels to display labels. section 15.2
  • Use matching app and component selectors to identify Pods and connect them to a Service. section 15.2

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Defines key-value labels and demonstrates inspection, selection, and Service linkage without covering the broader resource specification.
Examples
Uses a numbers-api Pod labeled app=numbers and component=api, then selects it through kubectl and a Service.
What you can do after
Apply it via kubectl to inspect resources and connect Pods with Services using matching selectors.
How it connects
Connects Kubernetes resources, Pods, Services, selectors, metadata, and kubectl.

Also explained in

Compare how 7 books explain label →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Docker in a Month of Lunches, Second Edition. The section links point to the parts of the book it draws on.