5

CKAD Part17 - Volumes, PersistentVolumes & PersistentVolumeClaims - briansde...

 3 years ago
source link: https://www.briansdevblog.com/2021/07/ckad-part17-volumes-persistentvolumes-persistentvolumeclaims/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Kubernetes Volumes

Internal container storage is ephemeral, which means that it exits only for the lifetime of the container. When the container is stopped any data written inside the container is lost. Volumes provide a means of storing data beyond the life of the container.

The Pod definition below contains an

emptyDir
emptyDir
Volume
Volume  An
emptyDir
emptyDir is an empty directory that is created when a Pod is assigned to a Node. All containers in a Pod can read and write in the
emptyDir
emptyDir
Volume
Volume which makes useful for multi container Pod use cases.
apiVersion: v1
kind: Pod
metadata:
name: pod-volume-1
spec:
containers:
- name: volume-container-1
image: busybox
command: ['sh', '-c', 'while true; do sleep 3600; done']
imagePullPolicy: IfNotPresent
volumeMounts:
- name: demo-volume # matches volume[x].name
mountPath: /tmp/storage # location inside container where storage is mounted
- name: volume-container-2
image: busybox
command: ['sh', '-c', 'while true; do sleep 3600; done']
imagePullPolicy: IfNotPresent
volumeMounts:
- name: demo-volume
mountPath: /tmp/storage
volumes:
- name: demo-volume
emptyDir: {} # creates an empty directory on node when Pod is created
apiVersion: v1
kind: Pod
metadata:
  name: pod-volume-1
spec:
  containers:
    - name: volume-container-1
      image: busybox
      command: ['sh', '-c', 'while true; do sleep 3600; done']
      imagePullPolicy: IfNotPresent
      volumeMounts:
        - name: demo-volume # matches volume[x].name
          mountPath: /tmp/storage # location inside container where storage is mounted
    - name: volume-container-2
      image: busybox
      command: ['sh', '-c', 'while true; do sleep 3600; done']
      imagePullPolicy: IfNotPresent
      volumeMounts:
        - name: demo-volume
          mountPath: /tmp/storage
  volumes:
    - name: demo-volume
      emptyDir: {} # creates an empty directory on node when Pod is created

The name attribute in

volumeMounts
volumeMounts must match name in the
volumes
volumes definition.

The screenshot above shows that we can exec into

volume-container-1
volume-container-1 and create a file in the
VolumeMount
VolumeMount. We can then exit
volume-container-1
volume-container-1 and exec into
volume-container-2
volume-container-2 and read the same file. This proves that the file exists on the
volumeMount
volumeMount and is accessible to all containers in the Pod.

State Persistence

There may be circumstances where you want to retain state beyond the life of a container or Pod. To do this you’ll need to store data in some kind of long term persistence storage outside of the container. Kubernetes allows you to do this using

PersistentVolumes
PersistentVolumes and
PersistentVolumeClaims
PersistentVolumeClaims.
  • PersistentVolume
    PersistentVolume or PV is a storage resource
  • PersistentVolumeClaim
    PersistentVolumeClaim or PVC is a an abstraction between the storage resource and the Pod.
    PersistentVolumeClaims
    PersistentVolumeClaims will automatically bind themselves to a
    PeristentVolume
    PeristentVolume that has a compatible
    StorageClass
    StorageClass and
    AccessMode
    AccessMode.

Persistence-Volumes-PersistenceVolumeClaims.png

Defining a PersistentVolume

A sample

PersistentVolume
PersistentVolume is defined below.
apiVersion: v1
kind: PeristeneVolume
metadata:
name: demo-pv
spec:
storageClassName: local-storage
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
apiVersion: v1
kind: PeristeneVolume
metadata:
  name: demo-pv
spec:
  storageClassName: local-storage
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
storageClassName
storageClassName defines which
PersistentVolumeClaim
PersistentVolumeClaim will be able to bind to this
PersistentVolume
PersistentVolume.
capacity
capacity defines the amount of storage made available
accessMode
accessMode specifies what read/write modes can be used to access the volume.
ReadWriteOnce
ReadWriteOnce means this
PersistentVolume
PersistentVolume can be read from and written to by one Pod at a time.
hostPath
hostPath – is a
PersistentVolume
PersistentVolume type specifically for development and testing on a single node. It uses a file or directory on the Node to emulate network attached storage.

You wouldn’t use

hostPath
hostPath in production. Instead you’d hook into some kind of cloud based storage, like an Amazon EBS Volume.

Defining a PersistentVolumeClaim

The sample

PersistentVolumeClaim
PersistentVolumeClaim below references the
PersistnetVolume
PersistnetVolume defined above.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: demo-pvc
spec:
storageClassName: local-storage
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 512Mi
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: demo-pvc
spec:
  storageClassName: local-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 512Mi
storageClassName
storageClassName is used to bind to the
PersistentVolume
PersistentVolume we created above, which has a matching
storageClass
storageClass
local-storage
local-storage.
accessModes
accessModes specifies what read/write modes will be used to access the volume.
resource.requests.storage
resource.requests.storage – defines how much storage the
PersistentVolumeClaim
PersistentVolumeClaim needs, in this instance
512Mi
512Mi is half the total amount available in the
PersistentVolume
PersistentVolume we defined earlier.

After creating the

PersistentVolume
PersistentVolume and
PersistentVolumeClaim
PersistentVolumeClaim we can see that the Status of both is  
Bound
Bound.

Using a PersistentVolumeClaim

The Pod definition below uses a

PersistentVolumeClaim
PersistentVolumeClaim to reference the
PersistentVolume
PersistentVolume we created above.
apiVersion: v1
kind: Pod
metadata:
name: demo-pvc-pod
spec:
containers:
- name: pvc-container
image: busybox
imagePullPolicy: IfNotPresent
command: ['/bin/sh', '-c', 'while true; do sleep 3600; done']
volumeMounts:
- name: demo-pvc
mountPath: 'tmp/pvc-data'
volumes:
- name: demo-pvc
persistentVolumeClaim:
claimName: demo-pvc
apiVersion: v1
kind: Pod
metadata:
  name: demo-pvc-pod
spec:
  containers:
    - name: pvc-container
      image: busybox
      imagePullPolicy: IfNotPresent
      command: ['/bin/sh', '-c', 'while true; do sleep 3600; done']
      volumeMounts:
        - name: demo-pvc
          mountPath: 'tmp/pvc-data'
  volumes:
    - name: demo-pvc
      persistentVolumeClaim:
        claimName: demo-pvc

We define a

Volume
Volume and specify
persistentVolumeClaim.claimName
persistentVolumeClaim.claimName to reference the
PersistentVolumeClaim
PersistentVolumeClaim created earlier. The
Volume
Volume is mounted to the container the same way any other
Volume
Volume is mounted, by specifying the
Volume
Volume name and a mount path inside the container.

To prove that the

PersistentVolume
PersistentVolume persists data beyond the life of a Pod, we can do the following.
  1. Create
    demo-pvc-pod
    demo-pvc-pod defined above
  2. Exec into the Pods container and create a file call hello.txt in
    /tmp/pvc-data
    /tmp/pvc-data
  3. Exit the container and delete the Pod
  4. Create a new
    demo-pvc-pod
    demo-pvc-pod and exec into its container.
  5. The hello.txt file you created for the first Pod should still be visible.

The sample code for these notes is available on Github.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK