k8s之emptyDir存储卷
一、简介
- emptyDir卷是最简单的卷,主要用于存储临时数据,当pod生命周期结束,emptyDir卷也就销毁。
- emptyDir卷应用场景一般是pod中多个容器共享数据,即在pod中定义一个emptyDir卷,然后容器A挂载到某个目录,容器B也挂载到某个目录,这样,容器A和容器B就能读写文件,就能共享数据。
二、存储介质
- medium:此目录所在的存储介质的类型,可用值为“default”或“Memory”。
- sizeLimit:当前存储卷的空间限额,默认值为nil,表示不限制。
三、实现emptyDir存储卷
root@k8s-master01:~/learning-k8s/examples/volumes# cat pod-with-emptyDir-vol.yaml
apiVersion: v1
kind: Pod
metadata:
name: pods-with-emptydir-vol
spec:
containers:
- image: ikubernetes/admin-box:v1.2
name: admin
command: ["/bin/sh", "-c"]
args: ["sleep 99999"]
resources: {}
volumeMounts:
- name: data
mountPath: /data
- image: ikubernetes/demoapp:v1.0
name: demoapp
resources: {}
volumeMounts:
- name: data
mountPath: /var/www/html
volumes:
- name: data
emptyDir:
medium: Memory
sizeLimit: 16Mi
dnsPolicy: ClusterFirst
restartPolicy: Always
root@k8s-master01:~/learning-k8s/examples/volumes# kubectl apply -f pod-with-emptyDir-vol.yaml
pod/pods-with-emptydir-vol created
root@k8s-master01:~/learning-k8s/examples/volumes# kubectl get pod
NAME READY STATUS RESTARTS AGE
pods-with-emptydir-vol 2/2 Running 0 5m20s
四、验证容器之间的数据共享
1、进入admin容器创建test.html文件,进入dempapp容器中验证。
root@k8s-master01:~/learning-k8s/examples/volumes# kubectl exec -it pods-with-emptydir-vol -c admin bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@pods-with-emptydir-vol /# cd /data/
root@pods-with-emptydir-vol data# echo "<h1>Index Page</h1>" > test.html
root@pods-with-emptydir-vol data# cat test.html
<h1>Index Page</h1>
2、在admin容器的挂载目录下生成了test.html
root@k8s-master01:~/learning-k8s/examples/volumes# kubectl exec -it pods-with-emptydir-vol -c demoapp /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
[root@pods-with-emptydir-vol /]# cd /var/www/html/
[root@pods-with-emptydir-vol /var/www/html]# ls
test.html
[root@pods-with-emptydir-vol /var/www/html]# cat test.html
<h1>Index Page</h1>
3、同理,在demoapp创建test02在admin容器也能看到
[root@pods-with-emptydir-vol /var/www/html]# echo $(date) $(hostname)> test02.html
[root@pods-with-emptydir-vol /var/www/html]# cat test
test.html test02.html
[root@pods-with-emptydir-vol /var/www/html]# cat test02.html
Mon Jan 22 13:19:03 UTC 2024 pods-with-emptydir-vol
[root@pods-with-emptydir-vol /var/www/html]# exit
root@k8s-master01:~/learning-k8s/examples/volumes# kubectl exec -it pods-with-emptydir-vol -c admin bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@pods-with-emptydir-vol /# cd /data/
root@pods-with-emptydir-vol data# ls
test.html test02.html
root@pods-with-emptydir-vol data# cat test02.html
Mon Jan 22 13:19:03 UTC 2024 pods-with-emptydir-vol