nodeselector && nodeAffinity
示例:确保Pod分配到具有SSD硬盘的节点上
打标签
- 标签
- 第一步:给节点添加标签
- 格式:kubectl label nodes =
- 例如:kubectl label nodes k8s-node1 disktype=ssd
- 验证:kubectl get nodes --show-labels
- 第二步:添加nodeSelector字段到Pod配置中
- 最后,验证:
kubectl get pods -o wide
加selector
-
[root@k8s-master1 6]# kubectl run pod --image=nginx --dry-run=client -o yaml >my-pod.yaml
-
编辑配置文件如下
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: my-pod
name: node-selector-pod
spec:
containers:
- image: nginx
name: pod
resources: {}
- 查看pod分配到哪个node节点
- kubeclt get pod -o wide
加入selector
apiVersion: v1
kind: Pod
metadata:
labels:
run: my-pod
name: node-selector-pod
spec:
nodeSelector:
diskType: "ssd"
containers:
- image: nginx
name: pod
resources: {}
如果制定的selector 不存在,pod容器会处于pending状态
affinity
- 强制策略- required 必须满足
- yaml文件
apiVersion: v1
kind: Pod
metadata:
name: with-node-affinity
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: web
image: nginx
~
- kubectl apply -f x.yaml
- 可选策略
- 尽量满足,但是不保证
apiVersion: v1
kind: Pod
metadata:
name: with-node-affinity-prefer
spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: disktype
operator: In
values:
- ssd2
containers:
-
name: web2
image: nginx
错误排查
1 node(s) had taints that the pod didn't tolerate, 2 node(s) didn't match node selector
- 原因因为master节点不参与调度, disktype=ssd 标签打到master节点
- 解决方法
- 给目标节点打上标签即可