In Docker world, docker volumes provide a way to store container data into the host machine as permanent storage. However, it is less managed and limited for multi-node cluster.
Kubernetes volumes are not top-level object as like Pod, Deployment etc., however these are component of a pod and defined as part of pod YAML specification. K8 Volumes are available to all containers in the pod and must be mounted in each container specific file location.
Kubernetes supports many types of volumes like,
- emptyDir: Used for mounting temporary empty directory from worker node Disk/RAM
- awsElasticBlockStore: Used for mounting AWS EBS volume into the pod
- azureDisk: Used for mounting Microsoft Azure data disk into the pod
- azureFile: Used for mounting Microsoft Azure File volume into the pod
- gcePersistentDisk: Used for mounting Google PD into the pod
- hostPath: Used for mounting Worker node filesystem into the pod
- nfs: Used for mounting existing NFS (Network file system) into the pod
- configMap/secret: Used for mounting these values into the pod
- persistentVolumeClaim: Used for mounting dynamically provisioned storage into the pod
A Pod can use any number of volume types simultaneously to persist container data.
emptyDir volume
An empty directory created when a Pod is assigned to a node and remains active until pod is running. All containers in the pod can read/write the contents to the emptyDir volume. An emptyDir volume will be erased automatically once the pod is terminated from the node.
- It is useful for sharing files between containers which are running in the same pod
- It is useful for doing disk-based merge sort on large dataset where memory is low
- It is useful when container filesystem is read-only and wants to write data temporarily.
apiVersion: v1
kind: Pod
metadata:
name: volume-emptydir
spec:
containers:
- name: alpine
image: alpine
command:
[
"sh",
"-c",
'mkdir var/mydoc; while true; do echo "random message text `date`" >> var/mydoc/index.html;sleep 10;done',
]
volumeMounts:
- name: vol-emptydir
mountPath: /var/mydoc
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
protocol: TCP
volumeMounts:
- name: vol-emptydir
mountPath: /usr/share/nginx/html
volumes:
- name: vol-emptydir
emptyDir: {}
- A multi-container pod gets created with volume type “emptyDir” named as “vol-emptydir”
- “vol-emptydir” volume gets created automatically when a pod is assigned to a worker-node.
- As name says, volume contains empty files/directories at initial stage.
- First “alpine” container creates random text message for every 10 seconds and appends to /var/mydoc/index.html file.
- First “alpine” container mounted a volume at ‘/var/mydoc’. So, all the files under this directory copied into volume (i.e., index.html file).
- Second “nginx” container mounted a same volume at ‘/usr/share/nginx/html’ (this is the default directory for nginx to serve index.html file ). As we mounted same volume which has “index.html”, nginx web server serves the file (i.e., index.html) which is created by the first container.
- As first container adds new random message to index.html file for every 10 seconds, we see different message each time when we request index.html from nginx webserver.
- Volume and its contents get deleted automatically when the Pod is deleted
- By default, Volume contents get stored on the worker node disk. However, “emptyDir” volume contents can be stored into the memory (RAM) by setting “medium” attribute
// create pod
$ kubectl apply -f pod-vol-emptydir.yaml
pod/pod-vol-emptydir created
// display pods
$ kubectl get po
NAME READY STATUS RESTARTS AGE
pod-vol-emptydir 2/2 Running 0 2m39s
// syntax
// kubectl port-forward <pod-name> <local-port>:<container-port>
$ kubectl port-forward pod-vol-emptydir 8081:80
Forwarding from 127.0.0.1:8081 -> 80
Forwarding from [::1]:8081 -> 80
run the following curl command to check random messages which are appending after every 10 seconds
$ curl http://localhost:8081
random message text Tue Nov 2 15:48:50 UTC 2021
$ curl http://localhost:8081
random message text Tue Nov 2 15:48:50 UTC 2021
random message text Tue Nov 2 15:49:00 UTC 2021
random message text Tue Nov 2 15:49:10 UTC 2021
An emptyDir volume does not persist data after pod termination. So, delete the Pod and recreate all above steps to check any existing data is printing while doing curl command
// delete pod
$ kubectl delete pod/pod-vol-emptydir
pod/pod-vol-emptydir deleted
// create pod
$ kubectl apply -f pod-vol-emptydir.yaml
pod/pod-vol-emptydir created
// display pods
$ kubectl get po
NAME READY STATUS RESTARTS AGE
pod-vol-emptydir 2/2 Running 0 1m10s
// syntax
// kubectl port-forward <pod-name> <local-port>:<container-port>
$ kubectl port-forward pod-vol-emptydir 8081:80
Forwarding from 127.0.0.1:8081 -> 80
Forwarding from [::1]:8081 -> 80
$ curl http://localhost:8081
random message text Tue Nov 2 16:01:50 UTC 2021
It is confirmed that only new data is printing after pod recreation when using emptyDir volume.
By default, Volume contents get stored on the worker node disk. However, “emptyDir” volume contents can be stored into the memory (RAM) by setting “medium” attribute
volumes:
- name: vol-emptydir
emptyDir:
medium: Memory
Kubernetes for Developers Journey.
- Kubernetes for Developers #25: PersistentVolume and PersistentVolumeClaim in-detail
- Kubernetes for Developers #24: Kubernetes Volume hostPath in-detail
- Kubernetes for Developers #23: Kubernetes Volume emptyDir in-detail
- Kubernetes for Developers #22: Access to Multiple Clusters or Namespaces using kubectl and kubeconfig
- Kubernetes for Developers #21: Kubernetes Namespace in-detail
- Kubernetes for Developers #20: Create Automated Tasks using Jobs and CronJobs
- Kubernetes for Developers #19: Manage app credentials using Kubernetes Secrets
- Kubernetes for Developers #18: Manage app settings using Kubernetes ConfigMap
- Kubernetes for Developers #17: Expose service using Kubernetes Ingress
- Kubernetes for Developers #16: Kubernetes Service Types - ClusterIP, NodePort, LoadBalancer and ExternalName
- Kubernetes for Developers #15: Kubernetes Service YAML manifest in-detail
- Kubernetes for Developers #14: Kubernetes Deployment YAML manifest in-detail
- Kubernetes for Developers #13: Effective way of using K8 Readiness Probe
- Kubernetes for Developers #12: Effective way of using K8 Liveness Probe
- Kubernetes for Developers #11: Pod Organization using Labels
- Kubernetes for Developers #10: Kubernetes Pod YAML manifest in-detail
- Kubernetes for Developers #9: Kubernetes Pod Lifecycle
- Kubernetes for Developers #8: Kubernetes Object Name, Labels, Selectors and Namespace
- Kubernetes for Developers #7: Imperative vs. Declarative Kubernetes Objects
- Kubernetes for Developers #6: Kubernetes Objects
- Kubernetes for Developers #5: Kubernetes Web UI Dashboard
- Kubernetes for Developers #4: Enable kubectl bash autocompletion
- Kubernetes for Developers #3: kubectl CLI
- Kubernetes for Developers #2: Kubernetes for Local Development
- Kubernetes for Developers #1: Kubernetes Architecture and Features
Happy Coding :)
No comments:
Post a Comment