Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to schedule pods to have affinity with a persistent volume claim?

I have a set of Kubernetes cronjobs that I want to run. The Persistent Volume Claim is ReadWriteOnce. There will be one job which updates the data on the volume, and the other jobs will read the data. The pvc can only be bound to one node at a time, but my understanding (this may be wrong) is that other pods can mount it read-only provided they are scheduled on the correct node. (Obviously only one pod can mount it read/write.)

I see that I can schedule two pods on the same node using podAffinity, but is there a way to use affinity to select the node that has the pvc attached? Or is it possible in GKE to do this? My claim looks like this:

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-persistent-volume
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 100Gi
  storageClassName: standard
like image 685
Luke Schlather Avatar asked Oct 15 '25 13:10

Luke Schlather


1 Answers

You should be able to do this using Pod Affinity with labels and a topologyKey set to kubernetes.io/hostname. Your affinity stanza would look something like:

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

which would schedule jobs with label app=mycronjob on the same node.

like image 53
Gari Singh Avatar answered Oct 17 '25 16:10

Gari Singh