Use ConfigMap to populate mountPath in k8s deployment.yml

1/25/2019

I have a docker container that maps a user's local directory so that it becomes a persistent volume. The directory path is customizable via an environment variable "MYSQL_DATA_FOLDER".

This works fine when running the docker container locally, however, when launching the container through minikube/kubernetes, this approach no longer works

When starting this container using only docker, I would use this command:

docker-compose run --service-ports --volume=$MYSQL_DATA_FOLDER:/var/lib/mysql --name db --rm database

Here's the docker-compose.yml file

version: '3.5'
services:
  database:
    image: mysql:5.7
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: ""
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
    volumes:
      - mydatavolume:${MYSQL_DATA_FOLDER}
    build:
      context: .
      dockerfile: Dockerfile

volumes:
  mydatavolume:

networks:
  default:
    name: dev_network
    driver: bridge

Here's the environment variable file (env-variables.env):

MYSQL_ALLOW_EMPTY_PASSWORD=yes
MYSQL_ROOT_PASSWORD=
MYSQL_DATA_FOLDER=/Users/foo/mysql/data

I created a ConfigMap with kubernetes using this command:

kubectl create configmap db-env --from-env-file=env-variables.env 

Here is the deployment.yml file:

apiVersion: v1
kind: Service
metadata:
  name: db-mysql
  labels:
    app: db
spec:
  ports:
    - port: 3306
  selector:
    app: db
    tier: mysql
  clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  labels:
    app: db
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
apiVersion: apps/v1 
kind: Deployment
metadata:
  name: db-mysql
  labels:
    app: db
spec:
  selector:
    matchLabels:
      app: db
      tier: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: db
        tier: mysql
    spec:
      containers:
      - image: mysql
        name: mysql
        envFrom:
          - configMapRef:
              name: db-env
        livenessProbe:
          tcpSocket:
            port: 3306
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: ${MYSQL_DATA_FOLDER}
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

What I want to do is somehow populate the mountPath value using the environment variable MYSQL_DATA_FOLDER so it can be user configurable.

I want to be able to "git pull" and go without having to do any post/pre-processing on the files.

Thanks for any clues

-- slashdottir
configuration
docker
environment-variables
kubernetes

1 Answer

1/25/2019

You can do it like below:

source env.file && cat deployment.yml  | sed "s/{{MYSQL_DATA_FOLDER}}/$MYSQL_DATA_FOLDER/g" | kubectl apply -f -

And your yaml file variable like:

...
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: {{MYSQL_DATA_FOLDER}}
...
-- coolinuxoid
Source: StackOverflow