148422597X Kubernetes Management Design Patterns [Vohra 2017-01-29] {E559F6BB}

marco.cirilli
from marco.cirilli More from this publisher
19.06.2021 Views

CHAPTER 6Using VolumesKubernetes pods are invariably associated with data, and the data can either be made integral to a Dockercontainer via its Docker image or decoupled from the Docker container.ProblemIf data (in on-disk files) is made integral to a Docker container, the following issues could result:Solution• The data is not persistent. The data is removed when a Docker container is restarted,which could also be due to a container crash.• The data is container-specific and cannot be shared with other containers as such.One of the principles of modular design is the Single Responsibility Principle (SRP). Kubernetes volumesimplement the SRP by decoupling the data from a container. A volume is just a directory with or withoutdata on some medium, which is different for different volume types. A volume is specified in a pod’s specand shared across different containers in the pod. A volume must be mounted in each container in a pod’sspec independently, although it may be mounted in the different containers at the same (or different)file/directory path. A container in a pod has access to the filesystem inherited from its Docker image andthe filesystem from a Kubernetes volume. A Docker image’s filesystem is still at the root of the filesystemhierarchy, and a volume can only be mounted on a directory path within the root filesystem. Since volumesprovide access to data outside a pod, volumes mounted in different pods are able to share the same datafrom the host or other external storage such as AWS EBS or a GitHub repository. Two types of volumeabstractions or plugins are available: Volume and PersistentVolume. While a Volume is coupled with a pod, aPersistentVolume is provisioned on the networked cluster and is independent of a pod. Figure 6-1 shows anexample of using an Amazon EBS Volume in a pod.© Deepak Vohra 2017D. Vohra, Kubernetes Management Design Patterns, DOI 10.1007/978-1-4842-2598-1_6135

Chapter 6 ■ Using VolumesPodDockerContainersAWSEBSVolumeVolume IDvol-62a59dc8volumeMounts:mountPath:/aws-ebs1volumeMounts:mountPath:/aws-ebs2spec:volumes:/awsElastic BlockStore:volumeID:”aws://us-east-1c/vol-62a59dc8”volumeMounts:mountPath:/aws-ebs1Figure 6-1. Using a volume in a podOverviewKubernetes volumes are storage units associated with pods. A volume can be shared by pod replicas ordedicated to a single pod. Several types of volumes are supported for different types of storage, such asAWS volume, GitHub repository, or directory on the host, to list a few. The different types of volumes aredescribed in Table 6-1.Table 6-1. Types of VolumesVolume TypeemptyDirhostPathgcePersistentDiskDescriptionA per-pod volume that is initially empty and shared by containers in a pod.Each container may mount the volume at the same or different path. Bydefault, the volume is stored on the medium backing the machine, whichcould be SSD or network storage. Alternatively, the medium could be set tomemory. When a pod is deleted the volume is deleted also, which meansthe volume is not persistent.Mounts a file or a directory form the host node’s file system into the pod.Writable by root only the volume data persists even if the pod is deleted. Allcontainers in the pod can access the volume. Designed for single node testonly and supported in a multi-node cluster.Mounts a Google Compute Engine Persistent Disk into a pod. The GCE PD’scontents are not deleted if a pod is deleted and the volume is unmounted.Supported only for nodes of type GCE VMs in the same GCE project.(continued)136

Chapter 6 ■ Using Volumes

Pod

Docker

Containers

AWS

EBS

Volume

Volume ID

vol-

62a59dc8

volumeMounts:

mountPath:

/aws-ebs1

volumeMounts:

mountPath:

/aws-ebs2

spec:

volumes:

/awsElastic BlockStore:

volumeID:”aws://us-

east-1c/vol-

62a59dc8”

volumeMounts:

mountPath:

/aws-ebs1

Figure 6-1. Using a volume in a pod

Overview

Kubernetes volumes are storage units associated with pods. A volume can be shared by pod replicas or

dedicated to a single pod. Several types of volumes are supported for different types of storage, such as

AWS volume, GitHub repository, or directory on the host, to list a few. The different types of volumes are

described in Table 6-1.

Table 6-1. Types of Volumes

Volume Type

emptyDir

hostPath

gcePersistentDisk

Description

A per-pod volume that is initially empty and shared by containers in a pod.

Each container may mount the volume at the same or different path. By

default, the volume is stored on the medium backing the machine, which

could be SSD or network storage. Alternatively, the medium could be set to

memory. When a pod is deleted the volume is deleted also, which means

the volume is not persistent.

Mounts a file or a directory form the host node’s file system into the pod.

Writable by root only the volume data persists even if the pod is deleted. All

containers in the pod can access the volume. Designed for single node test

only and supported in a multi-node cluster.

Mounts a Google Compute Engine Persistent Disk into a pod. The GCE PD’s

contents are not deleted if a pod is deleted and the volume is unmounted.

Supported only for nodes of type GCE VMs in the same GCE project.

(continued)

136

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!