一,为什么要为node指定label?
通常scheduler会把pod调度到所有可用的Node,有的情况下我们希望能把 Pod 部署到指定的 Node,
例如:
有的Node上配备了速度更快的SSD磁盘
有的Node上配备了性能更强的GPU,
这样pod中的应用才能更好的发挥node的硬件优势
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,用label让pod在指定的node上运行:
1,查看各个node的label:
[root@kubemaster k8s]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS kubemaster Ready master 27h v1.18.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=kubemaster,kubernetes.io/os=linux,node-role.kubernetes.io/master= node1 Ready <none> 72m v1.18.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node1,kubernetes.io/os=linux
说明:各node的label都是系统默认的,还没有我们手动添加的
2,给一个node添加label
格式:
kubectl label nodes <node-name> <label-key>=<label-value>
添加
[root@kubemaster k8s]# kubectl label node kubemaster apptype=web1 node/kubemaster labeled [root@kubemaster k8s]# kubectl label node node1 apptype=web2 node/node1 labeled
查看效果:
[root@kubemaster k8s]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS kubemaster Ready master 28h v1.18.3 apptype=web1beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=kubemaster,kubernetes.io/os=linux,node-role.kubernetes.io/master= node1 Ready <none> 75m v1.18.3 apptype=web2,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node1,kubernetes.io/os=linux
3,修改label
[root@kubemaster k8s]# kubectl label node node1 apptype=noweb --overwrite
node/node1 labeled
查看效果:
[root@kubemaster k8s]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS kubemaster Ready master 28h v1.18.3 apptype=web1,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=kubemaster,kubernetes.io/os=linux,node-role.kubernetes.io/master= node1 Ready <none> 85m v1.18.3 apptype=noweb,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node1,kubernetes.io/os=linux
4,删除label
#格式:
kubectl label nodes <node-name> <label-key>-
删除
[root@kubemaster k8s]# kubectl label nodes node1 apptype-
node/node1 labeled
三,如何把Pod 部署到指定label的 Node:
在yaml文件中添加:
spec:段下面
nodeSelector:
apptype: web
看一个例子:
[root@kubemaster k8s]# more nginx-rc.yaml apiVersion: v1 kind: ReplicationController metadata: name: nginx-demo spec: replicas: 2 selector: app: nginx-demo template: metadata: labels: app: nginx-demo spec: nodeSelector: apptype: web containers: - name: nginx-demo image: nginx ports: - containerPort: 80
查看当前node的label
[root@kubemaster k8s]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS kubemaster Ready master 28h v1.18.3 apptype=noweb,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=kubemaster,kubernetes.io/os=linux,node-role.kubernetes.io/master= node1 Ready <none> 107m v1.18.3 apptype=web,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=node1,kubernetes.io/os=linux
运行:
[root@kubemaster k8s]# kubectl apply -f nginx-rc.yaml
replicationcontroller/nginx-demo created
查看效果:
[root@kubemaster k8s]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-demo-b5vwt 1/1 Running 0 15m 172.16.166.136 node1 <none> <none> nginx-demo-j8n7d 1/1 Running 0 15m 172.16.166.135 node1 <none> <none>
四,查看kubernetes的版本
[root@kubemaster ~]# kubelet --version
Kubernetes v1.18.3
[root@kubemaster ~]# kubeadm version
kubeadm version: &version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.3", GitCommit:"2e7996e3e2712684bc73f0dec0200d64eec7fe40",
GitTreeState:"clean", BuildDate:"2020-05-20T12:49:29Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64”}
五,查看linux的版本
[root@kubemaster ~]# cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)
[root@kubemaster ~]# uname -r
4.18.0-193.el8.x86_64