Deploy nfs highly available rsync+inotify
Project environment:
Three hosts (centos7):
Nfs-server Master: 172.16.1.20
Nfs-server from: 172.16.1.30
Client (client): 172.16.1.40
Project operations: 1. Set up nfs server first.
Both master and slave nfs-server need to be built, and the same operation is needed.
[root@nfs-master ~] # yum-y install nfs-utils # install nfs Service [root@nfs-master ~] # yum-y install rpcbind # install remote Transmission Control Protocol [root@nfs-master ~] # vim / etc/exports # write nfs file / nfs-share 172.16.1.* (rw,sync,no_root_squash)
Parameter explanation:
172.16.1.address: indicates that the network segment is allowed, or the ip address can be customized.
Rw: readable and writable
Sync: synchronize data to disk
No_root_squash: with this option added, root users will have the highest permissions on shared directories, just as they do on local directories.
[root@nfs-master ~] # mkdir / nfs-share # create a shared directory [root@nfs-master ~] # systemctl start rpcbind # first start the service [root@nfs-master ~] # systemctl start nfs2, set up rsync:// and install rsync on nfs-server: [root @ nfs-slave ~] # yum-y install rsync// modify rsync configuration file: [root@nfs-slave ~] # vim / etc/rsyncd.conf
The modifications are as follows:
/ / create a data file for the authorization account: [root@nfs-slave ~] # vim / etc/rsyncd_users.db # file name can be customized, but must be the same as the data file name in the configuration file above
Note: the user name is the same as that in the rsync configuration file.
/ / Grant permission [root@nfs-slave ~] # chmod 600 / etc/rsyncd_users.db// to start the rsync service: [root@nfs-slave ~] # rsync-- daemon
3. Install the inotify tool on the main nfs-server:
Download and upload the inotify-tools-3.14.tar.gz installation package
[root@nfs-master ~] # tar zxf inotify-tools-3.14.tar.gz [root@nfs-master ~] # cd inotify-tools-3.14/ [root@nfs-master inotify-tools-3.14] #. / configure & & make & & make install
# # Writing trigger synchronization scripts:
[root@nfs-master ~] # vim inotify.sh
#! / bin/bash
INOTIFY_CMD= "inotifywait-mrq-e modify,create,move,delete / nfs-share"
RSYNC_CMD= "rsync-azH-delete-password-file=/etc/server.pass / nfs-share sunqiuming@172.16.1.30::rsync"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
Do
$RSYNC_CMD
Done
Note: this script is used to monitor the local shared directory / nfs-share and synchronizes the data to the / nfs-share directory on the nfs-server server (permission is required) as soon as any action on that directory is monitored.
/ / create the password file in the script (in order not to enter the password during synchronization)
[root@nfs-master ~] # echo "123456" > / etc/server.pass## file name is customized, as long as it is the same as in the above script
[root@nfs-master ~] # chmod 600 / etc/server.pass # Grant permissions
/ / A pair that grants permissions from a shared directory on the nfs-server service
[root@nfs-slave] # chmod 777 / nfs-share/ [root@nfs-slave ~] # ls-ld / nfs-share/drwxrwxrwx 2 root root 6 Nov 14 23:14 / nfs-share/
/ / add script files to boot:
[root@nfs-master ~] # echo'/ root/inotify.sh' > > / etc/rc.local
/ / execute the trigger script:
[root@nfs-master] # sh inotify.sh & # running in the background
4. Mount the client client: [root@client ~] # mkdir / test # create a test mount directory [root@client ~] # mount-t nfs- o soft,timeo=5 172.16.1.20:/nfs-share / test # Note: use soft mount. The default is hard mount, and soft mount is used when the server goes down. Will not block all the time # # write test files under the test directory: [root@client test] # echo "hello" > index.html [root@client test] # echo "ha ha ha" > index.php
/ / View on the main nfs-server:
[root@nfs-master ~] # cd / nfs-share/ [root@nfs-master nfs-share] # cat index.html hello [root@nfs-master nfs-share] # cat index.php ha ha ha
/ / check from nfs-server again (whether the script has been triggered and the data is synchronized)
[root@nfs-slave ~] # cd / nfs-share/ [root@nfs-slave nfs-share] # lsnfs-share [root@nfs-slave nfs-share] # cd nfs-share/ [root@nfs-slave nfs-share] # cat index.html hello [root@nfs-slave nfs-share] # cat index.php ha ha ha
Data synchronization succeeded.
5. Simulate nfs-server failure:
Write a test script on the client before simulating the failure:
[root@client ~] # vim nfs.shangxue Bash while true; do ping 172.16.1.20-c 4 & > / dev/nullif [$?-ne 0]; then umount-l / test & & mount-t nfs- o soft,timeo=5 172.16.1.30:/nfs-share / testfisleep 1done
Note: this script will check every second, as long as when the master nfs-server fails, it will be remounted to the shared directory on the slave nfs-server.
/ / execute the script:
[root@client ~] # sh nfs.sh & # Let it run in the background
/ / next, shut down or stop the service of the main nfs-server host (simulated downtime).
/ / finally verify that the client detects it and mounts the directory to the slave nfs-server server.
[root@client ~] # cd / test [root@client test] # lsnfs-share [root@client test] # lsnfs-share / 123.txt index.html index.php
-- you can see that when the primary nfs-server fails, the script will be remounted to the standby nfs-server so that the data will not be lost and the running service will not be interrupted.
-this is the end of this article. Thank you for reading-