Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the purpose of the tag Label of the Kubernetes special attribute

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about the role of the Kubernetes special attribute tag Label. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Preface

Label: classifies and groups various resources in K8s and adds a tag with special attributes

Selector: find the resource of the corresponding tag through a filtered syntax

Label is an important concept in K8s. A key=value key-value pair of a Label that can be attached to various resources.

It will be explained after a summary.

Matching rules for Label:

Name=nginx: this is a direct match.

Name! = nginx: matches resources with no name=nginx in the tag

Name in (A _ name=B B): matches all resources with name=An and name=B tags

Name not in (A): matches all resources that do not have label A

As you can see from the figure above: frontend matches the backend server through the service service. When the tag of service is app=nginx, then it matches the two sets of servers of backend. But when Role=backend-app is added to the service filter, Selector will only filter to the servers in the backend server group that contain these two tags and match them.

You can see the label of Pod with 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 LABELSbusybox 1 + 1 Running 28 (16h ago) 9d hpa-nginx-bd88bdd8f-h8vx7-1 + 1 Running 0 18h app=hpa-nginx,pod-template-hash=bd88bdd8f

Then we can try to tag Pod with kubectl: we can see from the above code that busybox does not have a tag, and the following command shows that there is already a tag app=busybox.

[root@k8s-master01 ~] # kubectl label pod busybox app=busyboxpod/busybox labeled [root@k8s-master01 ~] # kubectl get pod--show-labels NAME READY STATUS RESTARTS AGE LABELSbusybox 1 to 1 Running 28 (16h ago) 9d app=busyboxhpa-nginx-bd88bdd8f-h8vx7 1 to 1 Running 0 18h app=hpa-nginx,pod-template-hash=bd88bdd8f

If there is a lot of Pod in a cluster, we can find that the desired Pod;-A parameter is to view all the Pod by using tag filtering, but we will only find the Pod that meets this condition by adding-l app=busybox.

[root@k8s-master01] # kubectl get pod-A-l app=busyboxNAMESPACE NAME READY STATUS RESTARTS AGEdefault busybox 1 Running 28 (16h ago) 9d

So the question is, what if we don't want the tag or need to modify it?

Delete the 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 LABELSbusybox 1 to 1 Running 28 (16h ago) 9d hpa-nginx-bd88bdd8f-h8vx7 1 to 1 Running 0 18h app=hpa-nginx,pod-template-hash=bd88bdd8f

Modify the tag: you can see that the tag has been modified.

[root@k8s-master01] # kubectl get pod-- show-labels NAME READY STATUS RESTARTS AGE LABELSbusybox 1 Running 28 (16h ago) 9d app=busyboxhpa-nginx-bd88bdd8f-h8vx7 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 LABELSbusybox 1 Running 28 (16h ago) 9d app=busybox22222hpa-nginx-bd88bdd8f-h8vx7 1max 1 Running 0 18h app=hpa-nginx,pod-template-hash=bd88bdd8f

Labels is a very simple thing. Let's take a look at how Selector works.

I have thought about how to filter out multiple conditions of pod how to do?

[root@k8s-master01] # kubectl get pod-A-- show-labels NAMESPACE NAME READY STATUS RESTARTS AGE LABELSdefault busybox 1 Running 29 (3m19s ago) 9d app=busybox22222default hpa-nginx-bd88bdd8f-h8vx7 1/1 Running 0 18h app=hpa-nginx Pod-template-hash=bd88bdd8fkube-system calico-kube-controllers-5dffd5886b-4blh7 1ax 1 Running 2 (2d1h ago) 9d k8s Running 2 (2d1h ago) 9d Running 2 (2d1h ago) 9d controller-revision-hash=79878cdc56,k8s-app=calico-node Pod-template-generation=1kube-system calico-node-g8nqd 1/1 Running 0 9d controller-revision-hash=79878cdc56,k8s-app=calico-node,pod-template-generation=1kube-system calico-node-mdps8 1/1 Running 0 9d controller-revision-hash=79878cdc56,k8s-app=calico-node Pod-template-generation=1kube-system calico-node-nf4nt 1 Running 1 (4d1h ago) 9d controller-revision-hash=79878cdc56,k8s-app=calico-node,pod-template-generation=1kube-system calico-node-sq2ml 1 Running 1 (2d1h ago) 9d controller-revision-hash=79878cdc56,k8s-app=calico-node Pod-template-generation=1kube-system calico-typha-8445487f56-mg6p8 1ax 1 Running 09d k8s Running Running 1 (2d1h ago) 9d k8s-app=calico-typha Pod-template-hash=8445487f56kube-system calico-typha-8445487f56-tnssl 1ax 1 Running 09d k8s 67h79 appellation calicotyphatype language podriptemplateMuyhashi 8445487f56kubemuri system coredns-5db5696c7-67h79 1Compare 1 Running 1 (2d1h ago) 9d k8s-app=kube-dns Pod-template-hash=5db5696c7kube-system metrics-server-6bf7dcd649-5fhrw 1 Running 2 (4d1h ago) 9d k8s color appendages Running 2 (4d1h ago) 9d Running Running 5fff5f4ckubernetescolor dashboard kubernetes-dashboard-85f59f8ff7-js9j2 1b f7dcd649kubernetes color dashboard 1pare 1 Running 1 (2d1h ago) 9d k8s appellation dashboard kubernetes-dashboard-85f59f8ff7-js9j2 9d Pod-template-hash=85f59f8ff7

That's all our Pod, and I want to find the container that contains 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 AGEkube-system coredns-5db5696c7-67h79 1 Running 1 (2d1h ago) 9dkubernetes-dashboard kubernetes-dashboard-85f59f8ff7-js9j2 1 and 1 Running 12 (2d1h ago) 9d

The two Pod are found, indicating that the two Pod contain these two tags. You can see if the two Pod contain these two tags in the list of all containers above.

In another case, for example, I have a batch of Pod tags that look like this:

I want to see all the Pod that does not contain ABB=X, which can be written as follows:

Kubectl get po-l ABBJENTXMAGRAPHY A

In that case, only Pod02 and Pod03 will be found.

These are all the contents of this article entitled "what is the purpose of the tag Label of Kubernetes special attributes". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report