0

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?

Ken Tsoi
  • 1,195
  • 1
  • 17
  • 37
  • 3
    Are you aware of [inter-pod affinity](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#inter-pod-affinity-and-anti-affinity)? – Chin Huang Dec 15 '20 at 22:01
  • Thank you @ChinHuang for the enlightenment, I tried to implement that but have encounter some problems, can you check my new question: https://stackoverflow.com/questions/65328278/kubernetes-podaffinity-not-able-to-deploy-pods – Ken Tsoi Dec 16 '20 at 17:36

2 Answers2

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.

Ken Tsoi
  • 1,195
  • 1
  • 17
  • 37
0

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

Matt
  • 7,419
  • 1
  • 11
  • 22
  • Thanks @matt for the input, this sounds reasonable, and can be one of the options. But I will try the inter-pod affinity first though – Ken Tsoi Dec 16 '20 at 14:58
  • Sure, just wanted to cover all options :D – Matt Dec 16 '20 at 16:02
  • Actually to deploy the three different containers in different projects together in a pod is not going to be easy, haven't thought of a way to handle the CI and deployment script. – Ken Tsoi Dec 16 '20 at 16:08