Kubernetes: How to config a group of pods to be deployed on the same node?

半腔热情 提交于 2021-01-01 10:04:13

问题


The use case is like this:

So we have several pods using the same persistentVolumeClaim with the accessMode set to ReadWriteOnce (because the storage class of the PersistentVolume only support ReadWriteOnce).

From https://kubernetes.io/docs/concepts/storage/persistent-volumes/,

ReadWriteOnce -- the volume can be mounted as read-write by a single node

So these pods should be deployed on the same node in order to access the PVC (otherwise they will fail).

I would like to ask if there are any ways to config the deployment yaml file so that they can be deployed on the same node? or are there any ways to solve this problem?


回答1:


With the inter-pod affinity solution as suggested by Chin, I was able to solve the problem:

The following is my Deployment yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-go
  namespace: stage
  labels:
    app: test-go
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test-go
  template:
    metadata:
      labels:
        app: test-go
        service: git
    spec:
      affinity:
        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: service
                operator: In
                values:
                - git
            topologyKey: kubernetes.io/hostname
      containers:
      - name: test-go
        image: registry.gitlab.com/xxxxxxx/test-go
      imagePullSecrets:
        - name: registry-pull-secret

In the Deployment yaml file, set a label in the pod template spec.template.metadata.labels, and then add podAffinity config based on the label added, set topologyKey to kubernetes.io/hostname so that the pods will be deployed on the same node.




回答2:


Chin aready mentioned inter-pod affinity and this is a valid solution, but I would like to mention one more solution, maybe a little bit controversial but still valid in same cases.

If you have to have all containers running on one node, you can just put all of these conatiners in one pod and they will always go together, and what is most important, you can mount persistent volumes to it without any troubles. Just remember that overdoing it is considered bad practice; always prefer one container per pod over multiple containers per pod, although your usecase may be na exception to that rule, but its hard for me to say since I dont know anything about these pods.

Read some more about How pods manage multiple container



来源:https://stackoverflow.com/questions/65313780/kubernetes-how-to-config-a-group-of-pods-to-be-deployed-on-the-same-node

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