k8s 的 statefulSet 的 Pod 调度问题

2021-05-28 17:39:28 +08:00
 farmer01
有 3 台 Node,我想把 elasticsearch sclae 为 3,并且保证每台 Node 上都有一个 elasticsearch
打了 label 后,发现经常会有 1 个 node 上有 2 个 ES pod (因为设置了 hostPath,所以 2 个 ES Pod 分配到同一台 Node 上会导致第二个 ES 启动失败)

请问大佬们有没有办法保证这种 statefulSet 集群的 Pod 都被均匀地分配到不同 Node 上,特别是不能有一台 Node 上存在多个同 statefulSet 的情况
1198 次点击
所在节点    问与答
15 条回复
sniperking1234
2021-05-28 17:44:16 +08:00
使用 podAntiffinity
finian
2021-05-28 17:55:14 +08:00
`podAntiffinity` 是软满足(尽量满足),也可能会出现分布不均的情况。如果 K8s 是 1.18 及以上版本的话,用 `topologySpreadConstraints` 吧,并设置 `whenUnsatisfiable: DoNotSchedule`,这样能保证均匀分布
finian
2021-05-28 17:57:46 +08:00
@finian 还要设置 `maxSkew: 1`
RudyS
2021-05-28 18:00:39 +08:00
topologySpreadConstraints
anubu
2021-05-28 18:19:49 +08:00
用 PodAntiAffinity 的 requiredDuringSchedulingIgnoredDuringExecution 。

topologySpreadConstraints 可能并不完全适用你的场景,maxSkew 为 1 在正常情况下可以满足你的要求,但如果某一节点宕机则会调度 2 个 Pod 到某一节点,可能导致 hostPath 内有状态数据异常。
farmer01
2021-05-28 18:44:20 +08:00
@anubu 我用的是 1.20.2 版本的,然后在 1.14.x 版本上是正常的,会自动调度到不同 node.
Helm 里面我也看了,确认了有这些:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: es-accobot-hkpf
operator: In
values:
- es-accobot-hkpf
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- elasticsearch
topologyKey: kubernetes.io/hostname
finian
2021-05-28 19:02:22 +08:00
@anubu `podAntiffinity` 在正常情况下也可能会调度 2 个 Pod 到某一节点,它只是尽量满足约束条件,不是强制的。你说的某个节点宕机的情况下,在 `whenUnsatisfiable: DoNotSchedule` 设置下,不会出现 2 个 Pod 被调度到一个节点的情况(因为这时最大的 skew 为 2,不满足 maxSkew: 1 的约束),但是这时系统会停止调度(只部署了 2 个 Pod 在不同的节点,另外一个节点因为宕机无法调度)
anubu
2021-05-28 19:29:08 +08:00
@finian 感谢指正,应该是我理解错了,以为 maxSkew 约束条件计算不包含宕机节点。关于 podAntiffinity 是软性约束这点还不太确认,需要找些资料再学习一下。

@farmer01 不太确认版本问题和 podAntiffinity 无法硬性约束的问题。可以按照 2 楼的建议,使用 topologySpreadConstraints 。
swulling
2021-05-28 21:00:21 +08:00
@finian 可以设置 hard 还是 soft 约束。

hard 用 requiredDuringSchedulingIgnoredDuringExecution
finian
2021-05-28 21:42:42 +08:00
@swulling 你说得对。我错看成 preferredDuringSchedulingIgnoredDuringExecution 了。因为我们之前的场景是希望 Pod 均匀分布,且每个节点会多于一个,在 1.18 版本之前,只能使用 podAntiAffinity 的 preferredDuringSchedulingIgnoredDuringExecution,这时候是不能保证均匀分布的。后来 1.18 版本后才改成使用 topologySpreadConstraints
Illusionary
2021-05-29 00:10:36 +08:00
DaemonSet
wellsc
2021-05-29 00:12:39 +08:00
statefulSet 真的靠谱吗,目前还不敢尝试
lixiang2017
2021-05-29 03:35:36 +08:00
用的 DaemonSet 从来没出现过这种情况。
plko345
2021-05-29 08:35:46 +08:00
@finian podAntiAffinity 有个 requiredDuringSchedulingIgnoredDuringExecution
finian
2021-05-30 17:44:26 +08:00
@plko345 requiredDuringSchedulingIgnoredDuringExecution 不能满足一个节点部署多个 Pod 的需求(均匀分布且每个节点多于一个 Pod )

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/779876

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX