web1 | 192.168.183.175 CentOS7 |
web2 | 192.168.183.176 CentOS7 |
NFS | 192.168.183.163 ubuntu18.04 |
root@NFS:~# apt install nfs-kernel-server rpcbind
root@NFS:~# vim /etc/exports
root@NFS:~# tail -1 /etc/exports
/data 192.168.183.*(rw,sync,no_subtree_check,no_root_squash)
root@NFS:~# mkdir /data
root@NFS:~# chmod 777 /data
root@NFS:~# echo $HOSTNAME > /data/README.md
root@NFS:~# systemctl restart nfs-kernel-server.service
root@NFS:~# systemctl restart rpcbind
root@NFS:~# systemctl enable nfs-kernel-server
Synchronizing state of nfs-kernel-server.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable nfs-kernel-server
root@NFS:~# systemctl enable rpcbind
Synchronizing state of rpcbind.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable rpcbind
root@NFS:~# showmount -e 192.168.183.163
Export list for 192.168.183.163:
/data 192.168.183.*
[root@node1 ~]# yum install nfs-utils
[root@node1 ~]# showmount -e 192.168.183.163
Export list for 192.168.183.163:
/data 192.168.183.*
[root@node1 ~]# mkdir /NFS
[root@node1 ~]# mount -t nfs 192.168.183.163:/data /NFS
[root@node1 ~]# ls /NFS/
README.md
[root@node1 ~]# cat /NFS/README.md
NFS
[root@node1 ~]# echo $HOSTNAME >> /NFS/README.md
[root@node1 ~]# cat /NFS/README.md
NFS
node1
root@NFS:~# nfsstat
Server rpc stats:
calls badcalls badfmt badauth badclnt
92 0 0 0 0 Server nfs v4:
null compound
2 2% 90 97% Server nfs v4 operations:
op0-unused op1-unused op2-future access close
0 0% 0 0% 0 0% 9 3% 4 1%
commit create delegpurge delegreturn getattr
0 0% 0 0% 0 0% 2 0% 66 24%
getfh link lock lockt locku
6 2% 0 0% 0 0% 0 0% 0 0%
lookup lookup_root nverify open openattr
4 1% 0 0% 0 0% 5 1% 0 0%
open_conf open_dgrd putfh putpubfh putrootfh
0 0% 0 0% 71 26% 0 0% 4 1%
read readdir readlink remove rename
2 0% 2 0% 0 0% 0 0% 0 0%
renew restorefh savefh secinfo setattr
0 0% 0 0% 0 0% 0 0% 0 0%
setcltid setcltidconf verify write rellockowner
0 0% 0 0% 0 0% 1 0% 0 0%
bc_ctl bind_conn exchange_id create_ses destroy_ses
0 0% 0 0% 2 0% 2 0% 0 0%
free_stateid getdirdeleg getdevinfo getdevlist layoutcommit
0 0% 0 0% 0 0% 0 0% 0 0%
layoutget layoutreturn secinfononam sequence set_ssv
0 0% 0 0% 2 0% 86 31% 0 0%
test_stateid want_deleg destroy_clid reclaim_comp allocate
0 0% 0 0% 0 0% 2 0% 0 0%
copy copy_notify deallocate ioadvise layouterror
0 0% 0 0% 0 0% 0 0% 0 0%
layoutstats offloadcancel offloadstatus readplus seek
0 0% 0 0% 0 0% 0 0% 0 0%
write_same
0 0%
root@NFS:~# rpcinfo
root@NFS:~# useradd -s /bin/nologin nfsuser
root@NFS:~# getent passwd nfsuser
nfsuser:x:1001:1001::/home/nfsuser:/bin/nologin
root@NFS:~# vim /etc/exports
root@NFS:~# fg
vim /etc/exports
root@NFS:~# tail -1 /etc/exports
/data 192.168.183.*(rw,sync,no_subtree_check,anOnuid=1001,anOngid=1001)
root@NFS:~# exp
expand expiry export exportfs expr
root@NFS:~# exportfs -rv
exporting 192.168.183.*:/data
root@NFS:~# chown -R nfsuser.nfsuser /data/
使用autofs自动挂载或者写入/etc/fstab,autofs相对来说更稳妥一点,因为据我的经历,若NFS服务器挂掉,那么web端开机会因为挂载不到NFS盘而开不了机,这还是比较危险的
[root@node1 ~]# yum install -y autofs
[root@node1 ~]# vim /etc/auto.master
[root@node1 ~]# grep nfs /etc/auto.master
/NFS /etc/nfs.misc
[root@node1 ~]# vim /etc/nfs.misc
[root@node1 ~]# grep '' /etc/nfs.misc
web1 -rw 192.168.183.128:/data/
[root@node1 ~]# systemctl restart autofs
[root@node1 ~]# systemctl enable autofs
Created symlink from /etc/systemd/system/multi-user.target.wants/autofs.service to /usr/lib/systemd/system/autofs.service.
[root@node1 ~]# mkdir -p /NFS/web1
[root@node1 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 50G 1.2G 49G 3% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 7.7M 480M 2% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda1 1014M 130M 885M 13% /boot
/dev/mapper/centos-home 147G 33M 147G 1% /home
tmpfs 98M 0 98M 0% /run/user/1000
[root@node1 ~]# ls /NFS/web1
README.md
[root@node1 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 50G 1.2G 49G 3% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 7.7M 480M 2% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda1 1014M 130M 885M 13% /boot
/dev/mapper/centos-home 147G 33M 147G 1% /home
tmpfs 98M 0 98M 0% /run/user/1000
192.168.183.128:/data 118G 4.9G 107G 5% /NFS/web1
[root@node1 ~]# date >> /NFS/web1/node1_`date +%F`.txt
[root@node1 ~]# cat /NFS/web1/node1_2018-12-16.txt
2018年 12月 16日 星期日 22:28:53 CST
[root@node1 ~]# vim /etc/logrotate.d/nginx
[root@node1 ~]# cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {daily #转储周期为每天missingok #如果日志丢失,不报错,继续滚动下一个日志rotate 30 #保留30个备份dateext #使用当前日期作为命名格式compress #通过gzip压缩转储以后的日志,nocompress表示不对日志做gzip压缩处理delaycompress #转储的日志到下一次转储时才使用notifempty #当日志文件未空时,不进行轮转create 640 nginx adm 轮转时指定创建新文件的属性sharedscripts #运行postrotate脚本,作用是在所有日志都轮转后统一执行一次脚本postrotate #在logrotate转储之后需要执行的指令if [ -f /var/run/nginx.pid ]; thenkill -USR1 `cat /var/run/nginx.pid`fiendscript #脚本结束
}
[root@node1 ~]#
[root@node1 ~]# vim /etc/logrotate.d/php7.0-fpm
/var/log/php/*.log {rotate 30dailydateextmissingoknotifemptycompressdelaycompress
}
[root@node1 ~]# logrotate -d /etc/logrotate.d/nginx
reading config file /etc/logrotate.d/nginx
Allocating hash table for state file, size 15360 BHandling 1 logsrotating pattern: /var/log/nginx/*.log after 1 days (30 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.loglog does not need rotating (log has been already rotated)considering log /var/log/nginx/error.loglog does not need rotating (log has been already rotated)not running postrotate script, since no logs were rotated
[root@node1 ~]# logrotate -f /etc/logrotate.d/nginx
[qqq@node1 ~]$ vim /etc/php/7.0/fpm/php.ini
log_errors = On
error_log = /var/log/php/php_errors.log
[root@node1 ~]# vim /etc/php/7.0/fpm/pool.d/www.conf
slowlog = /var/log/php/slow.log
request_slowlog_timeout = 2s
[root@node1 ~]# rsync -avzH /var/log/nginx /NFS/web1
sending incremental file list
nginx/
nginx/access.log
nginx/error.log
nginx/ls.logsent 3,342 bytes received 77 bytes 6,838.00 bytes/sec
total size is 8,000 speedup is 2.34
[root@node1 ~]# ls /NFS/web1/nginx/ -l
总用量 8
-rw-r----- 1 nginx adm 0 12月 16 22:49 access.log
-rw-r----- 1 nginx adm 0 12月 16 22:49 error.log
-rw-r--r-- 1 root root 8000 12月 16 23:42 ls.log
[root@node1 ~]# rsync -avzH /var/log/php /NFS/web1/
sending incremental file list
php/
php/error.logsent 128 bytes received 39 bytes 111.33 bytes/sec
total size is 5 speedup is 0.03
[root@node1 ~]# ls /NFS/web1/php/ -l
总用量 4
-rw-r--r-- 1 root root 5 12月 16 23:53 error.log
[root@node1 ~]# crontab -e -u root
[root@node1 ~]# crontab -l
* */2 * * * /usr/bin/rsync -avzH /var/log/nginx /NFS/web1/
* */2 * * * /usr/bin/rsync -avzH /var/log/php /NFS/web1/