负载均衡 LVS
三种(应该是四种)负载均衡技术:不具备抗攻击能力
VS/NAT (通过NAT实现虚拟服务器) 压力都在调度器上 调度器输入内网的数据包会源路返回到外网 对后端的realserver 没有要求只要提供服务就行了
VS/TUN(隧道模式 在原有的模式下继续做封装缺点:数据包过大)(通过IP隧道实现虚拟服务器)成本高 可支持广域网
VS/DR(后端可以挂一百个服务器速度快)(通过直接路由实现虚拟服务器) 必须在内网(在同一个物理网络)
生产环境中主要用第四种:FullNAT(跨WELN)
redhat6:自带10种内核连接调度算法
VS/DR: (核心 更改mac地址 不支持回包)
调度机器:
首先安装一服务:确认一下
ipvsadm -l
ip addr add 172.25.42.130 (vip)
ipvsadm -A -t 172.25.42.130:80 -s rr (rr:论寻算法)
ipvsadm -a -t 172.25.42.130:80 -r 172.25.42.17:80 -g
ipvsadm -a -t 172.25.42.130:80-r 172.25.42.18:80 –g
ipvsadm -l
/etc/init.d/ipvsadm save
realserver1(dd8.example.com) 用apache测试 //对外不响应
yum install httpd -y
/etc/init.d/httpd start
cd /var/www/html
vim index.html
<h1>dd8.example.com</h1>
ipaddr add 172.25.42.130/32 dev eth0
realserver2(dd9.example.com)
yum install httpd -y
/etc/init.d/httpd start
cd /var/www/html
vim index.html
<h1>dd9.example.com</h1>
ip addr add 172.25.42.130/32 dev eth0
如果只做上述操作,发现调度机不起作用
在物理机上:arp -an 172.25.42.130
arp -d 172.25.42.130
ping 172.25.42.130
arp -an 172.25.42.130
会发现,两次IP的mac地址不一样,因为调度机,两台realyserver 上面都有172.25.42.130 这个IP 也就是说这三个相同的IP有三个不同的mac地址 此时ping 这个IP 只会随机选择一个
在 real server上:arptable(arp 防火墙)//此实验中 主要是为了屏蔽real server 上面的vip的mac
yum install arptables_jf.x86_64 -y
arptables -A IN -d 172.25.42.130 -j DROP (对外不发布172.25.42.130)
arptables -A OUT -s 172.25.42.130 -j mangle --mangle-ip-s 172.25.42.17 (谁用 172.25.42.130 访问我 我所回应的都是 172.25.42.17)//一个mac可以对应多个ip,在real server 里面不让vip用这个mac
yum install arptables_jf.x86_64 -y
arptables -A IN -d 172.25.42.130 -j DROP
arptables -A OUT -s 172.25.42.130 -j mangle --mangle-ip-s 172.25.42.17
对后台服务器不具备健康检查:
其中一台real server 的httpd服务关闭
在调度服务器中,发现调度器依然会正常调度:
此时,在物理机上测试:curl:172.25.42.130
优化:(加上健康检查)
调度机:ipvsadm –C
在heartbeat 的前提下(找一个已经安装过的软件包)
cd /etc/ha.d
rpm -qd ldirectord
cp /usr/share/doc/ldirectord-3.9.5/ldirectord.cf .
vim ldirectord.cf
# Sample for an http virtual service
virtual=172.25.42.130:80
real=172.25.42.17:80 gate //real server ip
real=172.25.42.18:80 gate //real server ip
fallback=127.0.0.1:80 gate //realserver 全部挂掉 调度机器(本机)去顶替
service=http
scheduler=rr
#persistent=600 //与存储或数据库等长久需要保持连接的设置有关
#netmask=255.255.255.255
protocol=tcp
checktype=negotiate
checkport=80
request="index.html"
# receive="TestPage"
# virtualhost=www.x.y.z
/etc/init.d/ldirectord start
测试:
关掉real server 的httpd
heartbeat+ldirectord+lvs:(两台已经做好 heartbeat的虚拟机)
调度机器1:/etc/init.d/ldirectord stop
ipvsadm -C
/etc/init.d/httpd stop
ip addr del 172.25.42.130/24 dev eth0 //因为交给集群来做 不需要手动开启和人为干预
vim haresources
dd6.example.com IPaddr::172.25.42.130/24/eth0 ldirectord httpd
scp ldirectord.cf haresources root@172.25.42.16:/etc/ha.d/
/etc/init.d/heartbeat start (两台都做此操作)
real server (手动开启httpd服务)
测试:
两台调度机互相切换:
dd6.example.com:/etc/init.d/heartbeat stop
服务健康切换:
keepalived+lvs:(/etc/init.d/heartbeat stop)
1.安装keepalived (keepalived 自带健康检查)
./configure --prefix=/usr/local/keepalived //源码编译
make && make install
2.(两台调度机都做)
/etc/init.d/heartbeat stop
ln-s/usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln-s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig
ln-s /usr/local/keepalived/etc/keepalived /etc
cd /usr/local/keepalived
cd sbin
ln -s /usr/local/keepalived/sbin/keepalived /sbin
chmod+x /usr/local/keepalived/etc/rc.d/init.d/keepalived
cd /usr/local
scp -r * root@172.25.42.16:/usr/local
cd /etc/keepalived
vim keepalived
yum install mailx -y
/etc/init.d/keepalived start
增加服务 vsftpd
/etc/init.d/keepalived stop
real server :yum install vsftpd -y
vim /etc/keepalived/keepalived
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_fromkeepalived@dd1.example.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 142
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.42.150
172.25.42.160
}
}
virtual_server 172.25.42.150 80 {
delay_loop 6
lb_algo rr
lb_kind DR
# persistence_timeout 50
protocol TCP
real_server 172.25.42.12 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.25.42.13 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
virtual_server 172.25.42.160 21 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 172.25.42.12 21 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.25.42.13 21 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
/etc/init.d/keepalived start
增加虚拟ip:
/etc/init.d/keepalived stop
real server :
ip addr add 172.25.42.140
arptables -A IN -d 172.25.42.140 -j DROP
arptables -A OUT -s 172.25.42.140 -j mangle --mangle-ip-s 172.25.42.140
/etc/init.d/keepalived start
测试:
keepalived 与 heartbeat 相比takeover速度非常快 几乎没有时间的延迟
我们可以在两台机器上面 ipvsadm -l 看到 不管这台机器是不是主调度器 上面都会有调度信息
原文地址:http://12117148.blog.51cto.com/12107148/1899954