当部署多个 FE 节点时,用户可以在多个 FE 之上部署负载均衡层来实现 Doris 的高可用。
代码实现
自己在应用层代码进行重试和负载均衡。比如发现一个连接挂掉,就自动在其他连接上进行重试。应用层代码重试需要应用自己配置多个 doris 前端节点地址。
JDBC Connector
如果使用 mysql jdbc connector 来连接 Doris,可以使用 jdbc 的自动重试机制:
1. jdbc:mysql:loadbalance://[host:port],[host:port].../[database][?propertyName1][=propertyValue1][&propertyName2][=propertyValue
详细可以参考MySQL 官网文档
ProxySQL 方式
ProxySQL 是灵活强大的 MySQL 代理层,是一个能实实在在用在生产环境的 MySQL 中间件,可以实现读写分离,支持 Query 路由功能,支持动态指定某个 SQL 进行 Cache,支持动态加载配置、故障切换和一些 SQL 的过滤功能。
Doris 的 FE 进程负责接收用户连接和查询请求,其本身是可以横向扩展且高可用的,但是需要用户在多个 FE 上架设一层 proxy,来实现自动的连接负载均衡。
安装 ProxySQL(yum 方式)
1. 配置yum源
2. # vim /etc/yum.repos.d/proxysql.repo
4. [proxysql_repo]
5. name= ProxySQL YUM repository
6. baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/\$releasever
7. gpgcheck=1
8. gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key
10. 执行安装
11. # yum clean all
12. # yum makecache
13. # yum -y install proxysql
15. 查看版本
16. # proxysql --version
17. ProxySQL version 1.4.13-15-g69d4207, codename Truls
19. 设置开机自启动
20. # systemctl enable proxysql
21. # systemctl start proxysql
22. # systemctl status proxysql
24. 启动后会监听两个端口, 默认为6032和6033。6032端口是ProxySQL的管理端口,6033是ProxySQL对外提供服务的端口 (即连接到转发后端的真正数据库的转发端口)。
25. # netstat -tunlp
26. Active Internet connections (only servers)
27. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
28. tcp 0 0 0.0.0.0:6032 0.0.0.0:* LISTEN 23940/proxysql
29. tcp 0 0 0.0.0.0:6033 0.0.0.0:* LISTEN
ProxySQL 配置
ProxySQL 有配置文件 /etc/proxysql.cnf 和配置数据库文件/var/lib/proxysql/proxysql.db。这里需要特别注意:如果存在"proxysql.db"文件 (在/var/lib/proxysql目录下),则 ProxySQL 服务只有在第一次启动时才会去读取proxysql.cnf文件并解析;后面启动会就不会读取proxysql.cnf文件了!如果想要让 proxysql.cnf 文件里的配置在重启 proxysql 服务后生效 (即想要让 proxysql 重启时读取并解析 proxysql.cnf 配置文件),则需要先删除 /var/lib/proxysql/proxysql.db数据库文件,然后再重启 proxysql 服务。这样就相当于初始化启动 proxysql 服务了,会再次生产一个纯净的 proxysql.db 数据库文件 (如果之前配置了 proxysql 相关路由规则等,则就会被抹掉)
查看及修改配置文件
这里主要是几个参数,在下面已经注释出来了,可以根据自己的需要进行修改
1. # egrep -v "^#|^$" /etc/proxysql.cnf
2. datadir="/var/lib/proxysql" #数据目录
3. admin_variables=
4. {
5. admin_credentials="admin:admin" #连接管理端的用户名与密码
6. mysql_ifaces="0.0.0.0:6032" #管理端口,用来连接proxysql的管理数据库
7. }
8. mysql_variables=
9. {
10. threads=4 #指定转发端口开启的线程数量
11. max_connections=2048
12. default_query_delay=0
13. default_query_timeout=36000000
14. have_compress=true
15. poll_timeout=2000
16. interfaces="0.0.0.0:6033" #指定转发端口,用于连接后端mysql数据库的,相当于代理作用
17. default_schema="information_schema"
18. stacksize=1048576
19. server_version="5.5.30" #指定后端mysql的版本
20. connect_timeout_server=3000
21. monitor_username="monitor"
22. monitor_password="monitor"
23. monitor_history=600000
24. monitor_connect_interval=60000
25. monitor_ping_interval=10000
26. monitor_read_only_interval=1500
27. monitor_read_only_timeout=500
28. ping_interval_server_msec=120000
29. ping_timeout_server=500
30. commands_stats=true
31. sessions_sort=true
32. connect_retries_on_failure=10
33. }
34. mysql_servers =
35. (
36. )
37. mysql_users:
38. (
39. )
40. mysql_query_rules:
41. (
42. )
43. scheduler=
44. (
45. )
46. mysql_replication_hostgroups=
47. (
48. )
连接 ProxySQL 管理端口测试
1. # mysql -uadmin -padmin -P6032 -hdoris01
2. // 查看main库(默认登陆后即在此库)的global_variables表信息
4. MySQL [(none)]> show databases;
5. +-----+---------------+-------------------------------------+
6. | seq | name | file |
7. +-----+---------------+-------------------------------------+
8. | 0 | main | |
9. | 2 | disk | /var/lib/proxysql/proxysql.db |
10. | 3 | stats | |
11. | 4 | monitor | |
12. | 5 | stats_history | /var/lib/proxysql/proxysql_stats.db |
13. +-----+---------------+-------------------------------------+
14. 5 rows in set (0.000 sec)
15. MySQL [(none)]> use main;
16. Reading table information for completion of table and column names
17. You can turn off this feature to get a quicker startup with -A
19. Database changed
20. MySQL [main]> show tables;
21. +--------------------------------------------+
22. | tables |
23. +--------------------------------------------+
24. | global_variables |
25. | mysql_collations |
26. | mysql_group_replication_hostgroups |
27. | mysql_query_rules |
28. | mysql_query_rules_fast_routing |
29. | mysql_replication_hostgroups |
30. | mysql_servers |
31. | mysql_users |
32. | proxysql_servers |
33. | runtime_checksums_values |
34. | runtime_global_variables |
35. | runtime_mysql_group_replication_hostgroups |
36. | runtime_mysql_query_rules |
37. | runtime_mysql_query_rules_fast_routing |
38. | runtime_mysql_replication_hostgroups |
39. | runtime_mysql_servers |
40. | runtime_mysql_users |
41. | runtime_proxysql_servers |
42. | runtime_scheduler |
43. | scheduler |
44. +--------------------------------------------+
45. 20 rows in set (0.000 sec)
ProxySQL 配置后端 Doris FE
使用 insert 语句添加主机到 mysql_servers 表中,其中:hostgroup_id 为 10 表示写组,为 20 表示读组,我们这里不需要读写分离,无所谓随便设置哪一个都可以。
1. [root@mysql-proxy ~]# mysql -uadmin -padmin -P6032 -h127.0.0.1
2. ............
3. MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.9.211',9030);
4. Query OK, 1 row affected (0.000 sec)
6. MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.9.212',9030);
7. Query OK, 1 row affected (0.000 sec)
9. MySQL [(none)]> insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.9.213',9030);
10. Query OK, 1 row affected (0.000 sec)
12. //如果在插入过程中,出现报错:
13. ERROR 1045 (#2800): UNIQUE constraint failed: mysql_servers.hostgroup_id, mysql_servers.hostname, mysql_servers.port
15. //说明可能之前就已经定义了其他配置,可以清空这张表 或者 删除对应host的配置
16. MySQL [(none)]> select * from mysql_servers;
17. MySQL [(none)]> delete from mysql_servers;
18. Query OK, 6 rows affected (0.000 sec)
20. //查看这 3 个节点是否插入成功,以及它们的状态。
21. MySQL [(none)]> select * from mysql_servers\G;
22. *************************** 1. row ***************************
23. hostgroup_id: 10
24. hostname: 192.168.9.211
25. port: 9030
26. status: ONLINE
27. weight: 1
28. compression: 0
29. max_connections: 1000
30. max_replication_lag: 0
31. use_ssl: 0
32. max_latency_ms: 0
33. comment:
34. *************************** 2. row ***************************
35. hostgroup_id: 10
36. hostname: 192.168.9.212
37. port: 9030
38. status: ONLINE
39. weight: 1
40. compression: 0
41. max_connections: 1000
42. max_replication_lag: 0
43. use_ssl: 0
44. max_latency_ms: 0
45. comment:
46. *************************** 3. row ***************************
47. hostgroup_id: 10
48. hostname: 192.168.9.213
49. port: 9030
50. status: ONLINE
51. weight: 1
52. compression: 0
53. max_connections: 1000
54. max_replication_lag: 0
55. use_ssl: 0
56. max_latency_ms: 0
57. comment:
58. 6 rows in set (0.000 sec)
60. ERROR: No query specified
62. 如上修改后,加载到RUNTIME,并保存到disk,下面两步非常重要,不然退出以后你的配置信息就没了,必须保存
63. MySQL [(none)]> load mysql servers to runtime;
64. Query OK, 0 rows affected (0.006 sec)
66. MySQL [(none)]> save mysql servers to disk;
67. Query OK, 0 rows affected (0.348 sec)
监控 Doris FE 节点配置
添 Doris FE 节点之后,还需要监控这些后端节点。对于后端多个 FE 高可用负载均衡环境来说,这是必须的,因为 ProxySQL 需要通过每个节点的 read_only 值来自动调整
它们是属于读组还是写组。
首先在后端 master 主数据节点上创建一个用于监控的用户名
1. //在 Doris FE master 主数据库节点行执行:
2. # mysql -P9030 -uroot -p
3. mysql> create user monitor@'192.168.9.%' identified by 'P@ssword1!';
4. Query OK, 0 rows affected (0.03 sec)
5. mysql> grant ADMIN_PRIV on *.* to monitor@'192.168.9.%';
6. Query OK, 0 rows affected (0.02 sec)
8. //然后回到mysql-proxy代理层节点上配置监控
9. # mysql -uadmin -padmin -P6032 -h127.0.0.1
10. MySQL [(none)]> set mysql-monitor_username='monitor';
11. Query OK, 1 row affected (0.000 sec)
13. MySQL [(none)]> set mysql-monitor_password='P@ssword1!';
14. Query OK, 1 row affected (0.000 sec)
16. //修改后,加载到 RUNTIME,并保存 disk
17. MySQL [(none)]> load mysql variables to runtime;
18. Query OK, 0 rows affected (0.001 sec)
20. MySQL [(none)]> save mysql variables to disk;
21. Query OK, 94 rows affected (0.079 sec)
23. //验证监控结果:ProxySQL监控模块的指标都保存在monitor库的log表中。
25. //以下是连接是否正常的监控(对connect指标的监控):
27. //注意:可能会有很多connect_error,这是因为没有配置监控信息时的错误,配置后如果connect_error的结果为NULL则表示正常。
29. MySQL [(none)]> select * from mysql_server_connect_log;
30. +---------------+------+------------------+-------------------------+---------------+
31. | hostname | port | time_start_us | connect_success_time_us | connect_error |
32. +---------------+------+------------------+-------------------------+---------------+
33. | 192.168.9.211 | 9030 | 1548665195883957 | 762 | NULL |
34. | 192.168.9.212 | 9030 | 1548665195894099 | 399 | NULL |
35. | 192.168.9.213 | 9030 | 1548665195904266 | 483 | NULL |
36. | 192.168.9.211 | 9030 | 1548665255883715 | 824 | NULL |
37. | 192.168.9.212 | 9030 | 1548665255893942 | 656 | NULL |
38. | 192.168.9.211 | 9030 | 1548665495884125 | 615 | NULL |
39. | 192.168.9.212 | 9030 | 1548665495894254 | 441 | NULL |
40. | 192.168.9.213 | 9030 | 1548665495904479 | 638 | NULL |
41. | 192.168.9.211 | 9030 | 1548665512917846 | 487 | NULL |
42. | 192.168.9.212 | 9030 | 1548665512928071 | 994 | NULL |
43. | 192.168.9.213 | 9030 | 1548665512938268 | 613 | NULL |
44. +---------------+------+------------------+-------------------------+---------------+
45. 20 rows in set (0.000 sec)
47. //以下是对心跳信息的监控(对ping指标的监控)
49. MySQL [(none)]> select * from mysql_server_ping_log;
50. +---------------+------+------------------+----------------------+------------+
51. | hostname | port | time_start_us | ping_success_time_us | ping_error |
52. +---------------+------+------------------+----------------------+------------+
53. | 192.168.9.211 | 9030 | 1548665195883407 | 98 | NULL |
54. | 192.168.9.212 | 9030 | 1548665195885128 | 119 | NULL |
55. ...........
56. | 192.168.9.213 | 9030 | 1548665415889362 | 106 | NULL |
57. | 192.168.9.213 | 9030 | 1548665562898295 | 97 | NULL |
58. +---------------+------+------------------+----------------------+------------+
59. 110 rows in set (0.001 sec)
61. //read_only日志此时也为空(正常来说,新环境配置时,这个只读日志是为空的)
63. MySQL [(none)]> select * from mysql_server_read_only_log;
64. Empty set (0.000 sec)
66. //3个节点都在hostgroup_id=10的组中。
68. //现在,将刚才 mysql_replication_hostgroups 表的修改加载到RUNTIME生效。
70. MySQL [(none)]> load mysql servers to runtime;
71. Query OK, 0 rows affected (0.003 sec)
73. MySQL [(none)]> save mysql servers to disk;
74. Query OK, 0 rows affected (0.361 sec)
76. //现在看结果
78. MySQL [(none)]> select hostgroup_id,hostname,port,status,weight from mysql_servers;
79. +--------------+---------------+------+--------+--------+
80. | hostgroup_id | hostname | port | status | weight |
81. +--------------+---------------+------+--------+--------+
82. | 10 | 192.168.9.211 | 9030 | ONLINE | 1 |
83. | 20 | 192.168.9.212 | 9030 | ONLINE | 1 |
84. | 20 | 192.168.9.213 | 9030 | ONLINE | 1 |
85. +--------------+---------------+------+--------+--------+
86. 3 rows in set (0.000 sec)
配置 Doris 用户
上面的所有配置都是关于后端 Doris FE 节点的,现在可以配置关于 SQL 语句的,包括:发送 SQL 语句的用户、SQL 语句的路由规则、SQL 查询的缓存、SQL 语句的重写等等。
本小节是 SQL 请求所使用的用户配置,例如 root 用户。这要求我们需要先在后端 Doris FE 节点添加好相关用户。这里以 root 和 doris 两个用户名为例。
1. //首先,在Doris FE master主数据库节点上执行:
3. # mysql -P9030 -uroot -p
4. .........
5. mysql> create user doris@'%' identified by 'P@ssword1!';
6. Query OK, 0 rows affected, 1 warning (0.04 sec)
8. mysql> grant ADMIN_PRIV on *.* to doris@'%';
9. Query OK, 0 rows affected, 1 warning (0.03 sec)
12. //然后回到 mysql-proxy 代理层节点,配置 mysql_users 表,将刚才的两个用户添加到该表中。
14. admin> insert into mysql_users(username,password,default_hostgroup) values('root','',10);
15. Query OK, 1 row affected (0.001 sec)
17. admin> insert into mysql_users(username,password,default_hostgroup) values('doris','P@ssword1!',10);
18. Query OK, 1 row affected (0.000 sec)
20. 加载用户到运行环境中,并将用户信息保存到磁盘
21. admin> load mysql users to runtime;
22. Query OK, 0 rows affected (0.001 sec)
24. admin> save mysql users to disk;
25. Query OK, 0 rows affected (0.108 sec)
27. // mysql_users 表有不少字段,最主要的三个字段为 username、password 和default_hostgroup:
29. //- username:前端连接ProxySQL,以及ProxySQL将SQL语句路由给MySQL所使用的用户名。
30. //- password:用户名对应的密码。可以是明文密码,也可以是hash密码。如果想使用hash密码,可以先在某个MySQL节点上执行
32. select password(PASSWORD),然后将加密结果复制到该字段。
34. //- default_hostgroup:该用户名默认的路由目标。例如,指定root用户的该字段值为10时,则使用root用户发送的SQL语句默认
35. // 情况下将路由到hostgroup_id=10组中的某个节点。
37. admin> select * from mysql_users\G
38. *************************** 1. row ***************************
39. username: root
40. password:
41. active: 1
42. use_ssl: 0
43. default_hostgroup: 10
44. default_schema: NULL
45. schema_locked: 0
46. transaction_persistent: 1
47. fast_forward: 0
48. backend: 1
49. frontend: 1
50. max_connections: 10000
51. *************************** 2. row ***************************
52. username: doris
53. password: P@ssword1!
54. active: 1
55. use_ssl: 0
56. default_hostgroup: 10
57. default_schema: NULL
58. schema_locked: 0
59. transaction_persistent: 1
60. fast_forward: 0
61. backend: 1
62. frontend: 1
63. max_connections: 10000
64. 2 rows in set (0.000 sec)
66. //虽然这里没有详细介绍mysql_users表,但只有active=1的用户才是有效的用户。
68. MySQL [(none)]> load mysql users to runtime;
69. Query OK, 0 rows affected (0.001 sec)
71. MySQL [(none)]> save mysql users to disk;
72. Query OK, 0 rows affected (0.123 sec)
74. //这样就可以通过sql客户端,使用doris的用户名密码去连接了ProxySQL了
通过 ProxySQL 连接 Doris 进行测试
下面,分别使用 root 用户和 doris 用户测试下它们是否能路由到默认的 hostgroup_id=10 (它是一个写组) 读数据。下面是通过转发端口 6033 连接的,连接的是转发到后端真正的数据库!
1. #mysql -uroot -p -P6033 -hdoris01 -e "show databases;"
2. Enter password:
3. ERROR 9001 (HY000) at line 1: Max connect timeout reached while reaching hostgroup 10 after 10000ms
5. //这个时候发现出错,并没有转发到后端真正的 Doris FE上
7. //通过日志看到有set autocommit=0 这样开启事务
9. //检查配置发现:
10. mysql-forward_autocommit=false
11. mysql-autocommit_false_is_transaction=false
13. //我们这里不需要读写分离,只需要将这两个参数通过下面语句直接搞成true就可以了
15. mysql> UPDATE global_variables SET variable_value='true' WHERE variable_name='mysql-forward_autocommit';
16. Query OK, 1 row affected (0.00 sec)
18. mysql> UPDATE global_variables SET variable_value='true' WHERE variable_name='mysql-autocommit_false_is_transaction';
19. Query OK, 1 row affected (0.01 sec)
21. mysql> LOAD MYSQL VARIABLES TO RUNTIME;
22. Query OK, 0 rows affected (0.00 sec)
24. mysql> SAVE MYSQL VARIABLES TO DISK;
25. Query OK, 98 rows affected (0.12 sec)
27. //然后我们在重新试一下,显示成功
29. [root@doris01 ~]# mysql -udoris -pP@ssword1! -P6033 -h192.168.9.211 -e "show databases;"
30. Warning: Using a password on the command line interface can be insecure.
31. +--------------------+
32. | Database |
33. +--------------------+
34. | doris_audit_db |
35. | information_schema |
36. | retail |
37. +--------------------+
OK,到此就结束了,你就可以用 Mysql 客户端,JDBC 等任何连接 MySQL 的方式连接 ProxySQL 去操作你的 Doris 了
Nginx TCP 反向代理方式
环境准备
注意:使用 Nginx 实现 Apache Doris 数据库的负载均衡,前提是要搭建 Apache Doris 的环境,Apache Doris FE 的 IP 和端口分别如下所示,这里我是用一个 FE 来做演示的,多个 FE 只需要在配置里添加多个 FE 的 IP 地址和端口即可
通过 Nginx 访问 MySQL 的 Apache Doris 和端口如下所示。
1. IP: 172.31.7.119
2. 端口: 9030
安装依赖
1. sudo apt-get install build-essential
2. sudo apt-get install libpcre3 libpcre3-dev
3. sudo apt-get install zlib1g-dev
4. sudo apt-get install openssl libssl-dev
安装 Nginx
1. sudo wget http://nginx.org/download/nginx-1.18.0.tar.gz
2. sudo tar zxvf nginx-1.18.0.tar.gz
3. cd nginx-1.18.0
4. sudo ./configure --prefix=/usr/local/nginx --with-stream --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module
5. sudo make && make install
配置反向代理
这里是新建了一个配置文件
1. vim /usr/local/nginx/conf/default.conf
然后在里面加上下面的内容
1. events {
2. worker_connections 1024;
3. }
4. stream {
5. upstream mysqld {
6. hash $remote_addr consistent;
7. server 172.31.7.119:9030 weight=1 max_fails=2 fail_timeout=60s;
8. ##注意这里如果是多个FE,加载这里就行了
9. }
10. ###这里是配置代理的端口,超时时间等
11. server {
12. listen 6030;
13. proxy_connect_timeout 300s;
14. proxy_timeout 300s;
15. proxy_pass mysqld;
16. }
17. }
启动 Nginx
指定配置文件启动
1. cd /usr/local/nginx
2. /usr/local/nginx/sbin/nginx -c conf.d/default.conf
验证
1. mysql -uroot -P6030 -h172.31.7.119
参数解释:
- -u 指定 Doris 用户名
- -p 指定 Doris 密码,我这里密码是空,所以没有
- -h 指定 Nginx 代理服务器 IP
- -P 指定端口
1. mysql -uroot -P6030 -h172.31.7.119
2. Welcome to the MySQL monitor. Commands end with ; or \g.
3. Your MySQL connection id is 13
4. Server version: 5.1.0 Doris version 0.15.1-rc09-Unknown
6. Copyright (c) 2000, 2022, Oracle and/or its affiliates.
8. Oracle is a registered trademark of Oracle Corporation and/or its
9. affiliates. Other names may be trademarks of their respective
10. owners.
12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
14. mysql> show databases;
15. +--------------------+
16. | Database |
17. +--------------------+
18. | information_schema |
19. | test |
20. +--------------------+
21. 2 rows in set (0.00 sec)
IP 透传
自 2.1.1 版本开始,Doris 支持 Proxy Protocol 协议。利用这个协议,可以是实现负载均衡的 IP 透传,从而在经过负载均衡后,Doris 依然可以获取客户端的真实 IP,实现白名单等权限控制。
注:
- 仅支持 Proxy Protocol V1。
- 仅支持并做用于 MySQL 协议端口,不支持和影响 HTTP、ADBC 等其他协议端口。
- 开启后,必须使用 Proxy Protocol 协议进行连接,否则连接失败。
下面以 Nginx 为例,介绍如何实现 IP 透传。
- Doris 开启 Proxy Protocol
在 FE 的 fe.conf 中添加:
1. enable_proxy_protocol = true
2. Nginx 开启 Proxy Protocol
1. events {
2. worker_connections 1024;
3. }
4. stream {
5. upstream mysqld {
6. hash $remote_addr consistent;
7. server 172.31.7.119:9030 weight=1 max_fails=2 fail_timeout=60s;
8. }
9. server {
10. listen 6030;
11. proxy_connect_timeout 300s;
12. proxy_timeout 300s;
13. proxy_pass mysqld;
14. # Enable Proxy Protocol to the upstream server
15. proxy_protocol on;
16. }
17. }
3. 通过代理连接 Doris
1. mysql -uroot -P6030 -h172.31.7.119
4. 验证
1. mysql> show processlist;
2. +------------------+------+------+-------------------+---------------------+----------+------+---------+------+-------+-----------------------------------+------------------+
3. | CurrentConnected | Id | User | Host | LoginTime | Catalog | Db | Command | Time | State | QueryId | Info |
4. +------------------+------+------+-------------------+---------------------+----------+------+---------+------+-------+-----------------------------------+------------------+
5. | Yes | 1 | root | 172.21.0.32:34390 | 2024-03-17 16:32:22 | internal | | Query | 0 | OK | 82edc460d93f4e28-8bbed058a068e259 | show processlist |
6. +------------------+------+------+-------------------+---------------------+----------+------+---------+------+-------+-----------------------------------+------------------+
7. 1 row in set (0.00 sec)
如果在 Host 列看到的真实的客户端 IP,则说明验证成功。否则,只能看到代理服务的 IP 地址。
同时,在 fe.audit.log 中也会记录真实的客户端 IP。
Haproxy 方式
HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。
安装
- 下载 HAProxy
下载地址:<https://src.fedoraproject.org/repo/pkgs/haproxy/> 2. 解压
1. tar -zxvf haproxy-2.6.15.tar.gz -C /opt/
2. mv haproxy-2.6.15 haproxy
3. 编译
进入到 haproxy 目录中
``` 1. yum install gcc gcc-c++ -y
-
make TARGET=linux-glibc PREFIX=/usr/local/haproxy
-
make install PREFIX=/usr/local/haproxy ```
配置
- 配置 haproxy.conf 文件
vim /etc/rsyslog.d/haproxy.conf
1. $ModLoad imudp
2. $UDPServerRun 514
3. local0.* /usr/local/haproxy/logs/haproxy.log
4. &~
2. 开启远程日志
vim /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS="-c 2 -r -m 0"
参数解析:
- -c 2 使用兼容模式,默认是 -c 5。 -r 开启远程日志
- -m 0 标记时间戳。单位是分钟,为0时,表示禁用该功能
- 使修改生效
systemctl restart rsyslog
4. 编辑负载均衡文件
vim /usr/local/haproxy/haproxy.cfg
``` #
# haproxy 部署在 172.16.0.3,这台机器上,用来代理 172.16.0.8,172.16.0.6,172.16.0.4 这三台部署 fe 的机器
#
global maxconn 2000 ulimit-n 40075 log 127.0.0.1 local0 info uid 200 gid 200 chroot /var/empty daemon group haproxy user haproxy
1. defaults
2. # 应用全局的日志配置
3. log global
4. mode http
5. retries 3 # 健康检查。3次连接失败就认为服务器不可用,主要通过后面的check检查
6. option redispatch # 服务不可用后重定向到其他健康服务器
7. # 超时配置
8. timeout connect 5000
9. timeout client 5000
10. timeout server 5000
11. timeout check 2000
13. frontend agent-front
14. bind *:9030 # 代理机器上的转换端口
15. mode tcp
16. default_backend forward-fe
18. backend forward-fe
19. mode tcp
20. balance roundrobin
21. server fe-1 172.16.0.8:9030 weight 1 check inter 3000 rise 2 fall 3
22. server fe-2 172.16.0.4:9030 weight 1 check inter 3000 rise 2 fall 3
23. server fe-3 172.16.0.6:9030 weight 1 check inter 3000 rise 2 fall 3
25. listen http_front # haproxy的客户页面
26. bind *:8888 # HAProxy WEB 的IP地址
27. mode http
28. log 127.0.0.1 local0 err
29. option httplog
30. stats uri /haproxy # 自定义页面的 url(即访问时地址为:172.16.0.3:8888/haproxy)
31. stats auth admin:admin # 控制面板账号密码 账号:admin
32. stats refresh 10s
33. stats enable
34. ```
启动
- 启动服务
/opt/haproxy/haproxy -f /usr/local/haproxy/haproxy.cfg
2. 查看服务状态
netstat -lnatp | grep -i haproxy
3. WEB 访问
ip:8888/haproxy
登陆密码:admin : admin
注意:WEB 登陆的端口、账户、密码需要在 haproxy.cfg 文件中配置 4. 测试端口是否转换成功
mysql -h 172.16.0.3 -uroot -P3307 -p
