Label Meaning
Labels are actually a pair of key/value, which are associated with objects, such as Pods. We tend to use labels to indicate the special characteristics of objects. The value of Labels has no meaning to the system itself, but only makes sense to users. The key of the labels attribute of the same resource object must be unique, and the label can be attached to various resource objects, such as Node, Pod, Service, RC, etc. A resource has multiple tags, which can realize management in different dimensions. The composition of the label: key=value. Label can be attached to the object when it is created, or it can be added or modified through the API after the object is created
Label naming convention
label must start with a letter or number, letters, numbers, hyphens, dots and underscores can be used, and the maximum length is 63 characters
Reasons for using Label
-When there are more and more resources of the same type, it is necessary to divide and manage the resources. At this time, you can use Label to name the resource objects to facilitate configuration, deployment and other management tasks, and improve the efficiency of resource management. The label function is similar to the Java package, which can manage different files separately, making the whole more organized and conducive to maintenance.
-Use Label to reference objects
Label use
-
Command creation
label [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--resource-version=version]
-
The kubectl get pods command will not list any labels by default, use the --show-labels option to view
kubectl get pods --show-labels
-
View by designated label
kubectl get pods -L creation_method,env
-
View the nodes that match the label conditions
kubectl get nodes -l label key=label values kubectl get nodes -l app=tomcat
-
View pods that match the tag key
kubectl get pods -L app
-
Add label app=tomcat to the Pod named tomcat
kubectl label pods tomcat app=tomcat
-
Change the label of the Pod named tomcat to app=tomcat1, and overwrite the existing value
kubectl label --overwrite pods tomcat app=tomcat1
-
Add labels to all pods in the namespace
kubectl label pods --all test=test
-
Delete the label named
app
. (Use-
minus sign to connect)kubectl label pods tomcat app-
-
yaml create label
Added two labels to tomcat's Pod, namelyapp:tomcat
andrelease:tomcat
apiVersion: v1 kind: Pod metadata: name: tomcat labels: app: tomcat release: stable spec: containers: -name: tomcat image: tomcat ports: -containerPort: 80
Label commonly used multi-dimensional label classification
-Version label (release): stable (stable version), canary (canary version), beta (beta)
-Environment (environment): dev (development), qa (test), production (production), op (operation and maintenance)
-Application (applaction): ui (design), as (application software), pc (computer side), sc (network aspect)
-Architecture layer (tier): frontend (front end), backend (back end), cache (cache)
-Partition label (partition): customerA (customer), customerB
-Quality control level (track): daily (every day), weekly (weekly)
apiVersion: v1
kind: Pod
metadata:
name: label-pod-test
labels: #Use the labels field to define labels, you can define multiple labels at a time, here define 3 labels
release: stable #Version: stable version
env: qa #Environment: Test
tier: frontend #Architecture class: frontend
spec:
containers:
-name: testTomcatLabel
image: tomcat #Deployed tomcat service
Post comment 取消回复