Labels - neerajk555/Kubernetes GitHub Wiki

  1. Create a pod with labels
# pod-with-labels.yaml
apiVersion: v1
kind: Pod
metadata:
  name: labeled-pod
  labels:
    app: myweb
    env: staging
spec:
  containers:
  - name: web
    image: nginx

RUN: kubectl apply -f pod-with-labels.yaml

  1. View the pod and its labels

kubectl get pods --show-labels

  1. Select the pod using a label selector

kubectl get pods -l app=myweb

You can also combine:

kubectl get pods -l 'app=myweb,env=staging'

  1. Add a label to an existing pod

kubectl label pod labeled-pod version=v1

  1. Remove a label

kubectl label pod labeled-pod version-