案例环境
主机 | IP地址 | 系统 |
---|---|---|
haproxy | 192.168.116.134/24 | CentOS7.7 |
web01 | 192.168.116.135/24 | CentOS7.7 |
web02 | 192.168.116.136/24 | CentOS7.7 |
1、基本环境配置
IP地址配置
主机名设置
关闭selinux
2、配置web网站
web1和web2安装nginx,并设置不同的首页内容(web02同样配置)
# 设置nginx yum源
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
#开启nginx
[root@wen01 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@wen01 ~]# systemctl start nginx
[root@wen01 ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since 二 2023-03-28 15:22:02 CST; 5s ago
Docs: http://nginx.org/en/docs/
Process: 82087 ExecStop=/bin/sh -c /bin/kill -s TERM $(/bin/cat /var/run/nginx.pid) (code=exited, status=0/SUCCESS)
Process: 82091 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 82092 (nginx)
CGroup: /system.slice/nginx.service
├─82092 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─82093 nginx: worker process
3月 28 15:22:02 wen01 systemd[1]: Stopped nginx - high performance web server.
3月 28 15:22:02 wen01 systemd[1]: Starting nginx - high performance web server...
3月 28 15:22:02 wen01 systemd[1]: Can't open PID file /var/run/nginx.pid (yet?) after start: No such file ...ectory
3月 28 15:22:02 wen01 systemd[1]: Started nginx - high performance web server.
Hint: Some lines were ellipsized, use -l to show in full.
#修改nginx首页内容并测试
[root@wen01 ~]# mv /sur/share/nginx/html/index.html /usr/share/nginx/html/index.html.bak
[root@wen01 ~]# vim /sur/share/nginx/html/index.html
<h1> web01 test </h1>
[root@wen01 html]# curl http://192.168.116.135
<h1> web01 test </h1>
[root@web02 html]# curl http://192.168.116.136
<h1> web02 test </h1>
3、安装haproxy
[root@haproxy ~]# yum install haproxy -y
[root@haproxy ~]# haproxy -version
HA-Proxy version 1.5.18 2016/05/10
Copyright 2000-2016 Willy Tarreau <willy@haproxy.org>
4、配置rsyslog服务来接收haproxy的日志
修改rsyslog配置文件
[root@haproxy ~]# vim /etc/rsyslog.conf
# Provides UDP syslog reception #开启UDP日志接收,使用514端口
$ModLoad imudp
$UDPServerRun 514
#添加日志规则
local2.* /var/log/haproxy.log
重启rsyslog服务
[root@haproxy ~]# systemctl restart rsyslog.service
5、配置haproxy
global
log 127.0.0.1:514 local2 info
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000 #最大连接数,根据应用实际情况进行调整,推荐使用10240
user haproxy
group haproxy
daemon #以后台形式运行haproxy
defaults
mode http #工作模式,所处理的类别,默认采用http模式,可配置成tcp作4层消息转发
log global
option httplog
option dontlognull
timeout connect 10s
timeout client 1m
timeout server 1m
frontend http_front
bind 192.168.116.134:80
stats uri /haproxy?stats
default_backend http_back
backend http_back
balance roundrobin
server web01 192.168.116.135:80 check
server web02 192.168.116.136:80 check
启动haproxy服务
[root@haproxy ~]# systemctl enable haproxy.service
[root@haproxy ~]# systemctl start haproxy.service
#放行80端口在web01、web02上面也需要配置,或者是关闭防火墙
[root@haproxy ~]# firewall-cmd --add-port=80/tcp --permanent
success
[root@haproxy ~]# firewall-cmd --reload
success
[root@haproxy ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports: 80/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
6、访问测试
[root@haproxy ~]# curl http://192.168.116.134
<h1> web01 test </h1>
[root@haproxy ~]# curl http://192.168.116.134
<h1> web02 test </h1>
[root@haproxy ~]# curl http://192.168.116.134
<h1> web01 test </h1>
[root@haproxy ~]# curl http://192.168.116.134
<h1> web02 test </h1>
访问haproxy页面