0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWS環境にNFSサーバーを構築する

Posted at

NFSとは

ネットワークを介して別のコンピュータの外部記憶装置(ストレージ)をマウントすることができ、そこに保存されているディレクトリやファイルをあたかも手元にあるかのように扱うことができる。ストレージを他のコンピュータに提供するコンピュータをNFSサーバ、サーバの持つストレージにアクセスして利用するコンピュータをNFSクライアントという。

まずはEC2を2台用意。

NFSserver
[root@nfs-server ec2-user]# ifconfig | grep inet
        inet 10.10.21.87  netmask 255.255.224.0  broadcast 10.10.31.255
        inet6 fe80::4b0:32ff:fe9e:700b  prefixlen 64  scopeid 0x20<link>
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
NFSclient
[root@nfs-client ec2-user]# ifconfig | grep inet
        inet 10.10.11.116  netmask 255.255.224.0  broadcast 10.10.31.255
        inet6 fe80::49b:4cff:fe5e:7415  prefixlen 64  scopeid 0x20<link>
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>

NFSserverからclientに共有するためのボリュームを用意する。
rootボリュームも共有することが出来るが、共有用に別途用意したほうが管理上都合が良さそう(スナップショットやアップデートetc...)な為用意する。

server側の設定

EBSを作成しNFSserverにアタッチ

[root@nfs-server ec2-user]# lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0   8G  0 disk
└─xvda1 202:1    0   8G  0 part /
xvdf    202:80   0  10G  0 disk
[root@nfs-server ec2-user]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        482M     0  482M   0% /dev
tmpfs           492M     0  492M   0% /dev/shm
tmpfs           492M  408K  492M   1% /run
tmpfs           492M     0  492M   0% /sys/fs/cgroup
/dev/xvda1      8.0G  1.7G  6.3G  22% /
tmpfs            99M     0   99M   0% /run/user/1000
[root@nfs-server ec2-user]# mkfs -t ext4 /dev/sdf
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

[root@nfs-server ec2-user]# file -s /dev/xvdf
/dev/xvdf: Linux rev 1.0 ext4 filesystem data, UUID=73571694-08a7-4cdc-acfe-e569f04694d1 (extents) (64bit) (large files) (huge files)
[root@nfs-server ec2-user]# lsblk -f
NAME    FSTYPE LABEL UUID                                 MOUNTPOINT
xvda
└─xvda1 xfs    /     bc07e2f4-d5ff-494b-adf1-6f6da7608cd6 /
xvdf    ext4         73571694-08a7-4cdc-acfe-e569f04694d1
[root@nfs-server ec2-user]# mkdir /data
[root@nfs-server ec2-user]# mount /dev/xvdf /data
[root@nfs-server ec2-user]# lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0   8G  0 disk
└─xvda1 202:1    0   8G  0 part /
xvdf    202:80   0  10G  0 disk /data
[root@nfs-server ec2-user]# cd /data/
[root@nfs-server data]# ls -la
total 20
drwxr-xr-x  3 root root  4096 Oct  6 05:15 .
dr-xr-xr-x 19 root root   269 Oct  6 05:18 ..
drwx------  2 root root 16384 Oct  6 05:15 lost+found
[root@nfs-server data]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        482M     0  482M   0% /dev
tmpfs           492M     0  492M   0% /dev/shm
tmpfs           492M  408K  492M   1% /run
tmpfs           492M     0  492M   0% /sys/fs/cgroup
/dev/xvda1      8.0G  1.7G  6.3G  22% /
tmpfs            99M     0   99M   0% /run/user/1000
/dev/xvdf       9.8G   37M  9.2G   1% /data
[root@nfs-server data]# cp -ip /etc/fstab /home/ec2-user/
[root@nfs-server data]# vim /etc/fstab
[root@nfs-server data]# cat /etc/fstab
#
UUID=bc07e2f4-d5ff-494b-adf1-6f6da7608cd6     /           xfs    defaults,noatime  1   1
/dev/xvdf   /data ext4    defaults,nofail  1   2
[root@nfs-server data]# diff /home/ec2-user/fstab /etc/fstab
2a3
> /dev/xvdf   /data ext4    defaults,nofail  1   2

exportの設定を記述

[root@nfs-server data]# cat /etc/exports
/data 10.10.0.0/255.255.0.0(rw,no_root_squash)

nfsを起動

[root@nfs-server data]# systemctl status nfs
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@nfs-server data]# systemctl start nfs
[root@nfs-server data]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@nfs-server data]# systemctl status nfs
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled; vendor preset: disabled)
  Drop-In: /run/systemd/generator/nfs-server.service.d
           └─order-with-mounts.conf
   Active: active (exited) since Wed 2021-10-06 05:55:44 UTC; 15s ago
 Main PID: 15493 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nfs-server.service

Oct 06 05:55:44 nfs-server systemd[1]: Starting NFS server and services...
Oct 06 05:55:44 nfs-server systemd[1]: Started NFS server and services.
[root@nfs-server data]#

exportの設定確認

[root@nfs-server data]# exportfs -v
/data         	10.10.0.0/255.255.0.0(rw,sync,wdelay,hide,no_subtree_check,sec=sys,secure,no_root_squash,no_all_squash)
[root@nfs-server data]# showmount -e
Export list for nfs-server:
/data 10.10.0.0/255.255.0.0

client側の設定

nfsグループを作成し、ec2-userを所属させる

[root@nfs-client ec2-user]# groupadd nfs
[root@nfs-client ec2-user]# id ec2-user
uid=1000(ec2-user) gid=1000(ec2-user) groups=1000(ec2-user),4(adm),10(wheel),190(systemd-journal)
[root@nfs-client ec2-user]# usermod -G nfs ec2-user
[root@nfs-client ec2-user]# id ec2-user
uid=1000(ec2-user) gid=1000(ec2-user) groups=1000(ec2-user),1001(nfs)

mountポイント作成

[root@nfs-client ec2-user]# mkdir -m 775 /data/
[root@nfs-client ec2-user]# cp -ip /etc/fstab /home/ec2-user/
[root@nfs-client ec2-user]# vim /etc/fstab
[root@nfs-client ec2-user]# cat /etc/fstab
#
UUID=bc07e2f4-d5ff-494b-adf1-6f6da7608cd6     /           xfs    defaults,noatime  1   1
10.10.21.87:/data /data nfs4 defaults 0 0
[root@nfs-client ec2-user]# diff /home/ec2-user/fstab /etc/fstab
2a3
> 10.10.21.87:/data /data nfs4 defaults 0 0

アクセス用の権限設定

[root@nfs-client ec2-user]# chown root:nfs /data

マウント

[root@nfs-client ec2-user]# mount /data
[root@nfs-client ec2-user]# df -h
Filesystem         Size  Used Avail Use% Mounted on
devtmpfs           482M     0  482M   0% /dev
tmpfs              492M     0  492M   0% /dev/shm
tmpfs              492M  408K  492M   1% /run
tmpfs              492M     0  492M   0% /sys/fs/cgroup
/dev/xvda1         8.0G  1.7G  6.3G  22% /
tmpfs               99M     0   99M   0% /run/user/1000
10.10.21.87:/data  9.8G   37M  9.2G   1% /data

マウントが確認できた。

server側にtestファイルを設置して動作確認

[root@nfs-server data]# ifconfig | grep inet
        inet 10.10.21.87  netmask 255.255.224.0  broadcast 10.10.31.255
        inet6 fe80::4b0:32ff:fe9e:700b  prefixlen 64  scopeid 0x20<link>
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
[root@nfs-server data]# pwd
/data
[root@nfs-server data]# ls -la
total 20
drwxr-xr-x  3 root root  4096 Oct  6 06:25 .
dr-xr-xr-x 19 root root   269 Oct  6 05:18 ..
drwx------  2 root root 16384 Oct  6 05:15 lost+found
[root@nfs-server data]# touch test
[root@nfs-server data]# ls -la
total 20
drwxr-xr-x  3 root root  4096 Oct  6 06:25 .
dr-xr-xr-x 19 root root   269 Oct  6 05:18 ..
drwx------  2 root root 16384 Oct  6 05:15 lost+found
-rw-r--r--  1 root root     0 Oct  6 06:25 test
[root@nfs-server data]#

client側で確認

[root@nfs-client data]# ifconfig | grep inet
        inet 10.10.11.116  netmask 255.255.224.0  broadcast 10.10.31.255
        inet6 fe80::49b:4cff:fe5e:7415  prefixlen 64  scopeid 0x20<link>
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
[root@nfs-client data]# pwd
/data
[root@nfs-client data]# ls -la
total 20
drwxr-xr-x  3 root root  4096 Oct  6 06:25 .
dr-xr-xr-x 19 root root   269 Oct  6 06:09 ..
drwx------  2 root root 16384 Oct  6 05:15 lost+found
-rw-r--r--  1 root root     0 Oct  6 06:25 test
[root@nfs-client data]#

動作確認できた。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?