Kubernetes-Label
Label
Label:对k8s中各种资源分类、分组,添加一个具有特别属性的标签。
Selector:通过一个过滤的语法进行查找到对应标签的资源。设置label
labels:
run: busybox
查询所有带有label的Pod
$ kubectl get pods --show-labels -A
NAMESPACE NAME READY STATUS RESTARTS AGE LABELS
default busybox 0/1 CrashLoopBackOff 6 7m2s <none>
default nginx-64c5bdd48d-49r5f 1/1 Running 0 16h app=nginx,pod-template-hash=64c5bdd48d
default nginx-64c5bdd48d-v7nfl 1/1 Running 0 16h app=nginx,pod-template-hash=64c5bdd48d
kube-public busybox 0/1 CrashLoopBackOff 6 7m6s run=busybox
kube-system calico-kube-controllers-756dd4db79-hxwpp 1/1 Running 0 37h k8s-app=calico-kube-controllers,pod-template-hash=756dd4db79
kube-system calico-node-b4njn 1/1 Running 0 37h controller-revision-hash=6c59f494b8,k8s-app=calico-node,pod-template-generation=1
kube-system calico-node-klbts 1/1 Running 0 37h controller-revision-hash=6c59f494b8,k8s-app=calico-node,pod-template-generation=1
kube-system calico-node-shhzk 1/1 Running 0 37h controller-revision-hash=6c59f494b8,k8s-app=calico-node,pod-template-generation=1
kube-system calico-node-tqmcq 1/1 Running 0 37h controller-revision-hash=6c59f494b8,k8s-app=calico-node,pod-template-generation=1
kube-system metrics-server-b7cff9c67-c4w6l 1/1 Running 0 37m k8s-app=metrics-server,pod-template-hash=b7cff9c67
查询指定label的pod
$ kubectl get pods --show-labels -l app=nginx
NAME READY STATUS RESTARTS AGE LABELS
nginx-64c5bdd48d-49r5f 1/1 Running 0 16h app=nginx,pod-template-hash=64c5bdd48d
nginx-64c5bdd48d-v7nfl 1/1 Running 0 16h app=nginx,pod-template-hash=64c5bdd48d
查询多个label的Pod
$ kubectl get pods --show-labels -l 'k8s-app in (calico-kube-controllers,metrics-server)' -A
NAMESPACE NAME READY STATUS RESTARTS AGE LABELS
kube-system calico-kube-controllers-756dd4db79-hxwpp 1/1 Running 0 37h k8s-app=calico-kube-controllers,pod-template-hash=756dd4db79
kube-system metrics-server-b7cff9c67-c4w6l 1/1 Running 0 39m k8s-app=metrics-server,pod-template-hash=b7cff9c67
Label删除
$ kubectl label node node1 app-