Kubernetes Volumes Explained with Examples
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 Container, What is Docker on Container – Get Started
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
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.
How to use kubernetes volumes to pod and containers?
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
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”
How to assign shared volume across all pods running on different worker nodes?
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
What is Docker – Get Started from Basics – Docker Tutorial
What is Container, What is Docker on Container – Get Started
How to Install Docker on CentOS 7 / RHEL 7
Docker Images Explained with Examples – Docker Tutorial
How to Run Docker Containers – Explained with Examples