1、查看当前所有node的标签
[root@centos7_9 tomcat1]#kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
192.168.1.92 Ready,SchedulingDisabled master 2d19h v1.20.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=192.168.1.92,kubernetes.io/os=linux,kubernetes.io/role=master
192.168.1.96 Ready node 2d19h v1.20.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=192.168.1.96,kubernetes.io/os=linux,kubernetes.io/role=node,node=first-node
192.168.1.97 Ready node 2d19h v1.20.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=192.168.1.97,kubernetes.io/os=linux,kubernetes.io/role=node
192.168.1.98 Ready node 2d19h v1.20.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=192.168.1.98,kubernetes.io/os=linux,kubernetes.io/role=node
192.168.1.99 Ready node 2d19h v1.20.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=192.168.1.99,kubernetes.io/os=linux,kubernetes.io/role=node
2、查看某个node的标签
[root@centos7_9 tomcat1]#kubectl describe node 192.168.1.96
Name: 192.168.1.96
Roles: node
Labels: beta.kubernetes.io/arch=amd64
beta.kubernetes.io/os=linux
kubernetes.io/arch=amd64
kubernetes.io/hostname=192.168.1.96
kubernetes.io/os=linux
kubernetes.io/role=node
node=first-node
Annotations: flannel.alpha.coreos.com/backend-data: {"VtepMAC":"3a:ff:a5:5c:09:d1"}
flannel.alpha.coreos.com/backend-type: vxlan
flannel.alpha.coreos.com/kube-subnet-manager: true
flannel.alpha.coreos.com/public-ip: 192.168.1.96
node.alpha.kubernetes.io/ttl: 0
volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp: Sun, 21 Aug 2022 20:07:24 +0800
3、node自定义标签
[root@centos7_9 tomcat1]#kubectl label node 192.168.1.97 project=k8snew
node/192.168.1.97 labeled
[root@centos7_9 tomcat1]#kubectl label node 192.168.1.97 node=second-node
node/192.168.1.97 labeled
[root@centos7_9 tomcat1]#kubectl describe node 192.168.1.97
Name: 192.168.1.97
Roles: node
Labels: beta.kubernetes.io/arch=amd64
beta.kubernetes.io/os=linux
kubernetes.io/arch=amd64
kubernetes.io/hostname=192.168.1.97
kubernetes.io/os=linux
kubernetes.io/role=node
node=second-node
project=k8snew
Annotations: flannel.alpha.coreos.com/backend-data: {"VtepMAC":"6a:e7:a3:49:c9:bb"}
flannel.alpha.coreos.com/backend-type: vxlan
flannel.alpha.coreos.com/kube-subnet-manager: true
flannel.alpha.coreos.com/public-ip: 192.168.1.97
node.alpha.kubernetes.io/ttl: 0
volumes.kubernetes.io/controller-managed-attach-detach: true
4、yaml引用node的label示范
[root@centos7_9 haproxy]#vim haproxyDep.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: k8s-new
name: haproxy-deployment
labels:
app: haproxy
spec:
replicas: 1
selector:
matchLabels:
app: haproxy
template:
metadata:
labels:
app: haproxy
spec:
containers:
- name: haproxy
image: www.shenqiqi.org/k8simages/haproxy-2.5.0:v1
imagePullPolicy: IfNotPresent
resources:
requests:
cpu: 200m
memory: 200Mi
ports:
- containerPort: 9999
protocol: TCP
name: status
- containerPort: 9527
protocol: TCP
name: haproxy01
nodeSelector: #注意必须配置在containers参数结束后的部分
node: first-node #之前配置的192.168.1.96节点的label
5、构建kubectl apply -f ./* ,查看构建只够分配到指定的label的节点
6、自定义node label的删除
[root@centos7_9 tomcat1]#kubectl label nodes 192.168.1.97 project-
命令格式即:kubectl label nodes node节点名称/nodeIP node对应的key-