Labels, Selectors and Annotations - Sandeep-K-Khandelwal/CKAD GitHub Wiki

Labels and Selectors

  • Labels and Selectors are a standard method to group things together and filter them based on their needs.
  • Labels are properties attached to each item. Selectors help you filter these items
  • Add label in the POD definition file:
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: App1
    function: Front-end
spec:
  containers:
    - name: nginx-container
      image: nginx
  • Use imperative command to filter the PODs:
kubectl get pods --selector app=App1
  • Use selector in the ReplicaSet:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-replicaset
  labels:
    app: myapp
    type: frontend
spec:
  replicas: 4
  selector:
    matchLabels:
      app: App1
  template:
    metadata:
      labels:
        type: frontend
        app: App1
    spec:
      containers:
      - name: nginx
        image: nginx

Annotations

Annotations are used to record other details for informatory purpose. For example tool details like name, version build information etc or contact details, phone numbers, etc.

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
   app: App1
   function: Front-end
  annotations:
    buildVersion: 1.3
spec:
  containers:
    - name: nginx-container
      image: nginx