一. 卷的概念
官方地址:卷 | Kuberneteshttps://v1-24.docs.kubernetes.io/zh-cn/docs/concepts/storage/volumes/
二. 卷的类型及使用
emptyDir卷
1. 创建编辑文件 vim emptydir.yaml
apiVersion: v1
kind: Pod
metadata:
name: vol1
spec:
containers:
- image: busyboxplus
name: vm1 #容器1
command: ["sleep", "300"]
volumeMounts:
- mountPath: /cache #挂载点
name: cache-volume #共享这个卷
- name: vm2 #容器2
image: nginx
volumeMounts:
- mountPath: /usr/share/nginx/html #挂载点
name: cache-volume #共享这个卷
volumes:
- name: cache-volume
emptyDir: #卷的类型
medium: Memory #存储
sizeLimit: 100Mi #大小删除pod: kubectl delete pod vol1
hostPath卷
1. 创建编辑文件 vim hostpath.yaml
apiVersion: v1
kind: Pod
metadata:
name: vol2
spec:
nodeName: k8s3 #这里是指定的节点
containers:
- image: nginx
name: test-container
volumeMounts:
- mountPath: /usr/share/nginx/html #挂接到默认发布目录
name: test-volume
volumes:
- name: test-volume
hostPath:
path: /data #会在分配到的宿主机上创建data目录然后挂接到容器
type: DirectoryOrCreate
2. 查看分配的节点
3. 此时尝试访问
4. 在分配的节点上创建一个页面 重新访问pod即可
nfs卷
实验环境:需要在所有k8s节点上安装nfs-utils软件包
yum install -y nfs-utils


apiVersion: v1
kind: Pod
metadata:
name: nfs
spec:
containers:
- image: nginx
name: test-container
volumeMounts:
- mountPath: /usr/share/nginx/html #容器直接挂接到nginx发布目录
name: test-volume
volumes:
- name: test-volume
nfs:
server: 192.168.72.171 #集群外nfs输出的server地址
path: /nfsdata出现这个报错是因为节点没有安装nfs 安装即可![]()
安装即可(删除上面没有创建好的pod重新创建)![]()


