linux k8s-创建NFS存储卷PV

Hanze | Aug 15, 2022 min read

https://www.cnblogs.com/along21/p/10338242.html

1. 事前准备

修改k8s集群服务的hosts文件,使之能解析nfs服务器

[root@master ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.24.18.70 master
172.24.18.71 node01
172.24.18.72 node02
172.24.18.74 mms
185.199.108.133 raw.githubusercontent.com

在k8s集群服务器,安装nfs-utils 工具

 yum -y install nfs-utils

nfs服务器上提供nfs服务

[root@nfs ~]# yum -y install nfs-utils
[root@nfs ~]# mkdir /data/volumes -p
[root@nfs ~]# vim /data/volumes/index.html
<h1>NFS stor</h1>
[root@nfs ~]# vim /etc/exports
/data/volumes   <网段>/24(rw,no_root_squash)
[root@nfs ~]# systemctl start nfs

2. 创建NFS存储卷(测试)

编写yaml文件,并创建

[root@master volumes]# vim vol-nfs-demo.yaml
apiVersion: v1
kind: Pod
metadata:
  name: vol-nfs
  namespace: default
spec:
  volumes:
  - name: html
    nfs:
      path: /data/volumes
      server: master
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html/
[root@master volumes]# kubectl apply -f vol-nfs-demo.yaml
pod/vol-nfs created
  1. 验证,访问服务成功
[root@master ~]# kubectl get pods -o wide
NAME      READY     STATUS    RESTARTS   AGE       IP             NODE
vol-nfs   1/1       Running   0          9s        10.244.1.115   node1
[root@master ~]# curl 10.244.1.115
<h1>NFS stor</h1>
comments powered by Disqus