Kubernetes PodAffinity not able to deploy pods

折月煮酒 提交于 2021-01-07 06:57:10

问题


So I have this problem, and try to implement podAffinity to solve it.

I have 3 nodes and want to deploy 2 pods on the same node. In the Deployment YAML files I have service:git under metadata.labels, and the following is the affinity setting:

  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: service
            operator: In
            values:
            - git
        topologyKey: kubernetes.io/hostname

But the pods failed to deploy, I got the following error:

0/3 nodes are available: 3 node(s) didn't match pod affinity rules, 3 node(s) didn't match pod affinity/anti-affinity.

Are there any problems with my configuration?

If not, I guess maybe it is because when the first pod is deployed, the system will try to find a node that contains a pod with the label service: git and fail (because it is the first one), and another pod also fail because of the same reason. Is this correct?

But then how to solve the problem (without resorting to workarounds)?


回答1:


  • You are using "requiredDuringSchedulingIgnoredDuringExecution:" so it will be looking a "running(already)" pod which has a label "service: git" and itseems you do not already have any pod with that label. so following a is a quick workaround where a test pod will be created with label "service: git" . so that podAffinity rule will find a destination node ( that would be the node where this testpod will be running)

kubectl run testpod --image=busybox --labels="service=git" -- sleep infinite

  • Once above pod is UP .. all the pods in your deployment also should get created. if not delete the deployment and re-apply it.

  • If you need a elegant solution then you can consider using "preferredDuringSchedulingIgnoredDuringExecution" instead of "requiredDuringSchedulingIgnoredDuringExecution"



来源:https://stackoverflow.com/questions/65328278/kubernetes-podaffinity-not-able-to-deploy-pods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!