-
Label: classify and group various resources in k8s, and add a label with special attributes
-
Selector: find the resource corresponding to the tag through a filtered syntax
Label is an important concept in k8s. A key value pair of a label with key=value can be attached to various resources.
After the summary, we will explain:
Matching rules for Label:
- name=nginx: this type is direct matching
- name!= Nginx: there is no resource with name=nginx in the matching tag
- name in (A,B): matches all resources with name=A and name=B tags
- name not in (A): matches all resources without label A
As can be seen from the above figure, frontend is matched to the backend server through the service. When the tag of the service is app=nginx, it will be matched to the two groups of backend servers. However, when role = backend app is added to the filter in the service, the Selector will only filter the servers containing these two tags in the backend server group and then match them.
You can see the label of Pod through the following command: through the – show labels parameter
The last column LABELS is the label of Pod
[root@k8s-master01 ~]# kubectl get pod --show-labels NAME READY STATUS RESTARTS AGE LABELS busybox 1/1 Running 28 (16h ago) 9d <none> hpa-nginx-bd88bdd8f-h8vx7 1/1 Running 0 18h app=hpa-nginx,pod-template-hash=bd88bdd8f
Then we can try to tag the Pod with kubectl: from the above code, we can see that busybox has no label. Through the following command, we can find that there is already a label app=busybox.
[root@k8s-master01 ~]# kubectl label pod busybox app=busybox pod/busybox labeled [root@k8s-master01 ~]# kubectl get pod --show-labels NAME READY STATUS RESTARTS AGE LABELS busybox 1/1 Running 28 (16h ago) 9d app=busybox hpa-nginx-bd88bdd8f-h8vx7 1/1 Running 0 18h app=hpa-nginx,pod-template-hash=bd88bdd8f
If there are A lot of pods in A cluster, we can find the desired Pod by tag filtering- Parameter A is to view all pods, but after adding - l app=busybox, only pods that meet this condition will be found.
[root@k8s-master01 ~]# kubectl get pod -A -l app=busybox NAMESPACE NAME READY STATUS RESTARTS AGE default busybox 1/1 Running 28 (16h ago) 9d
So the question is, what if we don't want this label or need to modify it?
- Delete label:
[root@k8s-master01 ~]# kubectl label pod busybox app- pod/busybox unlabeled [root@k8s-master01 ~]# kubectl get pod --show-labels NAME READY STATUS RESTARTS AGE LABELS busybox 1/1 Running 28 (16h ago) 9d <none> hpa-nginx-bd88bdd8f-h8vx7 1/1 Running 0 18h app=hpa-nginx,pod-template-hash=bd88bdd8f
- Modify label: you can see that the label has been modified.
[root@k8s-master01 ~]# kubectl get pod --show-labels NAME READY STATUS RESTARTS AGE LABELS busybox 1/1 Running 28 (16h ago) 9d app=busybox hpa-nginx-bd88bdd8f-h8vx7 1/1 Running 0 18h app=hpa-nginx,pod-template-hash=bd88bdd8f [root@k8s-master01 ~]# kubectl label pod busybox app=busybox22222 --overwrite pod/busybox labeled [root@k8s-master01 ~]# kubectl get pod --show-labels NAME READY STATUS RESTARTS AGE LABELS busybox 1/1 Running 28 (16h ago) 9d app=busybox22222 hpa-nginx-bd88bdd8f-h8vx7 1/1 Running 0 18h app=hpa-nginx,pod-template-hash=bd88bdd8f
Labels is a very simple thing. Next, let's see how the Selector is used
What should I do if I want to filter out a pod with multiple conditions?
[root@k8s-master01 ~]# kubectl get pod -A --show-labels NAMESPACE NAME READY STATUS RESTARTS AGE LABELS default busybox 1/1 Running 29 (3m19s ago) 9d app=busybox22222 default hpa-nginx-bd88bdd8f-h8vx7 1/1 Running 0 18h app=hpa-nginx,pod-template-hash=bd88bdd8f kube-system calico-kube-controllers-5dffd5886b-4blh6 1/1 Running 2 (2d1h ago) 9d k8s-app=calico-kube-controllers,pod-template-hash=5dffd5886b kube-system calico-node-fvbdq 1/1 Running 2 (2d1h ago) 9d controller-revision-hash=79878cdc56,k8s-app=calico-node,pod-template-generation=1 kube-system calico-node-g8nqd 1/1 Running 0 9d controller-revision-hash=79878cdc56,k8s-app=calico-node,pod-template-generation=1 kube-system calico-node-mdps8 1/1 Running 0 9d controller-revision-hash=79878cdc56,k8s-app=calico-node,pod-template-generation=1 kube-system calico-node-nf4nt 1/1 Running 1 (4d1h ago) 9d controller-revision-hash=79878cdc56,k8s-app=calico-node,pod-template-generation=1 kube-system calico-node-sq2ml 1/1 Running 1 (2d1h ago) 9d controller-revision-hash=79878cdc56,k8s-app=calico-node,pod-template-generation=1 kube-system calico-typha-8445487f56-mg6p8 1/1 Running 0 9d k8s-app=calico-typha,pod-template-hash=8445487f56 kube-system calico-typha-8445487f56-pxbpj 1/1 Running 1 (2d1h ago) 9d k8s-app=calico-typha,pod-template-hash=8445487f56 kube-system calico-typha-8445487f56-tnssl 1/1 Running 0 9d k8s-app=calico-typha,pod-template-hash=8445487f56 kube-system coredns-5db5696c7-67h79 1/1 Running 1 (2d1h ago) 9d k8s-app=kube-dns,pod-template-hash=5db5696c7 kube-system metrics-server-6bf7dcd649-5fhrw 1/1 Running 2 (4d1h ago) 9d k8s-app=metrics-server,pod-template-hash=6bf7dcd649 kubernetes-dashboard dashboard-metrics-scraper-7fcdff5f4c-9kk86 1/1 Running 1 (2d1h ago) 9d k8s-app=dashboard-metrics-scraper,pod-template-hash=7fcdff5f4c kubernetes-dashboard kubernetes-dashboard-85f59f8ff7-js9j2 1/1 Running 12 (2d1h ago) 9d k8s-app=kubernetes-dashboard,pod-template-hash=85f59f8ff7
The above is all our pods. I want to find the container containing kubernetes dashboard and Kube DNS in the tag:
[root@k8s-master01 ~]# kubectl get pod -A -l 'k8s-app in (kubernetes-dashboard,kube-dns)' NAMESPACE NAME READY STATUS RESTARTS AGE kube-system coredns-5db5696c7-67h79 1/1 Running 1 (2d1h ago) 9d kubernetes-dashboard kubernetes-dashboard-85f59f8ff7-js9j2 1/1 Running 12 (2d1h ago) 9d
After finding these two pods, it indicates that these two pods contain these two labels. You can check whether these two pods contain these two labels in the above list of all containers.
There is also a case. For example, I have a batch of Pod labels, which are as follows:
I want to view all pods that do not contain ABB=X, which can be written as follows:
kubectl get po -l ABB!=X,APC=A
In this case, only Pod02 and Pod03 will be found.