Haproxy、Keepalived双主高可用负载均衡

简介:

集合Haproxy,Keepalived双主双机高可用模型,不论是Haproxy还是Keepalived甚至是上游服务器均提高生产力并增强可用性,也就是如下架构中Haproxy,Keepalived,Httpd服务器任意宕机一台服务还是可以正常运行的


规划:

1
2
3
172.16.43.1 , 172.16.43.2 两台keepalived节点 (为haproxy做高可用)
172.16.43.1(172.16.43.2)  两台haproxy (为上游服务器做反带)
172.16.43.3 , 172.16.43.4 两台web后端服务器


i) 两台keepalived节点 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 安装keepalived, 两台均要做 (172.16.43.1,2)
yum -y  install  keepalived
#
# keepalived配置 (172.16.43.1)
# vim /etc/keepalived/keepalived.conf
#
global_defs {
     notification_email {
         root@localhost   # 本地邮件地址
     }
     notification_email_from keepadmin@localhost
     smtp_connect_timeout 3
     smtp_server 127.0.0.1
     router_id LVS_DEVEL_KING
}
#
vrrp_script chk_haproxy {
     script  "/etc/keepalived/chk_haproxy.sh"   # 检查脚本
     interval 2
     weight 2
}
#
vrrp_instance VI_1 {
     interface eth0
     state MASTER   # 172.16.43.1 这是主,那么 172.16.43.2 就是备
     priority 100   # 主 比 备 优先级高
     virtual_router_id 173    # vrid是行为vmac的根本
     garp_master_delay 1
#
     authentication {
         auth_type PASS
         auth_pass 1111
     }
     track_interface {
         eth0
     }
     virtual_ipaddress {
         172.16.43.88 /16  dev eth0
     }
     track_script {
         chk_haproxy   # 脚本跟踪监测
     }
}
#
vrrp_instance VI_2 {
     interface eth0
     state BACKUP   # master for slave routers
     priority 99   # 99 for master
     virtual_router_id 174
     garp_master_delay 1
#
     authentication {
         auth_type PASS
         auth_pass 11111
     }
     track_interface {
         eth0
     }
     virtual_ipaddress {
         172.16.43.188 /16  dev eth0
     }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 172.16.43.2 keepalived配置
# vim /etc/keepalived/keepalived.conf
#
global_defs {
     notification_email {
         root@localhost
     }
     notification_email_from keepadmin@localhost
     smtp_connect_timeout 3
     smtp_server 127.0.0.1
     router_id LVS_DEVEL_KING
}
#
vrrp_script chk_haproxy {
     script  "/etc/keepalived/chk_haproxy.sh"
     interval 2
     weight 2
}
#
vrrp_instance VI_1 {
     interface eth0
     state BACKUP   # BACKUP for slave routers
     priority 99   # 99 for BACKUP
     virtual_router_id 173
     garp_master_delay 1
#
     authentication {
         auth_type PASS
         auth_pass 1111
     }
     track_interface {
         eth0
     }
     virtual_ipaddress {
         172.16.43.88 /16  dev eth0
     }
     track_script {
         chk_haproxy
     }
}
#
vrrp_instance VI_2 {
     interface eth0
     state MASTER   # master for slave routers
     priority 10000   # 99 for master
     virtual_router_id 174
     garp_master_delay 1
#
     authentication {
         auth_type PASS
         auth_pass 11111
     }
     track_interface {
         eth0
     }
     virtual_ipaddress {
         172.16.43.188 /16  dev eth0
     }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
# 刚才两个节点 均要有 的监测脚本文件 , 防止 haproxy 停止而 keepalived 不切换的情况
# vim /etc/keepalived/chk_haproxy.sh
#
#
#!/bin/bash
#
if  ! `pidof haproxy &>  /dev/null `;  then
     /etc/rc .d /init .d /haproxy   start
fi
sleep  2
if  ! `pidof haproxy &>  /dev/null `;  then
     /etc/rc .d /init .d /keepalived  stop
fi

###   启动服务   ####    service keepalived start 


keepalived双主模型启动

wKioL1Nf6iLwG9TAAAd2IwA505U625.jpg


ii) 两台haproxy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 安装haproxy,两台均要 (172.16.43.1 , 2)
yum -y  install  haproxy
#
# 为haproxy分别提供配置文件 , 两台均一样 , 不需要更改
global
     log         127.0.0.1 local2
     chroot       /var/lib/haproxy
     pidfile      /var/run/haproxy .pid
     maxconn     4000
     user        haproxy
     group       haproxy
     daemon
#
defaults
     mode                    http   # http tcp health 模型, 这里监控 web 站点所以使用 http
     log                     global
     option                  httplog
     option                  dontlognull
     option                  redispatch   # 调度到健康的服务器
     option http-server-close  # 不接受长连接
     option forwardfor       except 127.0.0.0 /8   # 在响应头中加入forwardfor标记
     retries                 3
     timeout http-request    10s   # 超时时间设置
     timeout queue           1m
     timeout connect         10s
     timeout client          1m
     timeout server          1m
     timeout http-keep-alive 10s
     timeout check           10s
     maxconn                 30000
#
listen stats
     mode http
     bind 0.0.0.0:8080    # status 页面在8080提供服务
     stats  enable    #  status 允许操作
     stats hide-version  # status 隐藏haproxy版本信息
     stats uri      /haproxyadmin ?stats   # status 访问路径
     stats realm   Haproxy\ Statistics   # status 登陆验证信息
     stats auth    admin:admin   # status 页面登陆用户名或密码
     stats admin  if  TRUE
#
frontend http- in
     bind *:80
     mode http
     log global
     option httpclose
     option logasap
     option dontlognull
     capture request  header Host len 20
     capture request  header Referer len 60
     acl url_static       path_beg       -i  /static  /images  /javascript  /stylesheets
     acl url_static       path_end       -i .html .jpg .jpeg .gif .png .css .js
#
     use_backend static_servers           if  url_static
     default_backend dynamic_servers
#
backend static_servers
     balance roundrobin
     server imgsrv1 172.16.43.3:80 check maxconn 6000
#
backend dynamic_servers
     balance  source
     server websrv1 172.16.43.3:80 check maxconn 1000
     server websrv2 172.16.43.4:80 check maxconn 1000

###   启动服务   ####    service haproxy start 


输出状态页面

wKiom1Nf6mOyvPM4AApG0DuvVsI038.jpg

wKioL1Nf6jyjJBkLAApUCPIDuXg212.jpg

iii) 两台web后端服务器

1
2
3
# 安装 httpd , php
yum -y
install  httpd php

###   启动服务   ####    service httpd start


iv) 测试

动静分离

wKiom1Nf6q3R8wYnAA9TsBoB0x4743.jpg


高可用性

关闭了上游一台web服务器,可以见到服务请求没有任何问题, 172.16.43.88 , 188 没有问题

wKioL1Nf6oWQvxWNAAG7OuUuJHA886.jpg

wKiom1Nf6rXQM-LgAAkZxv46-kk155.jpg








     本文转自My_King1 51CTO博客,原文链接:http://blog.51cto.com/apprentice/1404853,如需转载请自行联系原作者



相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
相关文章
|
6月前
|
负载均衡 网络安全
|
5月前
|
负载均衡 应用服务中间件 Linux
Nginx系列教程(14) - LVS+KeepAlived+Nginx实现高性能负载均衡集群
Nginx系列教程(14) - LVS+KeepAlived+Nginx实现高性能负载均衡集群
164 0
|
6月前
|
负载均衡
Pgpool-II实现高可用+读写分离+负载均衡(七)---- recovery_1st_stage分析
recovery_1st_stage是Pgpool online recovery的第一阶段,位于PG_DATA目录下,主要功能就是使用pg_basebackup恢复(recovery)从节点。
189 0
|
2月前
|
存储 缓存 运维
解密一致性哈希算法:实现高可用和负载均衡的秘诀
解密一致性哈希算法:实现高可用和负载均衡的秘诀
120 0
|
3月前
|
Kubernetes 负载均衡 监控
Kubernetes高可用集群二进制部署(一)主机准备和负载均衡器安装
Kubernetes高可用集群二进制部署(一)主机准备和负载均衡器安装
|
4月前
|
tengine Kubernetes Cloud Native
Tengine-Ingress 高性能高可用的云原生网关
Tengine-Ingress 高性能高可用的云原生网关
|
5月前
|
负载均衡 算法 网络协议
Keepalived+LVS搭建高可用负载均衡
Keepalived+LVS搭建高可用负载均衡
178 1
|
5月前
|
负载均衡 关系型数据库 PostgreSQL
Pgpool-II实现高可用+读写分离+负载均衡(八)---- 维护工具
Pgpool提供了一些维护工具,用于日常观察Pgpool运行状态、上线、下线节点等操作。主要有:pcp_stop_pgpool,pcp_node_count,pcp_node_info,pcp_health_check_stats,pcp_proc_count,pcp_proc_info,pcp_detach_node,pcp_attach_node,pcp_recovery_node,pcp_promote_node,pcp_pool_status,pcp_watchdog_info,pcp_reload_config
180 0
|
5月前
|
负载均衡 前端开发 网络协议
Keepalived+HAProxy 搭建高可用负载均衡(二)
Keepalived+HAProxy 搭建高可用负载均衡
|
5月前
|
负载均衡 算法 调度
Keepalived+HAProxy 搭建高可用负载均衡
Keepalived+HAProxy 搭建高可用负载均衡
208 0