Kubernetes : use node labels in yaml deployment

7/9/2018

I have a quick question regarding kubernetes yaml files :

can I reference a node label value in a pvc, using a node label as variable?

Something like :

  volumes:
    - name: data-storage
      persistentVolumeClaim:
        claimName: main-dev-pvc-${node.failure-domain.beta.kubernetes.io/zone}

Is it possible?

-- Guillaume
kubernetes

2 Answers

7/10/2018

You cannot use variables in Kubernetes yaml files by default.

But there are some tools which have such functionality, helm, for example. Or you can use automation tool like Ansible for creating Kubernetes resources with some parameters according to some rules.

-- Artem Golenyaev
Source: StackOverflow

7/10/2018

Is the idea to force the use of PV created in a specific zone? StorageClass can help with that.

Custom StorageClass:

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: ssd-usc1a
provisioner: kubernetes.io/gce-pd
parameters:
  type: pd-ssd
  zones: us-central1-a

PVC that refers to the custom storage class:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: main-dev-pvc-ssd-usc1a
  namespace: dev-project
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  storageClassName: ssd-usc1a

Assuming the same namespace as for the PVC:

  volumes:
    - name: data-storage
      persistentVolumeClaim:
        claimName: main-dev-pvc-ssd-usc1a

Kubernetes will try to schedule the pod on a node in the same zone where the PV is (us-central1-a in the example above). Quote from the docs: "...The scheduler (via the VolumeZonePredicate predicate) will also ensure that pods that claim a given volume are only placed into the same zone as that volume, as volumes cannot be attached across zones". Therefore, the scheduling will fail if there isn't a such suitable node.

-- apisim
Source: StackOverflow