服务器上的配置(web1)
[root@web1 ~]# vim /etc/rsyncd.conf
## file of /etc/rsyncd.conf
motd file=/var/rsync/welcome.msg
pid file = /var/rsync/rsyncd.pid
lock file = /var/rsync/rsync.lock
log file = /var/rsync/rsyncd.log
[apache]
comment = hello,backup!welcome!
path = /usr/local/apache2
use chroot = no
max connections = 4
read only = yes
list = false
uid = nobody
gid = nobody
secrets file = /etc/rsyncd.srt
hosts allow = 192.168.0.224
hosts deny = 0.0.0.0/0
ignore errors = yes
transfer logging = yes
log format = "%a %f %l"
auth users = backup
上面定义了1个备份点,只允许 192.168.0.224 连接,需要验证的用户名为 backup
[root@web1 ~]# chmod 600 /etc/rsyncd.srt
创建运行目录以及启动服务:
[root@web1 ~]# mkdir /var/rsync
[root@web1 ~]# rsync --daemon
[root@web1 ~]# vim /etc/xinetd.d/rsync
service rsync
{
disable = no (将yes改成no)
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
检查rsync是否启动:
[root@web1 ~]# ps -ef | grep rsync
root 22256 1 0 16:19 ? 00:00:00 rsync --daemon
停止服务:
[root@web1 ~]# kill `cat /var/rsync/rsyncd.pid`
[root@web2 ~]# echo "123456" > /etc/rsyncd.srt
[root@web2 ~]# chmod 600 /etc/rsyncd.srt
[root@web2 ~]# vim /var/scripts/rsync.sh
#!/bin/bash
unset verb progress
for i in $*; do
[ "$i" = "verb" ] && verb=v
[ "$i" = "progress" ] && progress="--progress"
done
RSYNC_SERVER=192.168.0.223
AUTH_USER=backup
BAKROOT=/var/backup/
INC_DIR=increment
NOW=`date +%Y-%m-%d_%H:%M`
[ ! -e $BAKROOT ] && mkdir -p $BAKROOT
PASSWD_FILE=/etc/rsyncd.srt
PASSWD_PERM=`ls -l $PASSWD_FILE|awk '{print $1}'`
if [ "$PASSWD_PERM" != "-rw-------" ]; then
echo -e "\nWARNING: permission of passwd file changed to 0600 \n";
chmod 0600 $PASSWD_FILE
fi
LOGFILE=/var/log/rsync/rsyncbak.log
EXCLUDES=/var/log/rsync/exclude_file
[ ! -e $EXCLUDES ] && touch $EXCLUDES
BACKUP_MODULES="apache"
OPTIONS="--force --ignore-errors --delete --delete-excluded \
--exclude-from=$EXCLUDES $progress \
--password-file=$PASSWD_FILE --backup"
rm -f /var/log/rsync/log.*
log_id=0
for bakdir in $BACKUP_MODULES; do
log_id=`expr $log_id + 1`
SUBOPTS="--backup-dir=$BAKROOT/$INC_DIR/$BACKUP_MODULES/$NOW -az${verb}"
rsync $OPTIONS $SUBOPTS $AUTH_USER@$RSYNC_SERVER::$bakdir \
$BAKROOT/$bakdir | tee /var/log/rsync/log.$log_id
done
cat /var/log/rsync/log.* >> $LOGFILE
rm -f /var/log/rsync/log.*
#end of rsync.sh
[root@web2 ~]# chmod a+x /var/scripts/rsync.sh
[root@web2 ~]# mkdir /var/log/rsync
开始备份:
[root@web2 ~]# /var/scripts/rsync.sh verb progress
备份时,会自动从密码文件读取密码进行验证,备份的详细信息及进度会在当前终端输出,同时也记录到 backup.log 文件中。
在dns.eyo.com(网关服务器)编写iptables脚本:
[root@dns named]# vim /var/scriptd/ipt_nat.sh
#!/bin/bash
IPT="/sbin/iptables"
OUT_IP="173.16.16.1"
OUT_IF="eth0"
LAN_NET="192.168.0.0/24"
LAN_IF="eth1"
$IPT -F
$IPT -t nat -F
$IPT -t mangle -F
$IPT -t raw -F
$IPT -X
$IPT -P INPUT DROP
$IPT -P FORWARD DROP
$IPT -P OUTPUT ACCEPT
/sbin/sysctl -w net.ipv4.ip_forward=1
$IPT -A INPUT -s $LAN_NET -i $LAN_IF -j ACCEPT
$IPT -A INPUT -i $OUT_IF -p udp --dport 53 -j ACCEPT
$IPT -A FORWARD -s $LAN_NET -p tcp -m multiport --dport 22,80 -j ACCEPT
$IPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPT -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPT -t nat -A POSTROUTING -s $LAN_NET -o $OUT_IF -j SNAT --to-source $OUT_IP
$IPT -t nat -A PREROUTING -i $OUT_IF -d $OUT_IP -p tcp --dport 80 -j DNAT --to-destination 192.168.0.225
$IPT -t nat -A PREROUTING -i $OUT_IF -d $OUT_IP -p tcp --dport 222 -j DNAT --to-destination 192.168.0.223:22
$IPT -t nat -A PREROUTING -i $OUT_IF -d $OUT_IP -p tcp --dport 2222 -j DNAT --to-destination 192.168.0.224:22
$IPT -A FORWARD -d 192.168.0.225 -p tcp --dport 80 -j ACCEPT
$IPT -A FORWARD -d 192.168.0.223 -p tcp --dport 22 -j ACCEPT
$IPT -A FORWARD -d 192.168.0.224 -p tcp --dport 22 -j ACCEPT
此防火墙规则保证了内网能访问外网,将内网的服务器发布到公网,并且在公网上能ssh内网的服务器,以便管理员能及时解决问题
[root@dns ~]# chmod a+x /var/scriptd/ipt_nat.sh
[root@dns ~]# echo "/var/scriptd/ipt_nat.sh" >> /etc/rc.local
可以运行脚本进行测试,验证的时候内网主机的网关必须指向防火墙主机的内网卡,并且必须注意,一般修改网关后都需重启网络服务,但之前的lo:0的网卡会消失,所以重启网络服务后,记得再运行一次之前的lvs.sh脚本
[root@dns ~]# /var/scriptd/ipt_nat.sh
net.ipv4.ip_forward = 1
测试的时候可以利用另外一台虚拟机,IP设置为173.16.16.16/24,DNS指向173.16.16.1来模拟外网的PC机,它应该能够访问www.eyo.com,可以通过在内网的两台服务器上执行以下命令得到结论:
[root@web1 ~]# tail -f /usr/local/apache2/logs/www.eyo.com.access.com
173.16.16.16 - - [11/Jul/2011:17:47:50 +0800] "GET /images/201105/thumb_img/71_thumb_G_1306115963245.jpg HTTP/1.1" 200 3246 "http://www.eyo.com/" "Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.0.12) Gecko/2009070811 Red Hat/3.0.12-1.el5_3 Firefox/3.0.12"
173.16.16.16 - - [11/Jul/2011:17:47:50 +0800] "GET /themes/default/images/news.gif HTTP/1.1" 200 72 "http://www.eyo.com/themes/default/style.css" "Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.0.12) Gecko/2009070811 Red Hat/3.0.12-1.el5_3 Firefox/3.0.12"
[root@web2 ~]# tail -f /usr/local/apache2/logs/www.eyo.com.access.com
173.16.16.16 - - [11/Jul/2011:17:47:51 +0800] "GET /themes/default/images/whitebg.gif HTTP/1.1" 304 -
173.16.16.16 - - [11/Jul/2011:17:47:51 +0800] "GET /themes/default/images/blockbg.gif HTTP/1.1" 304 -
173.16.16.16 - - [11/Jul/2011:17:47:51 +0800] "GET /themes/default/images/tbfwbz.gif HTTP/1.1" 304 -
173.16.16.16 - - [11/Jul/2011:17:47:51 +0800] "GET /themes/default/images/tbpzbz.gif HTTP/1.1" 304 -
173.16.16.16 - - [11/Jul/2011:17:47:51 +0800] "GET /themes/default/images/helpdot.gif HTTP/1.1" 304 -
173.16.16.16 - - [11/Jul/2011:17:47:51 +0800] "GET /themes/default/images/help.gif HTTP/1.1" 304 -
通过日志文件证明,访问网站的IP地址确实是外网的,并且同时也验证了负载均衡。
另外还可以在内网的服务器上直接访问173.16.16.16的http服务(也可以在DNS服务器上构建域名进行访问)
还可以在外网PC上进行SSH测试:
当访问222端口时转到内网的web1:
[root@wan ~]# ssh -p 222 173.16.16.1
[email protected]'s password:
Last login: Mon Jul 11 18:05:32 2011 from 173.16.16.16
[root@web1 ~]# hostname
web1.eyo.com
当访问2222端口时转到内网的web2:
[root@wan ~]# ssh -p 2222 173.16.16.1
The authenticity of host '173.16.16.1 (173.16.16.1)' can't be established.
RSA key fingerprint is b2:d4:a5:01:d0:44:58:40:c2:95:6e:e0:7f:b6:36:22.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '173.16.16.1' (RSA) to the list of known hosts.
[email protected]'s password:
Last login: Fri Jun 3 13:07:19 2011
[root@web2 ~]# hostname
web2.eyo.com