1. 程式人生 > >專案1:FTP客戶源站

專案1:FTP客戶源站

之前使用的第三方搭建的ftp客戶偽源因機房搬遷問題,因此需要構建ftp源站,因為之前採用ftp主從模式,遇到故障需手動遷移,現在將增加一層keepalived進行通過vip訪問,自動故障轉移。(因為公網IP和客戶域名不方便透露,這裡採用內網的IP以及測試域名)

伺服器資訊:

#ip地址:

  centos6.7   192.168.43.200 (主)=》BJ_CTL_43_200:主

  centos6.7   192.168.43.201 (主)=》BJ_CTL_43_201:從

#磁碟資訊:

  12*8T容量

#記憶體資訊:

  126G

一:軟raid5配置

raid分為硬raid和軟raid,其功能相同,但是因為軟raid沒有獨立的硬體控制,因此效能上不如硬raid,但是軟raid配置實現簡單

常用raid級別:raid0,raid1,raid5,raid10(raid10就是raid0和raid1)各級別對比如下

 

注:n表示磁碟總數,raid5可以壞一塊磁碟

這裡採用的raid5,以下是raid5的配置

1:安裝工具
yum -y install mdadm parted
2:進行磁碟分割槽
parted -s /dev/sdb mklabel gpt mkpart primary 1 100%
parted -s /dev/sdc mklabel gpt mkpart primary 1 100%
parted -s /dev/sdd mklabel gpt mkpart primary 1 100%
3:建立raid5
mdadm -C /dev/md0 -l5 -n3 /dev/sdb1 /dev/sdc1 /dev/sdd1
說明:
    -C表示建立
    -l指定raid級別
    -n表示磁碟個數
    /dev/md0表示裝置名稱
    -x表示有幾塊熱備盤
4:建立掛載點
mkdir /cache
5:格式化磁碟
mkfs.ext4 /dev/md0
6:掛載磁碟
mount /dev/md0 /cache/
7:加入到fstab中
echo "/dev/md0                /cache                  ext4    defaults        0 0" >> /etc/fstab
8:準備配置檔案
echo DEVICE /dev/sd{b..d}1 > /etc/mdadm.conf
mdadm --detail -s >> /etc/mdadm.conf

具體的raid操作詳細見(https://www.cnblogs.com/spadeslinux/p/9063200.html)

二:FTP檔案系統

ftp有兩種工作模式:

  • 主動模式:客戶端隨機開啟一個埠去連線伺服器的21號埠,伺服器收到請求後,接受連線建立一條命令通道,當需要傳輸資料時,客戶端會再次開啟一個大於1024的隨機埠通過之前建立的命令通道傳送給伺服器,然後伺服器的20號埠去連線客戶端的該埠,通過三次握手後建立連線傳輸資料。
  • 被動模式:客戶端隨機開啟一個埠去連線伺服器的21號埠,伺服器收到請求後,接受連線建立一條命令通道,當需要傳輸資料時,客戶端從命令通過傳送資料請求上傳或下載,伺服器收到請求後隨機開啟一個埠並通過命令通道傳送給客戶端,客戶端收到該埠後也會開啟一個埠並連線伺服器的埠,通過三次握手後建立連線傳輸資料。

注意:ftp是基於tcp協議工作的,ftp的兩個資料埠(主動模式20號,被動模式不確定),命令埠為21。

以下是ftp的安裝配置,這裡採用的是主動模式和本地使用者

1:安裝vsftpd
yum -y install vsftpd
2:配置vsftpd(只列出修改部分)
cat /etc/vsftpd/vsftpd.conf
#禁止匿名使用者
anonymous_enable=NO
#將使用者禁錮在自己的家目錄
chroot_local_user=YES
#ftp日誌
xferlog_file=/var/log/ftp.log
3:啟動ftp並配置自啟
/etc/init.d/vsftpd restart
chkconfig vsftpd on
4:新增測試使用者進行上傳下載
useradd -s /sbin/nologin -d /cache/test test
echo "123456"|passwd --stdin test
注意:後期這裡是通過ansible來管理的,因此通過ansible的user模組來建立使用者

#測試上傳

 #測試下載

#檢視日誌

 

注意:i表示為上傳(input),o表示為下載(output)

三:rsync同步

剛剛測試了上傳下載沒有問題,接下來需要通過rsync自動同步資料,安裝單獨的rsync不採用xinetd

剛才將資料傳送給了43.200主節點,接下來配置rsync從下載資料
====================主節點操作
1:安裝rsync
yum -y install rsync 
2:配置rsync
cat /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 180
strict modes = yes
port = 873
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
transfer logging = yes
log format = %t (%p) %o %h (a) %m (%u) %f %l [%n] %M

[test]
path=/cache/test
comment=this is test data
auth users=test
uid=test
gid=test
incoming chmod = u+rwx,g+rwx,o+rx
secrets file = /etc/rsyncd.secrets
read only = no
list=no
3:設定許可權
chmod 777 -R /cache/
4:準備密碼檔案
cat /etc/rsyncd.secrets
test:123456
#許可權必須是600
chmod 600 /etc/rsyncd.secrets
5:啟動rsync
rsync --daemon
============================從節點同步
yum -y install rsync
ls -l /cache/test/
total 0
#客戶端下載
rsync -avzP [email protected]::test /cache/test/
#客戶端上傳
rsync -avzP file [email protected]::test
解析將從配置為主,將主配置為從。

 此時兩個主從節點都已可以互相同步,接下來需要監控cache目錄,只要目錄下發生變化就自動同步。採用rsync+inotify

這裡實時監控cache目錄並進行同步
#rsync配置
cat /etc/rsyncd.conf 
uid = root
gid = root
use chroot = no
max connections = 180
strict modes = yes
port = 873
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
transfer logging = yes
log format = %t (%p) %o %h (a) %m (%u) %f %l [%n] %M

[cache]
path=/cache
comment=this is cache data
auth users=cache
uid=cache
gid=cache
incoming chmod = u+rwx,g+rwx,o+rx
secrets file = /etc/rsyncd.secrets
read only = no
list=no
cat /etc/rsyncd.secrets 
cache:[email protected]#
useradd -s /sbin/nologin -d /cache cache
============================主節點配置
1:下載原始碼inotify包
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
2:編譯安裝
tar -zxf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify
make && make install
3:設定系統環境變數
grep "inotify" /etc/profile
export PATH=$PATH:/usr/local/inotify/bin
source /etc/profile
4:新增庫檔案
grep "inotify" /etc/ld.so.conf
/usr/local/inotify/lib
ldconfig
5:修改inotify預設引數
#檢視預設值引數
sysctl -a | grep max_queued_events
fs.inotify.max_queued_events = 16384
sysctl -a | grep max_user_watches
fs.inotify.max_user_watches = 8192
fs.epoll.max_user_watches = 95682
sysctl -a | grep max_user_instances
fs.inotify.max_user_instances = 128
#修改引數
sysctl -w fs.inotify.max_queued_events="99999999"
sysctl -w fs.inotify.max_user_watches="99999999"
sysctl -w fs.inotify.max_user_instances="65535"
說明:
    max_queued_events:inotify佇列的長度,如果值太小會導致監控檔案不準確
    max_user_watches:要同步的檔案包含多少目錄,必須大於原始檔目錄
    max_user_instances:每個使用者建立inotify例項最大值
6:編寫指令碼實現監控已經同步
cat rsync_inotify.sh 
#!/bin/bash
#監控cache目錄下的檔案,發生改變則同步到備份

backup_dir=/cache
user=cache
backup_ip=192.168.43.201
mod_rsync=cache
#監控目錄
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $backup_dir \
|while read file
do
  #進行上傳
  rsync -az $backup_dir ${user}@${backup_ip}::${mod_rsync} --password-file=/etc/cache.ps
done
7:準備密碼檔案
cat /etc/cache.ps
[email protected]#
chmod 600 /etc/cache.ps
8:執行指令碼
nohup sh rsync_inotify.sh &
9:在cache下載建立一個檔案