touati ahmed
touati ahmed

Reputation: 401

How to set the value of mountPath in an env variable in kubernetes?

I used a configMap to store the mount path value, but when i call in my pod it doesn't work it seems that the mountPath property can not be assigned to an env variable.

here is my code :

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
        env:
        - name: MOUNT_PATH
          valueFrom:
           configMapKeyRef:
            name: my-configMap
            key: MOUNT_PATH
        volumeMounts:
        - name: nginx-volume
          mountPath: $(MOUNT_PATH)
      volumes:
      - name: nginx-volume
        persistentVolumeClaim:
         claimName: my-pvc

     ---


apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configMap
  namespace: default
data:
  MOUNT_PATH: "/opt/somepath"

Upvotes: 4

Views: 12302

Answers (3)

Daniel Marques
Daniel Marques

Reputation: 1405

You can achieve what you want using subPath if you are using k8s 1.17 or higher.

    env:
    - name: MOUNT_PATH
      valueFrom:
        configMapKeyRef:
          name: myconfigmap
          key: MOUNT_PATH
    volumeMounts:
    - name: nginx-volume
      mountPath: /opt
      subPathExpr: $(MOUNT_PATH)
  volumes:
  - name: nginx-volume
    persistentVolumeClaim:
      claimName: my-pvc

  ---

  apiVersion: v1
  kind: ConfigMap
  metadata:
    name: myconfigmap
    namespace: default
  data:
    MOUNT_PATH: "somepath"

Result within the container:

# ls -l /opt
total 4
drwxrwxrwx    2 root     root          4096 Oct 29 14:58 somepath

Upvotes: 6

Mark Watney
Mark Watney

Reputation: 5950

I deployed it in my lab and It's not possible to dynamically define any parameter on a manifest while deploying something using kubectl.

When you define a env variable it will be accessible only after the pod get deployed and you're referencing it before it happens. So when you apply the manifest you created, there is no $(MOUNT_PATH) doesn't exist anywhere.

You can also achieve this programmatically by using a external tool as helm or kustomize as commented by @Burak Serdar.

When you do this what goes to the container is the information that it should mount the volume at literally $(MOUNT_PATH).

# df -h | grep MOUNT
tmpfs           3.3G  454M  2.9G  14% /$(MOUNT_PATH)

Using Helm you can define your mountPath on your values.yaml and use this variable in your manifest.

Few useful links regarding helm:

Helm Quickstart Guide An Introduction to Helm, the Package Manager for Kubernetes Using Helm and Kustomize to Build More Declarative Kubernetes Workloads

Upvotes: 0

Joshua Oliphant
Joshua Oliphant

Reputation: 932

I suggest checking the restrictions list in this doc for using ConfigMaps with Pods: https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#restrictions

A couple potential problems come to mind.

The ConfigMap needs to be created first, so that it is available to the Pod, so make sure that is happening.

Also, I see that you're creating the ConfigMap in the default namespace, is that where you are running the Pod? They need to be in the same namespace.

Upvotes: 0

Related Questions