keepalived 简介
keepalived是借用VRRP协议来实现高可用性的,VRRP协议是解决单点故障,使路由器和层三交换机实现冗余功能。
keepalived启动后会有三个进程
父进程:内存管理,子进程管理等等
子进程:VRRP子进程
子进程:healthchecker子进程
从图上可以看出,两个子进程都被系统WatchDog看管,两个子进程各自操作自己的事,healthchecker子进程负责检查各自服务器的健康程度,例如HTTP,LVS等等,如果healthchecker子进程检查到MASTER上服务不可用了,就会通知本机上的VRRP子进程,让他删除通告,并且去掉虚拟IP,转换为BACKUP状态
环境准备
1 操作系统
centos6.4 x86_64
2 实验拓扑
3 服务器时间同步
[root@node1 ~]#ntpdate ntp.api.bz [root@node2 ~]#ntpdate ntp.api.bz [root@web1 ~]#ntpdate ntp.api.bz [root@web2 ~]#ntpdate ntp.api.bz
4 安装keepalived ipvsadm
[root@node1 ~]# 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 复制配置文件到从服务器
[root@node1 ~]# scp /etc/keepalived/keepalived.conf 192.168.200.18:/etc/keepalived/keepalived.conf
7 从服务器 安装keepalived ipvsadm
[root@node2 ~]# yum install keepalived ipvsadm
8 修改配置文件
[root@node2 ~]# vim /etc/keepalived/keepalived.conf state BACKUP priority 99
9 开启服务
[root@node1 ~]# service keepalived start [root@node1 ~]# chkconfig --add keepalived [root@node1 ~]# chkconfig keepalived on
[root@node2 ~]# service keepalived start [root@node2 ~]# chkconfig --add keepalived [root@node2 ~]# chkconfig keepalived on
10 安装 http
[root@web1 ~]# yum install httpd
[root@web2 ~]# yum install httpd
11 建立测试网页
[root@web1 html]# cd /var/www/html/ [root@web1 html]# vim index.html this is web1
[root@web2 ~]# cd /var/www/html/ [root@web2 html]# vim index.html this is web2
12 启动服务
[root@web1 html]# service httpd start [root@web1 html]# chkconfig --add httpd [root@web1 html]# chkconfig httpd on
[root@web2 html]# service httpd start [root@web2 html]# chkconfig --add httpd [root@web2 html]# chkconfig httpd on
13 修改内存参数 配置vip
[root@web1 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore [root@web1 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce [root@web1 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore [root@web1 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce [root@web1 ~]# ifconfig lo:0 192.168.200.10 broadcast 192.168.200.10 netmask 255.255.255.255 up [root@web1 ~]# route add -host 192.168.200.10 dev lo:0
[root@web2 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore [root@web2 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce [root@web2 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore [root@web2 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce [root@web2 ~]# ifconfig lo:0 192.168.200.10 broadcast 192.168.200.10 netmask 255.255.255.255 up [root@web2 ~]# route add -host 192.168.200.10 dev lo:0
14 查看相关vip ipvs规则
[root@node1 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> 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
[root@node1 ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.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
web2
停止master的keepalived服务 ,vip ipvs规则会切换到backup服务器上
[root@node1 ~]# service keepalived stop [root@node2 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> 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 [root@node2 ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.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服务器
[root@web1 ~]# service httpd stop [root@node1 ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.200.10:80 wrr -> 192.168.200.139:80 Route 1 0
开启web1服务 ipvs自动会添加web1服务器
[root@node1 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.200.10:80 wrr
-> 192.168.200.138:80 Route 1 0 0
-> 192.168.200.139:80 Route 1 0
keepalived lvs 实现lvs高可用,布布扣,bubuko.com
原文地址:http://168322.blog.51cto.com/158322/1535120