Volume Overview
In this post, we will learn about the concept of Volume and how to use it in Kubernetes.
The schematic structure of Volume on Kubernetes is as shown in the figure above. Using the Volume parameter of Pod, define the value of PVC (persistent volume claim), and use this value to send the PVC to the PV (Persistent Volume) mapped to the storage to send the Pod This is the structure that maps the PV to be used in
Volume Type
Then let's learn about Volume Type. There are 4 types and 2 types of Volume as follows.
kind of Volume Type
- emptyDirtmp is a Volume that exists in the Pod when the Pod is created. That is, when the Pod is deleted, all data is also deleted.
- hostPath(Local) is used by mounting the file or directory of the host node to the Pod, and its contents are not deleted when the Pod is deleted.
- local uses a file system mounted on a specific lost node as a Volume.
- Network : Use storage that exists on the network or cloud as a Volume. (AWS, NFS, EBS, etc.)
Classification according to Volume Provisioning Type (Volume is automatically created or not)
- Static Volume Provisioning: Administrators manually create Persistent Volumes in Kubernetes
- DynamicVolume Provisoning automatically creates a persistent volume in Kubernetes at the request of the developer.
Persistent Volume(PV) & Persistent Volume Claim(PVC)
Using volumes in Kubernetes consists of two steps, as mentioned above. priority PersistentVolume (PV) to which storage is mapped, and PersistentVolumeClaim (PVC), which requests volume details with PV when using that PV in Pods, are separated into two stages. You can think of PV as storage itself. PVC means a request from a developer or cloud user to allocate a Volume to a PV. Set the capacity you want to use and the setting values for various volumes and make a request.
So let's take a look at the PV yaml file.
apiVersion: v1
kind: PersistentVolume ==> define Object Type
metadata:
name: pv0003 ==> name of PV
spec: ==> define PV Spec
capacity:
storage: 5Gi ==> capacity Volume
volumeMode: Filesystem ==> define type of Volume
accessModes: ==> define Access Mode
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: slow
mountOptions: ==> Volume mount info
- hard
- nfsvers=4.1
nfs:
path: /tmp
server: 172.17.0.2
|
If you look at the yaml file above, PV is being created, the capacity is 5G, and the NFS type volume is defined. Like Pods, Volumes have the following states.
Phase
- Available: The state in which the PV is created and before the PVC is bound.
- Bound – PVC bound to PV
- Released – the PVC has been deleted
- Failed – Failed status
If you look at the above parameter, there is AccessMode. This is the part that sets what kind of access to the Volume is allowed. A look at each mode is as follows.
Access Modes
- ReadWriteOnce – A volume can be mounted read-write by a single node.
- ReadOnlyMany – Volumes can be mounted read-only by multiple nodes.
- ReadWriteMany – Volumes can be mounted read-write by multiple nodes.
In the above Yaml file, there is a Reclaim Policy parameter. This parameter is the part that defines how to handle the file on the disk connected to the PV when the PVC bound to a specific PV is deleted. So let's see what values do what.
Reclaim Policy
- Retain – Retain PV and data even if PVC is deleted (requires deletion and re-creation of PV to re-write)
- Recycle – Keep PV but delete file (rm -rf /thevolume/*)
- Delete – Delete volume and data
Now let's look at the PVC sample yaml file.
root@master-VirtualBox:~/test/persistenvolume# vi pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim ==> define type of Object
metadata:
name: myclaim ==> name of PVC
spec:
accessModes:
- ReadWriteOnce
volumeMode: Filesystem
resources:
requests:
storage: 8Gi ==> requested quota
storageClassName: slow ==> defind IO(Input/Output) Speed
selector:
matchLabels:
release: "stable"
matchExpressions:
- {key: environment, operator: In, values: [dev]}
|
excerise
Now, let's use Volume. First, create a password for mysql as a secret object.
root@master-VirtualBox:~# kubectl create secret generic mysql-pass --from-literal=password=pwd
secret/mysql-pass created
|
Now, create a yaml file like the one below and save it after entering the following contents
root@master-VirtualBox:~/test# mkdir persistenvolume
root@master-VirtualBox:~/test# cd persistenvolume/
root@master-VirtualBox:~/test/persistenvolume# vi pv-yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: wp-pv-volume
labels:
type: local
spec:
capacity:
storage: 3Gi
volumeMode: Filesystem
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Delete
storageClassName: local-storage
local:
path: "/data/mysql"
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- node
|
Then, create a PVC yaml file, input the file contents and save it.
root@master-VirtualBox:~/test/persistenvolume# vi pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mysql-pv-claim
spec:
storageClassName: local-storage
accessModes:
- ReadWriteMany
resources:
requests:
storage: 2Gi
selector:
matchLabels:
type: local
|
Now, create a mysql Pod yaml file as a Backend (DB) Pod, enter the following content and save it.
root@master-VirtualBox:~/test/persistenvolume# vi mysql-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
tier: mysql
spec:
containers:
- image: mysql:5.6
imagePullPolicy: Always
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim ==> 위에서 생성한 PVC의 이름
|
Now, create a yaml file to create a Service object for communication between Pods, enter the following content and save it.
root@master-VirtualBox:~/test/persistenvolume# vi mysql-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
ports:
- port: 3306
selector:
app: wordpress
tier: mysql
clusterIP: None
|
Now, create a yaml file to create a Pod that provides Wordpress service with Frontend, enter the following content and save it
root@master-VirtualBox:~/test/persistenvolume# vi wp-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: frontend
strategy:
type: Recreate
replicas: 1
template:
metadata:
labels:
app: wordpress
tier: frontend
spec:
containers:
- image: wordpress:4.8-apache
imagePullPolicy: Always
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: wordpress-mysql
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- containerPort: 80
name: wordpress
|
Then, to create a Service Object for communication with the Frontend Pod, create a Service yaml file, enter the following, and save it.
oot@master-VirtualBox:~/test/persistenvolume# vi wp-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: wordpress
labels:
app: wordpress
spec:
type: NodePort
ports:
- port: 8080
targetPort: 80
protocol: TCP
selector:
app: wordpress
tier: frontend
|
Now, after distributing all the created objects, search the information of the created object.
root@master-VirtualBox:~/test# kubectl apply -f /root/test/persistenvolume/
deployment.apps/wordpress-mysql created
service/wordpress-mysql created
persistentvolumeclaim/mysql-pv-claim created
deployment.apps/wordpress created
service/wordpress created
root@master:/lab/dashboard# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 172.168.1.1 <none> 443/TCP 4h56m
wordpress NodePort 172.168.1.177 <none> 8080:32382/TCP 10m
wordpress-mysql ClusterIP None <none> 3306/TCP 10m
|
Now, send a service request to http://172.168.1.177:32382 to the part marked in red above, and check whether the volume is mounted under the data directory in the Worker Node where the Pod is running after checking whether it is received normally.
root@node:/data/mysql# ll
total 110616
drwxr-xr-x 5 vboxadd vboxsf 4096 12월 5 15:39 ./
drwxr-xr-x 3 root root 4096 12월 5 15:12 ../
-rw-rw---- 1 vboxadd vboxsf 56 12월 5 15:38 auto.cnf
-rw-rw---- 1 vboxadd vboxsf 12582912 12월 5 15:38 ibdata1
-rw-rw---- 1 vboxadd vboxsf 50331648 12월 5 15:38 ib_logfile0
-rw-rw---- 1 vboxadd vboxsf 50331648 12월 5 15:38 ib_logfile1
drwx------ 2 vboxadd vboxsf 4096 12월 5 15:38 mysql/
drwx------ 2 vboxadd vboxsf 4096 12월 5 15:38 performance_schema/
drwx------ 2 vboxadd vboxsf 4096 12월 5 15:39 wordpress/
|
'Kubernetes' 카테고리의 다른 글
[Cloud] 13. making kubernetes Helm Template (41) | 2023.02.13 |
---|---|
[Cloud] 12. using Kubernetes Helm (17) | 2023.02.12 |
[Cloud] 10. Using Kubernetes Configmap (23) | 2023.02.10 |
[Cloud] 9. Kubernetes Daemonset deploy (42) | 2023.02.08 |
[Cloud] 8. Kubernetes kind of Prob and allocation of resource (41) | 2023.02.07 |