############################################################# keepalived keepalived是VRRP协议的完美实现,最早是为lvs设计的高可用模块。 VRRP协议:解决静态路由单点故障的问题太。VRRP通过竞选协议来实现虚拟路由器的功能, 所有的协议报文都是通过IP多播(组播)(组播地址224.0.0.18)对外表现为一个周知的MAC地址 00-00-5E-00-01-X,对外不管谁是MASTER,都是同样的MAC和IP。

############################################################# 配置后端httpd服务端口都为8080 vim /etc/httpd/conf/httpd.conf Listen 8080

service httpd restart ############################################################# 两台他同时安装haproxy yum install haproxy -y vim /etc/haproxy/haproxy.cfg frontend http_front #前端网页配置 mode http #默认mode是http bind *:80 stats uri /haproxy?stats default_backend http_back

backend http_back balance roundrobin #默认算法轮询,也可以是source相当于nginx的ip_hash,cookie option httpchk GET /index.html #默认检查端口,基于url的健康检查,ngingx不支持 option forwardfor header X-REAL-IP #客户端真实ip,后端程序通过X-REAL-IP获取 server linux-node1 192.168.56.20:8080 check inter 2s rise 3 fall 4 weight 2 #inter 2s 两秒检查 rise 3 3次ok才正常4次ng才失败 server linux-node2 192.168.56.21:8080 check inter 2s rise 3 fall 4 weight 1

service haproxy start ############################################################# 安装keepalived yum install keepalived -y

vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived

global_defs { notification_email { lmkmike@qq.com

} notification_email_from haproxy-ha@qq.com smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL

}

vrrp_instance VI_1 { state MASTER #角色 另外改BACKUP interface eth0 #网卡 virtual_router_id 51 #不能重复 priority 150 #权重 另外改100 advert_int 1 #检查时间默认1s authentication { auth_type PASS auth_pass haproxy_ha } virtual_ipaddress { 192.168.56.22 192.168.56.23

}

}

下面暂时不用配置 ############################################################# 启动keepalived

service keepalived start

############################################################# 查看ip是否绑定 ip ad li [root@master ~]# ip ad li 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:40:82:2f brd ff:ff:ff:ff:ff:ff inet 192.168.56.20/24 brd 192.168.56.255 scope global eth0 inet 192.168.56.22/32 scope global eth0 inet 192.168.56.23/32 scope global eth0 inet6 fe80::20c:29ff:fe40:822f/64 scope link valid_lft forever preferred_lft forever ############################################################# 停止master的keepalived服务,看backup是否获取ip service keepalived stop

############################################################# 开启master的keepalived服务,看master是否获取 service keepalived stop

############################################################# 抓包可见ip 224.0.0.18 [root@minion ~]# tcpdump -n 'host 224.0.0.18' tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes 19:50:29.152236 IP 192.168.56.20 > 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 150, authtype simple, intvl 1s, length 24 19:50:30.157829 IP 192.168.56.20 > 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 150, authtype simple, intvl 1s, length 24 19:50:31.159852 IP 192.168.56.20 > 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 150, authtype simple, intvl 1s, length 24 19:50:32.165625 IP 192.168.56.20 > 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 150, authtype simple, intvl 1s, length 24 19:50:33.167739 IP 192.168.56.20 > 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 150, authtype simple, intvl 1s, length 24 19:50:34.176987 IP 192.168.56.20 > 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 150, authtype simple, intvl 1s, length 24 19:50:35.183179 IP 192.168.56.20 > 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 150, authtype simple, intvl 1s, length 24 19:50:36.185416 IP 192.168.56.20 > 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 150, authtype simple, intvl 1s, length 24 19:50:37.187998 IP 192.168.56.20 > 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 150, authtype simple, intvl 1s, length 24 19:50:38.192458 IP 192.168.56.20 > 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 150, authtype simple, intvl 1s, length 24 19:50:39.194564 IP 192.168.56.20 > 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 150, authtype simple, intvl 1s, length 24