Haproxy七层代理的基本、全局配置及热更新方法

提供后端检测,防止真实服务器掉线后,在调度器中分配任务时,也会发送到掉线的RS中。

一、haproxy的基本配置

在这里插入图片描述

1.1实验环境
主机名IP
clientVMware-nat 172.25.254.102; GW:172.25.254.108
haproxyNAT-eth0:172.25.254.107 ; 仅主机-eth1:192.168.0.107
server1仅主机 192.168.0.13 ; GW:192.168.0.107
server2仅主机 192.168.0.23 ; GW:192.168.0.107
1.2网络配置

(1)client测试机

[root@client ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection 
[connection]
id=eth0
type=ethernet
interface-name=eth0

[ipv4]
address1=172.25.254.102/24,172.25.254.107  -- 此网关为harpoxy的外网接口IP
method=manual

[root@client ~]# nmcli connection reload     # 重新加载网络配置
[root@client ~]# nmcli connection up eth0    # 重启网卡

# 关闭防火墙
[root@client ~]# systemctl stop firewalld     # 暂时关闭防火墙
[root@client ~]# systemctl mask firewalld     # 将防火墙写入null,永久关闭 
[root@client ~]# setenforce 0     # 设置selinux为宽容模式

(2)haproxy

# NAT-eth0网卡,连接外网
[root@client ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection 
[connection]
id=eth0
type=ethernet
interface-name=eth0

[ipv4]
address1=172.25.254.107/24,172.25.254.2
method=manual
dns=114.114.114.114;

# 仅主机-eth1网卡,连接内网
[root@haproxy ~]# vim /etc/NetworkManager/system-connections/eth1.nmconnection 
[connection]
id=eth1
type=ethernet
interface-name=eth1

[ipv4]
address1=192.168.0.107/24
method=manual

[root@haproxy ~]# nmcli connection reload     # 重新加载网络配置
[root@haproxy ~]# nmcli connection up eth0    # 重启网卡
[root@haproxy ~]# nmcli connection up eth1

# 关闭防火墙
[root@haproxy ~]# systemctl stop firewalld     # 暂时关闭防火墙
[root@haproxy ~]# systemctl mask firewalld     # 将防火墙写入null,永久关闭 
[root@haproxy ~]# setenforce 0     # 设置selinux为宽容模式

(3)server端

# server1
[root@server1 ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection 
[connection]
id=eth0
type=ethernet
interface-name=eth0

[ipv4]
address1=192.168.0.13/24,192.168.0.107
method=manual


# server2
[root@server2 ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection 
[connection]
id=eth0
type=ethernet
interface-name=eth0

[ipv4]
address1=192.168.0.23/24,192.168.0.107
method=manual

# 两台server主机重新加载网卡,并且关闭防火墙
[root@server ~]# nmcli connection reload     # 重新加载网络配置
[root@server ~]# nmcli connection up eth0    # 重启网卡

# 关闭防火墙
[root@server ~]# systemctl stop firewalld     # 暂时关闭防火墙
[root@server ~]# systemctl mask firewalld     # 将防火墙写入null,永久关闭 
[root@server ~]# setenforce 0     # 设置selinux为宽容模式
1.3server安装nginx服务
# server1
[root@server1 ~]# yum install nginx -y       # 安装nginx服务
[root@server1 ~]# systemctl enable --now nginx  # 立即启动nginx服务并设置开机启动
[root@server1 ~]# echo server1-192.168.0.13 > /usr/share/nginx/html/index.html            # 写入内容到nginx的发布目录的index.html便于测试

# server2
[root@server1 ~]# yum install nginx -y
[root@server1 ~]# systemctl enable --now nginx
[root@server1 ~]# echo server1-192.168.0.13 > /usr/share/nginx/html/index.html
1.4 haproxy的安装和服务配置
  • 安装haproxy服务

    [root@haproxy ~]# yum install haproxy -y   
    [root@haproxy ~]# rpm -qc haproxy    # 查找haproxy的配置文件
    /etc/haproxy/haproxy.cfg
    
  • haprox的基本配置

    [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg    # 编译haproxy的配置文件
    # 编译代理设定,设置前端(前后端分开编写)
    68 #---------------------------------------------------------------------
    69 # main frontend which proxys to the backends
    70 #---------------------------------------------------------------------
    71 
    72 frontend webcluster     -- 名字唯一         
    73     bind *:80           -- 监听所有端口
    74     mode http           -- 
    75     use_backend webcluster-host  -- 使用的后端
    76 
    77 backend webcluster-host   -- 定义后端
    78     balance roundrobin    -- 设置调度规则
    79     server web1 192.168.0.13:80    -- 后端服务器
    80     server web2 192.168.0.23:80
    
    
    [root@haproxy ~]# systemctl restart haproxy # 重启haproxy服务
    
    # 当重启服务出现问题时可通过查看其提示大的文件进行查看
    [root@haproxy ~]# haproxy -f /etc/haproxy/haproxy.cfg -c
    
  • 测试

    [root@client ~]# for i in {1..6};do curl 172.25.254.107;done
    server2-192.168.0.23
    server1-192.168.0.13
    server2-192.168.0.23
    server1-192.168.0.13
    server2-192.168.0.23
    server1-192.168.0.13
    
    # 关闭server1的nginx服务,查看数据的走向
    [root@server1 ~]# systemctl stop nginx
    [root@client ~]# for i in {1..6};do curl 172.25.254.107;done
    server2-192.168.0.23
    server2-192.168.0.23
    server2-192.168.0.23
    server2-192.168.0.23
    server2-192.168.0.23
    server2-192.168.0.23
    
    # 重启server1的nginx服务,数据走向恢复正常
    [root@client ~]# for i in {1..6};do curl 172.25.254.107;done
    server1-192.168.0.13
    server2-192.168.0.23
    server1-192.168.0.13
    server2-192.168.0.23
    server1-192.168.0.13
    server2-192.168.0.23
    
  • 优化基本配置

    [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg    # 编译haproxy的配置文件
    # 编译代理设定,设置前端(前后端合着编写)
     82 listen webcluster
     83     bind *:80
     84     mode http
     85     balance roundrobin
     86     server web1 192.168.0.13:80
     87     server web2 192.168.0.23:80
    [root@haproxy ~]# systemctl restart haproxy  # 重启haproxy服务
    
    # 测试,与前面效果一致
    [root@client ~]# for i in {1..6};do curl 172.25.254.107;done
    server1-192.168.0.13
    server2-192.168.0.23
    server1-192.168.0.13
    server2-192.168.0.23
    server1-192.168.0.13
    server2-192.168.0.23
    

二、 haproxy的基本配置信息

Haproxy的配置文件haproxy.cfg有两大部分组成,分别是

  • global:全局配置段
    • 进程及安全配置相关的参数
    • 性能调整相关参数
    • Debug参数
  • proxies:代理配置段
    • defaults:为frontend,backend,listen提供默认配置
    • frontend:前端,相当于nginx中的server {}
    • backend:后端,相当于nginx中的upstream{}
    • listen:同时拥有前端和后端配置,配置简单,生产推荐使用
2.1global配置(全局配置)
2.1.1 global配置参数说明
参数类型作用
chroot全 局锁定运行目录
deamon全 局以守护进程运行
user,group,uid,gid全局运行haproxy的用户身份
stats socket全 局套接字文件
nbproc N全 局开启的haproxy worker进程数,默认进程数是一个
nbthread 1(和nbproc 互斥)全 局指定每个haproxy进程开启的线程数,默认为每个进程一个 线程
cpu-map 1 0全 局绑定haproxy worker进程至指定CPU,将第1个work进程绑 定至0号CPU
cpu-map 2 1全局绑定haproxy worker进程至指定CPU,将第2个work进程绑定至1号CPU
maxconn N全局每个haproxy进程的最大并发连接数
maxsslconn N全局每个haproxy进程ssl最大连接数,用于haproxy配置了证书的场景下
maxConnrate N全局每个进程每秒创建的最大连接数量
spread-checks N全局后端server状态check随机提前或延迟百分比时间,建议2- 5(20%-50%)之间,默认值0
pidfile全 局指定pid文件路径
log 127.0.0.1 local2 info全局定义全局的syslog服务器;日志服务器需要开启UDP协议,最多可以定义两个
[root@haproxy ~]# cat /etc/haproxy/haproxy.cfg 
. . . . . .
 12 global
 26     log         127.0.0.1 local2     -- 以日志记录为2的形式记录在主机
 27 
 28     chroot      /var/lib/haproxy     -- 设定的haproxy的运行目录
 29     pidfile     /var/run/haproxy.pid -- haproxy的pid文件
 30     maxconn     4000                 -- 最大连接数
 31     user        haproxy              -- 运行用户
 32     group       haproxy              -- 运行组
 33     daemon                           -- 设置后台运行
 . . . . . .
2.1.2多进程和线程
  • 多进程
[root@haproxy ~]# pstree -p | grep haproxy        # 查看haproxy进程数量
           |-haproxy(1995)---haproxy(1997)---{haproxy}(1998)[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg    # 设置多进程
 38     # utilize system-wide crypto-policies
 39     ssl-default-bind-ciphers PROFILE=SYSTEM
 40     ssl-default-server-ciphers PROFILE=SYSTEM
 41  
 42     nbproc 2        -- 启用多进程
 43     cpu-map 1 0     -- 进程和cpu核心绑定防止cpu抖动宠儿减少系统资源消耗
 44     cpu-map 2 1     -- 2 表示第二个进程, 1 表示第二个核心

[root@haproxy ~]# systemctl restart haproxy    # 重新启动haproxy服务
[root@haproxy ~]# pstree -p | grep haproxy     # 再次查看进程
           |-haproxy(2121)-+-haproxy(2123)
           |               `-haproxy(2124)
  • 多线程
[root@haproxy ~]# pstree -p | grep haproxy    # 查看haproxy子进程id      
           |-haproxy(2121)-+-haproxy(2123)
           |               `-haproxy(2124)
 
[root@haproxy ~]# cat /proc/2123/status | grep -i thread   # 查看西安
Threads:	1
Speculation_Store_Bypass:	thread vulnerable

# 打开多线程
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
 45     nbthread 2     -- 设置为多线程

[root@haproxy ~]# systemctl restart haproxy        # 重启haproxy服务
[root@haproxy ~]# pstree -p | grep haproxy         # 查看进程号
           |-haproxy(2230)---haproxy(2232)---{haproxy}(2233)
[root@haproxy ~]# cat /proc/2232/status | grep -i thread  #查看线程数量
Threads:	2
Speculation_Store_Bypass:	thread vulnerable

注:以上多线程不可与多进程一同打开!!!

2.1.3 haproxy的状态界面
[root@haproxy ~]# vim //etc/haproxy/haproxy.cfg
111 #---------------------------------------------------------------------
112 # round robin balancing between the various backends
113 #---------------------------------------------------------------------
114 listen stats                      -- 监听stats状态
115     mode http                     -- 设置监听模块为http,处理http的流量
116     bind 0.0.0.0:8888             -- 指定 HAProxy 监听的地址和端口,可以通过访问 http://<your_server_ip>:8888 来查看 HAProxy 的统计信息。
117     stats enable                  -- 启用 HAProxy 的统计信息页面
118     log global                    -- 指定使用全局日志设置
119     stats uri /haproxy-status     -- 设置统计信息页面的 URI,通过访问 http://<your_server_ip>:8888/haproxy-status,你可以看到 HAProxy 的统计信息。
120     stats auth haha:haha            -- 设置了基本的 HTTP 认证,需要输入这对用户名和密码才能查看内容

[root@haproxy ~]# systemctl restart haproxy  # 重启haproxy服务
  • 测试

访问haproxy的状态界面,进入到登陆界面

在这里插入图片描述

进入到状态界面,页面显示 HAProxy 的运行状态,包括前端、后端、服务器的负载等信息。

在这里插入图片描述

2.1.4haproxy的日志分离
# 根据haproxy的配置文件中的日志格式,在日志配置编译文件中编写存放日志的格式及文件
[root@haproxy ~]# vim /etc/rsyslog.conf 

 28 # Provides UDP syslog reception
 29 # for parameters see http://www.rsyslog.com/doc/imudp.html
 30 module(load="imudp") # needs to be done just once           # 存放haproxy的日志文件需要开启UDP协议
 31 input(type="imudp" port="514") 
 
 66 local2.*                                     /var/log/haproxy.log
 
[root@haproxy ~]# touch /var/log/haproxy.log        # 创建存放haproxy的日志文件
[root@haproxy ~]# chmod 600 /var/log/haproxy.log    # 修改文件权限

[root@haproxy ~]# systemctl restart rsyslog
[root@haproxy ~]# systemctl restart haproxy

[root@haproxy ~]# /var/log/haproxy.log    # 查看日志
2.2 proxies配置
2.2.1 proxies参数说明
参数类型作用
defaults[ ]proxies默认配置项,针对以下的frontend、backend和listen生效,可以多个name也可以没有name
frontendproxies前端servername,类似于Nginx的一个虚拟主机 server和LVS服务集群。
backendproxies后端服务器组,等于nginx的upstream和LVS中的RS服务器
listenproxies将frontend和backend合并在一起配置,相对于frontend和backend配置更简洁,生产常用

注:name字段只能使用大小写字母,数字,‘-’(dash),‘_‘(underscore),’.’ (dot)和 ‘:’(colon),并且严格区分大小写

当不设定frontend、backend和listen段时,默认执行defaults的配置,当这三个优先配置了则先执行

2.2.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>%{X-Forwarded-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  # 对后端服务器检测的默认参数;每隔多长时间检测一次。
2.2.2.1Proxies配置-frontend
  • frontend配置参数:

    #格式:
    bind [<address>]:<port_range> [, ...] [param*]
    
    bind:指定HAProxy的监听地址,可以是IPV4或IPV6,可以同时监听多个IP或端口,可同时用于listen字段中
    
    # 注意:如果需要绑定在非本机的IP,需要开启内核参数:net.ipv4.ip_nonlocal_bind=1
    backlog <backlog> #针对所有server配置,当前端服务器的连接数达到上限后的后援队列长度,注意:不支持backend
    
  • frontend配置示例:

    [root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
    . . . 
    frontend lee-webserver-80
    bind 172.25.254.100:80
    mode http
    use_backend lee-webserver-80-RS #调用backend的名称
    . . .
    
2.2.2.2 Proxies配置-backend
  • 定义一组后端服务器,backend服务器将被frontend进行调用。

注意: backend 的名称必须唯一,并且必须在listen或frontend中事先定义才可以使用,否则服务无法启动

mode http|tcp  # 指定负载协议类型,和对应的frontend必须一致
               # http为七层协议,tcp为四层协议
option  # 配置选项
server  # 定义后端real server,必须指定IP和端口

注意:option后面加 httpchk,smtpchk,mysql-check,pgsql-check,ssl-hello-chk方法,可用于实现更多应用层检测功能。

  • server配置
#针对一个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机时提供服务,类似SorryServer
disabled 	# 将后端服务器标记为不可用状态,即维护状态,除了持久模式
			#将不再接受连接,状态为深黄色,优雅下线,不再接受新用户的请求
			
redirect prefix http://www.baidu.com/ 	# 将请求临时(302)重定向至其它URL,只适用于http模式
maxconn <maxconn> 	#	当前后端server的最大并发连接数

练习:

1.配置server的健康策略等

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
85 listen webcluster
 86     bind *:80
 87     mode http
 88     balance roundrobin
 
 91     server web1 192.168.0.13:80 check inter 2 fall 3 rise 5 weight 2  
 92     server web2 192.168.0.23:80 check inter 2 fall 3 rise 5 weight 1
# 配置一个名为 web num 的后端服务器,指定其 IP 和端口,并配置健康检查策略和负载均衡权重。

# 测试
[root@client ~]# for i in {1..6};do curl 172.25.254.107;done
server1-192.168.0.13
server2-192.168.0.23
server1-192.168.0.13
server2-192.168.0.23
server1-192.168.0.13
server2-192.168.0.23

1.server web1: 这是定义后端服务器的名称,可以使用这个名称在 HAProxy 配置中引用这个服务器。

2.192.168.0.13:80: 这是后端服务器的 IP 地址和端口号。HAProxy 将会把请求转发到这个 IP 地址的 80 端口(通常用于 HTTP 服务)。

3.check: 这表示 HAProxy 会对这个后端服务器执行健康检查。通过定期发送请求(通常是 HTTP 请求)来确定服务器是否正常运行。

4.inter 2: 这指定了健康检查的间隔时间(以秒为单位)。在这个配置中,HAProxy 每隔 2 秒对服务器进行一次健康检查。

5.fall 3: 这个参数指定了服务器被标记为“失效”(DOWN)的条件。在这个配置中,如果连续 3 次健康检查失败,HAProxy 会认为这个服务器已失效,并停止将流量转发到它。

6.rise 5: 这个参数指定了服务器从“失效”状态恢复到“健康”(UP)状态所需的成功健康检查次数。在这个配置中,如果连续 5 次健康检查成功,服务器将被标记为健康状态,HAProxy 会恢复将流量转发到它。

7.weight 2: 这个参数指定了服务器在负载均衡算法中的权重。权重越高,服务器将会接收到更多的请求。在这个配置中,web1 的权重为 2,这意味着它可能会接收到的流量比权重为 1 的服务器多一倍。

2.配置backup当真实服务器下线时,可以调用临时备份服务器,告知真实服务器已经下线

# 在以上基础创建新的server
# 同上1.2网络配置,配置好网络之后,下载http服务
[root@sorry-server ~]# yum install httpd -y    # 下载apache服务
[root@sorry-server ~]# systemctl enable --now httpd   # 启动httpd服务
[root@sorry-server ~]# echo Sorry 下班啦!!! > /var/www/html/index.html     # 写入访问内容到httd的发布目录,便于测试

# 在Client中进行访问测试
[root@client ~]# curl 192.168.0.33
Sorry 下班啦!!!

若想偷懒,也可以在haproxy主机中进行下载,但需要在http的配置文件修改端口,否则本身机子的80端口会响应,无法达到测试效果

[root@haproxy ~]# vim /etc/httpd/conf/httpd.conf  
 46 #Listen 12.34.56.78:80
 47 Listen 8080    -- 修改访问端口
[root@haproxy ~]# systemctl restart httpd    # 重启httpd服务

# 在测试时要跟上端口号
[root@client ~]# curl 192.168.0.33:8080
Sorry 下班啦!!!
# 在haproxy中对backend进行编译
[root@haproxy ~]# vim //etc/haproxy/haproxy.cfg
93     server web_sorry 192.168.0.33:80 backup
[root@haproxy ~]# systemctl restart haproxy    # 重启haproxy服务

# 测试 ,为了看到效果,将两台真实服务器down掉
[root@server1 ~]# systemctl stop nginx    # 关闭真实服务器的nginx服务
[root@server2 ~]# systemctl stop nginx

[root@client ~]# curl 172.25.254.107   # 访问haproxy进行测试
Sorry 下班啦!!!

3.对disabled服务器优雅下线的练习

# 不需要关闭真实服务器的nginx服务,在haproxy的主机进行配置
[root@haproxy ~]# vim //etc/haproxy/haproxy.cfg
91     server web1 192.168.0.13:80 check inter 2 fall 3 rise 5 weight 2 disabled
[root@haproxy ~]# systemctl restart haproxy    # 重启haproxy服务

# 测试
[root@client ~]# for i in {1..6}; do curl 172.25.254.107; done
server2-192.168.0.23
server2-192.168.0.23
server2-192.168.0.23
server2-192.168.0.23
server2-192.168.0.23
server2-192.168.0.23
# 测试结果看到,server已经下线,数据都打向server2

4.对访问进行临时重定向

[root@haproxy ~]# vim //etc/haproxy/haproxy.cfg
90     redirect prefix http://www.baidu.com/
[root@haproxy ~]# systemctl restart haproxy

在浏览器进行测试:

访问haproxy,自动定向到设定的网站

在这里插入图片描述

2.3socat工具

对haproxy的热更新处理

对服务器动态权重和其它状态可以利用 socat工具进行调整,Socat 是 Linux 下的一个多功能的网络工具,名字来由是Socket CAT,相当于netCAT的增强版.Socat 的主要特点就是在两个数据流之间建立双向通道,且支持众多协议和链接方式。如 IP、TCP、 UDP、IPv6、Socket文件等

利用工具socat 对服务器动态权重调整:

1.下载socat工具到haproxy主机中
[root@haproxy ~]# yum install socat -y   # 下载socat
[root@haproxy ~]# socat -h      # 安装好后可直接查看其工具的帮助
[root@haproxy ~]# echo "help" | socat stdio /var/lib/haproxy/stats # 使用 socat 工具通过指定的 socket 文件与 HAProxy 的统计接口通信,查看HAProxy 统计和管理相关的命令说明

enable server : enable a disabled server (use 'set server' instead) #启用服务器
# 以下为常用选项
. . . . . 
set maxconn server : change a server's maxconn setting
set server : change a server's state, weight or address #设置服务器
get weight : report a server's current weight #查看权重
set weight : change a server's weight (deprecated) #设置权重
. . . . . .

2.修改haproxy的配置文件,对sock文件进行赋权
[root@haproxy ~]# vim //etc/haproxy/haproxy.cfg
38     stats socket /var/lib/haproxy/stats mode 600 level admin
[root@haproxy ~]# systemctl restart haproxy

常用示例练习:

1.# 查看haproxy状态
[root@haproxy ~]#  echo "show info" | socat stdio /var/lib/haproxy/stats
Name: HAProxy
Version: 2.4.17-9f97155
Release_date: 2022/05/13
Nbthread: 2
Nbproc: 1
2.# 查看集群状态  
[root@haproxy ~]# echo "show servers state" | socat stdio /var/lib/haproxy/stats
1
# be_id be_name srv_id srv_name srv_addr srv_op_state srv_admin_state srv_uweight srv_iweight srv_time_since_last_change srv_check_status srv_check_result srv_check_health srv_check_state srv_agent_state bk_f_forced_id srv_f_forced_id srv_fqdn srv_port srvrecord srv_use_ssl srv_check_port srv_check_addr srv_agent_addr srv_agent_port
2 webcluster 1 web1 192.168.0.13 2 0 2 2 339 6 0 7 7 0 0 0 - 80 - 0 0 - - 0
2 webcluster 2 web2 192.168.0.23 2 0 1 1 339 6 0 7 7 0 0 0 - 80 - 0 0 - - 0
2 webcluster 3 web_sorry 192.168.0.33 2 0 1 1 628 1 0 2 0 0 0 0 - 80 - 0 0 - - 0
4 static 1 static 127.0.0.1 0 0 1 1 628 8 2 0 6 0 0 0 - 4331 - 0 0 - - 0
6 app 1 app1 127.0.0.1 0 0 1 1 627 8 2 0 6 0 0 0 - 5001 - 0 0 - - 0
6 app 2 app2 127.0.0.1 0 0 1 1 627 8 2 0 6 0 0 0 - 5002 - 0 0 - - 0
6 app 3 app3 127.0.0.1 0 0 1 1 627 8 2 0 6 0 0 0 - 5003 - 0 0 - - 0
6 app 4 app4 127.0.0.1 0 0 1 1 627 8 2 0 6 0 0 0 - 5004 - 0 0 - - 0

可查看到进群配置文件中所配置的主机名称等信息
3.# 查看集群权重
[root@haproxy ~]# echo get weight webcluster/web1 | socat stdio /var/lib/haproxy/stats
2 (initial 2)

[root@haproxy ~]# echo get weight webcluster/web2 | socat stdio /var/lib/haproxy/stats
1 (initial 1)

注意:此命令中的webcluster/web2为haproxy而配置文件中设定的backend名称/服务名称

4.# 设置权重
[root@haproxy ~]# echo "set weight webcluster/web1 1 " | socat stdio /var/lib/haproxy/stats

# 测试对比
# 未修改权重前 ,server1权重为2,轮询两次
[root@client ~]# for i in {1..10}; do curl 172.25.254.107; done
server1-192.168.0.13
server2-192.168.0.23
server1-192.168.0.13
server1-192.168.0.13
server2-192.168.0.23
server1-192.168.0.13
server1-192.168.0.13
server2-192.168.0.23
server1-192.168.0.13
server1-192.168.0.13
#修改权重后 , server1和server2权重都为1,轮询正常
[root@client ~]# for i in {1..10}; do curl 172.25.254.107; done
server2-192.168.0.23
server1-192.168.0.13
server2-192.168.0.23
server1-192.168.0.13
server2-192.168.0.23
server1-192.168.0.13
server2-192.168.0.23
server1-192.168.0.13
server2-192.168.0.23
server1-192.168.0.13

# 修改权重回来
[root@haproxy ~]# echo "set weight webcluster/web1 2 " | socat stdio /var/lib/haproxy/stats
5.# 下线后端服务器
[root@haproxy ~]# echo "disable server webcluster/web1 " | socat stdio /var/lib/haproxy/stats

# 测试
[root@client ~]# for i in {1..10}; do curl 172.25.254.107; done
server2-192.168.0.23
server2-192.168.0.23
server2-192.168.0.23
server2-192.168.0.23
server2-192.168.0.23
server2-192.168.0.23
6.# 上线后端服务器
[root@haproxy ~]# echo "enable server webcluster/web1 " | socat stdio /var/lib/haproxy/stats

# 测试
[root@client ~]# for i in {1..6}; do curl 172.25.254.107; done
server1-192.168.0.13
server1-192.168.0.13
server2-192.168.0.23
server1-192.168.0.13
server1-192.168.0.13
server2-192.168.0.23
2.3.1针对对进程处理方法

如果开启多进程那么我们在对进程的sock文件进行操作时其对进程的操作时随机的

如果需要指定操作进程那么需要用多sock文件方式来完成

# 进入配置文件,设置多个cocat文件
[root@haproxy ~]# vim //etc/haproxy/haproxy.cfg
stats socket /var/lib/haproxy/stats1 mode 600 level admin process 1  -- 指定stats1处理的进程为进程1
stats socket /var/lib/haproxy/stats2 mode 600 level admin process 2

nbproc 2          -- 开启多进程
cpu-map 1 0
cpu-map 2 1
[root@haproxy ~]# systemctl restart haproxy

这样每个进程就会有单独的sock文件来进行单独管理

[root@haproxy ~]# ll /var/lib/haproxy/
总用量 0
srw------- 1 root root 0 89 14:08 stats
srw------- 1 root root 0 89 14:31 stats1
srw------- 1 root root 0 89 14:31 stats2
  • 17
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值