keepalived lvs 实现lvs高可用
keepalived 简介keepalived是借用VRRP协议来实现高可用性的,VRRP协议是解决单点故障,使路由器和层三交换机实现冗余功能。
http://168322.blog.运维网.com/attachment/201408/3/158322_1407078441M9cs.png
keepalived启动后会有三个进程
父进程:内存管理,子进程管理等等
子进程:VRRP子进程
子进程:healthchecker子进程
从图上可以看出,两个子进程都被系统WatchDog看管,两个子进程各自操作自己的事,healthchecker子进程负责检查各自服务器的健康程度,例如HTTP,LVS等等,如果healthchecker子进程检查到MASTER上服务不可用了,就会通知本机上的VRRP子进程,让他删除通告,并且去掉虚拟IP,转换为BACKUP状态
环境准备
1 操作系统
centos6.4 x86_64
2 实验拓扑
http://168322.blog.运维网.com/attachment/201408/3/158322_1407078442QBm8.png
3 服务器时间同步
#ntpdate ntp.api.bz
#ntpdate ntp.api.bz
#ntpdate ntp.api.bz
#ntpdate ntp.api.bz
4 安装keepalived ipvsadm
# yum install keepalived ipvsadm
5 修改配置文件
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.10
}
}
virtual_server 192.168.200.10 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
net_mask 255.255.255.0
protocol TCP
real_server 192.168.200.138 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.200.139 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
6 复制配置文件到从服务器
# scp /etc/keepalived/keepalived.conf 192.168.200.18:/etc/keepalived/keepalived.conf
7 从服务器 安装keepalived ipvsadm
# yum install keepalived ipvsadm
8 修改配置文件
# vim /etc/keepalived/keepalived.conf
state BACKUP
priority 99
9 开启服务
# service keepalived start
# chkconfig --add keepalived
# chkconfig keepalived on
# service keepalived start
# chkconfig --add keepalived
# chkconfig keepalived on
10安装 http
# yum install httpd
# yum install httpd
11 建立测试网页
# cd /var/www/html/
# vim index.html
this is web1
# cd /var/www/html/
# vim index.html
this is web2
12 启动服务
# service httpd start
# chkconfig --add httpd
# chkconfig httpd on
# service httpd start
# chkconfig --add httpd
# chkconfig httpd on
13 修改内存参数 配置vip
# 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
# ifconfig lo:0 192.168.200.10 broadcast 192.168.200.10 netmask 255.255.255.255 up
# route add -host 192.168.200.10 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
# ifconfig lo:0 192.168.200.10 broadcast 192.168.200.10 netmask 255.255.255.255 up
# route add -host 192.168.200.10 dev lo:0
14 查看相关vipipvs规则
# ip addr
1: lo:mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0:mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:3e:54:30 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.17/24 brd 192.168.200.255 scope global eth0
inet 192.168.200.10/32 scope global eth0
inet6 fe80::20c:29ff:fe3e:5430/64 scope link
valid_lft forever preferred_lft forever
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP192.168.200.10:80 wrr
-> 192.168.200.138:80 Route 1 0 0
-> 192.168.200.139:80 Route 1 0 0
15 测试
web1
http://168322.blog.运维网.com/attachment/201408/3/158322_1407078442oLCr.png
web2
http://168322.blog.运维网.com/attachment/201408/3/158322_1407078443nXAU.png
停止master的keepalived服务 ,vip ipvs规则会切换到backup服务器上
# service keepalived stop
# ip addr
1: lo:mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0:mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:af:d6:a0 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.18/24 brd 192.168.200.255 scope global eth0
inet 192.168.200.10/32 scope global eth0
inet6 fe80::20c:29ff:feaf:d6a0/64 scope link
valid_lft forever preferred_lft forever
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP192.168.200.10:80 wrr
-> 192.168.200.138:80 Route 1 0 0
-> 192.168.200.139:80 Route 1 0 0
停止web1服务 ipvs规则会清除web1服务器
# service httpd stop
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP192.168.200.10:80 wrr
-> 192.168.200.139:80 Route 1 0
开启web1服务 ipvs自动会添加web1服务器
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP192.168.200.10:80 wrr
-> 192.168.200.138:80 Route 1 0 0
-> 192.168.200.139:80 Route 1 0
页:
[1]