Skip to content

Latest commit

 

History

History
223 lines (145 loc) · 3.95 KB

labels.md

File metadata and controls

223 lines (145 loc) · 3.95 KB

Nodes


Show all labels for the node node01


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


Label worker node node01 with label type=critical


show

kubectl label node node01 type=critical
# node/node01 labeled


Remove label type=critical from worker node node01


show

kubectl label node node01 type-
# node/node01 labeled


Namespaces


Create and label Label namespace alpha with label type:critical


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


Pods


Create a new pod with name nginx-labels and using the nginx image and labels tier=frontend


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


Create pod nginx-labels with nginx image and label name=nginx, tier=frontend, env=dev


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 all labels of the pod nginx-labels


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


Change the labels of pod 'nginx-labels' to be version=v2

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


Get the label env for the pods (show a column with env labels)

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


Get only the version=v2 pods

show

kubectl get pod -l version=v2
# OR  
kubectl get pod -l 'version in (v2)'
OR  
kubectl get pod --selector=version=v2


Remove the name label from the nginx-labels pod

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

Clean up

kubectl delete namespace alpha
kubectl delete pod nginx-labels --force --grace-period=0