master1:DIP 192.168.2.20 vip1:192.168.2.18 vip2:192.168.2.19
master2:DIP 192.168.2.30 vip1:192.168.2.18 vip2:192.168.2.19
web1: RIP 192.168.2.10 vip1:192.168.2.18 vip2:192.168.2.19
web2: RIP 192.168.2.11 vip1:192.168.2.18 vip2:192.168.2.19
安装配置之前:请注意系统时间同步,iptables,selinux.
1:安装配置master1:
# yum install keepalived
配置/etc/keepadlived.conf文件
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 { #定义VRRP实例,实例名自定义
state MASTER #指定Keepalived的角色,MASTER为主服务器,BACKUP为备用服务器
interface eth0
#指定HA监测的接口
virtual_router_id 51 #虚拟路由标识(1-255),在一个VRRP实例中主备服务器ID必须一样
priority 100 #优先级,数字越大越优先,主服务器优先级必须高于备服务器
advert_int 1 #设置主备之间同步检查时间间隔,单位秒
authentication { #设置验证类型和密码
auth_type PASS #验证类型
auth_pass 1111 #设置验证密码,同一实例中主备密码要保持一致
}
virtual_ipaddress { #定义虚拟IP地址
192.168.2.18 brd 192.168.2.18 dev eth0 label eth0:0
}
}
vrrp_instance VI_2 {
state BAKCUP
interface eth0
virtual_router_id 61
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.2.19 brd 192.168.2.19 dev eth0 label eth0:1
}
}
virtual_server 192.168.2.18 80 { #设置虚拟服务器
delay_loop 6 #设置健康状态检查时间
lb_algo rr #设置负载调度算法
lb_kind DR #设置LVS实现负载均衡的机制
nat_mask 255.255.255.0 #设置掩码
# persistence_timeout 50 #会话保持时间
protocol TCP #指定转发协议类型
real_server 192.168.2.10 80 { #后端服务器节点
weight 1 #设置服务节点的权重
HTTP_GET { #设置检测方式
url { #设定返回状态码为200表示Realserver存活
path /
status_code 200
}
connect_timeout 3 #设置响应超时时间
nb_get_retry 3 #设置超时重试次数
delay_before_retry 3 #设置超时重试间隔
}
}
real_server 192.168.2.11 80 {
weight 1
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
virtual_server 192.168.2.19 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.2.10 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.2.11 80 {
weight 1
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
2:安装配置master2
# yum install keepalived
将master1/etc/keepalived/keepalived.conf 传递级master2
# scp /etc/keepalived/keepalived.conf [email protected]:/etc/keepalived/
修改配置文件如下
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.18 brd 192.168.2.18 dev eth0 label eth0:0
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 61
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.2.19 brd 192.168.2.19 dev eth0 label eth0:1
}
}
其他配置不变
3:测试keepalived
[root@master1 ~]# service keepalived start
[root@master2~]# service keepalived start
4:配置2台web
[root@web1 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@web1 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@web1 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@web1 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@web1 ~]# sysctl -a | grep arp
[root@web1 ~]# ifconfig lo:0 192.168.2.18 broadcast 192.168.2.18 netmask 255.255.255.255 up
[root@web1 ~]# ifconfig lo:1 192.168.2.19 broadcast 192.168.2.19 netmask 255.255.255.255 up
[root@web1 ~]# route add -host 192.168.2.18 dev lo:0
[root@web1 ~]# route add -host 192.168.2.19 dev lo:1
[root@web2 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@web2 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@web2 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@web2 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@web2 ~]# sysctl -a | grep arp
[root@web2~]# ifconfig lo:0 192.168.2.18 broadcast 192.168.2.18 netmask 255.255.255.255 up
[root@web2 ~]# ifconfig lo:1 192.168.2.19 broadcast 192.168.2.19 netmask 255.255.255.255 up
[root@web2 ~]# route add -host 192.168.2.18 dev lo:0
[root@web2 ~]# route add -host 192.168.2.19 dev lo:1
5:测试:
[root@web1 ~]# yum install httpd
[root@web1 ~]# service httpd start
[root@web1 ~]# echo "thi is 192.168.2.10" > /var/www/html/index.html
[root@web2 ~]# yum install httpd
[root@web2 ~]# service http start
[root@test2 ~]# echo "thi is 192.168.2.11" > /var/www/html/index.html
访问:192.168.2.18 192.168.2.19 显示结果正常