一、存储卷
1.1、emptyDir存储卷
当pod被分配给节点时,首先创建emptyDir卷,并且只要该Pod在该节点上运行,该卷就会存在。正如卷的名字所述,它最初是空的。Pod中的容器可以读取和写入emptyDir卷中的相同文件,尽管该卷可以挂载到每个容器中的相同或不同路径上。当出于任何原因从节点中删除 pod时,emptyDir中的数据将被永久删除
vim demo1.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx1
image: nginx
ports:
- name: http
containerPort: 80
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html/
- name: nginx2
image: nginx
volumeMounts:
- name: html
mountPath: /data/
command: ['/bin/bash','-c','while true;do echo $(date) >> /data/index.html;sleep 10;done']
volumes:
- name: html
emptyDir: {}
kubectl apply -f demo3.yaml
kubectl get pods -o wide
kubectl exec -it nginx -c nginx2 sh
cat /data/index.html
kubectl exec -it nginx -c nginx1 sh
cat /usr/share/nginx/html/index.html



1.2 hostPath存储卷
hostpath卷将 node节点的文件系统中的文件或目录挂载到集群中。hostPath可以实现持久存储,但是在node节点故障时,也会导致数据的丢失
vim demo4.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx3
labels:
app: nginx3
spec:
containers:
- name: nginx1
image: nginx
ports:
- name: http
containerPort: 80
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html/
readOnly: false
volumes:
- name: html
hostPath:
path: /data/pod/volume
type: DirectoryOrCreate
kubectl apply -f demo2.yaml
kubectl get pods -o wide
echo '111111' > /data/pod/volume/index.html
curl http://IP
kubectl exec -it nginx3 -c nginx1 sh
echo '222222' >> /usr/share/nginx/html/index.html





1.3、nfs共享存储卷
1.3.1、开启nfs服务器
hostnamectl set-hostname nfs
systemctl stop firewalld
setenforce 0
mkdir -p /data/volume
chmod 777 /data/volume
yum install -y rpcbind nfs-utils
vim /etc/exports
/data/volume 192.168.80.0/24(rw,sync,no_root_squash)
systemctl start rpcbind
systemctl start nfs
exportfs -rv
showmount -e
echo '111111' > /data/volume/index.html



1.3.2、节点配置nfs映射
echo '192.1268.80.14 nfs' > /etc/hosts
1.3.3、编写yaml文件
vim demo5.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx3
labels:
app: nginx3
spec:
containers:
- name: nginx3
image: nginx
ports:
- name: http
containerPort: 80
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html/
readOnly: false
volumes:
- name: html
nfs:
path: /data/volume
server: nfs
kubectl apply -f demo5.yaml
kubectl get pods -o wide
curl http://IP



