准备环境:iptables 和selinux 均是关闭的。
desktop12.example.com
desktop41.example.com(这2 台和之前的一样是高可用的HA,假设这2 台
是真机,也可以是虚拟机,只要你的电脑内存足够大)
desktop58.example.com
desktop67.example.com(这2 台是负载均衡的real server,这2 台是虚拟机)
步骤(1)在desktop12.example.com
desktop41.example.com上:lftp 192.168.0.254 cd pub/docs/keepalived
get keepalived-1.2.2.tar.gz
tar zxf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-71.el6.x86_64/
(在编译的时候可能会报错,可能你需要安装 yum install ipvsadm kernel-devel libnl-devel popt-devel -y直到出现四个:yes
Use IPVS Framework: Yes
IPVS sync daemon support : Yes
IPVS use libnl: Yes
Use VRRP Framework : Yes
注意,这里必须要安装kernel-devel.否则可能出现/usr/src/kernels/2.6.32-71.7.1.el6.x86_64/后面的那个补不齐(也就是说安装完是可以自动 补齐的!),Use IPVS Framework : No
IPVS sync daemon support : No
Use VRRP Framework : Yes
Use Debug flags : No不能出现四个yes。
make && make install
成功之后:
(2)cd /usr/local/keepalived/etc/keepalived
mkdir /etc/keepalived
cp keepalived.conf /etc/keepalived/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
(3)编辑配置文件:
vi /etc/keepalived/keepalived.conf
- global_defs {
- notification_email {
- [email protected] #接收警报的 email 地址,可以添加多个
- }
- notification_email_from keepalived@localhost
- smtp_server 127.0.0.1 #使用本机转发 email
- smtp_connect_timeout 30
- router_id LVS_DEVEL #load balancer 的标识 ID,用于 email 警报
- }
- vrrp_instance VI_1 {
- state MASTER #备机改为 BACKUP,此状态是由 priority 的值来决定的,当前
- priority 的值小于备机的值,那么将会失去 MASTER 状态,在这里谁的priority
- 大谁的优先级高)
- interface br0 #(根据你的HA机子决定,本人选的是desktop12和
desktop41所以是br0,要是虚拟的话可以是eth0)- virtual_router_id 81 #主、备机的 virtual_router_id 必须相同
- priority 150 #主机的优先级,备份机改为 50
- advert_int 1 #主备之间的通告间隔秒数
- authentication { #主备之间的通告间隔秒数
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.0.1 #HA虚拟ip,可加多个
- }
- }
- virtual_server 192.168.0.1 80 {
- delay_loop 6 #每隔6秒查询realserver状态
- lb_algo rr #lvs调度算法,这里使用轮询
- lb_kind DR #lvs负载均衡机制,这里使用直连路由
- # nat_mask 255.255.255.0
- # persistence_timeout 50 #同一IP的连接60秒内被分配到同一台realserver
- protocol TCP
- real_server 192.168.0.67 80 {
- weight 1
- TCP_CHECK {
- connect_timeout 3
- nb_get_retry 3 #故障重试秒数
- delay_before_retry 3 #重试延迟
- }
- }
- real_server 192.168.0.58 80 {
- weight 1
- TCP_CHECK {
- connect_timeout 3
- nb_get_retry 3
- delay_before_retry 3
- }
- }
- } (除以上有效语句之外的其余部分最好删除,否则会影响效果的!)
编辑完之后:scp /etc/keepalived/keepalived.conf desktop41.example.com
之后去desktop41: vi /etc/keepalived/keepalived.conf
state BACKUP
priority 50
(4)分别在realserver 2台:desktop67.example.com和desktop58.example.com执行以下:
vi /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2(注意将桥接的那几个注释掉这几条的意思是屏蔽所有的arp封包,意思是不让其进行解析,只通过192.168.0.1这个虚拟IP连接过来的哦!)
sysctl -p(使其生效)
echo `hostname` > /var/www/html/index.html
service httpd start
ifconfig eth0:0 192.168.0.1 netmask 255.255.255.255 up
route add -host 192.168.0.1 dev eth0:0
(5)测试:
测试:
访问 http://192.168.0.1,看到页面在两个 realserver 上切换表示成功!
你也可以通过 ipvsadm -l 查看详细连接情况!
通过: tail -f /var/log/message查看启动成功与否!以及出现什么问题等。
当在一个机子上启动时,你通过ip addr list 可以查看到
(可以看到给你添加了 inet 192.168.0.1/32 scope global br0这一条,当你2台高可用都启动的时候会自动去访问的是master那台,因为优先级高,这里数值大的优先级高,当一台关闭的时候会被另一个 接管)
同时你可以添加服务vsftpd 在keepalived.conf中添加代码:
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- [email protected]
- }
- notification_email_from [email protected]
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
- vrrp_instance VI_1 {
- state BACKUP
- interface eth0
- virtual_router_id 91
- priority 50
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.0.2
- 192.168.0.4
- }
- }
- virtual_server 192.168.0.2 80 {
- delay_loop 6
- lb_algo rr
- lb_kind DR
- # nat_mask 255.255.255.0
- # persistence_timeout 50
- protocol TCP
- real_server 192.168.0.73 80 {
- weight 1
- TCP_CHECK {
- connect_timeout 3
- nb_get_retry 3
- delay_before_retry 3
- }
- }
- real_server 192.168.0.74 80 {
- weight 1
- TCP_CHECK {
- connect_timeout 3
- nb_get_retry 3
- delay_before_retry 3
- }
- }
- }
- virtual_server 192.168.0.4 21 {
- delay_loop 6
- lb_algo rr
- lb_kind DR
- # nat_mask 255.255.255.0
- # persistence_timeout 50
- protocol TCP
- real_server 192.168.0.73 21 {
- weight 1
- TCP_CHECK {
- connect_timeout 3
- nb_get_retry 3
- delay_before_retry 3
- }
- }
- real_server 192.168.0.74 21 {
- weight 1
- TCP_CHECK {
- connect_timeout 3
- nb_get_retry 3
- delay_before_retry 3
- }
- }
- }
在使用的时候你可以有时候在tail -f /var/log/message的时候出现
七. 报错解决方案
1.主服务器停止后,备用服务没有启用
监控主服务器上的日志 Jun 28 09:18:32 rust Keepalived_vrrp: receive an invalid ip number count associated with VRID! Jun 28 09:18:32 rust Keepalived_vrrp: bogus VRRP packet received on eth0 !!! Jun 28 09:18:32 rust Keepalived_vrrp: VRRP_Instance(VI_1) Dropping received VRRP packet... 监控备用服务器上的keepalived日志Jun 28 06:25:05 stye Keepalived_vrrp: bogus VRRP packet received on eth0 !!!
Jun 28 06:25:05 stye Keepalived_vrrp: VRRP_Instance(VI_1) ignoring received
advertisment...
解决方案:
改变配置文件/etc/keepalived/keepalived.conf 中virtual_route_id的值
比如
virtual_router_id 60 主从方都要改,默认为51或者是停止keepalived服务然后start,不要用restart!
本文出自 “资料小结” 博客,谢绝转载!