show
kubectl get nodes node01 --show-labels
# NAME STATUS ROLES AGE VERSION LABELS
# node01 Ready <none> 61m v1.18.0 accelerator=nvidia-tesla-p100,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node01,kubernetes.io/os=linux
show
kubectl label node node01 type=critical
# node/node01 labeled
show
kubectl label node node01 type-
# node/node01 labeled
show
kubectl create namespace alpha
kubectl label namespace alpha type=critical
kubectl get namespace alpha --show-labels
# NAME STATUS AGE LABELS
# alpha Active 70s type=critical
show
kubectl run nginx-labels --image=nginx --labels=tier=frontend
# verification
kubectl get pod nginx-labels --show-labels
# NAME READY STATUS RESTARTS AGE LABELS
# nginx-labels 1/1 Running 0 16s tier=frontend
show
kubectl run nginx-labels --image=nginx --labels=name=nginx,tier=frontend,env=dev,version=v1
OR
cat << EOF > nginx-labels.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
env: dev
name: nginx
tier: frontend
version: v1
name: nginx-labels
spec:
containers:
- image: nginx
name: nginx
EOF
kubectl apply -f nginx-labels.yaml
show
kubectl get pod nginx-labels --show-labels
# NAME READY STATUS RESTARTS AGE LABELS
# nginx-labels 1/1 Running 0 26s env=dev,name=nginx,tier=frontend,version=v1
show
kubectl label pod nginx-labels version=v2 --overwrite
kubectl get pod nginx-labels --show-labels
# NAME READY STATUS RESTARTS AGE LABELS
# nginx-labels 1/1 Running 0 110s env=dev,name=nginx,tier=frontend,version=v2
show
kubectl get pod -L env
# OR
kubectl get pod --label-columns=env
# NAME READY STATUS RESTARTS AGE ENV
# nginx-labels 1/1 Running 0 25s dev
show
kubectl get pod -l version=v2
# OR
kubectl get pod -l 'version in (v2)'
OR
kubectl get pod --selector=version=v2
show
kubectl label pod nginx-labels name-
kubectl get pod nginx-labels --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-labels 1/1 Running 0 4m49s env=dev,tier=frontend,version=v2
kubectl delete namespace alpha
kubectl delete pod nginx-labels --force --grace-period=0