准备两台调度主机,两台real server主机
下载软件: keepalived-1.2.5.tar.gz
将以前的piranha 集群服务关闭
yum install gcc make openssl-devel kernel-devel popt-devel libnl-devel -y
如果没有安装kernek-devel 软件包,则此keepalived 不具备负载均衡功能
tar zxvf keepalived-1.2.5.tar.gz #两台调度主机需要安装
cd keepalived-1.2.5
./configure #如果报错,则安装需要的软件包
下面为 configure 后的输出内容,必须支持IPVS
Keepalived configuration
Keepalived
version : 1.1.17
Compiler : gcc
Compiler flags : g
O2
Extra Lib : lpopt
lssl
lcrypto
Use IPVS Framework : Yes #注意编译时一定要支持lvs
IPVS sync daemon support : Yes
Use VRRP Framework : Yes
Use LinkWatch : No
Use Debug flags : No
make && make install
默认将keepalived 安装到/usr/local/etc/ 目录下
cd /usr/local/etc/ && ls #查看keepalived的安装
cp -r keepalived/ /etc/
cd rc.d/init.d/
cp keepalived /etc/init.d/
cd ../..sysconfig/
cp keepalived /etc/sysconfig/ #两台主机都要安装keepalived
复制安装文件到相应的目录下
cd /etc/keepalived/
vim keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
1165862826@qq.com #接收警报的email地址,可以添加多个
}
notification_email_from keepalived@desktop99.example.com
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状态
interface eth0 #HA监测网络接口
virtual_router_id 99 #vrid只能在1-255范围内,且主备的vrid必须相同
priority 100 #备份调度主机的优先级要比主调度主机的小才行
advert_int 1 #主备之间的通告间隔秒数
authentication { #主备切换时的验证
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #HA虚拟ip,可加多个
192.168.0.240
}
}
virtual_server 192.168.0.240 80 { 设置虚拟服务
delay_loop 6 #每隔6秒查询realserver状态
lb_algo rr #lvs 调度算法,这里使用轮叫
lb_kind DR #lvs负载均衡机制,这里使用直连路由
# nat_mask 255.255.255.0
# persistence_timeout 50 #同一IP的连接50秒内被分配到同一台realserver,如果是ftp的话,不需要注释持续链接。
protocol TCP #用TCP协议检查realserver状态
real_server 192.168.0.34 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3 #故障重试秒数
delay_before_retry 3 #重试延迟
}
}
real_server 192.168.0.81 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
scp keepalived.conf 192.168.0.100:/etc/keepalived/
scp过去,要改主机名,state状态,优先级要比主的小
ln -s /usr/local/sbin/keepalived /usr/sbin/ keepalived服务只会在/usr/sbin目录下查找keepalived的命令 在两台调度主机上作连接,并启动
/etc/init.d/keepalived start
遇到的问题:
启动keepalived服务后,VIP并没有启动,需要手动配置。
tail -f /var/log/messages
VRRP_Instance(VI_1) Entering MASTER STATE 显示此提示信息表示好了
在备份调度主机
vim keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
1165862826@qq.com
}
notification_email_from keepalived@desktop100.example.com
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 99
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.240
}
}
virtual_server 192.168.0.240 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.34 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.0.81 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
备份调度主机的优先级要比主调度主机的小才行,
tail -f /var/log/messages
VRRP_Instance(VI_1) Entering BACKUP STATE 显示此提示信息表示好了
分别在 realserver 上执行以下命令
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
sysctl -p
ifconfig eth0:0 192.168.0.240 netmask 255.255.255.255 up
route add -host 192.168.0.240 dev eth0:0
如果在lo:0 上设置 192.168.0.240 ,则不需要关闭 arp,只需:
ifconfig lo:0 192.168.0.240 netmask 255.255.255.255 up
route add -host 192.168.0.240 dev lo:0
echo `hostname` > /var/www/html/index.html
service httpd start
在两台调度主机:ip addr show 查看在eth0是否有vip,即192.168.0.240,哪台调度主机上面有vip,则此主机为主调度主机
ipvsadm -l
当访问时,两台调度主机的调度列表保持同步,这样当一台主机宕掉,另一台可以接替,且session会话不会丢失,只是转移到另一台主机。
同前面的集群实验一样,如果访问时只能访问一台real server,则需手动设置arp:
arp -d 192.168.0.240 删除之前的arp缓存
arp -s 192.168.0.240 52:54:00:53:1f:c3
注:keepalived 的高可用效率很高,当主调度主机挂掉,备份的立刻接替。
测试:
访问 http://192.168.0.240,看到页面在两个 realserver 上切换表示成功!
你也可以通过 ipvsadm -Lnc 查看详细连接情况!