配置 Pod 以使用 PersistentVolume 作为hostPath存储

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 的存储

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维那些事~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值