搭建HAproxy+KeepAlived高可用负载均衡系统

搭建环境描述

主机名 IP地址 集群角色 虚拟IP
haproxy-server 10.0.0.5 主haproxy server 10.0.0.10
backup-haproxy 10.0.0.8 备用haproxy server 10.0.0.10
a1 10.0.0.6:81 后端真实server
a2 10.0.0.7:81 后端真实server
b1 10.0.0.6:82 后端真实server
b2 10.0.0.7:82 后端真实server
c1 10.0.0.6:83 后端真实server
c2 10.0.0.7:83 后端真实server

整个高可用HAproxy集群系统的拓扑结构如下图所示:

配置HAproxy负载均衡服务器

上一部分已经介绍过了,也把10.0.0.8这个备haproxy-server配置成相同的配置

cat /usr/local/haproxy/conf/haproxy.cfg 
global
	maxconn		20000
	log			127.0.0.1 local0 info
	user		nobody
	group		nobody
	chroot		/usr/local/haproxy
	daemon
	pidfile		/usr/local/haproxy/logs/haproxy.pid
defaults
	mode			http
	retries			3
	timeout	connect	10s
	timeout	client	20s
	timeout	server	30s
	timeout	check	5s
frontend www
	bind			*:80
	mode			http
	log				global
	option			httplog
	option			forwardfor
	option			httpclose
	option			dontlognull
	#新建了3个规则,分别是匹配到不同的域名,访问不同的backend
	acl	a	hdr_beg(host)	a.com
	acl	b	hdr_beg(host)	b.com
	acl	c	hdr_beg(host)	c.com
	use_backend	a	if	a
	use_backend	b	if	b
	use_backend	c	if	c
	default_backend test-proxy-srv


backend test-proxy-srv
	mode            http
	option		redispatch
	option		abortonclose
	balance		roundrobin
	cookie		SERVERID
	option	httpchk	HEAD	/index.html
	server	web1	10.0.0.6:80	cookie	server1	weight	4	check	inter	2000	rise	2	fall	3
	server	web2	10.0.0.7:8000	cookie	server2	weight	6	check	inter	2000	rise	2	fall	3
	timeout connect 5s
	timeout server  5s
	retries         2

backend a
	mode            http
	option		redispatch
	option		abortonclose
	balance		roundrobin
	cookie		SERVERID
	option	httpchk	HEAD	/index.html
	server	a1	10.0.0.6:81	cookie	server1	weight	6	check	inter	2000	rise	2	fall	3
	server	a2	10.0.0.7:81	cookie	server2	weight	6	check	inter	2000	rise	2	fall	3
backend b
	mode            http
	option		redispatch
	option		abortonclose
	balance		roundrobin
	cookie		SERVERID
	option	httpchk	HEAD	/index.html
	server	b1	10.0.0.6:82	cookie	server1	weight	6	check	inter	2000	rise	2	fall	3
	server	b2	10.0.0.7:82	cookie	server2	weight	6	check	inter	2000	rise	2	fall	3
backend c
	mode            http
	option		redispatch
	option		abortonclose
	balance		roundrobin
	cookie		SERVERID
	option	httpchk	HEAD	/index.html
	server	c1	10.0.0.6:83	cookie	server1	weight	6	check	inter	2000	rise	2	fall	3
	server	c2	10.0.0.7:83	cookie	server2	weight	6	check	inter	2000	rise	2	fall	3
listen	stats
	bind	0.0.0.0:9188
	mode	http
	log	127.0.0.1	local0	err
	stats	refresh	30s
	stats	uri	/status
	stats	realm	hahahahaha
	stats	auth	admin:admin
	stats	hide-version
	stats	admin	if	TRUE

两个haproxy的服务器配置文件一致

然后是keepalived配置

cat /etc/keepalived/keepalived.conf
! 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 KEEPALIVED_HAPROXY
}
vrrp_script check_haproxy {
   script "/usr/bin/killall -0 haproxy"
   interval 2
}
vrrp_instance VI_2 {
    state BACKUP
    nopreempt
    interface ens33
    virtual_router_id 100
    priority 100
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    notify_master "/etc/keepalived/master.sh"
    notify_backup "/etc/keepalived/backup.sh"
    notify_fault "/etc/keepalived/fault.sh"
    track_script {
    	check_haproxy
    }
    virtual_ipaddress {
        10.0.0.10
    }
}

vim /etc/keepalived/keepalived.conf
! 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 KEEPALIVED_HAPROXY
}
vrrp_script check_haproxy {
   script "/usr/bin/killall -0 haproxy"
   interval 2
}
vrrp_instance VI_2 {
    state BACKUP
    interface ens33
    virtual_router_id 100
    priority 50
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    notify_master "/etc/keepalived/master.sh"
    notify_backup "/etc/keepalived/backup.sh"
    notify_fault "/etc/keepalived/fault.sh"
    track_script {
        check_haproxy
    }
    virtual_ipaddress {
        10.0.0.10
    }
posted @ 2023-02-22 10:33  厚礼蝎  阅读(43)  评论(0编辑  收藏  举报