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
Volumes mount at the specified paths within the image. Volumes can not mount onto other volumes or have hard links to other volumes. Each Container in the Pod's configuration must independently specify where to mount each volume. Kubernetes supports several types of volumes.
Docker now provides volume drivers, but the functionality is very limited for now (e.g. as of Docker 1.7 only one volume driver is allowed per Container and there is no way to pass parameters to volumes). A Kubernetes volume, on the other hand, has an explicit lifetime - the same as the Pod that encloses it.
By default, emptyDir volumes are stored on whatever medium is backing the node - that might be disk or SSD or network storage, depending on your environment. However, you can set the emptyDir.medium field to "Memory" to tell Kubernetes to mount a tmpfs (RAM-backed filesystem) for you instead.
The kubelet restarts the container but with a clean state. A second problem occurs when sharing files between containers running together in a Pod . The Kubernetes volume abstraction solves both of these problems. Familiarity with Pods is suggested.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With