This tutorial post will help you to understand kubernetes volumes and its usage with best examples. At the end of this article, you will be able to understand,
1. What is Kubernetes Volumes?
2. What are the types of Kubernetes Volumes?
3. How to use kubernetes volumes to pod and containers?
4. How to assign a single volume to specific container in a pod?
5. How to share a same volume to all containers within a pod?
6. How to assign a dedicated volumes to each container in a pod?
7. How to assign a shared volume across all pods running on different worker nodes?
In the previous posts, already we have explained the below topics related to Kubernetes. Refer those links to understand this topic from basics.
What is Kubernetes - Learn Kubernetes from Basics
How to Install Kubernetes on Linux (RedHat / CentOS)
How to Install Kubernetes On Ubuntu 16.04 LTS
How to Create Kubernetes Deployment, Services & Pods Using Kubectl
How to Create Kubernetes YAML for Deployment, Service & Pods
Kubernetes Volumes Explained with Examples
Kubernetes Persistent Volumes and Claims Explained
2. What are the types of Kubernetes Volumes?
3. How to use kubernetes volumes to pod and containers?
4. How to assign a single volume to specific container in a pod?
5. How to share a same volume to all containers within a pod?
6. How to assign a dedicated volumes to each container in a pod?
7. How to assign a shared volume across all pods running on different worker nodes?
In the previous posts, already we have explained the below topics related to Kubernetes. Refer those links to understand this topic from basics.
What is Kubernetes - Learn Kubernetes from Basics
How to Install Kubernetes on Linux (RedHat / CentOS)
How to Install Kubernetes On Ubuntu 16.04 LTS
How to Create Kubernetes Deployment, Services & Pods Using Kubectl
How to Create Kubernetes YAML for Deployment, Service & Pods
Kubernetes Volumes Explained with Examples
Kubernetes Persistent Volumes and Claims Explained
Kubernetes Volumes Explained with Examples
Lets get started.
What is Kubernetes Volumes?
Kubernetes Volumes are used to store data that should be accessible across all your containers running in a pod based on the requirement.What are the types of Kubernetes Volumes?
Kubernetes supports many kind of storage types, these are determined by how it is created and assigned to pods.Local Node Types - emptyDIR, hostpath, local
File Sharing types - nfs
Storage types - fc, iscsi
Special Purpose Types - Secret, Git repo
Cloud Provider types - Vsphere, Cinder, awsElasticBlockStore, azureDisk, gcepersistentDisk
Distributed filesystem types - glusterfs, cephfs
Special type - persistent volume, persistent volume claim
Note:
1. emptyDIR - Its a type of storage types that writes data only in memory till the pods running. So you data will be erased when the pod is deleted. So its not a persistent kind of types.
2. hostpath, local, fc and other types are persistent kind only, but volume wont be available across the nodes. it will be available only on local nodes. So we may need to setup something shared volume using traditional storage mount across all the nodes.
3. Persistent volume type volumes can be accessible across all the nodes.
Also You can Watch this Entire Tutorial video on our YouTube Channel.
How to use kubernetes volumes to pod and containers?
Use an option "Volumes" along with name and types as below in a deployment file for the entire PODS and use the "volumeMounts" along with mountPath where the volume to be mounted for the container. we must use the volume name unique and exactly as specified in specification for the containers. If not you will end up with error.
Example:
spec:
volumes:
- name: volume
hostPath:
path: /mnt/data
containers:
- name: container1-nginx
image: nginx
volumeMounts:
- name: volume
mountPath: "/var/nginx-data"
- name: container2-tomcat
image: tomcat
Above example tells that, volume name "volume" specified in "spec" section with Path "/mnt/data" will be used as a volume for this entire pod. It will be mounted only on container "container1-nginx" since it is claimed to be mounted on path "/var/nginx-data" using "volumeMounts" option.
How to assign single volume to specific container in a pod?
In order to use a volume only to specific container running in a pod, we must use volumemounts option. so that particular container will use the volume specified in spec.
kind: Deployment
apiVersion: apps/v1
metadata:
name: ebay-app
spec:
selector:
matchLabels:
environment: dev
app: ebay
replicas: 1
template:
metadata:
labels:
environment: dev
app: ebay
spec:
volumes:
- name: volume
hostPath:
path: /mnt/data
containers:
- name: container1-nginx
image: nginx
volumeMounts:
- name: volume
mountPath: "/var/nginx-data"
- name: container2-tomcat
image: tomcat
So we have claimed the volume name "volume" from specification and mapped to the container "container1-nginx" that would mount the volume under "/var/nginx-data", This volume will be only available to the first container "container1-nginx" not to the second container "container2-tomcat". This is how we can assign a single volume to specific container in a pod.
How to share same volume to all containers within a pod?
In order to share a same volume to all containers running in a pod, we must use volumemounts option in all containers.
kind: Deployment
apiVersion: apps/v1
metadata:
name: ebay-app
spec:
selector:
matchLabels:
environment: dev
app: ebay
replicas: 1
template:
metadata:
labels:
environment: dev
app: ebay
spec:
volumes:
- name: volume
hostPath:
path: /mnt/data
containers:
- name: container1-nginx
image: nginx
volumeMounts:
- name: volume
mountPath: "/var/nginx-data"
- name: container2-tomcat
image: tomcat
volumeMounts:
- name: volume
mountPath: "/var/tomcat-data"
This time, we have used volumeMount option for both containers with different path, as per the code definition, same volume "volume" will be mount on both containers in path "/var/nginx-data" on container1-nginx and "/var/tomcat-data" on container2-tomcat respectively.
How to assign dedicated volumes to each container in a pod?
In order to assign a dedicated volumes to each containers running in a pod, we must use volumes and volumemounts option in all containers accordingly as per the example given below.
kind: Deployment
apiVersion: apps/v1
metadata:
name: ebay-app
spec:
selector:
matchLabels:
environment: dev
app: ebay
replicas: 1
template:
metadata:
labels:
environment: dev
app: ebay
spec:
volumes:
- name: volume1
hostPath:
path: /mnt/data1
- name: volume2
hostPath:
path: /mnt/data2
containers:
- name: container1-nginx
image: nginx
volumeMounts:
- name: volume1
mountPath: "/var/nginx-data"
- name: container2-tomcat
image: tomcat
volumeMounts:
- name: volume2
mountPath: "/var/tomcat-data"
As per the above example, volume1 will be used by the first container "container1-nginx" and volume2 will be used by the second container "container2-tomcat". This is how we can assign dedicated volumes to each containers running in a pod.
How to assign shared volume across all pods running on different worker nodes?
Why do we actually need this setup is, so far we have seen volumes that is used only on single pod running on one worker node. so your data wont be available when pod is rescheduled to other node since your hostpath you have used is local directory.
If you want your data to be available for all worker nodes, we must have shared volumes concepts to overcome such situation. We can use a special type ie PersistentVolume and PersistentVolumeClaim or our traditional approach that mount a shared volume from storage and use that mounted path in the deployment file.
You can checkout this video for the traditional approach and will explain you about persistentvolume and persistentvolume claim in the next article.
Also You can Watch this Entire Tutorial video on our YouTube Channel.
Keep practicing and have fun. Leave your comments if any.
Also refer other articles,
What is Kubernetes - Learn Kubernetes from Basics
How to Install Kubernetes on Linux (RedHat / CentOS)
How to Install Kubernetes On Ubuntu 16.04 LTS
How to Create Kubernetes Deployment, Services & Pods Using Kubectl
How to Create Kubernetes YAML for Deployment, Service & Pods
Kubernetes Volumes Explained with Examples
Kubernetes Persistent Volumes and Claims Explained
Keep practicing and have fun. Leave your comments if any.
Stay connected with us on social networking sites, Thank you.
0 Comments