发布: 更新时间:2024-06-22 18:50:48
host-61-118 : 192.168.61.118
host-61-119:192.168.61.119
vip:192.168.61.220
[root@host-61-118 ~]# ssh -V
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
sed -i s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config
1️⃣ 两台服务器配置配置SFTP:
1. 创建SFTP组
# groupadd sftp
2. 创建sftp用户,用户名为sftp,密码为mysftp
# useradd -g sftp -s /bin/false mysftp
# echo 'mysftp'|passwd --stdin myssftp
3. 创建sftp数据存储目录sftp,并将mysftp默认目录指到该目录下
# mkdir /sftp/mysftp -pv
# usermod -d /sftp/mysftp mysftp
4. 配置sshd_config,严格按照如下配置,否则重启sshd后,sftp可登录,但ssh远程登陆失败现象!
# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
......
Subsystem sftp internal-sftp
Match Group sftp
ChrootDirectory /sftp/%u
ForceCommand internal-sftp
AllowTcpForwarding no
X11Forwarding no
......
5. 设置chroot目录权限
# chown root:sftp /sftp/mysftp
# chmod 755 /sftp/mysftp
6. 建立SFTP用户登录后可写入的数据目录
# 按照上述配置,ssh重启后用户mysftp已可登录,但是使用chroot指定根目录后,根目录是无法写入的,所以要新建一个目录供mysftp上传文件。该目录的所有者为mysftp,所有组为sftp,所有者有写权限,所有组无写权限
# mkdir /sftp/mysftp/upload
# chown mysftp:sftp /sftp/mysftp/upload
# chmod 755 /sftp/mysftp/upload
7. 重启sshd服务
systemctl restart sshd
8. 验证SFTP环境
[root@host-61-118 ~]# sftp mysftp@host-61-118
The authenticity of host 'host-61-118 (192.168.61.118)' can't be established.
ECDSA key fingerprint is SHA256:kOHc1UsswEh7edXbzH8bSa6fLkPNn9IxkOmTvV7YWdY.
ECDSA key fingerprint is MD5:04:a1:33:24:b9:cc:fb:33:58:05:a5:bb:7f:8f:e1:e0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'host-61-118' (ECDSA) to the list of known hosts.
mysftp@host-61-118's password:
Connected to host-61-118.
sftp> ls
upload
sftp> cd upload/
sftp> ls
9. 如果ssh是非22端口,比如是6666端口,则连接命令:sftp -o port=6666 mysftp@host-61-118
2️⃣ host-61-119搭建rsync服务端,用作备份存储服务器
1. 安装rsync
yum install -y rsync
2. 修改rsync配置文件,
# 注意:uid 填写的是root,gid填写的是sftp,是因为/sftp
[root@host-61-119 ~]# grep -Ev "^$|#" /etc/rsyncd.conf
uid = root
gid = sftp
use chroot = no
max connections = 200
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
ignore errors
list = false
read only = false
transfer logging = yes
log file = /top/rsyncd.log
auth users = rsync_user
secrets file = /etc/syncd.passwd
hosts allow = 10.111.61.118/24
hosts deny = 0.0.0.0/24
timeout = 900
[sftp_backup]
path = /sftp/mysftp
3. 创建用户认证文件
echo 'rsync_user:my_rsync' >/etc/syncd.passwd
4. 设置文件访问权限
chmod 600 /etc/rsyncd.conf
chmod 600 /etc/syncd.passwd
5. 重启rsync服务,并配置开机自启
systemctl restart rsyncd.service
systemctl enable rsyncd.service
3️⃣ host-61-118 搭建rsync客户端,并配置sersync数据实时同步
1. 安装rsync
yum install -y rsync
2. 创建rsync认证所需的密码文件
[root@host-61-118 ~]# cat /etc/rsync.passwd
my_rsync
3. 设置密码文件为600权限权限
chmod 600 /etc/rsync.passwd
4. 下载sersync,并配置环境变量
tar -zxf sersync2.5.4_64bit_binary_stable_final.tar.gz
# sersync目录只有两个文件:一个是二进制程序文件,一个是xml格式的配置文件
[root@host-61-118 /opt]# tree GNU-Linux-x86/
GNU-Linux-x86/
├── confxml.xml
└── sersync2
mv GNU-Linux-x86/sersync2 /bin/
mv GNU-Linux-x86/confxml.xml /etc/
5. 修改sersync配置文件
[root@host-61-118 /opt]# cat /etc/confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<!--是否开启debug调试-->
<debug start="false"/>
<fileSystem xfs="false"/>
<!--不开启文件过滤功能,当为true时,以下类型的文件将不同步-->
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<!--监控事件,默认监控delete/close_write/moved_from/moved_to/create folder-->
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<!--修改此行为true,文件属性变化后也会同步-->
<attrib start="true"/>
<modify start="false"/>
</inotify>
<!--rsync的命令配置段-->
<sersync>
<!--需要同步的源目录或文件,建议同步目录-->
<localpath watch="/sftp/mysftp">
<!--# 指定备份服务器地址和rsync daemon的模块名,如果下面开启了ssh start,此时name为远程shell方式运行时的目标目录-->
<remote ip="192.168.61.119" name="sftp_backup"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<!-- 指定rsync的选项:
-a 归档模式,表示递归传输并保持文件属性。
-r 归档模式,表示递归传输并保持文件属性。
-t 保持mtime属性。强烈建议任何时候都加上"-t",否则目标文件mtime会设置为系统时间,导致下次更新检查出mtime不同从而导致增量传输无效。
-u 仅在源mtime比目标已存在文件的mtime新时才拷贝。注意,该选项是接收端判断的,不会影响删除行为
-z 传输时进行压缩提高效率
-->
<commonParams params="-artuz"/>
<!--修改为true,指定备份服务器的rsync配置的用户和密码文件-->
<auth start="true" users="rsync_user" passwordfile="/etc/rsync.passwd"/>
<!--指定rsync的非标准端口号-->
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<!--默认使用rsync daemon运行rsync命令,true为使用远程shell模式-->
<ssh start="false"/>
</rsync>
<!--#错误重传及日志文件路径-->
<failLog path="/tmp/rsync_fail_log.log" timeToExecute="60"/><!--default every 60mins execute once-->
<!--不开启crontab功能-->
<crontab start="false" schedule="600"><!--600mins-->
<!--不开启crontab定时传输的筛选功能-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>
<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>
5. 查看sersync2 -h帮助
[root@host-61-118 /opt]# sersync2 -h
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
_______________________________________________________
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
______________________________________________________
6. 以后台方式启动同步
sersync2 -dro /usr/local/sersync/confxml.xml
#sersync支持多实例,也即监控多个目录时,只需分别配置不同配置文件,然后使用sersync2指定对应配置文件运行
[root@host-61-118 /opt]# sersync2 -rd -o /etc/sersync.d/nginx.xml
7. 配置sersync开机自启动
echo '/bin/sersync2 -dro /etc/confxml.xml' >>/etc/rc.local
chmod +x /etc/rc.local
#这样,host-61-118机器的/sftp/mysftp目录下的文件就会自动实时同步到host-61-119机器的/sftp/mysftp目录下
# 注意:这是单向实时同步!如果要想做双向实时同步!那就需要在host-61-119机器上再启动一个sersync(同时,host-61-118也要做个rsyncd.conf文件)
4️⃣ SFTP结合keepalived做双机高可用
1. 两台机器同时安装keepalived
yum install -y keepalived
# host-61-118 keepalived.conf 配置文件
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script check_sshd {
script "/etc/keepalived/check_sshd.sh"
interval 2
weight -5
fall 3
rise 2
}
vrrp_instance VI_1 {
state MASTER
interface ens192
mcast_src_ip 192.168.61.118
virtual_router_id 220
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass 1220
}
virtual_ipaddress {
192.168.61.220/24
}
track_script {
check_sshd
}
}
# host-61-119 keepalived.conf 配置文件
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script check_sshd {
script "/etc/keepalived/check_sshd.sh"
interval 2
weight -5
fall 3
rise 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
mcast_src_ip 192.168.61.119
virtual_router_id 220
priority 70
advert_int 2
authentication {
auth_type PASS
auth_pass 1220
}
virtual_ipaddress {
192.168.61.220/24
}
track_script {
check_sshd
}
}
2. 配置check_sshd.ssh检测脚本
#!/bin/bash
source /etc/profile
if [ "$(/bin/systemctl is-active sshd)" != "active" ];then
/bin/systemctl restart sshd
sleep 5
if [ "$(/bin/systemctl is-active sshd)" != "active" ];then
/bin/systemctl restart keepalived
fi
fi
3. 启动keepalived,并配置开机自启动
systemctl start keepalived.service
systemctl enable keepalived.service
4. 高可用测试:
-> 先关闭host-61-118机器的Keepalived服务,发现vip资源就会自动漂到host-61-119机器上继续提供服务。
当host-61-118机器的Keepalived服务恢复后,vip资源就会自动抢占回来。
-> 关闭host-61-118机器的ssh服务,通过脚本会自动启动ssh服务,当启动失败后,会强制重启Keepalived服务,从而实现vip资源的漂移!
5️⃣ 测试实时同步
[root@host-61-119 /opt]# sftp mysftp@host-61-220
mysftp@host-61-220's password:
Connected to host-61-220.
sftp> ls
upload
sftp> cd upload/
sftp> put MegaCli.tar.gz
Uploading MegaCli.tar.gz to /upload/MegaCli.tar.gz
MegaCli.tar.gz 100% 2779KB 15.7MB/s 00:00
sftp>
[root@host-61-118 /sftp/mysftp]# stat upload/MegaCli.tar.gz
File: ‘upload/MegaCli.tar.gz’
Size: 2845866 Blocks: 5560 IO Block: 4096 regular file
Device: 803h/2051d Inode: 270248282 Links: 1
Access: (0644/-rw-r--r--) Uid: (20001/ mysftp) Gid: (20001/ sftp)
Context: unconfined_u:object_r:default_t:s0
Access: 2024-06-22 15:37:52.260541142 +0800
Modify: 2024-06-22 15:37:42.106734109 +0800
Change: 2024-06-22 15:37:42.106734109 +0800
Birth: -
[root@host-61-119 /sftp/mysftp]# stat upload/MegaCli.tar.gz
File: ‘upload/MegaCli.tar.gz’
Size: 2845866 Blocks: 5560 IO Block: 4096 regular file
Device: 803h/2051d Inode: 404149882 Links: 1
Access: (0644/-rw-r--r--) Uid: (20001/ mysftp) Gid: (20001/ sftp)
Access: 2024-06-22 15:37:52.372865018 +0800
Modify: 2024-06-22 15:37:42.106734109 +0800
Change: 2024-06-22 15:37:52.372865018 +0800
Birth: -