nfs-storage について
nfs-storage は、Linux システム間のネットワーク共有ストレージを設定する管理者向けのスキルです。NFSv4 のエクスポートとクライアントマウント、オンデマンドマウント、セキュリティ、性能調整、Kubernetes 連携を扱います。
主な機能
- 01NFSv4 エクスポートの設定
- 02共有の手動・永続マウント
- 03autofs によるオンデマンドマウント
- 04Kerberos セキュリティの設定
- 05NFS 性能と Kubernetes ストレージの調整
活用シーン
- 01複数の Linux サーバー間でディレクトリを共有
- 02Kubernetes ワークロードへの共有ストレージ提供
- 03ホームディレクトリやアプリデータの一元化
- 04ローカルストレージからネットワークストレージへの移行
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ bagelhole/ devops-security-agent-skills --skill nfs-storageインストール後、Claude Code で/nfs-storageを入力して読み込まれたか確認します。
詳細
NFS ストレージ
Linux システム間でネットワークファイル共有を行うための、NFS サーバーとクライアントを設定します。NFSv4 サーバーのセットアップ、エクスポートオプション、クライアントのマウント、オンデマンドマウント用の autofs、Kerberos セキュリティ、パフォーマンスチューニング、Kubernetes との統合について説明します。
使用する場面
- 複数の Linux サーバー間でディレクトリを共有する場合(Web ファーム、ビルドクラスター)
- コンテナ化されたワークロード向けに共有ストレージを提供する場合(Kubernetes ReadWriteMany)
- サーバー群全体でホームディレクトリやアプリケーションデータを一元管理する場合
- プロジェクトファイルを共有する開発環境を構築する場合
- ローカルストレージからネットワーク接続ストレージへ移行する場合
前提条件
- NFS サーバー:
nfs-kernel-server(Debian/Ubuntu)またはnfs-utils(RHEL/CentOS) - NFS クライアント:
nfs-common(Debian/Ubuntu)またはnfs-utils(RHEL/CentOS) - サーバーとクライアント間のネットワーク接続(NFSv4 の場合、TCP/UDP 2049)
- NFS トラフィックを許可するファイアウォールルール
- NFSv4 Kerberos を使用する場合:
krb5-userと、正常に動作する KDC
NFS サーバーのセットアップ
インストール
# Debian / Ubuntu
apt update && apt install -y nfs-kernel-server
# RHEL / CentOS
dnf install -y nfs-utils
# Enable and start the NFS server
systemctl enable --now nfs-server
# Verify NFS is running
systemctl status nfs-server
rpcinfo -p | grep nfs
エクスポート設定(/etc/exports)
# /etc/exports
# Syntax: <directory> <client-spec>(options)
# Share /data to a specific subnet with read-write access
/data 10.0.0.0/24(rw,sync,no_subtree_check,no_root_squash)
# Share /shared read-only to everyone
/shared *(ro,sync,no_subtree_check)
# Share /home to specific hosts
/home server01.example.com(rw,sync,no_subtree_check)
/home server02.example.com(rw,sync,no_subtree_check)
# Share /var/nfs/projects with root squash (default, map root to nobody)
/var/nfs/projects 10.0.0.0/24(rw,sync,no_subtree_check,root_squash)
# NFSv4 pseudo-root export (recommended for NFSv4)
/srv/nfs 10.0.0.0/24(rw,sync,fsid=0,crossmnt,no_subtree_check)
/srv/nfs/data 10.0.0.0/24(rw,sync,no_subtree_check,no_root_squash)
/srv/nfs/shared 10.0.0.0/24(ro,sync,no_subtree_check)
エクスポートオプションの説明
| オプション | 説明 |
|---|---|
rw | 読み書きアクセス |
ro | 読み取り専用アクセス |
sync | 応答する前にデータをディスクに書き込みます(安全ですが低速です) |
async | データをディスクに書き込む前に応答します(高速ですが、破損のリスクがあります) |
no_subtree_check | サブツリーのチェックを無効にします(信頼性が向上します) |
root_squash | リモートの root(UID 0)を nobody にマッピングします(デフォルトで、より安全です) |
no_root_squash | リモートの root がサーバー上で root として動作することを許可します(慎重に使用してください) |
all_squash | すべてのリモート UID/GID を nobody にマッピングします |
anonuid=1000 | 匿名ユーザーを特定の UID にマッピングします |
anongid=1000 | 匿名グループを特定の GID にマッピングします |
crossmnt | クライアントがサブマウントにアクセスできるようにします |
fsid=0 | NFSv4 の疑似ルートとして指定します |
エクスポート設定の反映
# Apply changes to exports (no server restart needed)
exportfs -ra
# Show current exports
exportfs -v
# Export a new directory on the fly (temporary, not persistent)
exportfs -o rw,sync,no_subtree_check 10.0.0.0/24:/tmp/share
# Unexport a directory
exportfs -u 10.0.0.0/24:/tmp/share
サーバーのファイアウォール設定
# UFW (Ubuntu)
ufw allow from 10.0.0.0/24 to any port nfs
ufw allow from 10.0.0.0/24 to any port 111 # rpcbind (NFSv3)
# firewalld (RHEL/CentOS)
firewall-cmd --permanent --add-service=nfs
firewall-cmd --permanent --add-service=rpc-bind
firewall-cmd --permanent --add-service=mountd
firewall-cmd --reload
# NFSv4 only needs TCP 2049 (no rpcbind or mountd)
firewall-cmd --permanent --add-port=2049/tcp
firewall-cmd --reload
NFS クライアントの設定
手動マウント
# Install NFS client
apt install -y nfs-common # Debian/Ubuntu
dnf install -y nfs-utils # RHEL/CentOS
# Discover exports from the server
showmount -e nfs-server.example.com
# Mount an NFS share manually
mkdir -p /mnt/data
mount -t nfs nfs-server.example.com:/data /mnt/data
# Mount with specific NFS version and options
mount -t nfs -o vers=4.2,tcp,hard,intr nfs-server.example.com:/data /mnt/data
# Verify the mount
mount | grep nfs
df -hT /mnt/data
# Unmount
umount /mnt/data
/etc/fstab による永続的なマウント
# /etc/fstab entries for NFS
# Basic NFSv4 mount
nfs-server.example.com:/data /mnt/data nfs4 defaults,_netdev 0 0
# Mount with performance and reliability options
nfs-server.example.com:/data /mnt/data nfs4 hard,intr,rsize=1048576,wsize=1048576,timeo=600,retrans=3,_netdev 0 0
# Read-only mount
nfs-server.example.com:/shared /mnt/shared nfs4 ro,_netdev 0 0
# Mount with specific UID/GID mapping (useful for containers)
nfs-server.example.com:/data /mnt/data nfs4 defaults,_netdev,uid=1000,gid=1000 0 0
# Mount all fstab entries
mount -a
# Test fstab entry without actually mounting
mount --fake -a -v
マウントオプションの説明
| オプション | 説明 |
|---|---|
hard | NFS リクエストを無期限に再試行します(データの整合性を保つため推奨) |
soft | retrans 回の再試行後にエラーを返します(データ破損のリスクがあります) |
intr | ハードマウントされた NFS リクエストの中断を許可します |
rsize=1048576 | 読み取りバッファーサイズ(バイト単位、1 MB、NFSv4 の最大値) |
wsize=1048576 | 書き込みバッファーサイズ(バイト単位、1 MB) |
timeo=600 | タイムアウト(1/10 秒単位、60 秒) |
retrans=3 | エラー(soft)またはメッセージ(hard)を返すまでの再試行回数 |
_netdev | マウント前にネットワークを待機します(起動時に重要です) |
noatime | アクセス時刻を更新しません(パフォーマンスが向上します) |
nconnect=8 | 複数の TCP 接続を使用します(カーネル 5.3 以降。スループットが向上します) |
Autofs(オンデマンドマウント)
# Install autofs
apt install -y autofs # Debian/Ubuntu
dnf install -y autofs # RHEL/CentOS
# Configure the master map
# /etc/auto.master or /etc/auto.master.d/nfs.autofs
/mnt/nfs /etc/auto.nfs --timeout=300
/etc/auto.nfs
# Format: mount-point options location
# Mounts will appear under /mnt/nfs/<mount-point>
data -rw,hard,intr,rsize=1048576,wsize=1048576 nfs-server.example.com:/data
shared -ro,hard,intr nfs-server.example.com:/shared
home -rw,hard,intr nfs-server.example.com:/home/&
# Wildcard: mount any subdirectory from the server automatically
# /etc/auto.master entry: /mnt/nfs /etc/auto.nfs
* -rw,hard,intr nfs-server.example.com:/srv/nfs/&
# Enable and start autofs
systemctl enable --now autofs
# Test: simply cd into the mount point and it appears
ls /mnt/nfs/data # Triggers auto-mount
# The share unmounts automatically after the timeout (300 seconds idle)
# Check autofs status
systemctl status autofs
automount -v # Verbose debugging mode (foreground)
パフォーマンスチューニング
サーバー側のチューニング
# Increase the number of NFS daemon threads (default is 8)
# /etc/default/nfs-kernel-server (Debian) or /etc/sysconfig/nfs (RHEL)
RPCNFSDCOUNT=32
# Or set at runtime
echo 32 > /proc/fs/nfsd/threads
# Restart NFS to apply
systemctl restart nfs-server
# Tune NFS server read/write sizes in sysctl
# These are auto-negotiated but can be adjusted
echo 1048576 > /proc/fs/nfsd/max_block_size
# Kernel network buffer tuning (see performance-tuning skill)
sysctl -w net.core.rmem_max=134217728
sysctl -w net.core.wmem_max=134217728
クライアント側のチューニング
# Use large read/write buffer sizes in mount options
mount -t nfs4 -o rsize=1048576,wsize=1048576,noatime nfs-server:/data /mnt/data
# Use multiple TCP connections (Linux kernel 5.3+)
mount -t nfs4 -o nconnect=8 nfs-server:/data /mnt/data
# Check current mount options and NFS statistics
nfsstat -c # Client NFS statistics
nfsstat -s # Server NFS statistics
mountstats /mnt/data # Detailed per-mount stats
# Test NFS throughput with dd
dd if=/dev/zero of=/mnt/data/testfile bs=1M count=1024 oflag=direct
dd if=/mnt/data/testfile of=/dev/null bs=1M iflag=direct
rm /mnt/data/testfile
# Test with fio for more realistic workloads
fio --name=nfs-test --directory=/mnt/data --ioengine=libaio --direct=1 \
--rw=randrw --bs=4k --numjobs=4 --size=1G --runtime=60 --group_reporting
Kubernetes NFS 統合
# nfs-pv.yaml -- Static PersistentVolume
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv
spec:
capacity:
storage: 100Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs
nfs:
server: nfs-server.example.com
path: /data
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-pvc
spec:
accessModes:
- ReadWriteMany
storageClassName: nfs
resources:
requests:
storage: 100Gi
# For dynamic provisioning, install the NFS CSI driver via Helm
helm repo add csi-driver-nfs https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/master/charts
helm install csi-driver-nfs csi-driver-nfs/csi-driver-nfs --namespace kube-system
# Then create a StorageClass pointing to your NFS server and share path.
トラブルシューティング
| 症状 | 診断コマンド | よくある対処法 |
|---|---|---|
| mount: アクセスが拒否されました | showmount -e server, exportfs -v | /etc/exports を確認し、exportfs -ra を実行して、サブネットを確認します |
| mount がハングする | mount -v、ネットワークを確認 | ファイアウォールで TCP 2049 が許可されていることを確認します。bg マウントオプションを使用します |
| 古いファイルハンドル | ls /mnt/data が古いハンドルエラーを返す | アンマウントして再マウントします:umount -f /mnt/data && mount -a |
| ファイルへのアクセスが拒否される | ls -la、UID マッピングを確認 | UID を一致させるか、all_squash,anonuid=1000,anongid=1000 を使用します |
| NFS のパフォーマンスが遅い | nfsstat -c、mountstats /mnt/data | rsize/wsize を増やし、nconnect=8 を追加して、NFS スレッドを調整します |
| Autofs がマウントしない | systemctl status autofs、automount -v | /etc/auto.master の構文を確認し、サーバーに到達できることを確認します |
| NFSv4 の ID マッピングが正しくない | 両側で id username | /etc/idmapd.conf のドメインが一致していることを確認します |
| NFS を待機して起動がハングする | fstab のオプションを確認 | fstab のエントリーに _netdev と bg オプションを追加します |
| Docker ボリュームのマウントに失敗する | docker volume inspect、dmesg | Docker ホストに NFS クライアントパッケージがインストールされていることを確認します |
関連スキル
linux-administration-- サーバーのセットアップとネットワーク設定block-storage-- NFS サーバーのデータディレクトリ用の基盤ストレージperformance-tuning-- NFS のスループット向上に向けたカーネルとネットワークの調整backup-recovery-- NFS でホストされるデータのバックアップobject-storage-- クラウドネイティブなワークロード向けの代替ストレージモデル
コメント
Markdown 対応