在某些特殊情況下,需要將某些服務(wù)固定在一臺(tái)宿主機(jī)上, k8s可以使用label給node節(jié)點(diǎn)打上標(biāo)簽來滿足這種需求.
Label添加刪除和修改
添加label
# 語(yǔ)法
kubectl label nodes <node-name> <label-key>=<label-value>
# 查看現(xiàn)有node及l(fā)abel
[root@master ~]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master Ready master 54d v1.13.4 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master,node-role.kubernetes.io/master=
node01 Ready <none> 54d v1.13.4 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node01
node02 Ready <none> 6d19h v1.13.4 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node02
# 添加一個(gè)key為disktype和value為ssd的label
[root@master ~]# kubectl label nodes node01 disktype=ssd
node/node01 labeled
# 查看是否被添加
[root@master ~]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master Ready master 54d v1.13.4 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master,node-role.kubernetes.io/master=
node01 Ready <none> 54d v1.13.4 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/hostname=node01
刪除Label
# 語(yǔ)法
kubectl label nodes <node-name> <label-key>-
# 刪除key為disktype的label
[root@master ~]# kubectl label nodes node01 disktype-
node/node01 labeled
[root@master ~]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master Ready master 54d v1.13.4 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master,node-role.kubernetes.io/master=
node01 Ready <none> 54d v1.13.4 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node01
node02 Ready <none> 6d19h v1.13.4 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node02
修改Label的值城丧,
#語(yǔ)法: 需要加上--overwrite參數(shù):
kubectl label nodes <node-name> <label-key>=<label-value> --overwrite
[root@master ~]# kubectl label nodes node01 disktype=ssd
node/node01 labeled
[root@master ~]# kubectl label nodes node01 disktype=hdd --overwrite
node/node01 labeled
[root@master ~]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master Ready master 54d v1.13.4 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master,node-role.kubernetes.io/master=
node01 Ready <none> 54d v1.13.4 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=hdd,kubernetes.io/hostname=node01
node02 Ready <none> 6d19h v1.13.4 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node02
Pod選擇Label
# 添加nodeSelector選項(xiàng)用來選擇對(duì)應(yīng)的node
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
env: test
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
nodeSelector:
disktype: ssd