Kubernetes, VolumeMounts a file, not a directory

11/27/2018

I am going to use K8S to orchestrate docker containers. In k8s, I need to copy a file from host directory (/configs/nginx/cas-server.conf) to pod container directory(/etc/nginx/nginx.conf), but the current k8s only allows mount a directory, not to mount/copy a file. How to solve this problem?

Below is my nginx-cas-server-deply.yaml file.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-cas-server-depl
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-cas-server-pod
    spec:
      containers:
      - name: nginx-cas-server-pod
        image: nginx
        imagePullPolicy: Never
        ports:
          - containerPort: 100
        volumeMounts:
        - mountPath: /etc/nginx/nginx.conf
          name: nginx-cas-server-conf
        - mountPath: /app/cas-server/public
          name: nginx-cas-server-public
      volumes:
      - name: nginx-cas-server-conf
        hostPath:
          path: /configs/nginx/cas-server.conf
      - name: nginx-cas-server-public
        hostPath:
          path: /cas-server/public
-- user84592
kubernetes

3 Answers

11/27/2018

Kubernetes mounts the entire folder, so all the contents of the folder will be visible to your container.

Instead of copying the file you can create a symbolic link to the file /etc/nginx/nginx.conf which can point to /configs/nginx/cas-server.conf. But for doing this the ideal way is by updating the entrypoint of your docker image

Reference links https://www.cyberciti.biz/faq/creating-soft-link-or-symbolic-link/

-- unnik
Source: StackOverflow

11/27/2018

In a configuration for your Deployment, you need to use mountPath with directory and file names and subPath field with file name. Also, what is important, you need to have file on a Node named exactly as you want it to be mounted, therefore if you want to mount to /etc/nginx/nginx.conf, file should be named nginx.conf

Here is the example:

Content of the directory on the Node:

# ls /config/
nginx.conf  some_dir

Configuration file for Nginx Deployment

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    run: nginx
  name: nginx
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      run: nginx
  template:
    metadata:
      labels:
        run: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        volumeMounts:
        - mountPath: /etc/nginx/nginx.conf 
          name: test
          subPath: nginx.conf
      volumes:
      - hostPath:
          path: /config
        name: test
-- Artem Golenyaev
Source: StackOverflow

11/27/2018

You can mount file from host to pod using hostPath, I am doing it for my elasticsearch cluster where I want to mount my elasticsearch.yml file from host to pod.

You need to keep in mind that the file is mounted (not copied) and hence the change you made in one file reflect at both places. Please have a look at the following yaml file:

{
  "kind": "StatefulSet",
  "apiVersion": "apps/v1beta1",
  "metadata": {
    "name": "ES",
    "labels": {
      "state": "es"
    }
  },
  "spec": {
      "spec": {
        "containers": [
          {
            "name": "es",
            "image": "",
            "imagePullPolicy": "IfNotPresent",
            "command": [
              "/bin/sh",
              "-c"
            ],
            "volumeMounts": [
              {
                "mountPath":"/data/elasticsearch/conf/elasticsearch.yml",
                "name":"esconf"
              }
            ]
          }
        ],
        "volumes": [
          {
            "name": "esconf",
            "hostPath": {
              "path": "/prafull/data/md_elasticsearch.yml",
              "type": "FileOrCreate"
            }
          }
        ],
        "restartPolicy": "Always",
        "imagePullSecrets": [
          {
            "name": "gcr-imagepull-json-key"
          }
        ]
      }
    }
  }
}

Hope this helps

-- Prafull Ladha
Source: StackOverflow