Ask Your Question
2

Can a volume be mounted for particular nodes that have specific labels in a Kubernetes environment with multiple clusters?

asked 2021-11-20 11:00:00 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-04-11 15:00:00 +0000

david gravatar image

Yes, it is possible to mount a volume for specific nodes that have specific labels in a Kubernetes environment with multiple clusters. This can be achieved by using nodeSelector or nodeAffinity in the respective Pod or Deployment manifest.

With nodeSelector, the Pod or Deployment can be configured to run only on nodes that have a specific label using a key-value pair. Here is an example:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
    volumeMounts:
    - name: my-volume
      mountPath: /data
  volumes:
  - name: my-volume
    hostPath:
      path: /mnt/data
nodeSelector:
  env: development

In the above example, the Pod will only run on nodes that have the label 'env: development'. The volume my-volume will also be mounted on those nodes.

Similarly, with nodeAffinity, a Pod or Deployment can be configured to run on nodes that meet certain requirements. For example, to run the Pod on nodes that have a specific label and are running in a certain availability zone, the following manifest can be used:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
    volumeMounts:
    - name: my-volume
      mountPath: /data
  volumes:
  - name: my-volume
    hostPath:
      path: /mnt/data
affinity:
  nodeAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      nodeSelectorTerms:
      - matchExpressions:
        - key: env
          operator: In
          values:
          - development
    preferredDuringSchedulingIgnoredDuringExecution:
    - weight: 1
      preference:
        matchExpressions:
        - key: topology.kubernetes.io/zone
          operator: In
          values:
          - us-west1-a

In the above example, the Pod will only run on nodes that have the label 'env: development' and are in the us-west1-a availability zone. The volume my-volume will also be mounted on those nodes.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-11-20 11:00:00 +0000

Seen: 15 times

Last updated: Apr 11 '21