揭秘高性能负载均衡神器:HAProxy深度解析与实战应用

Haproxy介绍

HAProxy是法国开发者威利塔罗(Willy Tarreau) 在2000年使用C语言开发的一个开源软件,是一款具备高并发(一万以上)、高性能的TCP和HTTP负载均衡器,支持基于cookie的持久性,自动故障切换,支持正则表达式及web状态统计,目前最新TLS版本为2.8。

HAProxy 是一款免费、快速、可靠的反向代理服务器,可为基于 TCP 和 HTTP 的应用程序提供高可用性、负载平衡和代理服务。它特别适用于流量非常大的网站,并为世界上访问量最大的网站中的很大一部分提供支持。多年来,它已成为事实上的标准开源负载平衡器,现在随大多数主流 Linux 发行版一起提供,并通常默认部署在云平台中。

从2013年HAProxy 分为社区版和企业版,企业版将提供更多的特性和功能以及全天24小时的技术支持等服务。

HAproxy安装与配置

实验环境:

三台主机

haproxy--100

webserver1--10

webserver2--20

1/2--安装nginx

echo webserver1 - 172.25.254.10 > /user/share/nginx/html/index.html

#设置开机启动
systemctl enable --now nginx

haproxy:


[root@haproxy ~]# dnf install haproxy -y

配置文件路径


rpm -qc haproxy
 
[root@haproxy ~]# rpm -qc haproxy 
/etc/haproxy/haproxy.cfg  ---- 配置文件
/etc/logrotate.d/haproxy
/etc/sysconfig/haproxy
[root@haproxy ~]#

frontend webcluster
    bind *:80
    mode http
    use_backend webcluster-host   #调用backend的名称
 
 
backend webcluster-host
    mode http
    server web1 172.25.250.10:80 check inter 2 fall 2 rise 5

第二种写法

重启服务

[root@haproxy ~]# systemctl restart haproxy.service

多线程和进程


[root@haproxy ~]# pstree -p | grep haproxy
           |-haproxy(32906)-+-haproxy(32908)
           |                `-haproxy(32909)

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
global
    log         127.0.0.1 local2
 
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
 
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
 
    # utilize system-wide crypto-policies
    ssl-default-bind-ciphers PROFILE=SYSTEM
    ssl-default-server-ciphers PROFILE=SYSTEM
    #nbproc 2  #启用多进程
    #cpu-map 1 0
    #cpu-map 2 1
    nbthread 2   #启用多线程

Proxies配置-defaults

defaults
mode http # HAProxy实例使用的连接协议
log global #指定日志地址和记录日志条目的
syslog/rsyslog日志设备
#此处的 global表示使用 global配置段中
设定的log值。
option httplog #日志记录选项,httplog表示记录与 HTTP
会话相关的各种属性值
#包括 HTTP请求、会话状态、连接数、源地
址以及连接时间等
option dontlognull #dontlognull表示不记录空会话连接日志
option http-server-close #等待客户端完整HTTP请求的时间,此处为等
待10s。
option forwardfor except 127.0.0.0/8 #透传客户端真实IP至后端web服务器
#在apache配置文件中加入:<br>%{XForwarded-For}i
#后在webserer中看日志即可看到地址透传
信息
option redispatch #当server Id对应的服务器挂掉后,强制定
向到其他健康的服务器,重新派发
option http-keep-alive #开启与客户端的会话保持
retries 3 #连接后端服务器失败次数
timeout http-request 1000s #等待客户端请求完全被接收和处理的最
长时间
timeout queue 60s #设置删除连接和客户端收到503或服务不可
用等提示信息前的等待时间
timeout connect 120s #设置等待服务器连接成功的时间
timeout client 600s #设置允许客户端处于非活动状态,即既不发
送数据也不接收数据的时间
timeout server 600s #设置服务器超时时间,即允许服务器处于既
不接收也不发送数据的非活动时间
timeout http-keep-alive 60s #session 会话保持超时时间,此时间段内
会转发到相同的后端服务器
timeout check 10s #指定后端服务器健康检查的超时时间
maxconn 3000
default-server inter 1000 weight 3

推荐值 -- queue--60s

server配置

check                 #对指定real进行健康状态检查,如果不加此设置,默认不开启检查,只有check后面没 有其它配置也可以启用检查功能

                           #默认对相应的后端服务器IP和端口,利用TCP连接进行周期性健康性检查,注意必须指定 端口才能实现健康性检查

addr  <IP>             #可指定的健康状态监测IP,可以是专门的数据网段,减少业务网络的流量

port  <num>          #指定的健康状态监测端口

inter  <num>         #健康状态检查间隔时间,默认2000 ms

fall  <num>            #后端服务器从线上转为线下的检查的连续失效次数,默认为3

rise  <num>           #后端服务器从下线恢复上线的检查的连续有效次数,默认为2

weight  <weight>   #默认为1,最大值为256,0(状态为蓝色)表示不参与负载均衡,但仍接受持久连接

backup                  #将后端服务器标记为备份状态,只在所有非备份主机down机时提供服务,类似Sorry Server

disabled                #将后端服务器标记为不可用状态,即维护状态,除了持久模式 #将不再接受连接,状态为深黄色,优雅下线,不再接受新用户的请求

redirect prefix http://www.baidu.com/   #将请求临时(302)重定向至其它URL,只适用于http模式

maxconn  <maxconn>           #当前后端server的最大并发连接数

下线指定realserver,相当于关闭,若想开启,则删除disabled

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

listen webcluster
    bind *:80
    mode http
    balance roundrobin
    server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2 disabled
    server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1 disabled
    server web_sorry 172.25.254.100:8080 backup

网页重定向

listen webcluster
    bind *:80
    mode http
    balance roundrobin
    redirect prefix http://www.baidu.com/  #重定向
    #server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2 disabled
    #server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1 disabled

在100里开httpd进行sorry-server:

[root@haproxy ~]# yum install httpd -y

[root@haproxy ~]# vim /etc/httpd/conf/httpd.conf

#Listen 12.34.56.78:80
Listen 8080        #把80端口改为8080端口

[root@haproxy ~]# systemctl start httpd.service 
[root@haproxy ~]# echo sorry 下班了!!> /var/www/html/index.html

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
#server web_sorry 172.25.254.100:8080 backup

socat工具(动态调整)

配置

加上

下载

命令

[root@haproxy ~]# yum install socat -y    #查看帮助
[root@haproxy ~]# echo "help" | socat stdio /var/lib/haproxy/stats    #查看状态
[root@haproxy ~]# echo "show info" | socat stdio /var/lib/haproxy/stats    #查看server状态
[root@haproxy ~]# echo get weight webcluster/web1 | socat stdio /var/lib/haproxy/stats    #查看权重
[root@haproxy ~]# echo "set weight webcluster/web1 1" | socat stdio /var/lib/haproxy/stats    #修改权重
[root@haproxy ~]# echo "disable server webcluster/web1" | socat stdio /var/lib/haproxy/stats    #下线主机
[root@haproxy ~]# echo "enable server webcluster/web1" | socat stdio /var/lib/haproxy/stats    #上线主机

haproxy多进程如何热处理

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats mode 600 level admin process 1
    stats socket /var/lib/haproxy/stats2 mode 600 level admin process 2

    # utilize system-wide crypto-policies
    ssl-default-bind-ciphers PROFILE=SYSTEM
    ssl-default-server-ciphers PROFILE=SYSTEM
    #nbproc 2
    #cpu-map 1 0
    #cpu-map 2 1
    nbthread 2

HAproxy算法

静态

static-rr

基于权重的轮询调度

不支持热处理、慢启动

--慢启动是指在服务器刚刚启动上不会把他所应该承担的访问压力全部给它,而是先给一部分,当没 问题后在给一部分

listen webcluster
    bind *:80
    mode http
    balance static-rr
    #balance first
    #balance roundrobin
    #balance leastconn
    #balance source
    #balance uri
    #balance url_param name,userid
    #balance har(User-Agent)
    #hash-type consistent
    #cookie WEBCOOKIE insert nocache indirect
    server web1 192.168.86.10:80 check inter 2 fall 3 rise 5 weight 2
    server web2 192.168.86.20:80 check inter 2 fall 3 rise 5 weight 1
    #server web_sorry 192.168.86.100:8080 backup


[root@haproxy ~]# systemctl restart haproxy.service 

first

根据服务器在列表中的位置,自上而下进行调度

listen webcluster
    bind *:80
    mode http
    balance first
    server web1 192.168.86.10:80 maxconn 1 check inter 2 fall 3 rise 5 weight 2
    server web2 192.168.86.20:80 check inter 2 fall 3 rise 5 weight 1



[root@haproxy ~]# systemctl restart haproxy.service 

最大链接数改为1,看效果

在多台主机中执行死循环测试效果

动态

roundrobin

基于权重的轮询调度,且负载小

支持慢启动

listen webcluster
    bind *:80
    mode http
    balance roundrobin
    server web1 192.168.86.10:80 check inter 2 fall 3 rise 5 weight 2
    server web2 192.168.86.20:80 check inter 2 fall 3 rise 5 weight 1
    #server web_sorry 192.168.86.100:8080 backup

leastconn

基于链接的

支持权重、慢启动--根据当前链接最少且权重多的

    balance leastconn

其他(既可以是静态也可以是动态)(动静结合)

source

源地址hash,基于用户源地址hash并将请求转发到后端服务器,后续同一个源地址请求将被转发至同一个后端web服务器。此方式当后端服务器数据量发生变化时,会导致很多用户的请求转发至新的后端服务器,默认为静态方式,但是可以通过hash-type支持的选项更改这个算法一般是在不插入Cookie的TCP模式下使用,也可给拒绝会话cookie的客户提供最好的会话粘性,适用于session会话保持但不支持cookie和缓存的场景源地址有两种转发客户端请求到后端服务器的服务器选取计算方式,分别是取模法和一致性hash。

缺点--因为权重的变化、导致会话丢失

    balance source

map-base取模法

map-based:取模法,对source地址进行hash计算,再基于服务器总权重的取模,最终结果决定将此请求转发至对应的后端服务器。

此方法是静态的,即不支持在线调整权重,不支持慢启动,可实现对后端服务器均衡调度

缺点是当服务器的总权重发生变化时,即有服务器上线或下线,都会因总权重发生变化而导致调度结果整体改变

一致性hash

一致性哈希,当服务器的总权重发生变化时,对调度结果影响是局部的,不会引起大的变动hash mode该hash算法是动态的,支持使用 socat等工具进行在线权重调整,支持慢启动

算法:

1、后端服务器哈希环点keyA=hash(后端服务器虚拟ip)%(2^32)

2、客户机哈希环点key1=hash(client_ip)%(2^32) 得到的值在[0---4294967295]之间,

3、将keyA和key1都放在hash环上,将用户请求调度到离key1最近的keyA对应的后端服务器

hash环顺时针旋转

hash环------2^32数字落在一个环上

hash环得到的随机数2^32-1

随机数

cat  /dev/random

生成原理:鼠标的运动轨迹、键盘的敲击

uri

uri(uri、urn),uri包含url

  基于对用户请求的URI的左半部分或整个uri做hash,再将hash结果对总权重进行取模后根据最终结果将请求转发到后端指定服务器适用于后端是缓存服务器场景默认是静态算法,也可以通过hash-type指定map-based和consistent,来定义使用取模法还是一致性hash

<scheme>://<user>:<password>@<host>:<port>/<path>;<params>?<query>#<frag>
左半部分:/<path>;<params>
整个uri:/<path>;<params>?<query>#<frag>

协议://用户;密码@域名;端口/文件路径;传递的指令?查询目标#片段


[root@webserver1 ~]# echo RS1 172.25.254.10 index1 > /var/www/html/index1.html
[root@webserver1 ~]# echo RS1 172.25.254.10 index2 > /var/www/html/index2.html
[root@webserver1 ~]# echo RS1 172.25.254.10 index3 > /var/www/html/index3.html
[root@webserver2 ~]# echo RS1 172.25.254.20 index1 > /var/www/html/index1.html
[root@webserver2 ~]# echo RS1 172.25.254.20 index2 > /var/www/html/index2.html
[root@webserver2 ~]# echo RS1 172.25.254.20 index3 > /var/www/html/index3.html
 
[root@client ~]# curl 172.25.254.100/index.html
RS2 server - 172.25.254.20
[root@client ~]# curl 172.25.250.100/index1.html
RS1 172.25.254.10 index1
[root@client ~]# curl 172.25.250.100/index2.html
RS1 172.25.254.20 index2
[root@client ~]# curl 172.25.250.100/index3.html
RS1 172.25.254.10 index3

url_param取模法

可支持多个url_param hash

 url_param对用户请求的url中的 params 部分中的一个参数key对应的value值作hash计算,并由服务器总权重相除以后派发至某挑出的服务器,后端搜索同一个数据会被调度到同一个服务器,多用与电商通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个real server如果无没key,将按roundrobin算法

    balance url_param name,userid

测试:curl 172.25.254.100/index.html

hdr

针对用户每个http头部(header)请求中的指定信息做hash,此处由 name 指定的http首部将会被取出并做hash计算,然后由服务器总权重取模以后派发至某挑出的服务器,如果无有效值,则会使用默认的轮询调度。  

    balance har(User-Agent)

测试:curl 172.25.254.100/index.html

-vA--指定浏览器

算法总结

#静态
static-rr------------->tcp/http
first------------------>tcp/http
#动态
roundrobin-------->tcp/http
leastconn--------->tcp/http
#以下静态和动态取决于hash_type是否consistent
source------------->tcp/http
Uri------------------>http
url_param--------->http
hdr------------------>http

first #使用较少
static-rr #做了session共享的web集群
roundrobin
leastconn #数据库
source
#基于客户端公网IP的会话保持
Uri--------------->http #缓存服务器,CDN服务商,蓝汛、百度、阿里云、腾讯
url_param--------->http #可以实现session保持
hdr #基于客户端请求报文头部做下一步处理

roundrobin主流

HAproxy高级功能

基于cookie的会话保持

[root@localhost ~]# vim /etc/haproxy/haproxy.cfg

[root@localhost ~]# systemctl restart haproxy.service

cookie WEBCOOKIE insert nocache indirect

    server web1 192.168.86.10:80 cookie lee1 check inter 2 fall 3 rise 5 weight 2

    server web2 192.168.86.20:80 cookie lee2 check inter 2 fall 3 rise 5 weight 1

重启测试


[root@haproxy ~]# curl -b WEBCOOKIE=haha1 172.25.254.100
webserver1 - 172.25.254.10
[root@haproxy ~]# curl -b WEBCOOKIE=haha2 172.25.254.100
webserver2 - 172.25.254.20
[root@haproxy ~]# curl -b WEBCOOKIE=haha1 172.25.254.100
webserver1 - 172.25.254.10
[root@haproxy ~]# curl -b WEBCOOKIE=haha2 172.25.254.100
webserver2 - 172.25.254.20

状态页

通过web界面,显示当前HAProxy的运行状态

stats enable                                   #基于默认的参数启用stats page
stats hide-version                          #将状态页中haproxy版本隐藏
stats refresh <delay>                     #设定自动刷新时间间隔,默认不自动刷新
stats uri <prefix>                            #自定义stats page uri,默认值:/haproxy?stats
stats auth <user>:<passwd>          #认证时的账号和密码,可定义多个用户,每行指定一个用户
                                                       #默认:no authentication
stats admin { if | unless } <cond>    #启用stats page中的管理功能

listen stats
    mode http
    bind *:9999
    stats enable
    stats refresh 5
    stats uri /status
    stats auth lee:lee

访问ip://9999/status

IP透传

web服务器中需要记录客户端的真实IP地址,用于做访问统计、安全防护、为分析、区域排行等场景。

四层

listen webcluster
    bind *:80
    mode tcp
    #balance static-rr
    #balance first
    balance roundrobin
    #balance leastconn
    #balance source
    #balance uri
    #balance url_param name,userid
    #balance har(User-Agent)
    #hash-type consistent
    #cookie WEBCOOKIE insert nocache indirect
    server web1 192.168.86.10:80 check inter 2 fall 3 rise 5 weight 2
    server web2 192.168.86.20:80 check inter 2 fall 3 rise 5 weight 1
    #server web_sorry 192.168.86.100:8080 backup

nginx中测试查看

[root@webserver2 ~]# cat /var/log/nginx/access.log

httpd中更改日志实现透传、nginx中支持

    #在此处加入一行%{X-Forwarded-For}i
    LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

测试查看

ACL

访问控制列表

是一种基于包过滤的访问控制技术

它可以根据设定的条件对经过服务器传输的数据包进行过滤(条件匹配)即对接收到的报文进行匹配和过滤,基于请求报文头部中的源地址、源端口、目标地址、目标端口、请求方法、URL、文件后缀等信息内容进行匹配并执行进—步操作,比如允许其通过或丢弃。

定义ACL匹配规范,即:判断条件

hdr string,提取在一个HTTP请求报文的首部
hdr([<name> [,<occ>]]):完全匹配字符串,header的指定信息,<occ> 表示在多值中使用的值的出
现次数
hdr_beg([<name> [,<occ>]]):前缀匹配,header中指定匹配内容的begin
hdr_end([<name> [,<occ>]]):后缀匹配,header中指定匹配内容end
hdr_dom([<name> [,<occ>]]):域匹配,header中的dom(host)
hdr_dir([<name> [,<occ>]]):路径匹配,header的uri路径
hdr_len([<name> [,<occ>]]):长度匹配,header的长度匹配
hdr_reg([<name> [,<occ>]]):正则表达式匹配,自定义表达式(regex)模糊匹配
hdr_sub([<name> [,<occ>]]):子串匹配,header中的uri模糊匹配 模糊匹配c 报文中a/b/c也会匹
配 

参数

-i           不区分大小写

-m         使用指定的正则表达式匹配方法

-n          不做DNS解析

-u          禁止acl重名,否则多个同名ACL匹配或关系


整数比较: eq、ge、gt、 le、1t字符比较:

- exact match( -m str):字符串必须完全匹配模式

- substring match (-m sub) :在提取的字符串中查找模式,如果其中任何一个被发现,ACL将匹配

- prefix match (-m beg):在提取的字符串首部中查找模式,如果其中任何一个被发现,ACL将匹配

- suffix match (-m end) :将模式与提取字符串的尾部进行比较,如果其中任何一个匹配,则ACL进行匹配

- subdir match (-m dir) :查看提取出来的用斜线分隔(“/")的字符串,如其中任一个匹配,则ACL进行匹配

- domain match (-m dom):查找提取的用点(“.")分隔字符串,如果其中任何一个匹配,则ACL进行匹配

eg:

​
frontend webcluster

    bind *:80

    mode http

    acl test hdr_dom(host) -i www.timinglee.org

    #acl test hdr_end(host) -i .org

    #acl test hdr_beg(host) -i bbs

    #acl domain hdr_beg(host) -i bbs

    #acl test base_sub -m sub bbs

    #acl test base_reg -i lee/$

    #acl test path_sub -m sub lee

    use_backend webcluster-host if test    #四种符号

    #use_backend webcluster-host if ! test

    #use_backend webcluster-host if test domain

    #use_backend webcluster-host if test || domain    #||  --  两者满足一个

    default_backend default-host

 

backend webcluster-host

    mode http

    server web1 192.168.86.10:80 check inter 2 fall 2 rise 5

 

backend default-host

    mode http

    server web2 192.168.86.20:80 check inter 2 fall 2 rise 5

​

C:\Windows\System32\drivers\etc 里改为www

测试Ping

​
192.168.86.100        www.timinglee.com www.timinglee.org bbs.timinglee.org

​

基于域名的访问


[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
frontend webcluster
    bind *:80
    mode http
    acl test hdr_dom(host) -i www.timinglee.org
    use_backend webcluster-host if test
    default_backend default-host
backend webcluster-host
    mode http
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
    
backend default-host
    mode http
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
[root@haproxy ~]# systemctl restart haproxy.service

基于ip的访问

frontend webcluster
    bind *:80
    mode http
	#基于ip的访问
    acl ctrl_ip src 172.25.254.1 172.25.254.20 192.168.0.0/24
    user_backend webcluster-host if ctrl_ip    #允许ip
    #http-request denf if ctrl_ip    #拒绝ip

    default_backend default-host

基于浏览器

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 
frontend webcluster
    bind *:80
    mode http
    acl test hdr_dom(host) -i www.xixi.com
    acl ctrl_ip src 192.168.10.1 192.168.10.10 172.25.0.0/24

   ############################################
    http-request deny if test  
    default_backend default-host


[root@haproxy ~]# systemctl restart haproxy.service

基于后缀实现动静分离

[root@webserver1 ~]# yum install php -y
[root@webserver1 ~]# vim /var/www/html/index.php 
[root@webserver1 ~]# cat /var/www/html/index.php 
<?php
		phpinfo();
?>
[root@webserver1 ~]# systemctl restart httpd

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 

frontend webcluster
    bind *:80
    mode http
    acl badweb hdr_sub(User-Agent) -i curl wget
    acl static path_end -i .html .jpg .png .css .js
    acl php path_end -i .php

   ############################################
    use_backend webcluster-host if php
    default_backend default-host

[root@haproxy ~]# systemctl restart haproxy.service 

基于访问路径实现动静分离

[root@webserver1 ~]# mkdir -p /var/www/html/lee/
[root@webserver1 ~]# echo xixi 192.168.0.10 /var/www/html/lee/index.html

[root@webserver2 ~]# mkdir -p /usr/share/nginx/html/lee/
[root@webserver2 ~]# echo haha 192.168.0.20 > /usr/share/nginx/html/lee/index.html

frontend webcluster
    bind *:80
    mode http
    acl static path_sub   -m sub static
    acl php    path_sub   -m sub php 
    
    use_backend webcluster-host if php 
    default_backend default-host
 
    default_backend default-host
 

自定义错误页面

查看503位置

[root@haproxy ~]# rpm -ql haproxy | grep http
/usr/share/doc/haproxy/design-thoughts/http2.txt
/usr/share/doc/haproxy/design-thoughts/http_load_time.url
/usr/share/doc/haproxy/internals/http-cookies.txt
/usr/share/doc/haproxy/internals/http-docs.txt
/usr/share/doc/haproxy/internals/http-parsing.txt
/usr/share/doc/haproxy/option-http_proxy.cfg
/usr/share/haproxy/400.http
/usr/share/haproxy/403.http
/usr/share/haproxy/408.http
/usr/share/haproxy/500.http
/usr/share/haproxy/502.http
/usr/share/haproxy/503.http
/usr/share/haproxy/504.http
[root@haproxy ~]# mkdir -p /etc/haproxy/errorpage/

[root@haproxy errorpage]# cp /usr/share/haproxy/503.http /etc/haproxy/errorpage/

[root@haproxy ~]# cd /etc/haproxy/errorpage/
[root@haproxy errorpage]# vim 503.http

指定报错文件,写到defaults里

    errorfile 503 /etc/haproxy/errorpage/503.http
HTTP/1.0 503 Service Unavailable 
Cache-Control: no-cache 
Connection: close 
Content-Type: text/html;charset=UTF-8 
 
<html><body><h1>什么动物生气最安静</h1>
大猩猩!!
</body></html>

重定向--访问错误定向到百度

    errorloc 503 https://www.baidu.com

Haproxy四层负载

装数据库

[root@webserver1 ~]# yum install mariadb-server -y
[root@webserver2 ~]# yum install mariadb-server -y

改两台server机配置id

[root@webserver1 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server_id=1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
 
systemctl start mariadb
 
[root@webserver1 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server_id=2
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
 
systemctl start mariadb

远程登录

[root@webserver1 ~]# mysql -e "grant all on *.* to lee@'%' identified by 'lee';"
[root@webserver2 ~]# mysql -e "grant all on *.* to lee@'%' identified by 'lee';"

做负载

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen dbserver
    bind *:3306
    mode tcp
    balance static-rr    #轮询
    server db1 172.25.254.10:3306 check inter 2 fall 2 rise 5
    server db2 172.25.254.20:3306 check inter 2 fall 2 rise 5
    
[root@haproxy ~]# systemctl restart haproxy.service

测试

(最好再开另一台主机测试)

[root@haproxy ~]# mysql -ulee -plee -h 192.168.86.100
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.5.16-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|           1 |
+-------------+
1 row in set (0.001 sec)

Haproxy https实现

加密访问

[root@haproxy ~]# mkdir -p /etc/haproxy/certs

[root@haproxy ~]#openssl req -newkey rsa:2048 -nodes -sha256 –keyout /etc/haproxy/certs/timinglee.org.key 
-x509 -days 365 -out /etc/haproxy/certs/timinglee.org.crt

Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Shannxi
Locality Name (eg, city) [Default City]:Xi'an 
Organization Name (eg, company) [Default Company Ltd]:timinglee
Organizational Unit Name (eg, section) []:webserver
Common Name (eg, your name or your server's hostname) []:www.timinglee.org  
Email Address []:admin@timinglee.org
[root@haproxy ~]# 

 
[root@haproxy ~]# ls /etc/haproxy/certs/
timinglee.org.crt  timinglee.org.key

[root@haproxy ~]#cat /etc/haproxy/certs/timingding.org.key /etc/haproxy/certs/timingding.org.crt >
 /etc/haproxy/certs/timingding.pem

vim /etc/haproxy/haproxy.cfg
 
listen web-https
    bind *:443 ssl crt /etc/haproxy/certs/timingding.pem
    mode http
    balance roundrobin
    server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
    server web1 172.25.254.10:80 check inter 2 fall 2 rise 5

全站加密

vim /etc/haproxy/haproxy.cfg
 
frontend webcluster
    bind *:80
    mode http
    redirect scheme https if !{ ssl_fc }  
[root@haproxy ~]# vim /lib/systemd/system/haproxy.service
[root@haproxy ~]# cd /etc/haproxy/haproxy.cfg
[root@haproxy conf.d]# vim webcluster.cfg


[root@haproxy conf.d]# vim /etc/haproxy/haproxy.cfg
#listen stats
    #mode http
    #bind *:9999
    #stats enable
    #stats refresh 3
    #stats uri /status
    #stats auth lee:lee

  • 23
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值