Label is an identifier added to resources in kubernetes to distinguish and select them
A Label will be attached to various object resources in the form of key / value key -value pairs
Such as Node, Pod, Service, etc.
A resource object can define any number of Labels .
The same Label can also be added to any number of resource objects (the distinction between service load and subsequent pod resources)
Label is usually determined when the resource object is defined, and can also be dynamically added or deleted after the object is created
Realize multi-dimensional grouping of resources through Label
In order to perform resource allocation, scheduling, configuration, deployment and other management tasks flexibly and conveniently
Commonly used Label examples are as follows:
> Version tag: "version":"1.0"
> 环境标签:"environment":"dev","environment":"test","environment":"port"
> Schema tags: "tier":"frontend","tier":"backend"
After the label is defined, the selection of the label must also be considered , which requires the use of Label Selector
Label is used to define an identifier for a resource object
Label Selector is used to query and filter resource objects with certain labels
How to define labels
-Equation - based definition
version=1.0 #equal to
version!=1.0 #Not equal to
- Set -based definition
version in (1.0,2.0) #here
version noin (1.0,2.0) #not here
Label common commands
#Create Pod and label Pod resources
[root@master ~]# kubectl create -f pod_nginx.yml
pod/nginx created
#View pods
[root@master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 12s
#View pod labels: --show-labels ( no labels )
[root@master ~]# kubectl get pod -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 95s <none>
#Put version tag for nginx pod
[root@master ~]# kubectl label pod nginx -n dev version=1.0
pod/nginx labeled
#View pod label
[root@master ~]# kubectl get pod -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 6m2s version=1.0
#Label the pod of nginx ( a resource object can define any number of Labels )
[root@master ~]# kubectl label pod nginx -n dev tier=frontend(架构标签,表示前端服务)
pod/nginx labeled
#View pod label
[root@master ~]# kubectl get pod -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 20m tier=frontend,version=1.0
#Update the version tag: --overwrite ( overwrite )
[root@master ~]# kubectl get pod -n dev --show-labels
pod/nginx labeled
#View pod label
[root@master ~]# kubectl get pod -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 26m tier=frontend, version=2.0 #to update the label
# Equation -based label filtering ( create another nginx pod and label it for filtering )
[root@master ~]# vim pod_nginx.yml
apiVersion: v1
kind: Pod
metadata:
name: nginx2 #修改pod名称
namespace: dev
spec:
containers:
- image: nginx:1.17.1
name: pod
ports:
- name: nginx-port
containerPort: 8080 #修改端口
protocol: TCP
# create pods
[root@master ~]# kubectl create -f pod_nginx.yml
pod/nginx2 created
#View pods
[root@master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 45m
nginx2 1/1 Running 0 7m27s
#View pod labels ( nginx2 has no labels by default )
[root@master ~]# kubectl get pod -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 45m tier=frontend,version=2.0
nginx2 1/1 Running 0 8m1s <none>
# Label the pod of nginx2
[root@master ~]# kubectl label pod nginx2 -n dev version=1.0
pod/nginx2 labeled
#View pod label
[root@master ~]# kubectl get pod -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 48m tier=frontend,version=2.0
nginx2 1/1 Running 0 11m version=1.0
label filter
#Filter tags according to the tag selector : -l "label"
[root@master ~]# kubectl get pod -l "version=1.0" -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx2 1/1 Running 0 12m version=1.0
#Filter labels not equal to "version=1.0" : "version!=1.0"
[root@master ~]# kubectl get pod -l "version!=1.0" -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 53m tier=frontend,version=2.0
#Set-based tag filtering: in(filter)
[root@master ~]# kubectl get pods -l "version in (1.0)" -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx2 1/1 Running 0 22m version=1.0
#Set-based label filtering: notin (do not filter)
[root@master ~]# kubectl get pods -l "version notin (1.0)" -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 60m tier=frontend,version=2.0
delete label
# remove tag: tag-name-
[root@master ~]# kubectl label pod nginx -n dev tier-
pod/nginx labeled
[root@master ~]# kubectl label pod nginx -n dev version-
pod/nginx labeled
#view tags
[root@master ~]# kubectl get pod -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 64m <none>
nginx2 1/1 Running 0 26m version=1.0
Profile form update tab
[root@master ~]# vim pod_nginx.yml
apiVersion: v1
kind: Pod
metadata:
name: nginx1
namespace: dev
labels:
version: "3.0" #更新标签为3.0
env: "test" #添加新标签
spec:
containers:
- image: nginx:1.17.1
name: pod
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
#Execution file (apply configures resources through configuration files)
[root@master ~]# kubectl apply -f pod_nginx.yml
#view tags
[root@master ~]# kubectl get pod -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 110m <none>
nginx2 1/1 Running 0 72m env=test,version=3.0