1、在节点上创建一个文件
1.1、创建一个/test 目录
mkdir /test
1.2、在 /test 目录中创建一个 index.html 文件
echo 'Hello from Kubernetes storage' >/test/index.html
2、创建 PersistentVolume
[root@test-centos test-pv]# cat task-pv-volume.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: task-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/test"
2.1、创建 PersistentVolume
[root@test-centos test-pv]# kubec get pv task-pv-volumeml
2.2、查看 PersistentVolume 的信息
[root@test-centos test]# kubectl get pv task-pv-volume
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
task-pv-volume 10Gi RWO Retain Bound default/task-pv-claim manual 16m
3、创建 PersistentVolumeClaim
创建一个 PersistentVolumeClaim。 Pod 使用 PersistentVolumeClaim 来请求物理存储。 在本练习中,你将创建一个 PersistentVolumeClaim,它请求至少 3 GB 容量的卷, 该卷至少可以为一个节点提供读写访问。
[root@VM-2-101-centos test-pv]# cat task-pv-claim.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: task-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
3.1、创建 PersistentVolumeClaim
[root@test-centos test-pv]# kubectl apply -f task-pv-claim.yaml
3.2、查看 PersistentVolume 信息
[root@VM-2-101-centos test]# kubectl get pv task-pv-volume
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
task-pv-volume 10Gi RWO Retain Bound default/task-pv-claim manual 19m
4、创建测试Pod
[root@VM-2-101-centos test-pv]# cat nginx_pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: task-pv-pod
spec:
volumes:
- name: task-pv-storage
persistentVolumeClaim:
claimName: task-pv-claim
containers:
- name: task-pv-container
image: nginx
ports:
- containerPort: 80
name: "http-server"
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: task-pv-storage
4.1、创建 Pod
[root@test-centos test-pv]# kubectl apply -f nginx_pod.yaml
4.2、检查 Pod 中的容器是否运行正常
[root@test-centos test]# kubectl get pod task-pv-pod
NAME READY STATUS RESTARTS AGE
task-pv-pod 1/1 Running 0 18m
4.3、进入pod验证
[root@test-centos test]# kubectl exec -it task-pv-pod bash
root@task-pv-pod:/# cat /usr/share/nginx/html/index.html
Hello from Kubernetes storage
root@task-pv-pod:/# curl localhost
Hello from Kubernetes storage
4.4、输出结果是你之前写到 hostPath 卷中的 index.html 文件中的内容
Hello from Kubernetes storage
如果你看到此消息,则证明你已经成功地配置了 Pod 使用 PersistentVolumeClaim 的存储