为何需要存储卷
Pod需要设置卷来源(spec.volume)和挂载点(spec.containers.volumeMounts)两个信息后才可以使用相应的Volume。
本地(hostPath,emptyDir)
网络(NFS,Ceph,GlusterFS)
公有云(AWS EBS)
K8S资源(configmap,secret)
emptyDir
emptyDir:是一个临时存储卷,与Pod生命周期绑定一起,如果Pod删除了卷也会被删除
应用场景:Pod中容器之间数据共享
apiVersion: v1
kind: Pod
metadata:
name: producer-consumer
spec:
containers:
- name: producer
image: busybox
volumeMounts:
- name: shared-volume
mountPath: /producer_dir
args:
- /bin/sh
- -c
- echo "hello this is producer" > /producer_dir/hello ; sleep 3600
- name: consumer
image: busybox
volumeMounts:
- name: shared-volume
mountPath: /consumer_dir
args:
- /bin/sh
- -c
- cat /consumer_dir/hello ; sleep 3600
volumes:
- name: shared-volume
emptyDir: {}
验证
[root@master yaml]# kubectl logs producer-consumer consumer
hello this is producer
hostPath
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: busybox
image: busybox
args:
- /bin/sh
- -c
- sleep 36000
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
hostPath:
path: /tmp
type: Directory
PV & PVC
准备工作NFS
yum install nfs-utils 所有节点上都安装
systemctl stop firewalld
systemctl disable firewalld
systemctl start nfs
systemctl enable nfs
mkdir /data
vim /etc/exports
/data *(rw, no_root_squash)
systemctl reload nfs
案例 pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv0001
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteMany
nfs:
path: /data
server: 172.16.10.216
案例pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc0001
spec:
accessModes:
- ReadWriteMany
storageClassName: ""
resources:
requests:
storage: 2Gi