master1节点操作
# 创建目录
mkdir /nfs/share3/v{1,2,3,4} -p
# 设置共享文件配置
cat >> /etc/exports <
# 查看pv的创建方法
kubectl explain pv
# 查看怎么把nfs定义成pv
kubectl explain pv.spec.nfs
pv是集群级别的资源,不需要定义namespace
vi /root/test2/pv.yaml
内容
apiVersion: v1
kind: PersistentVolume
metadata:
name: v1
spec:
capacity:
# pv的存储空间容量
storage: 100Mi
# 访问模板
accessModes: ["ReadWriteOnce"]
nfs:
# 把nfs的存储空间创建成pv
path: /nfs/share3/v1
server: 192.168.187.154
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: v2
spec:
capacity:
storage: 200Mi
accessModes: ["ReadWriteMany"]
nfs:
path: /nfs/share3/v2
server: 192.168.187.154
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: v3
spec:
capacity:
storage: 300Mi
accessModes: ["ReadOnlyMany"]
nfs:
path: /nfs/share3/v3
server: 192.168.187.154
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: v4
spec:
capacity:
storage: 400Mi
accessModes: ["ReadWriteOnce","ReadWriteMany"]
nfs:
path: /nfs/share3/v4
server: 192.168.187.154
kubectl explain pv.spec.accessModes
kubectl apply -f /root/test2/pv.yaml
# 查看
kubectl get pv
master1节点操作
# 其他字段不懂通过explain来查看
kubectl explain pvc
vi /root/test2/pvc-v1.yaml
内容
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
# pvc名称
name: pvc-v1
spec:
# 访问模式
accessModes: ["ReadWriteMany"]
resources:
requests:
# pvC的存储空间容量
storage: 190Mi
kubectl apply -f /root/test2/pvc-v1.yaml
# 查看pv
kubectl get pv
# 查看pvc
kubectl get pvc
pvc从pv中选择一个 accessModes包含ReadWriteMany,且容量>=190M的pv。
上面选择了v2
kubectl delete -f /root/test2/pod-nginx-volume.yaml
vi /root/test2/pod-nginx-volume.yaml
内容
apiVersion: v1
kind: Pod
metadata:
# pod的名称
name: pod-nginx-volume
spec:
containers:
- name: c-nginx
image: nginx:1.14
# 容器挂载存储卷
volumeMounts:
# 容器存储卷目录
- mountPath: /usr/share/nginx/html
# 引入名称为nginx-volume的存储定义
name: nginx-volume
# 设置存储卷定义
volumes:
# 存储卷类型
- persistentVolumeClaim:
# pvc名称
claimName: pvc-v1
# 存储卷名称
name: nginx-volume
kubectl apply -f /root/test2/pod-nginx-volume.yaml
kubectl get pods pod-nginx-volume -o wide
kubectl -it exec pod-nginx-volume -c c-nginx -- /bin/sh
cd /usr/share/nginx/html
echo 'hello' > index.html
# 先删除Pod
kubectl delete -f /root/test2/pod-nginx-volume.yaml
# 再删除pvc
kubectl delete -f /root/test2/pvc-v1.yaml
# 查看
kubectl get pv
kubectl delete pv v2
# 查看
kubectl get pv
删除后,在创建pv
kubectl apply -f /root/test2/pv.yaml
# 查看
kubectl get pv
# 创建pvc
kubectl apply -f /root/test2/pvc-v1.yaml
# 查看
kubectl get pvc
# 进入容器,查看数据是否存在
kubectl apply -f /root/test2/pod-nginx-volume.yaml
kubectl -it exec pod-nginx-volume -c c-nginx -- /bin/sh
cd /usr/share/nginx/html
cat index.html