lvs+DR置文档:(64位机器)
#create by laoseng
#lvs最好的是用url进行健康检测。但是端口方便,一般用。在配置文件,其中的url字符串是md5加密的值.学习补充!lvs重启后恢复需要15s
环境:
系统版本:Centos5.4(64位)
软件版本:ipvsadm-1.24.tar.gz,keepalived-1.1.17.tar.gz
lvs1(Master):
eth0: 192.168.1.131
eth1:192.168.2.131
vip: ip:192.168.1.133
lvs2(Backup):
eth0: 192.168.1.132
eth1:192.168.2.132
vip: 暂时无
#建议内网外ip分配采用最后8位相同的方式,这样便于管理
web:
realServer1 eth0:192.168.1.134
realServer2 eth0:192.168.1.135
mkdir -p /tool/laoseng/lvs
cd /tool/laoseng/lvs
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz
modprobe ip_vs #把ip_vs模块加载到内核
lsmod|grep ip_vs
cat /etc/redhat-release
uname -r
yum install -y kernel-devel
ln -s /usr/src/kernels/2.6.32-220.el6.i686 /usr/src/linux
2.6.32-220.el6.i686
#安装ipvsadm软件
cd /tool/laoseng/lvs
tar zxvf ipvsadm-1.24.tar.gz
cd ipvsadm-1.24
make
make install
cd ../
#安装keepalived软件
cd /tool/laoseng/lvs
tar zxvf keepalived-1.1.17.tar.gz
cd keepalived-1.1.17
./configure
make
make install
cd ..
#查看安装状况并复制到启动目录下:
ls -l /usr/local/sbin/keepalived
ls -l /usr/local/etc/rc.d/init.d/keepalived
ls -l /usr/local/etc/sysconfig/keepalived
ls -l /usr/local/etc/keepalived
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
service keepalived start
#配置转发及其防火墙
sed -i 's#net.ipv4.ip_forward = 0#net.ipv4.ip_forward=1#' /etc/sysctl.conf
sysctl -p
#配置文档keepalived.conf
vim /etc/keepalived/keepalived.conf
#====================lvs1上的======start=======================
! Configuration File for keepalived
global_defs { #全局配置
notification_email {
1572665580@qq.com #通知邮件。
}
notification_email_from 1572665580@qq.com
smtp_server 127.0.0.1 #连接本机的mail
smtp_connect_timeout 30
router_id LVS_laoseng01 #唯一的lvs1和lvs2应该配置不同名字。这里设置lvs1:LVS_laoseng01 lvs2:LVS_laoseng02
}
vrrp_instance VI_1 { #一个实例,一个实例代表一个服务
state MASTER #lvs1和lvs2上设置不同,lvs1:MASTER lvs2:BACKUP
interface eth0 #获取数据的网卡,一般生产环境上是外网的ip。
lvs_sync_daemon_inteface eth1
virtual_router_id 51
priority 150 #备份服务上优先级要低于100,如改为90
advert_int 1 #检测的间隔,按秒。
authentication { #检测密钥,就是接头暗号。
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #是对外服务的ip。就是让用户访问的。VIP.就是以后网站对外提供服务的ip。域名需要绑定此ip
#192.168.200.16
192.168.1.133
}
}
virtual_server 192.168.1.133 80 { #那个vip提供转发,就是接受到用户请求后转发的ip
delay_loop 6 #轮训
lb_algo wrr #算法
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50 #会话保持 50秒
protocol TCP #tcp服务
real_server 200.166.188.108 80 { #转发给的真是的webip
weight 1
TCP_CHECK {
connect_timeout 8 #超时时间
nb_get_retry 3 #尝试次数。
delay_before_retry 3 #延迟。
connect_port 80 #连接端口。
}
}
real_server 200.166.188.109 80 {
weight 1
TCP_CHECK {
connect_timeout 8
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
#====================lvs1上的======end=========================
#lvs2上配置更改根据上边。
#启动服务
/etc/init.d/keepalived start
#查看进程和lvs状态
ps -ef |grep keepalived
ipvsadm -L -n
#==========start======================在web realserver服务器=================================
#在每个web端执行脚本ipvs_rs可以放到/usr/local/sbin/
#!/bin/bash
# Written by laoseng (1572665580@qq.com)
# description: Config realserver lo and apply noarp
WEB_VIP=192.168.1.133
. /etc/rc.d/init.d/functions #加载系统的函数库。
case "$1" in
start)
ifconfig lo:0 $WEB_VIP netmask 255.255.255.255 broadcast $WEB_VIP #绑定ip.linux上一个网卡可以绑定多个,lo:0中的0只能到255
/sbin/route add -host $WEB_VIP dev lo:0 #添加一个主机路由到lo:0上
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore #下边的四行是抑制arp响应。通过的arp欺骗来完成。让lvs选择发给谁,而不是让他们自动响应。
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $WEB_VIP >/dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stoped"
;;
status)
# Status of LVS-DR real server.
islothere=`/sbin/ifconfig lo:0 | grep "$WEB_VIP"`
isrothere=`netstat -rn | grep "lo:0" | grep "$web_VIP"`
if [ ! "$islothere" -o ! "isrothere" ];then
# Either the route or the lo:0 device
# not found.
echo "LVS-DR real server Stopped."
else
echo "LVS-DR Running."
fi
;;
*)
# Invalid entry.
echo "$0: Usage: $0 {start|status|stop}"
exit 1
;;
esac
exit 0
#==========end======================在web realserver服务器===================================
#查看ip状态
ifconfig
#解决694 udp端口
iptables -A INPUT -p udp --dport 694 -j APPCPT;