给节点设置标签
当我们运行一个pod的时候,master会根据自己的算法来调度pod运行在哪个节点,具体是哪个节点,只有在pod被创建后才知道。
我们可以通过在每个节点上设置一些标签,然后指定pod运行在特定标签的节点上,就可以手动地指定pod运行在哪个节点。
- 查看所有节点的标签
kubectl get node --show-labels
- 查看某特定节点的标签
kubectl get node sun004 --show-labels
- 给节点设置标签
# 语法
kubectl label node 节点名 key=value
# 例子
kubectl label node sun004 user=sun
- 查看标签是否生效
kubectl get node sun004 --show-labels
- 取消节点的某个标签
kubectl label node sun004 user-
- 再次查看标签
kubectl get node sun004 --show-labels
- 特殊的标签
node-role.kubernetes.io/名字
,该标签用于设置kubectl get node结果中ROLES那列值的,其中node-role.kubernetes.io/
后面的部分就是显示在ROLES下面的
kubectl get node
- 给两台worker添加node-role.kubernetes.io标签
kubectl label node sun004 node-role.kubernetes.io/worker1=""
kubectl label node sun005 node-role.kubernetes.io/worker2=""
- 再次查看
kubectl get node
- 取消该标签的命令与取消普通标签命令一致
创建在特定节点上运行的pod
在pod里通过nodeSelector可以让pod在含有特定标签的节点上运行。如果有多个节点含有指定标签,则在这几个节点上任一个上运行,如果没有含有这个标签的节点,pod创建不出来,状态一直为pending。
- yaml文件如下
vi podlabel.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
role: myrole
name: web1
spec:
nodeSelector:
user: sun
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: web
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
- 创建pod并查看pod运行的节点(无对应标签,pending)
kubectl apply -f podlabel.yaml
kubectl get pod -o wide
- 创建pod并查看pod运行的节点(有对应标签,创建成功)
kubectl label node sun005 user=sun
kubectl delete -f podlabel.yaml
kubectl apply -f podlabel.yaml
kubectl get pod -o wide
Annotations设置
所有node和pod以及后面要讲的其他对象(如deployment),都有一个属性Annotations,这个属性可以理解为注释。
- 查看sun005的Annotations属性
kubectl describe nodes sun005
- 设置Annotations
kubectl annotate nodes sun005 aa=123
kubectl describe nodes sun005
- 取消Annotations
kubectl annotate nodes sun005 aa-
kubectl describe nodes sun005