(20) Kubernetes Practical Introduction - Label

1. Concept

Label is an important concept in kubernetes. Its function is to add logos to resources to distinguish and select them.
Features of label:
1) A label will be attached to various objects in the form of key/value key-value pairs, such as Node, Pod, Service, etc.
2) A resource object can define any number of labels, and the same label can also be Add to any number of resource objects.
3) The label is usually determined when the resource object is defined. Of course, it can also be dynamically added or deleted after the object is created.

二、label selector

  1. Multi-dimensional grouping of resources can be realized through labels, so that resource allocation, scheduling, configuration, deployment and other management tasks can be carried out flexibly and conveniently.
    Some commonly used label examples are as follows:
    Version information: "version": "release"
    Environment information: "environment" ": "dev"
    architecture information: "tier": "frontend"
    label definition is completed, you need to consider the choice of the label, which requires the use of label selectot, label is used to identify a resource object, label selector is used Query and filter resource objects with certain tags.
    There are currently two types of label selectors:
    1) Label selector
    name = slave based on the equation : select all objects that contain key=name and value=slave in the
    label env != prod: select all objects that contain key=env and value in the label! =prod object
    2) Set-based label selector
    name in (master, slave) selects all objects that contain key=name and value=master or slave
    in label name not in (master, slave) selects all objects that contain key=name in label And value!=master and !=slave object
  2. There can be multiple label options. At this time, combine multiple label selectors and separate them with commas, for example: name in (master, slave), env != prod

Third, the use of labels

  1. Command mode
#为pod资源打标签
kubectl label pod nginx-pod version=1.0 -n dev
#为pod资源更新标签
kubectl label pod nginx-pod version=2.0 -n dev --overwrite
#查看标签
kubectl get pod nginx-pod -n dev --show-labels
#筛选标签
kebectl get pod -n dev -l "version=2.0" --show-labels
#删除标签tier
kubectl label pod nginx -n dev tier-
  1. Configuration method
    Create a pod-nginx.yaml, the configuration file specifies the label
apiVersion: v1
kind: Pod
metadata: 
	name: nginx
	namespace: dev
	label: 
		version: 1.0
		env: dev
spec:
	containers:
	- image: nginx:1.17.1
	  imagePullPolicy: IfNotPresent
	  name: pod
	  port: 
	  - name: nginx-port
	    containerPort: 80
	    protocol: TCP

Execute the corresponding update command:

kubectl apply -f pod-nginx.yaml

》》》Bloggers update their learning experience for a long time, recommend likes and follow! ! !
》》》If there is something wrong, please leave a message in the comment area, thank you! ! !

Guess you like

Origin blog.csdn.net/qq_41622739/article/details/114177769