gpt4 book ai didi

Kubernetes Pod 在节点之间的分布

转载 作者:行者123 更新时间:2023-12-02 23:29:10 24 4
gpt4 key购买 nike

有没有办法让kubernetes尽可能多的分发pod?我对所有部署和全局请求都有“请求”作为 HPA。所有节点都是相同的。

刚刚遇到了一种情况,我的 ASG 缩小了一个节点,并且一项服务完全不可用,因为所有 4 个 Pod 都位于缩小的同一个节点上。

我希望保持每个部署都必须将其容器分布在至少 2 个节点上的情况。

最佳答案

在这里我利用Anirudh's answer添加示例代码。

我的初始 kubernetes yaml看起来像这样:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: say-deployment
spec:
replicas: 6
template:
metadata:
labels:
app: say
spec:
containers:
- name: say
image: gcr.io/hazel-champion-200108/say
ports:
- containerPort: 8080
---
kind: Service
apiVersion: v1
metadata:
name: say-service
spec:
selector:
app: say
ports:
- protocol: TCP
port: 8080
type: LoadBalancer
externalIPs:
- 192.168.0.112

此时,kubernetes 调度程序以某种方式决定所有 6 个副本应部署在同一节点上。

然后我added requiredDuringSchedulingIgnoredDuringExecution 强制 Pod 部署在不同的节点上:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: say-deployment
spec:
replicas: 3
template:
metadata:
labels:
app: say
spec:
containers:
- name: say
image: gcr.io/hazel-champion-200108/say
ports:
- containerPort: 8080
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: "app"
operator: In
values:
- say
topologyKey: "kubernetes.io/hostname"
---
kind: Service
apiVersion: v1
metadata:
name: say-service
spec:
selector:
app: say
ports:
- protocol: TCP
port: 8080
type: LoadBalancer
externalIPs:
- 192.168.0.112

现在所有 Pod 都在不同的节点上运行。由于我有 3 个节点和 6 个 Pod,因此其他 3 个 Pod(6 减 3)无法运行(待处理)。这是因为我需要它:requiredDuringSchedulingIgnoredDuringExecution

kubectl get pods -o wide 

NAME READY STATUS RESTARTS AGE IP NODE
say-deployment-8b46845d8-4zdw2 1/1 Running 0 24s 10.244.2.80 night
say-deployment-8b46845d8-699wg 0/1 Pending 0 24s <none> <none>
say-deployment-8b46845d8-7nvqp 1/1 Running 0 24s 10.244.1.72 gray
say-deployment-8b46845d8-bzw48 1/1 Running 0 24s 10.244.0.25 np3
say-deployment-8b46845d8-vwn8g 0/1 Pending 0 24s <none> <none>
say-deployment-8b46845d8-ws8lr 0/1 Pending 0 24s <none> <none>

现在如果我 loosen此要求与 preferredDuringSchedulingIgnoredDuringExecution:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: say-deployment
spec:
replicas: 6
template:
metadata:
labels:
app: say
spec:
containers:
- name: say
image: gcr.io/hazel-champion-200108/say
ports:
- containerPort: 8080
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: "app"
operator: In
values:
- say
topologyKey: "kubernetes.io/hostname"
---
kind: Service
apiVersion: v1
metadata:
name: say-service
spec:
selector:
app: say
ports:
- protocol: TCP
port: 8080
type: LoadBalancer
externalIPs:
- 192.168.0.112

前 3 个 Pod 部署在 3 个不同的节点上,就像前面的情况一样。其余3个(6个pod减去3个节点)根据kubernetes内部考虑部署在各个节点上。

NAME                              READY     STATUS    RESTARTS   AGE       IP            NODE
say-deployment-57cf5fb49b-26nvl 1/1 Running 0 59s 10.244.2.81 night
say-deployment-57cf5fb49b-2wnsc 1/1 Running 0 59s 10.244.0.27 np3
say-deployment-57cf5fb49b-6v24l 1/1 Running 0 59s 10.244.1.73 gray
say-deployment-57cf5fb49b-cxkbz 1/1 Running 0 59s 10.244.0.26 np3
say-deployment-57cf5fb49b-dxpcf 1/1 Running 0 59s 10.244.1.75 gray
say-deployment-57cf5fb49b-vv98p 1/1 Running 0 59s 10.244.1.74 gray

关于Kubernetes Pod 在节点之间的分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41159843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com