How to create nodeAffinity on PVC

Hello, one of the mock exam exercises asks to create a PVC with nodeAffinity.
Unfortunately the k8s docs do not help much with this, other than saying I can user nodeAfffinity.required. But what after that, how is that info inserted into the yaml?
I cannot get the yaml structure to add the nodeAffinity to the PVC.Google is of no use either. Can anyone please give me a hint on this?

Also, please let me know if the hostPath is fine too, as I could not find any concrete example of how that is typed into a yaml.

Thanks a lot in advance.

Hi @Mundofik

Can you please share the link to the lab for this?

kubectl explain can help you with nodeAffinity and your usage of hostPath is fine.

Hope this helps

Regards.

Right, I always forget about kubectl explain! Thanks @Santosh_KodeKloud

I managed to make it work with this yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: orange-pv-cka07-st
spec:  
  capacity:
    storage: 150Mi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: orange-stc-cka07-str
  hostPath:
    path: /opt/orange-data-cka07-str
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values: ["cluster1-controlplane"]
1 Like