linux系统------HAProxy 配置
目录
环境准备
配置haproxy
修改haproxy配置文件
配置nginx服务
web1
web2
启用haproxy服务
测试
访问192.168.158.128
此时访问192.168.158.128:1080
访问192.168.158.128:1080/stats
环境准备
关闭防火墙
systemctl stop firewalld
关闭安全上下文
setenforce 0
配置haproxy
192.168.158.128主机
下载haproxy
yum -y install haproxy
修改haproxy配置文件
vim /etc/haproxy/haproxy.cfg
[root@lvs1 ~]# cat /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# https://www.haproxy.org/download/1.8/doc/configuration.txt
#
#---------------------------------------------------------------------global #(全局配置)log 127.0.0.1 local2 # 定义全局的syslog服务器;最多可以定义两个chroot /var/lib/haproxy # 锁定运行目录pidfile /var/run/haproxy.pid # 指定pid文件路径user haproxy # 运行haproxy的用户身份group haproxy # 运行haproxy的用户身份daemon # 以守护进程运行maxconn 4000 # 每个haproxy进程的最大并发连接数defaults #(默认配置)会被所有frontend和backend继承mode http # 所处理的类别 (http是第7层的应用层) log global # 定义日志为global(全局)配置中的日志定义option httplog # 日志类别http日志格式option dontlognull # 不记录健康检查的日志信息 retries 3 # 后端服务器连接失败后的重试次数timeout http-request 5s # 等待客户端发送完整HTTP请求的最大时间timeout queue 1m # 请求在队列中等待的最长时间timeout connect 5s # 连接后端服务器的超时时间timeout client 1m # 客户端不活动时的超时时间timeout server 1m # 与后端服务器通信的超时时间timeout http-keep-alive 5s # 保持HTTP连接的持续时间timeout check 5s # 健康检查操作的超时时间maxconn 3000 # 每个前端服务的最大并发连接数frontend main #(前端虚拟server)处理客户端请求bind *:80 # 监听所有IP地址的80端口default_backend http_back # 将所有流量转发到名为http_back的后端组backend http_back #(后端真实server)后端服务器组配置balance roundrobin # 负载均衡算法:轮询(还有leastconn, source等)server node1 192.168.158.130:80 check # 改成 web1的ip和端口号server node2 192.168.158.131:80 check # 改成 web2的ip和端口号
listen stats #定义监控页面 (内置监控统计页面配置)mode http # 使用HTTP模式option httplog # 启用HTTP日志记录bind *:1080 #绑定端口1080 (监听所有IP地址的1080端口) stats refresh 30s #每30秒更新监控数据 stats uri /stats #访问监控页面的uri(监控页面的访问路径) stats realm HAProxy\ Stats #监控页面的认证提示 stats auth admin:admin #监控页面的用户名和密码(身份验证凭据)
配置nginx服务
web1
192.168.158.130主机(web1)
下载nginx并启用nginx服务
yum -y install nginx
systemctl start nginxecho web1 > /usr/share/nginx/html/index.html
web2
192.168.158.131主机 (web2)
下载nginx并启用nginx服务
yum -y install nginx
systemctl start nginxecho web1 > /usr/share/nginx/html/index.html
启用haproxy服务
回到192.168.158.128主机
systemctl start haproxy.service
测试
访问192.168.158.128
此时访问192.168.158.128:1080
503 服务不可用
没有服务器可用于处理此请求。