负载均衡
nginx
lvs
haproxy
官网 https://www.haproxy.co
m/
自由及开放源代码软件
HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用
性、负载均衡,以及基于TCP和HTTP的应用程序代理。
HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保
持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并
发连接。并且它的运行模式使得它可以很简单安全地整合进用户当前的架构
中, 同时可以保护用户的web服务器不被暴露到网络上。
HAProxy实现了一种事件驱动 , 单一进程模型,此模型支持非常大的并发连
接数。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的
锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时
间管理的用户空间(User-Space) 实现所有这些任务,所以没有这些问题。此
模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他
们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。
包括 GitHub、Bitbucket[3]、Stack Overflow[4]、Reddit、Tumblr、
Twitter[5][6]和 Tuenti[7]在内的知名网站,及亚马逊网络服务系统都使用
了HAProxy。1.安装
2.配置
root@haproxy \~\]# yum -y install ntpdate.x86_64 \[root@haproxy \~\]# yum -y install ntp \[root@haproxy \~\]# ntpdate cn.ntp.org.cn 13 Aug 19:39:27 ntpdate\[1955\]: adjust time server 120.197.116.202 offset 0.059032 sec \[root@haproxy \~\]# systemctl start ntpd \[root@haproxy \~\]# systemctl enable ntpd \[root@haproxy \~\]# yum -y install haproxy18.x86_64 \[root@haproxy \~\]# vim /etc/haproxy/haproxy.cfg 60 #----------------------------------------------------- ---------------- 61 # main frontend which proxys to the backends 62 #----------------------------------------------------- ---------------- 63 frontend main \*:80 64 acl url_static path_beg -i /static /images /javascript /stylesheets 65 acl url_static path_end -i .jpg .gif .png .css .js 66 67 # use_backend static if url_static 68 default_backend web ...... 77 #---------------------------------------------------- -----------------**3.重启,设置开机启动** 78 # round robin balancing between the various backends 79 #----------------------------------------------------- ---------------- 80 backend web 81 balance roundrobin 82 server web01 10.1.1.200:80 check 83 server web02 10.1.1.201:80 check 84 \[root@haproxy \~\]# systemctl restart haproxy \[root@haproxy \~\]# systemctl enable haproxy**4.测试4.添加统计页面** # 定义web管理界面 listen statistics bind \*:9090 #定义监听端口 mode http #默认使用协议 stats enable #启用stats stats uri /hadmin?stats #自定义统计页面的url stats auth admin:admin #统计页面的账号密码 stats hide-version #隐藏在统计页面上的 haproxy版本信息 stats refresh 30s #统计页面自动刷新时间 stats admin if TRUE #如果认证通过就做管理 功能,可以管理后端服务器 stats realm hapadmin #统计页面密码框上提示 文件,默认为haproxy\\statistics 1.503错误,503service unavaliable 2.请求服务间歇性报错,一会儿正常,一会儿不正常 1. 两台服务有一台异常 1. 网关没有清除 3.9090无效,写错了 4.haproxy无法正常启动,查看配置文件是否异常 a 内⽹主机 不可以被⽹络访问 b 外⽹主机 可以被外⽹通过ip或者域名访问 使⽤b代理a主机,通过访问b主机管理a主机 访问b主机的特定端⼝,管理a主机的22端⼝ 现在我们说b为服务端,提供代理服务 a主机是客户端,被代理了 服务端 **开启端⼝⽂件** **配置⽂件** # 打开控制台⾯板端⼝ \[root@hcss-ecs-e083 \~\]# firewall-cmd --zone=public --add-port=7500/tcp --permanent # 打开frp服务端⼝ \[root@hcss-ecs-e083 \~\]# firewall-cmd --zone=public --add-port=7000/tcp --permanent # 打开⼀组客户端⼝ \[root@hcss-ecs-e083 \~\]# firewall-cmd --zone=public --add-port=6000-6100/tcp --permanent # 加载防⽕墙更新 \[root@hcss-ecs-e083 \~\]# firewall-cmd --reload \[root@hcss-ecs-e083 \~\]# ls -l frp_0.33.0_linux_amd64/frps\* -rwxrwxr-x 1 yuanyu yuanyu 12976128 Apr 27 2020 frp_0.33.0_linux_amd64/frps -rw-rw-r-- 1 yuanyu yuanyu 4639 Apr 27 2020 frp_0.33.0_linux_amd64/frps_full.ini -rw-rw-r-- 1 yuanyu yuanyu 114 Jul 18 21:41 frp_0.33.0_linux_amd64/frps.ini**配置⽂件** \[root@hcss-ecs-e083 \~\]# vim \~/frp_0.33.0_linux_amd64/frps.ini --------------------------------------------------- ----------- # 服务名称 \[common
服务端⼝
bind_port = 7000
控制台⾯板账号
dashboard_user=xxxxxx
控制台密码
dashboard_pwd=xxxxxxx
控制台端⼝
dashboard_port=7500
代理标识
token=xxxxxxxx客户端
⽂件
配置⽂件
root@hcss-ecs-e083 \~\]# ls -l frp_0.33.0_linux_amd64/frpc\* -rwxrwxr-x 1 yuanyu yuanyu 10629120 Apr 27 2020 frp_0.33.0_linux_amd64/frpc -rw-rw-r-- 1 yuanyu yuanyu 7575 Apr 27 2020 frp_0.33.0_linux_amd64/frpc_full.ini -rw-rw-r-- 1 yuanyu yuanyu 126 Apr 27 2020 frp_0.33.0_linux_amd64/frpc.ini # 服务器名称 \[common
服务器ip
server_addr = x.x.x.x# 服务器端⼝
server_port = 7000
代理标识
token=xxxxxxx
被代理的主机名称,不允许和其他重复
被代理主机名称xxx
代理服务类型
type = tcp
本地ip
local_ip = 127.0.0.1
本地被代理的端⼝
local_port = 22
选择代理后的端⼝
remote_port = ⾃选端⼝xxxxxxx60000
1.修改配置文件
42 defaults
43 mode tcp
44 log global
45 option httplog
46 option dontlognull
......
63 frontend main *:3306
64 acl url_static path_beg -i /static
/images /javascript /stylesheets
65 acl url_static path_end -i .jpg .gif
.png .css .js
66
67 # use_backend static if url_static
68 default_backend mysql
....
85 backend mysql
86 balance roundrobin
87 server master 10.1.1.11:3306 check
88 server slave 10.1.1.12:3310 check
2.测试
root@client bin\]# ./mysql -h10.1.1.30 -P3306 -uzhangmin - pzhangmin mysql: \[Warning\] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \\g. Your MySQL connection id is 19 Server version: 8.0.33 MySQL Community Server - GPL Copyright (c) 2000, 2023, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement. mysql\> show variables like 'server_id'; +---------------+-------+ \| Variable_name \| Value \| +---------------+-------+ \| server_id \| 10 \| +---------------+-------+ 1 row in set (0.00 sec) mysql\> exit Bye \[root@client bin\]# ./mysql -h10.1.1.30 -P3306 -uzhangmin - pzhangmin mysql: \[Warning\] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \\g. Your MySQL connection id is 25 Server version: 8.0.33 MySQL Community Server - GPL Copyright (c) 2000, 2023, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement. mysql\> show variables like 'server_id'; +---------------+-------+ \| Variable_name \| Value \| +---------------+-------+ \| server_id \| 11 \| +---------------+-------+ 1 row in set (0.01 sec)mysql\> exit Bye \[root@client bin\]# cd bin/