6.node节点选择器
我们在创建pod资源的时候,pod会根据schduler进行调度,那么默认会调度到随机的一个工作节点,如果我们想要pod调度到指定节点或者调度到一些具有相同特点的node节点,怎么办呢? 可以使用pod中的nodeName或者nodeSelector字段指定要调度到的node节点
1、nodeName
指定pod节点运行在哪个具体node上
由于只开了一主一从,需要先k8s01的污点去掉
kubectl taint nodes k8s01 node-role.kubernetes.io/master-
[root@k8s01 ~]# cat pod-node.yaml
apiVersion: v1
kind: Pod
metadata:
name: demo-pod
namespace: default
labels:
app: myapp
env: env
spec:
nodeName: k8s01
containers:
- name: tomcat-pod-java
ports:
- containerPort: 8080
image: tomcat:8.5-jre8-alpine
imagePullPolicy: IfNotPresent
- name: busybox
image: busybox:1.28
imagePullPolicy: IfNotPresent
command:
- "/bin/sh"
- "-c"
- "sleep 3600"
kubectl apply -f pod-node.yaml
[root@k8s01 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
demo-pod 2/2 Running 0 13s 10.244.73.67 k8s01 <none> <none>
2、nodeSelector
指定pod调度到具有哪些标签的node节点上 #给node节点打标签,打个具有disk=ceph的标签
kubectl label node k8s02 disk=ceph标签:node,kubernetes,节点,io,pod,k8s,k8s01,选择器 From: https://blog.51cto.com/u_13236892/5786030
[root@k8s01 ~]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s01 Ready control-plane,master 24h v1.20.6 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s01,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=
k8s02 Ready worker 24h v1.20.6 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disk=ceph,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s02,kubernetes.io/os=linux,node-role.kubernetes.io/worker=worker
[root@k8s01 ~]# cat pod-1.yaml
apiVersion: v1
kind: Pod
metadata:
name: demo-pod-1
namespace: default
labels:
app: myapp
env: dev
spec:
nodeSelector:
disk: ceph
containers:
- name: tomcat-pod-java
ports:
- containerPort: 8080
image: tomcat:8.5-jre8-alpine
imagePullPolicy: IfNotPresent
kubectl apply -f pod-1.yaml
#查看pod调度到哪个节点
[root@k8s01 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READIN ESS GATES
demo-pod 2/2 Running 0 7m46s 10.244.73.67 k8s01 <none> <none>
demo-pod-1 1/1 Running 0 19s 10.244.236.145 k8s02 <none> <none>