Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes, VolumeMounts a file, not a directory

Tags:

kubernetes

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
like image 614
user84592 Avatar asked Nov 27 '18 01:11

user84592


People also ask

Where do volumes Mount in Kubernetes?

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.

What is the difference between a Kubernetes Volume and a volume driver?

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.

Where are emptydir volumes stored in Kubernetes?

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.

What is Kubernetes Volume abstraction?

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.


2 Answers

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
like image 55
Artem Golenyaev Avatar answered Nov 15 '22 08:11

Artem Golenyaev


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

like image 28
Prafull Ladha Avatar answered Nov 15 '22 09:11

Prafull Ladha