1. 程式人生 > >NFS備份全網日誌(NFS+rsync)

NFS備份全網日誌(NFS+rsync)

NFS配置引數參考:http://server.51cto.com/sManage-150923.htm

有8臺web伺服器,現在要搭建一臺NFS伺服器用來存放8臺web的日誌(nginx的錯誤日誌,php的錯誤日誌,php的慢執行日誌)

 

現在模擬一下這個場景


web1 192.168.183.175    CentOS7
web2 192.168.183.176    CentOS7
NFS 192.168.183.163    ubuntu18.04

一、NFS伺服器的搭建:

1.1安裝NFS

[email protected]:~# apt install nfs-kernel-server rpcbind

1.2修改配置檔案,設定要掛載的目錄,允許192.168.183.*主機訪問

[email protected]:~# vim /etc/exports 
[email protected]:~# tail -1 /etc/exports 
/data 192.168.183.*(rw,sync,no_subtree_check,no_root_squash)

1.3建立共享目錄,給足許可權

[email protected]:~# mkdir /data
[email protected]:~# chmod 777 /data
[email protected]:~# echo $HOSTNAME > /data/README.md

1.4重啟nfs-kernel-server,rpcbind並將二者加入開機啟動項

[email protected]:~# systemctl restart nfs-kernel-server.service 
[email protected]:~# systemctl restart rpcbind 
[email protected]
:~# 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 [email protected]:~# 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

1.5檢視可掛載的共享目錄

[email protected]:~# showmount -e 192.168.183.163
Export list for 192.168.183.163:
/data 192.168.183.*

二、web端的設定

2.1安裝nfs客戶端

[[email protected] ~]# yum install nfs-utils

2.2檢視可掛載的共享目錄

[[email protected] ~]# showmount -e 192.168.183.163
Export list for 192.168.183.163:
/data 192.168.183.*

2.3建立目錄,將NFS端的目錄掛載到本地

[[email protected] ~]# mkdir /NFS
[[email protected] ~]# mount -t nfs 192.168.183.163:/data /NFS
[[email protected] ~]# ls /NFS/
README.md
[[email protected] ~]# cat /NFS/README.md 
NFS
[[email protected] ~]# echo $HOSTNAME >> /NFS/README.md 
[[email protected] ~]# cat /NFS/README.md 
NFS
node1

三、在NFS端檢視詳情

3.1檢視NFS執行狀態

[email protected]:~# 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%     

3.2檢視rpc執行情況

[email protected]:~# rpcinfo

3.3優化NFS服務,將登入NFS主機的使用者全部設定成指定的ID

[email protected]:~# useradd -s /bin/nologin nfsuser
[email protected]:~# getent passwd nfsuser 
nfsuser:x:1001:1001::/home/nfsuser:/bin/nologin
[email protected]:~# vim /etc/exports 
[email protected]:~# fg
vim /etc/exports
[email protected]:~# tail -1 /etc/exports 
/data 192.168.183.*(rw,sync,no_subtree_check,anonuid=1001,anongid=1001)
[email protected]:~# exp
expand    expiry    export    exportfs  expr      
[email protected]:~# exportfs -rv
exporting 192.168.183.*:/data
[email protected]:~# chown -R nfsuser.nfsuser /data/

四、讓web機器實現開機自動掛載NFS(autofs)

使用autofs自動掛載或者寫入/etc/fstab,autofs相對來說更穩妥一點,因為據我的經歷,若NFS伺服器掛掉,那麼web端開機會因為掛載不到NFS盤而開不了機,這還是比較危險的

4.1在所有web端安裝autofs

[[email protected] ~]# yum install -y autofs

4.2修改主配置

這相當於定義子配置檔案的位置以及掛載目錄

[[email protected] ~]# vim /etc/auto.master
[[email protected] ~]# grep nfs /etc/auto.master
/NFS	/etc/nfs.misc

4.3編輯子配置

[[email protected] ~]# vim /etc/nfs.misc
[[email protected] ~]# grep '' /etc/nfs.misc 
web1	-rw	192.168.183.128:/data/

4.4重啟autofs服務並加入開機啟動項

[[email protected] ~]# systemctl restart autofs
[[email protected] ~]# systemctl enable autofs
Created symlink from /etc/systemd/system/multi-user.target.wants/autofs.service to /usr/lib/systemd/system/autofs.service.

4.5見證奇蹟的時刻:

注意,可能建立/NFS/web1會提示許可權不足,請關閉autofs服務重試

[[email protected] ~]# mkdir -p /NFS/web1
[[email protected] ~]# 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
[[email protected] ~]# ls /NFS/web1
README.md
[[email protected] ~]# 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
[[email protected] ~]# date >> /NFS/web1/node1_`date +%F`.txt
[[email protected] ~]# cat /NFS/web1/node1_2018-12-16.txt 
2018年 12月 16日 星期日 22:28:53 CST

4.6 其他web同理

五、web機器日誌切割

日誌切割參考:https://www.cnblogs.com/kevingrace/p/6307298.html

所有日誌進行切割處理,每天一次,日誌字尾要加上時間,儲存30天

5.1nginx預設有這個切割,基本不用處理

注意:配置檔案中不要寫後面的註釋,此處僅作演示

[[email protected] ~]# vim /etc/logrotate.d/nginx 
[[email protected] ~]# 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 ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript	#指令碼結束
}
[[email protected] ~]# 

5.2php的指令碼

[[email protected] ~]# vim /etc/logrotate.d/php7.0-fpm
/var/log/php/*.log {
        rotate 30
        daily
        dateext
        missingok
        notifempty
        compress
        delaycompress
}

5.3寫完配置檔案要使用下面命令對配置檔案進行檢查

[[email protected] ~]# logrotate -d /etc/logrotate.d/nginx 
reading config file /etc/logrotate.d/nginx
Allocating hash table for state file, size 15360 B

Handling 1 logs

rotating 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.log
  log does not need rotating (log has been already rotated)considering log /var/log/nginx/error.log
  log does not need rotating (log has been already rotated)not running postrotate script, since no logs were rotated

或者手動讓其輪轉一次看看效果:

[[email protected] ~]# logrotate -f /etc/logrotate.d/nginx 

5.4關於php的日誌需要說明一下:

php正常有三個配置檔案:php.ini,php-fpm.conf,www.conf,具體區別可以網上查

php的錯誤日誌在php.ini中定義,可以自行去看:

[[email protected] ~]$ vim /etc/php/7.0/fpm/php.ini
log_errors = On
error_log = /var/log/php/php_errors.log

php的慢執行日誌在www.conf中

[[email protected] ~]# vim /etc/php/7.0/fpm/pool.d/www.conf
slowlog = /var/log/php/slow.log
request_slowlog_timeout = 2s

六、rsync同步

要使用rsync將日誌同步到NFS盤中

rsync參考:https://www.cnblogs.com/kevingrace/p/5689491.html

rsync參考2:https://www.ilanni.com/?s=rsync

6.1同步nginx的日誌

注意,nginx後面沒有加斜槓,表示同步的nginx目錄,加了斜槓就表示同步nginx目錄裡面的檔案

[[email protected] ~]# rsync -avzH /var/log/nginx /NFS/web1
sending incremental file list
nginx/
nginx/access.log
nginx/error.log
nginx/ls.log

sent 3,342 bytes  received 77 bytes  6,838.00 bytes/sec
total size is 8,000  speedup is 2.34
[[email protected] ~]# 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

6.2同步php日誌(包括php錯誤日誌,php慢執行日誌)

[[email protected] ~]# rsync -avzH /var/log/php /NFS/web1/
sending incremental file list
php/
php/error.log

sent 128 bytes  received 39 bytes  111.33 bytes/sec
total size is 5  speedup is 0.03
[[email protected] ~]# ls /NFS/web1/php/ -l
總用量 4
-rw-r--r-- 1 root root 5 12月 16 23:53 error.log

6.3將rsync加入計劃任務

[[email protected] ~]# crontab -e -u root
[[email protected] ~]# crontab -l
* */2 * * * /usr/bin/rsync -avzH /var/log/nginx /NFS/web1/
* */2 * * * /usr/bin/rsync -avzH /var/log/php /NFS/web1/

七、雜項

7.1web伺服器要ntp同步時間

現在都用chrony了,那就用chrony吧,隨便搜了一篇文章:

https://blog.csdn.net/linuxprobe18/article/details/80460068

7.2zabbix要監控web機器的時區正確

參考:https://blog.csdn.net/qq_33317586/article/details/84102681