Network File System (NFS) lets multiple machines access a shared directory over a network. It can provide shared storage for Kubernetes workloads that may move between nodes.
This example describes a small CentOS 7 test setup with one server and one client. CentOS 7 is end-of-life; use a supported distribution and a production-ready storage design for new deployments. Replace the example addresses and network with your own.
Configure the NFS server
Install the NFS utilities and create a shared directory:
bash_snippet
Bash · 2 lines|
| sudo yum install -y nfs-utils |
| sudo mkdir -p /data/nfs |
Add an export to
/etc/exports, restricting access to your client network:plain text_snippet
Plain Text · 1 line|
| /data/nfs 192.0.2.0/24(rw,sync,root_squash) |
192.0.2.0/24 is a documentation network, not an actual client network. Configure directory ownership and permissions for the client users who need to write. root_squash maps remote root to an unprivileged user; do not disable it merely to make a test work.Start the services and load the export:
bash_snippet
Bash · 3 lines|
| sudo systemctl enable --now rpcbind nfs-server |
| sudo exportfs -ra |
| sudo exportfs -v |
Allow the NFS traffic needed by your chosen protocol version through the firewall, restricted to trusted clients. NFSv3 may need additional RPC services and ports; do not expose an NFS server directly to the internet.
Mount from a client
Install
nfs-utils on the client and mount the export. Replace the address with your NFS server's actual address:bash_snippet
Bash · 4 lines|
| sudo yum install -y nfs-utils |
| sudo mkdir -p /data/nfsmount |
| sudo mount -t nfs 192.0.2.10:/data/nfs /data/nfsmount |
| mount | grep /data/nfsmount |
Write a test file as a user with permission on the export and verify it on the server:
bash_snippet
Bash · 1 line|
| printf 'hello NFS\n' > /data/nfsmount/test.txt |
When finished testing, unmount the client:
bash_snippet
Bash · 1 line|
| sudo umount /data/nfsmount |
Use NFS with Kubernetes
A Kubernetes persistent volume can reference an existing NFS export. Dynamic provisioning requires a suitable NFS CSI driver or provisioner; installing an NFS server alone does not create a StorageClass. In a management UI such as Kuboard, configure the provisioner's NFS server address and exported path according to the installed provisioner's requirements.
Validate permissions, network access, backup, performance, and availability before using the storage for production workloads.
✦ 卷终 · 柯航存卷 ✦