Volumes are created by specifying information about the Volume in the .spec.volumes field inside a Pod manifest file. The following manifest snippet will create a Volume of type hostPath, using the parameters defined in the path and type properties.
hostPath is the Volume type most similar to a Docker Volume, where the Volume exists as a directory from the host node's filesystem:
apiVersion: v1
kind: Pod
spec:
...
volumes:
- name: host-volume
hostPath:
path: /data
type: Directory
This Volume will now be available to all containers within the Pod. However, the Volume is not automatically mounted onto each container. This is done by design because not all containers may need to use the Volume; it allows the configuration to be explicit rather than implicit.
To mount the Volume to a container, specify the volumeMounts option in the container's specification:
apiVersion: v1
kind: Pod
spec:
containers:
- name: elasticsearch
image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.2.4
ports:
- containerPort: 9200
- containerPort: 9300
env:
- name: discovery.type
value: single-node
volumeMounts:
- mountPath: /usr/share/elasticsearch/data
name: host-volume
...
The mountPath specifies the directory inside the container where the Volume should be mounted at.
To run this Pod, you first need to create a /data directory on your host machine and change its ownership to having a UID and GID of 1000:
$ sudo mkdir data
$ sudo chown 1000:1000 /data
Now, when we run this Pod, you should be able to query it on <pod-ip>:9200 and see the content written to the /data directory:
$ tree /data
data/
└── nodes
└── 0
├── node.lock
└── _state
├── global-0.st
└── node-0.st
3 directories, 3 files