emptyDir

emptyDir is the simplest volume type, which will create an empty volume for containers in the same Pod to share. When the Pod is removed, the files in emptyDir will be erased, as well. emptyDir is created when a Pod is created. In the following configuration file, we'll create a Pod running Ubuntu with commands to sleep for 3600 seconds. As you can see, one volume is defined in the volumes section with name data, and the volumes will be mounted under the /data-mount path in the Ubuntu container:

// configuration file of emptyDir volume
# cat 2-6-1_emptyDir.yaml
apiVersion: v1
kind: Pod
metadata:
name: ubuntu
labels:
name: ubuntu
spec:
containers:
- image: ubuntu
command:
- sleep
- "3600"
imagePullPolicy: IfNotPresent
name: ubuntu
volumeMounts:
- mountPath: /data-mount
name: data
volumes:
- name: data
emptyDir: {}

// create pod by configuration file emptyDir.yaml
# kubectl create -f 2-6-1_emptyDir.yaml
pod "ubuntu" created
Check which node the Pod is running on
By using the kubectl describe pod <Pod name> | grep Node command, you can check which node the Pod is running on.

After the Pod is running, you can use docker inspect <container ID> on the target node and you can see the detailed mount points inside your container:

  "Mounts": [
...
{
"Type": "bind",
"Source": "/var/lib/kubelet/pods/98c7c676-e9bd-11e7-9e8d-080027ac331c/volumes/kubernetes.io~empty-dir/data",
"Destination": "/data-mount",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
...
]

Kubernetes mounts /var/lib/kubelet/pods/<id>/volumes/kubernetes.io~empty-dir/<volumeMount name> to /data-mount for the Pod to use. If you create a Pod with more than one container, all of them will mount the same destination /data-mount with the same source. The default mount propagation is rprivate, which means any mount points on the host are invisible in the container, and vice versa.

emptyDir could be mounted as tmpfs by setting emptyDir.medium as Memory.

Taking the previous configuration file 2-6-1_emptyDir_mem.yaml as an example, it would be as follows:

volumes:
-
name: data
emptyDir:
medium: Memory

We could verify whether it's successfully mounted with the kubectl exec <pod_name> <commands> command. We'll run the df command in this container:

# kubectl exec ubuntu df
Filesystem 1K-blocks Used Available Use% Mounted on
...
tmpfs 1024036 0 1024036 0% /data-mount
...

Note that tmpfs is stored in memory instead of in the filesystem. No file will be created, and it'll be flushed in every reboot. In addition, it is constrained by memory limits in Kubernetes. For more information about container resource constraint, refer to Working with Namespace in this chapter.

If you have more than one container inside a Pod, the Kubectl exec command will be kubectl exec <pod_name> <container_name> <commands>.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.116.51