Today, I will continue to introduce the knowledge of Linux operation and maintenance. The main content of this article is the label and label selector in Kubernetes.
1. Overview of tags and tag selectors
(1) Label
In a Kubernetes cluster, the label itself is a key-value pair type of data, and can be attached to any resource object. It can be specified when the resource object is created, or added after the resource object is created. In a Kubernetes cluster, a resource object can have multiple labels, and multiple resource objects can also have the same label.
Labels in a Kubernetes cluster have a strict format. The definition of Key can use letters, numbers, underscores, hyphens, and periods, but it can only start with a character or a number. Value can be empty, or letters, numbers, hyphens, and dots can be used, but numbers or letters must be used at the beginning and end.
(2) Label selector
In a Kubernetes cluster, label selectors are often used as query conditions or selection criteria for labels. Kubernetes currently supports two label selectors, equivalence - based and set -based label selectors.
There are three kinds of label selectors based on the equivalence relationship :
"==", "=" and "!=", the first two indicate equal, and the last one indicates not equal to
the label selector based on the set relationship, there are the following four :
key in (VALUE1, VALUE2, VALUE3...), indicating that the specified key value exists in the following set.
key notin (VALUE1, VALUE2, VALUE3...), indicating that the specified key value does not exist in the following set.
key, indicating that a label with this key name exists.
!key, indicating that there is no label with this key name.
The use of tag selectors follows the following logic:
1. When multiple tag selectors are specified at the same time, the logical relationship of these multiple tag selectors is "AND".
2. Using an empty tag selector means selecting every resource object.
3. An empty tag selector cannot select any resources.
Note that an empty tag selector and an empty tag selector have different meanings here. An empty tag selector means no tag selector, and an empty tag selector means that although there is a tag selector, the value of the tag selector is empty.
2. Commands related to tags and tag selectors
Next, I will introduce the related commands of tags and tag selectors:
(1) View the label of the Pod object
On the basis of viewing the Pod object, we add the --show-labels parameter to view the label of the Pod object, for example, execute the command:
kubectl get pods --show-labels
You can view the currently running Pod and its label. The execution result of this command is as follows:
In addition, the -L parameter can display the specified label of the Pod object and execute the command:
kubectl get pods -L 【键A】
All Pod objects can be displayed, and the value of the Pod with the key A label will be displayed, and the Pod without the key A label will be displayed as empty.
For example, execute the command:
kubectl get pods -L label1,label2
The current Pod object and its label1 and label2 labels can be displayed. The execution result of this command is as follows:
(2) Tag selector to filter tags
In the kubectl command, the -l parameter can be used as a filter for labels, for example, to execute the command:
kubectl get pods -l label1,label2
You can filter Pods that contain both label1 and label2 labels. The execution result of this command is as follows:
(3) Add a label after the Pod object is created
We can assign a label to the Pod object when it is created, or we can use the label command to add a label after the Pod object is created. The command format is as follows:
kubectl label pods/【Pod名】 【标签键】=【标签值】
For example, execute the command:
kubectl label pods/pod-demo-test label3=label3
You can add label3=label3 to the Pod object of pod-demo-test. The execution result of this command is as follows:
(4) Modify the label of the Pod object
In addition to adding labels to already running Pod objects, we can also modify the labels of Pod objects. This operation also requires the use of the label command, and the --overwrite command should be added at the end. For example, execute the command:
kubectl label pods/pod-demo-test label3=modify --overwrite
The original label3=label3 of the Pod can be changed to label3=modify. The execution result of this command is as follows:
Original is not easy, please explain the source of reprint: https://blog.csdn.net/weixin_40228200