keepalived安装配置

1、 下载:
wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
2、 安装:

tar -zxvf keepalived-1.2.7.tar.gz cd keepalived-1.2.7./configure --prefix=/usr/local/webserver/keepalived

Configure后的模块说明:


Make && make installcp /usr/local/webserver/keepalived/sbin/keepalived /usr/sbin/ cp /usr/local/webserver/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ cp /usr/local/webserver/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/mkdir /etc/keepalived cd /etc/keepalived


3、 配置主keepalived

默认安装是没有配置文件的。新建一个:
vi /etc/keepalived/keepalived.conf


全局配置, 这里对整个keepalived都有用:


global_defs{ notification_email{ szjava@126.com } notification_email_from 157814195@qq.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id id_1 }


解释一下:
notification_email 指定keepalived在发生事情的时候,发送邮件告知,可以有多个地址,每行一个。
smtp_server 发送email的smtp地址
smtp_connect_timeout 超时时间
router_id 运行keepalived的机器的一个标识


VRRPD配置,分为两部份,:vrrp_sync_groups 和 vrrp_instance

vrrp_sync_groups vg1 { group { v_211 } } vrrp_instance v_211 { state MASTER interface eth0 virtual_router_id 51 mcast_src_ip 192.168.0.211 priority 101#权值 advert_int 1#默认检查时间 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress {#虚拟ip地址 192.168.0.212 } }


解释:
state 指定实例初始化的状态,如果都是backup,那么就按照priority的值来确定谁是master。Priority最好相差50
interface 就是实例绑定的网卡,对外提供服务的网口
track_interface 设置额外的监控,里面的任意一个网卡出错,都会进入FAULT状态。
mcast_src_ip 发送多播包的地址,如果不设置,默认使用绑定的网卡的primary IP。
virtual_router_idVRID标记(0-255)
priority 高优先级的为master,最好相差大于50
advert_int 检查间隔时间,默认1s
virtual_ipaddress 里面指定VIP,也就是切换到master时,这些IP会被添加,切换到backup时,就删除这些VIP。通过ip addr show 可以看到这些VIP。
lvs_sync_daemon_interface lvs syncd绑定的网卡,类似发送心跳
authentication 这一段设置认证
auth_type 认证方式,支持PASS和HA(据说HA有问题)
auth_pass 认证密码
nopreempt 设置不抢占,注意这个设置只能设置在backup状态主机上,而且这个主机的priority必须比另外的主机高
reempt_delay 抢占延迟,默认5分钟


4、 启动主keepalived:
/etc/rc.d/init.d/keepalived start
查看是否启动虚拟ip:
Ip a



5、 配置备份keepalived
global_defs { notification_email { szjava@126.com } notification_email_from 157814195@qq.com smtp_server 127.0.0.1 stmp_connect_timeout 30 router_id id_2 }vrrp_sync_groups vg1 { group { v_212 } }vrrp_instance v_212 { state BACKUP interface eth0 virtual_router_id 51 mcast_src_ip 192.168.0.210 priority 50#权值 advert_int 1#默认检查时间 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress {#虚拟ip地址 192.168.0.212 } }



注意防火墙,防火墙应该添加:
vi /etc/sysconfig/iptables
-A RH-Firewall-1-INPUT -p vrrp -j ACCEPT


原理说明:
1、 通过vrrp协议广播,每个keepalived vrrp都去争取master
2、 以virtual_router_id为组队标识。同为一个vip服务的keepalived的virtual_router_id相同
3、 以priority 为权值,同一个virtual_router_id下那个priority大那个就是master,其它为backup






LVS相关配置
1、安装

ln -sv /usr/src/kernels/2.6.18-164.el5-i686/ /usr/src/linuxwget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz tar -zxvf ipvsadm-1.24.tar.gz cd ipvsadm-1.24 make && make install






keepalived.conf添加:
virtual_server 192.168.0.215 80 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 50 protocol TCPreal_server 192.168.0.210 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.0.211 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 } } }



真实服务器运行sh:
vi /root/lvs_real.sh

#!/bin/bash SNS_VIP=192.168.0.215/etc/rc.d/init.d/functionscase "$1" in start) /sbin/ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP /sbin/route add -host $SNS_VIP dev lo:0 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore 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) /sbin/ifconfig lo:0 down /sbin/route del $SNS_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" ; ; *) echo "Usage: $0 {start|stop}" exit 1 esacexit 0


chmod +x /root/lvs_real.sh
./lvs_real.sh




使用ipvsadm查看:
ipvsadm -L -n


【keepalived安装配置】

    推荐阅读