介绍了通过Kubernetes的调度机制,将虚拟机部署到特定的节点。kubernetes的亲和、反亲和支持节点和pod两个层级。本文是通过pod的亲和(affinity)与反亲和(anti-affinity)规则实现虚拟机的调度。
Pod间亲和性与反亲和性有两种类型:
1. requiredDuringSchedulingIgnoredDuringExecution:强制选择的规则,当不满足规则时,pod会一直pending。
2. preferredDuringSchedulingIgnoredDuringExecution:“软”调度规则,程序会根据规则尝试调度到对应节点,但不会强制执行, 使用preferredDuringSchedulingIgnoredDuringExecution时需要设置weight字段,其取值范围是1到100。调度器会根据该值,将pod调度到权重较高的节点上。
实际应用:
1. 非强制性反亲和策略:尽量将虚拟机分配调度到不同物理机上,当没有更多物理机可分配时,回归普通的分配策略。
spec:
template:
metadata:
labels:
"groupName": "group1"
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: "groupName"
operator: In
values:
- "group1"
topologyKey: kubernetes.io/hostname
2. 强制性反亲和策略:强制将虚拟机分配调度到不同物理机上,当没有更多物理机可分配时,虚拟机重新启动失败。
spec:
template:
metadata:
labels:
"groupName": "group2"
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: "groupName"
operator: In
values:
- "group2"
topologyKey: kubernetes.io/hostname
3. 非强制性亲和策略:尽量将虚拟机分配调度到同一台物理机上,当物理机资源不足时,回归普通的分配策略。(系统根据物理机节点资源情况分配虚拟机,有可能分配在不同物理机上)。
spec:
template:
metadata:
labels:
"groupName": "group3"
spec:
affinity:
podAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: "groupName"
operator: In
values:
- "group3"
topologyKey: kubernetes.io/hostname
4. 强制性亲和策略:强制将虚拟机分配调度到同一台物理机上,当物理机资源不足时,虚拟机重新启动失败。
spec:
template:
metadata:
labels:
"groupName": "group4"
spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: "groupName"
operator: In
values:
- "group4"
topologyKey: kubernetes.io/hostname