haproxy 负载均衡 http
环境说明
服务 | IP |
---|---|
haproxy服务器(DR)(CA) | 192.168.47.115 |
httpd服务器(RS1) | 192.168.47.134 |
httpd服务器(RS2) | 192.168.47.135 |
关闭防火墙
所有主机
[root@DR ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@DR ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@DR ~]# setenforce 0
[root@RS1 ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS1 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@RS1 ~]# setenforce 0
[root@RS2 ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS2 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@RS2 ~]# setenforce 0
在RS上安装httpd服务
//rs1
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# echo "RS1" > /var/www/html/index.html
[root@RS1 ~]# systemctl enable --now httpd
[root@RS1 ~]# curl 192.168.47.120
RS1
//rs2
[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# echo "RS2" > /var/www/html/index.html
[root@RS2 ~]# systemctl enable --now httpd
[root@RS2 ~]# curl 192.168.47.121
RS2
证书生成
//安装mod_ssl模块
[root@RS1 ~]# yum -y install mod_ssl
[root@RS2 ~]# yum -y install mod_ssl
[root@RS1 ~]# mkdir ~/keys
[root@RS1 ~]# cd keys/
[root@RS1 keys]# openssl genrsa -out pass.key 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
...........................+++++
....................................................................+++++
e is 65537 (0x010001)
[root@RS1 keys]# openssl req -new -key pass.key -out pass.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:hubei
Locality Name (eg, city) [Default City]:wuhan
Organization Name (eg, company) [Default Company Ltd]:test
Organizational Unit Name (eg, section) []:123
Common Name (eg, your name or your server's hostname) []:123.com
Email Address []:123@qq.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:12345678
An optional company name []:
//生成证书crt
[root@RS1 keys]# openssl x509 -req -days 365 -in pass.csr -signkey pass.key -out pass.crt
Signature ok
subject=C = cn, ST = hubei, L = wuhan, O = test, OU = 123, CN = 123.com, emailAddress = 123@qq.com
Getting Private key
[root@RS1 keys]# ls
pass.crt pass.csr pass.key
//复制证书到指定位置
[root@RS1 ~]# cp -r keys/ /etc/httpd/
//修改配置文件
[root@RS1 ~]# vim /etc/httpd/conf.d/ssl.conf
#取消注释
DocumentRoot "/var/www/html"
ServerName www.example.com:443
#修改路径
SSLCertificateFile /etc/httpd/keys/pass.crt
SSLCertificateKeyFile /etc/httpd/keys/pass.key
//重启httpd
[root@RS1 ~]# systemctl restart httpd
//RS2上操作一样
[root@RS2 ~]# mkdir ~/keys
[root@RS2 ~]# cd keys/
[root@RS2 keys]# openssl genrsa -out pass.key 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
......+++++
...+++++
e is 65537 (0x010001)
[root@RS2 keys]# openssl req -new -key pass.key -out pass.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:hubei
Locality Name (eg, city) [Default City]:wuhan
Organization Name (eg, company) [Default Company Ltd]:test
Organizational Unit Name (eg, section) []:123
Common Name (eg, your name or your server's hostname) []:123.com
Email Address []:123@qq.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:12345678//复制证书到指定位置
An optional company name []:
[root@RS2 keys]# openssl x509 -req -days 365 -in pass.csr -signkey pass.key -out pass.crt
Signature ok
subject=C = cn, ST = hubei, L = wuhan, O = test, OU = 123, CN = 123.com, emailAddress = 123@qq.com
Getting Private key
[root@RS2 keys]# ls
pass.crt pass.csr pass.key
//复制证书到指定位置
[root@RS2 ~]# cp -r keys/ /etc/httpd/
//修改配置文件
[root@RS2 ~]# vim /etc/httpd/conf.d/ssl.conf
#取消注释
DocumentRoot "/var/www/html"
ServerName www.example.com:443
#修改路径
SSLCertificateFile /etc/httpd/keys/pass.crt
SSLCertificateKeyFile /etc/httpd/keys/pass.key
//重启httpd
[root@RS2 ~]# systemctl restart httpd
haproxy服务部署
//安装依赖包
[root@DR ~]# yum -y install openssl make gcc pcre-devel bzip2-devel openssl-devel systemd-devel wget vim
//创建用户
[root@DR ~]# useradd -r -M -s /sbin/nologin haproxy
//下载haproxy包
[root@DR ~]# cd /usr/src/
[root@DR src]# wget https://github.com/haproxy/haproxy/archive/refs/tags/v2.4.0.tar.gz
[root@DR src]# tar xf v2.4.0.tar.gz
[root@DR src]# ls
debug haproxy-2.4.0 kernels v2.4.0.tar.gz
[root@DR src]# cd haproxy-2.4.0/
[root@DR haproxy-2.4.0]# make clean
[root@DR haproxy-2.4.0]# make -j $(grep 'processor' /proc/cpuinfo |wc -l) \
TARGET=linux-glibc \
USE_OPENSSL=1 \
USE_ZLIB=1 \
USE_PCRE=1 \
USE_SYSTEMD=1
[root@DR haproxy-2.4.0]# make install PREFIX=/usr/local/haproxy
//设置环境变量
[root@DR haproxy-2.4.0]# echo "export PATH=/usr/local/haproxy/sbin:$PATH">/etc/profile.d/haproxy.sh
[root@DR haproxy-2.4.0]# . /etc/profile.d/haproxy.sh
[root@DR haproxy-2.4.0]# which haproxy
/usr/local/haproxy/sbin/haproxy
//配置内核参数
[root@DR ~]# echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
[root@DR ~]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[root@DR ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
//提供配置文件
[root@DR ~]# mkdir /etc/haproxy
[root@DR ~]# vim /etc/haproxy/haproxy.cfg
[root@DR ~]# cat /etc/haproxy/haproxy.cfg
#--------------全局配置----------------
global
log 127.0.0.1 local0 info
#log loghost local0 info
maxconn 20480
#chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid
#maxconn 4000
user haproxy
group haproxy
daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option dontlognull
option httpclose
option httplog
#option forwardfor
option redispatch
balance roundrobin
timeout connect 10s
timeout client 10s
timeout server 10s
timeout check 10s
maxconn 60000
retries 3
#--------------统计页面配置------------------
listen admin_stats
bind 0.0.0.0:8189
stats enable
mode http
log global
stats uri /haproxy_stats
stats realm Haproxy\ Statistics
stats auth admin:admin
#stats hide-version
stats admin if TRUE
stats refresh 30s
#---------------web设置-----------------------
listen webcluster
bind 0.0.0.0:80
mode http
#option httpchk GET /index.html
log global
maxconn 3000
balance roundrobin
cookie SESSION_COOKIE insert indirect nocache
server web01 192.168.47.120:80 check inter 2000 fall 5
server web02 192.168.47.121:80 check inter 2000 fall 5
//haproxy.service文件编写
[root@DR ~]# cat > /usr/lib/systemd/system/haproxy.service <<EOF
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
EOF
[root@DR ~]# systemctl daemon-reload
//启动日志
[root@DR ~]# vim /etc/rsyslog.conf
# Save boot messages also to boot.log
local7.* /var/log/boot.log
local0.* /var/log/haproxy.log //加入这行
[root@DR ~]# systemctl restart rsyslog
[root@DR ~]# systemctl enable --now haproxy
[root@DR ~]# systemctl status haproxy.service
● haproxy.service - HAProxy Load Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2021-10-18 14:25:32 CST; 4min 54s ago
Main PID: 68657 (haproxy)
Tasks: 3 (limit: 11300)
Memory: 7.7M
CGroup: /system.slice/haproxy.service
├─68657 /usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
└─68659 /usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid
10月 18 14:25:32 DR systemd[1]: Starting HAProxy Load Balancer...
10月 18 14:25:32 DR systemd[1]: Started HAProxy Load Balancer.
10月 18 14:25:32 DR haproxy[68657]: [NOTICE] (68657) : New worker #1 (68659) forked
[root@DR ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8189 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
测试负载均衡
此时web访问调度器IP就可以调度到RS1和RS2
访问web界面
用户密码都是:admin
配置https负载均衡
root@DR ~]# cat /etc/haproxy/haproxy.cfg
#--------------全局配置----------------
global
log 127.0.0.1 local0 info
#log loghost local0 info
maxconn 20480
#chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid
#maxconn 4000
user haproxy
group haproxy
daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
mode tcp //将此处改成tcp协议
log global
option dontlognull
option httpclose
option httplog
#option forwardfor
option redispatch
balance roundrobin
timeout connect 10s
timeout client 10s
timeout server 10s
timeout check 10s
maxconn 60000
retries 3
#--------------统计页面配置------------------
listen admin_stats
bind 0.0.0.0:8189
stats enable
mode http
log global
stats uri /haproxy_stats
stats realm Haproxy\ Statistics
stats auth admin:admin
#stats hide-version
stats admin if TRUE
stats refresh 10s
#---------------web设置-----------------------
listen webcluster
bind 0.0.0.0:443 //将端口改成443
mode tcp //使用tcp协议
#option httpchk GET /index.html
log global
maxconn 3000
balance roundrobin
server web01 192.168.47.120:443 check inter 2000 fall 5 //将端口改成443
server web02 192.168.47.121:443 check inter 2000 fall 5 //将端口改成443
[root@DR ~]# systemctl restart haproxy.service