避免kubernetes调度程序在kubernetes集群的单个节点中运行所有pod

时间:2016-06-13 07:59:17

标签: kubernetes scheduler kubernetes-pod

我有一个kubernetes集群,有4个节点和一个主节点。我试图在所有节点中运行5 nginx pod。目前,调度程序有时会在一台计算机上运行所有pod,有时在不同的计算机上运行。

如果我的节点发生故障并且我的所有pod都在同一个节点上运行会怎样?我们需要避免这种情况。

如何强制调度程序以循环方式在节点上运行pod,这样如果任何节点出现故障,那么至少有一个节点应该有NGINX pod处于运行模式。

这可能与否?如果可能,我们如何实现这种情况?

5 个答案:

答案 0 :(得分:4)

我认为pod-pod反亲和功能会对你有所帮助。 Intra-pod反关联允许您根据节点上已运行的pod上的标签来约束您的pod可以调度的节点。这是一个例子。

Host: SomeName.000webhostapp.com

注意:我在这里使用 preferredDuringSchedulingIgnoredDuringExecution ,因为您有比节点更多的广告连播。

有关更多详细信息,请参阅以下链接中的 插播广告联盟关联和反关联(测试版功能) 部分: "Using data attributes"

答案 1 :(得分:4)

使用podAntiAfinity

参考:Kubernetes in Action Chapter 16. Advanced scheduling

带有 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:latest

Kubelet --max-pods

您可以在kubelet配置中指定节点的最大pod数,以便在节点关闭的情况下,它将阻止K8S使来自故障节点的pod的其他节点饱和。

答案 2 :(得分:2)

如果容器指定了所需内存和CPU数量的资源请求,则调度程序应该展开您的pod。看到 http://kubernetes.io/docs/user-guide/compute-resources/

答案 3 :(得分:2)

使用 Pod 拓扑扩展约束

从 2021 年开始,(v1.19 及更高版本)您可以默认使用 Pod Topology Spread Constraints topologySpreadConstraints,我发现它比 podAntiAfinity 更适合这种情况。

主要区别在于 Anti-affinity 只能限制每个节点一个 pod,而 Pod Topology Spread Constraints 可以限制每个节点 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-895an official blog post.

答案 4 :(得分:0)

我们可以使用 Taint 或 toleration 来避免将 Pod 部署到节点中或不部署到节点中。


Tolerations are applied to pods, and allow (but do not require) the pods to schedule onto nodes with matching taints.

Taints and tolerations work together to ensure that pods are not scheduled onto inappropriate nodes. One or more taints are applied to a node; this marks that the node should not accept any pods that do not tolerate the taints.

示例部署 yaml 将类似于

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:
      containers:
      - name: nginx-service
        image: nginx:latest
      tolerations:
      - key: "key1"
        operator: "Equal"
        value: "value1"
        effect: "NoSchedule"

您可以在 https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/#:~:text=Node%20affinity%2C%20is%20a%20property,onto%20nodes%20with%20matching%20taints 找到更多信息。

相关问题