添加标签到节点
kubectl label nodes <node-name> <label-key>=<label-value>
添加节点选择器
apiversion: app/v1
kind: pod
metadata:
name: nginx
label:
env: test
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
nodeSelector:
disktype: ssd
节点间的亲和性
亲和性 : affinity
apiversion: app/v1
kind: pod
metadata:
name: nginx
label:
env: test
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
affinity:
nodeAffinity:
reuiqredDuringSchedulingIgnoreDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: key
operator: In
values:
- val1
- val2
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1 # 权重评分,通过遍历所有的节点计算总分,总分最高获胜
preference:
matchExpressions:
- key: another-node-label-key
operator: In
values:
- another-node-label-value
- In:label 的值在某个列表中
- NotIn:label 的值不在某个列表中
- Gt:label 的值大于某个值
- Lt:label 的值小于某个值
- Exists:某个 label 存在
- DoesNotExist:某个 label 不存在
pod 间的亲和性和反亲和性
第一个 pod 最好调度某个指定的主机上,上面至少一个节点运行这样的 pod:apiVersion: v1
kind: Pod
metadata:
name: with-pod-affinity
labels:
app: pod-affinity-pod
spec:
containers:
- name: with-pod-affinity
image: nginx
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- busybox-pod
topologyKey: kubernetes.io/hostname
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- node-affinity-pod
topologyKey: kubernetes.io/hostname
app=busybox-pod
第二个则是最好不要调度到这样的节点:app=node-affinity-pod