Skip to content

1. Label

  1. Label是一个键值对,可以附加在任何对象上,比如Node,Pod,Service,RC等。Label和资源对象是多对多的关系,即一个Label可以被添加到多个对象上,一个对象也可以定义多个Label。
  2. Label的作用主要用来实现精细的、多维度的资源分组管理,以便进行资源分配,调度,配置,部署等工作。
  3. Label通俗理解就是“标签”,通过标签来过滤筛选指定的对象,进行具体的操作。k8s通过Label Selector(标签选择器)来筛选指定Label的资源对象,类似SQL语句中的条件查询(WHERE语句)。
  4. Label Selector有基于等式和基于集合的两种表达方式,可以多个条件进行组合使用。
  • 基于等式:name=redis-slave(匹配name=redis-slave的资源对象);env!=product(匹配所有不具有标签env=product的资源对象)
  • 基于集合:name in (redis-slave,redis-master);name not in (php-frontend)(匹配所有不具有标签name=php-frontend的资源对象)

使用场景

  1. kube-controller进程通过资源对象RC上定义的Label Selector来筛选要监控的Pod副本数,从而实现副本数始终保持预期数目。
  2. kube-proxy进程通过Service的Label Selector来选择对应Pod,自动建立每个Service到对应Pod的请求转发路由表,从而实现Service的智能负载均衡机制。
  3. kube-scheduler实现Pod定向调度:对Node定义特定的Label,并且在Pod定义文件中使用NodeSelector标签调度策略。

2. Label添加删除

https://kubernetes.io/docs/tasks/configure-pod-container/assign-pods-nodes/

2.1 node

添加

bash
#语法
kubectl label nodes node-name label_name=label_value

#案例
[root@kube-master yaml]# kubectl label nodes kube-node03 test=hello
node/kube-node03 labeled
#语法
kubectl label nodes node-name label_name=label_value

#案例
[root@kube-master yaml]# kubectl label nodes kube-node03 test=hello
node/kube-node03 labeled

查看

bash
# 查看所有标签
kubectl get nodes --show-labels
# 查看所有标签
kubectl get nodes --show-labels

删除

bash
#删除一个label,只需在命令行最后指定label的key名并与一个减号相连即可:
[root@kube-master yaml]# kubectl label nodes kube-node03 test-
node/kube-node03 labeled
#删除一个label,只需在命令行最后指定label的key名并与一个减号相连即可:
[root@kube-master yaml]# kubectl label nodes kube-node03 test-
node/kube-node03 labeled

修改

bash
#修改一个label的值,需要加上--overwrite参数
[root@kube-master yaml]# kubectl label nodes kube-node03 test=world --overwrite
node/kube-node03 labeled
#修改一个label的值,需要加上--overwrite参数
[root@kube-master yaml]# kubectl label nodes kube-node03 test=world --overwrite
node/kube-node03 labeled

2.2 pod

bash
# Update pod 'foo' with the label 'unhealthy' and the value 'true'.
kubectl label pods foo unhealthy=true

# Update pod 'foo' with the label 'status' and the value 'unhealthy', overwriting any existing value.
kubectl label --overwrite pods foo status=unhealthy

# Update all pods in the namespace
kubectl label pods --all status=unhealthy

# Update a pod identified by the type and name in "pod.json"
kubectl label -f pod.json status=unhealthy

# Update pod 'foo' only if the resource is unchanged from version 1.
kubectl label pods foo status=unhealthy --resource-version=1

# Update pod 'foo' by removing a label named 'bar' if it exists.
# Does not require the --overwrite flag.
kubectl label pods foo bar-
# Update pod 'foo' with the label 'unhealthy' and the value 'true'.
kubectl label pods foo unhealthy=true

# Update pod 'foo' with the label 'status' and the value 'unhealthy', overwriting any existing value.
kubectl label --overwrite pods foo status=unhealthy

# Update all pods in the namespace
kubectl label pods --all status=unhealthy

# Update a pod identified by the type and name in "pod.json"
kubectl label -f pod.json status=unhealthy

# Update pod 'foo' only if the resource is unchanged from version 1.
kubectl label pods foo status=unhealthy --resource-version=1

# Update pod 'foo' by removing a label named 'bar' if it exists.
# Does not require the --overwrite flag.
kubectl label pods foo bar-