我有一个包含4个节点和一个主节点的kubernetes集群。我正在尝试在所有节点上运行5 nginx pod。目前,调度器有时在一台机器上运行所有的pod,有时在不同的机器上运行。
如果我的节点宕机了,而我所有的pod都在同一个节点上运行,会发生什么?我们需要避免这种情况。
如何强制调度器以轮询方式在节点上运行pod,以便如果任何节点宕机,则至少有一个节点应该有NGINX pod处于运行模式。
这是可能还是不可能?如果可能,我们如何实现这个场景?
发布于 2018-03-18 18:59:48
使用podAntiAfinity
参考:
带requiredDuringSchedulingIgnoredDuringExecution的podAntiAfinity可用于防止将相同的pod调度到相同的主机名。如果您喜欢更宽松约束,请使用preferredDuringSchedulingIgnoredDuringExecution.
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 5
template:
metadata:
labels:
app: nginx
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution: <---- hard requirement not to schedule "nginx" pod if already one scheduled.
- topologyKey: kubernetes.io/hostname <---- Anti affinity scope is host
labelSelector:
matchLabels:
app: nginx
container:
image: nginx:latestKubelet --max-pods
您可以在kubelet配置中指定节点的最大pod数,以便在节点关闭的情况下,它将防止K8S使用故障节点中的pod饱和另一个节点。
发布于 2017-04-01 13:17:01
我认为跨实例反亲和力功能会对您有所帮助。pod间反亲和性允许您根据已在节点上运行的pod上的标签来约束pod有资格调度到哪些节点上。下面是一个例子。
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
run: nginx-service
name: nginx-service
spec:
replicas: 3
selector:
matchLabels:
run: nginx-service
template:
metadata:
labels:
service-type: nginx
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: service-type
operator: In
values:
- nginx
topologyKey: kubernetes.io/hostname
containers:
- name: nginx-service
image: nginx:latest备注:我在这里使用preferredDuringSchedulingIgnoredDuringExecution,因为您的pod比节点多。
有关更多详细信息,请参阅以下链接的pod间亲和性和反亲和性(测试版功能)部分:https://kubernetes.io/docs/concepts/configuration/assign-pod-node/
发布于 2021-01-18 15:48:19
使用Pod拓扑扩散约束
从2021年开始,(v1.19和更高版本)你可以默认使用 topologySpreadConstraints,我发现它比podAntiAfinity更适合这种情况。
主要区别在于反亲和性只能限制每个节点一个pod,而Pod拓扑传播约束可以限制每个节点N个Pod。
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-example-deployment
spec:
replicas: 6
selector:
matchLabels:
app: nginx-example
template:
metadata:
labels:
app: nginx-example
spec:
containers:
- name: nginx
image: nginx:latest
# This sets how evenly spread the pods
# For example, if there are 3 nodes available,
# 2 pods are scheduled for each node.
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: nginx-example有关更多详细信息,请参阅KEP-895和an official blog post.
https://stackoverflow.com/questions/37784480
复制相似问题