haproxy实验环境的部署
在rhel9或者openEuler环境下搭建
haproxy配置:
root@localhost ~]#systemctl stop firewalld.service
[root@localhost ~]#systemctl mask firewalld.service
[root@localhost ~]# vmset.sh eth0 172.25.254.100 haproxy.huihui.org
web1配置:
[root@localhost ~]#vmset.sh eth0 172.25.254.10 web.hui.org
[root@localhost ~]#dnf install nginx -y (yum install httpd -y)
[root@localhost ~]#echo web1 - 172.25.254.10 > /usr/share/nginx/html/index.html
[root@localhost ~]#[root@localhost ~]#systemctl enable --now httpd
web2配置:
[root@localhost ~]#vmset.sh eth0 172.25.254.20 web.hui.org
[root@localhost ~]#dnf install nginx -y (yum install httpd -y)
[root@localhost ~]#echo web1 - 172.25.254.20 > /usr/share/nginx/html/index.html
[root@localhost ~]#[root@localhost ~]#systemctl enable --now httpd
检测:
haproxy:
haproxy 的基本部署方法及负载均衡
hap:
[root@localhost ~]# dnf install haproxy -y
查看配置文件
[root@localhost ~]#rpm -qc haproxy
/etc/haproxy/haproxy.cfg(工作守则)
/etc/logrotate.d/haproxy(日志)
/etc/sysconfig/haproxy(软件属性)
[root@localhost ~]#vim ~/.vimrc
set ts=4 ai sw=4
[root@localhost ~]#vim /etc/haproxy/haproxy.cfg
frontend webcluster
bind *:80 (端口)
mode http
use_backend webcluster-host (后端)
backend webcluster-host
balance roundrobin (调度规则)
server web1 172.25.254.10:80
server web2 172.25.254.20:80
启动服务:
[root@localhost ~]#systemctl restart haproxy
测试:
web2 停止nginx服务/http服务只能访问web1
[root@localhost ~]#systemctl stop nginx.service
启动:
[root@localhost ~]#systemctl start nginx.service
另外一种写法:
listen webcluster
bind *:80
mode http
use_backend roundrobin
server web1 172.25.254.10:80
server web2 172.25.254.20:80
haproxy的全局配置参数分离及日志分离
参数:
log 127.0.0.1 local2 (日志级别是2)
chroot /var/lib/haproxy (运行目录)
pidfile /var/run/haproxy.pid(pid文件)
maxconn 4000(最大连接数)
user haproxy
group haproxy
daemon(后台运行)
stats socket 套接字文件
nbproc N 全局开启的haproxy worker 进程数,默认进程数是一个
查看进程
[root@localhost ~]#pstree -p | grep haproxy
|-haproxy(3224)---haproxy(3226)---{haproxy}(3227)
[root@localhost ~]#vim /etc/haproxy/haproxy.cfg
nbproc 2
各自绑定:多线程nbproc
nbproc 2
cpu-map 1 0(第一的线程1用的是第一个核心0)
cpu-map 2 1(第二个线程2用的是第二个核心1)
:wq
[root@localhost ~]# systemctl restart haproxy.service
[root@localhost ~]#pstree -p | grep haproxy
多线程和多进程是互斥的,不能既要又要;
[root@localhost ~]#cat /proc/3353/status | grep -i thread
多进程:nbthread
hap自定义日志:
[root@localhost ~]#vim /etc/rsyslog.conf
local2.* /var/log/haproxy.log
[root@localhost ~]#systemctl restart rsyslog.service
[root@localhost ~]#systemctl restart haproxy.service
[root@localhost ~]#ll /var/log/haproxy.log
proxies中的常用配置参数
sorry server:
hap
[root@localhost ~]#dnf install httpd(先不要开启服务)
[root@localhost ~]#vim /etc/httpd/conf/httpd.conf
Listen 8080
[root@localhost ~]#systemctl enable --now httpd
[root@localhost ~]#echo sorry 你今天开心了吗? > /var/www/html/index.html
[root@localhost ~]#vim /etc/haproxy/haproxy.cfg
root@localhost ~]#systemctl restart haproxy.service
web1、web2关闭http、nginx服务
[root@localhost ~]#systemctl stop nginx
[root@localhost ~]#systemctl stop http
[root@localhost ~]#systemctl status nginx查看服务是否dead
[root@localhost ~]#systemctl status http
hap测试一下:
disabled下线维护(不可用状态)
[root@localhost ~]#vim /etc/haproxy/haproxy.cfg
# 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
# server web_sorry 172.25.254.100:8080 backup
[root@localhost ~]#systemctl restart haproxy.service
维护完删除disabled就可以,然后重启;
访问重定向:
[root@localhost ~]#vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode http
use_backend roundrobin
redirect prefix http://www.baidu,com/
haproxy热更新方法
hap
[root@localhost ~]#vim /etc/haproxy/haproxy.cfg
只能查看不能做更改
stats socket /var/lib/haproxy/stats (加权)mode 600 level admin(不加mode开始的参数相当于普通用户)
[root@localhost ~]#systemctl restart haproxy.service
[root@localhost ~]#ll /var/lib/haproxy/stats
srw------- 1 root root 0 8月 12 00:10 /var/lib/haproxy/stats
怎么使用套接字
hap
[root@localhost ~]#dnf install socat -y
[root@localhost ~]#socat -h查看帮助(支持tcp/udp等协议)
[root@localhost ~]#echo "help" | socat stdio /var/lib/haproxy/stats(帮助)
实例:
查看haproxy状态
[root@localhost ~]#echo "show info" | socat stdio /var/lib/haproxy/stats
查看集群状态
[root@localhost ~]#echo "show servers state" | socat stdio /var/lib/haproxy/stats
查看权重
[root@localhost ~]#echo get weight webcluster/web1 | socat stdio /var/lib/haproxy/stats
设置权重
[root@localhost ~]#echo "set weight webcluster/web1 1 " | socat stdio /var/lib/haproxy/stats
[root@localhost ~]#echo "set weight webcluster/web1 2 " | socat stdio /var/lib/haproxy/stats
下线后端服务器
[root@localhost ~]#echo "disable server webcluster/web1 " | socat stdio /var/lib/haproxy/stats
上线后端服务器
[root@localhost ~]# echo "enable server webcluster/web1 " | socat stdio /var/lib/haproxy/stats
多进程的处理方法:
[root@localhost ~]#ll /var/lib/haproxy/
[root@localhost ~]#vim /etc/haproxy/haproxy.cfg
打开
stats socket /var/lib/haproxy/stats1 mode 600 level admin process 1
stats socket /var/lib/haproxy/stats2 mode 600 level admin process 2
nbproc 2
cpu-map 1 0
cpu-map 2 1
:wq
[root@localhost ~]#systemctl restart haproxy.service
[root@localhost ~]#ll /var/lib/haproxy/