keepalived+haproxy安裝配置

#############################################################
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 {
[email protected]

}
notification_email_from [email protected]
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














發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章