doina

一个小菜鸟运维工程师.

kubernetes 对node添加Label并进行选择

某些特殊情况下,需要将某些服务固定在一台宿主机上, k8s可以使用label给node节点打上标签来满足这种需求.

Label添加删除和修改

添加label

# 语法
kubectl label nodes <node-name> <label-key>=<label-value> 
# 查看现有node及label
[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

# 添加一个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

# 语法
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的值,

#语法: 需要加上--overwrite参数:
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选项用来选择对应的node
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    env: test
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
  nodeSelector:
    disktype: ssd
点赞

发表评论

电子邮件地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据