lvs+keepalived
LVS提供负载均衡,keepalived提供健康检查,
故障转移,提高系统的可用性!
采用这样的架构以后,很容易对现有系统进行扩展,
只要在后端添加或者减少realserver,
更改lvs的 配置文件,就能实现无缝配置变更!
server1:master负载均衡器
server4:backup负载均衡器
server2:后端web服务器
server3:后端web服务器
还原实验环境:
server1:
ip addr del 172.25.75.100/24 dev tunl0
modprobe -r ipip
/etc/init.d/ldirectord stop
chkconfig ldirectord off
server2 server3:
ip addr del 172.25.75.100/32 dev tunl0
modprobe -r ipip
1、获得keepalived安装包,并解压
tar zxf keepalived-2.0.6.tar.gz
cd keepalived-2.0.6
yum install openssl-devel -y
3、进入解压好的keepalived-2.0.6目录下,进行源码编译
./configure --prefix=/usr/local/keeplived --with-init=SYSV
make && make install #注意要在解压的目录下
6、
scp -r keepalived server4:/usr/local/
7、在server1、server4赋予执行权限,添加链接
赋予权限
chmod +x /usr/local/keepalived/etc/rc.d/init.d/keepalived
[root@server4 ~]# chmod +x /usr/local/keepalived/
制作软链接
cd /mnt/keepalived-2.0.6/keepalived/etc/init.d
[root@server1 init.d]# ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@server1 init.d]# ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@server1 init.d]# ln -s /usr/local/keepalived/etc/keepalived/ /etc/
[root@server1 init.d]# ln -s /usr/local/keepalived/sbin/keepalived /sbin/
[root@server1 init.d]# /etc/init.d/ldirectord stop
Stopping ldirectord... success
[root@server1 init.d]# chkconfig ldirectord off
8、server1、server4安装调度器ipvsadm,并给server3、server2添加ip
server1-4:
yum install ipvsadm -y
server2、server3:
ip addr add 172.25.75.100/32 dev eth0
9、在server1,编辑keepalived配置文件,设置为MASTER,并启动服务
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { ##全局定义块
notification_email { ##邮件通知
root@localhost
}
notification_email_from keeplived@localhost #设置邮件的发送地址
smtp_server 127.0.0.1 #设置 smtp server 地址
smtp_connect_timeout 30 #设置连接 smtp 服务器超时时间
router_id LVS_DEVEL #load balancer 的标识 ID,用于 email
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER #备机改为 BACKUP,此状态是由 priority 的值来决定的
interface eth0 #HA 监测网络接口
virtual_router_id 51 #主、备机的 virtual_router_id 必须相同,取值 0-255
priority 100 #主机的优先级,备份机改为 50,主机优先级一定要大于备机
advert_int 1 #主备之间的通告间隔秒数
authentication {
auth_type PASS #设置验证类型,主要有 PASS 和 AH 两种
auth_pass 1111
}
virtual_ipaddress { ##指定漂移地址(VIP),即切换到MASTER时,这些IP会被添加,切换到BACKUP时,这些IP会被删除(传给ip addr命令),所以每台服务器可以不用绑定任何的虚拟地址。
172.25.75.100
}
}
## 虚拟服务配置
virtual_server 172.25.75.100 80 { #定义虚拟服务器
delay_loop 3
lb_algo rr #lvs 调度算法,这里使用轮叫
lb_kind DR #LVS 是用 DR 模式
#persistence_timeout 50 #注释掉(像lftp等连接性服务,会有连接时间,此处用httpd做实验)
protocol TCP #指定转发协议类型,有 tcp 和 udp 两种
real_server 172.25.75.2 80 { #配置服务节点
TCP_CHECK{ ##TCP方式的健康检查,realserve 的状态检测设置部分,单位是秒
weight 1 ##默认为1,0为失效
connect_timeout 3 #3 秒无响应超时
retry 3 #重试次数
delay_before_retry 3 #重试间隔
}
}
real_server 172.25.75.3 80 {
TCP_CHECK{
weight 1
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
/etc/init.d/keepalived start #启动服务
10、将配置文件远程同步给server4,并修改
scp keepalived.conf server4:/etc/keepalived/
vim /etc/keepalived/keepalived.conf #修改配置文件
更改内容:
vrrp_instance VI_1 {
state BACKUP #备机改为 BACKUP
interface eth0
virtual_router_id 51
priority 50 #备份机改为 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.75.100
}
}
/etc/init.d/keepalived start #启动服务
11、给server1、server4安装邮件服务
yum install mailx -y
在负载均衡器上查看ip
发现server1的eth0网卡上生成一个IP:172.25.75.100/32
关闭keepalived服务看ip是否会漂移到server4上
查看是否轮巡
关掉server3的httpd服务:
会在server1上受到邮件: