Kubernetes Mounting Hostpath to specific location within container

3/29/2018

I'm trying to translate a bunch of docker-compose files into kubernetes yamls. I have used kompose, which has gotten me part way, but I'm getting stuck on one particular part for multiple containers.

This is one of the containers. Notice the docker container is mounting /u/data/. . . to /var/lib/mysql. This is actually necessary as the mysql directory contains the database and configurations.

server1-backend-mysql:
image: mysql
container_name: server-backend-mysql
restart: always
volumes:
   - /u/data/server-backend-mysql:/var/lib/mysql
networks:
  - eolnet

What is the correct way to make this happen in Kubernetes? Note that for k8 I will be mounting an nfs volume (this is only for testing purposes).

I did look into hostpath, but so far no luck.

-- flyingcars34
docker
docker-compose
kubernetes
linux

1 Answer

3/29/2018

When declaring a Pod, specify the volume at spec.volumes, and then the volume mount at spec.containers[*].volumeMounts:

apiVersion: v1
kind: Pod
metadata:
  name: server1-backend-mysql
spec:
  containers:
  - image: mysql
    name: mysql
    volumeMounts:
    - mountPath: /var/lib/mysql
      name: mysql-data
  volumes:
  - name: mysql-data
    hostPath:
      path: /u/data/...
      type: Directory

When declaring a Deployment or StatefulSet (which you should do instead of declaring a Pod), move the respective configurations to spec.template.spec.volumes and spec.template.spec.containers[*].volumeMounts. For more information, have a look at the documentation.


As a side note unrelated to your question: if you're planning to run MySQL from a NFS volume, keep in mind that running MySQL from NFS is possible, but not something that MySQL is really optimized for. Be sure to configure your MySQL server accordingly, and check if your environment permits you to use a networked block device (and not a network file system) like a Ceph RBD volume or similar.

-- helmbert
Source: StackOverflow