环境说明:
所需主机 | IP |
---|---|
服务器 haproxy | 192.168.152.132 |
服务器 RS1 | 192.168.152.134 |
服务器 RS2 | 192.168.152.135 |
配置haproxy负载均衡http和httpds
- RS上配置http
//RS1下载httpd,设置开机自启,写个网站
[root@RS1 ~]# yum -y install wget httpd
[root@RS1 ~]# cd /var/www/html/
[root@RS1 html]# echo 'RS1' > index.html
[root@RS1 html]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
//RS2下载httpd,设置开机自启,写个网站
[root@RS2 ~]# yum -y install wget httpd
[root@RS2 ~]# cd /var/www/html/
[root@RS2 html]# echo 'RS2' >index.html
[root@RS2 html]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
- RS上配置https,生成证书
[root@RS2 ~]# ls
anaconda-ks.cfg certificate.sh
[root@RS2 ~]# yum -y install expect //下载工具包
[root@RS2 ~]# bash certificate.sh //执行脚本生成证书
证书生成成功...
证书存放在/root/目录下
anaconda-ks.cfg www.example.com.crt www.example.com.key
certificate.sh www.example.com.csr
//把crt和key传给RS1
[root@RS2 ~]# scp www.example.com.crt www.example.com.key 192.168.152.134:/root/
The authenticity of host '192.168.152.134 (192.168.152.134)' can't be established.
ECDSA key fingerprint is SHA256:ixzGjAkndT/VFCzEmxX9N4cQn6s4W7UhEUHNj6USkc4.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.152.134' (ECDSA) to the list of known hosts.
root@192.168.152.134's password:
www.example.com.crt 100% 4559 1.7MB/s 00:00
www.example.com.key 100% 1679 775.8KB/s 00:00
- RS上配置https
[root@RS1 ~]# yum -y install mod_ssl //安装模块
[root@RS1 ~]# mkdir /etc/httpd/ssl
[root@RS1 ~]# mv www* /etc/httpd/ssl/
[root@RS1 ~]# cd /etc/httpd/ssl/
[root@RS1 ssl]# ls
www.example.com.crt www.example.com.key
[root@RS1 ssl]# cd ..
[root@RS1 httpd]# ls
conf conf.d conf.modules.d logs modules run ssl state
[root@RS1 httpd]# cd conf.d/
[root@RS1 conf.d]# ls
autoindex.conf README ssl.conf userdir.conf welcome.conf
[root@RS1 conf.d]# vim ssl.conf //修改配置文件
//找到此两行,取消注释
DocumentRoot "/var/www/html"
ServerName www.example.com:443
//修改此两行路径
SSLCertificateFile /etc/httpd/ssl/www.example.com.crt
SSLCertificateKeyFile /etc/httpd/ssl/www.example.com.key
[root@RS1 conf.d]# systemctl restart httpd
[root@RS1 conf.d]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 *:443 *:*
//RS2配置同上
- 下载haproxy,编译安装
//haproxy安装
[root@haproxy ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
//创建用户
[root@haproxy ~]# useradd -r -M -s /sbin/nologin haproxy
//下载haproxy安装包
[root@haproxy ~]# ls
anaconda-ks.cfg haproxy-2.3.0.tar.gz
[root@haproxy ~]# du -sh *
4.0K anaconda-ks.cfg
2.8M haproxy-2.3.0.tar.gz
//解压安装包
[root@haproxy ~]# tar xf haproxy-2.3.0.tar.gz
[root@haproxy ~]# ls
anaconda-ks.cfg haproxy-2.3.0 haproxy-2.3.0.tar.gz
[root@haproxy ~]# cd haproxy-2.3.0/
[root@haproxy haproxy-2.3.0]# ls
BRANCHES CONTRIBUTING include MAINTAINERS reg-tests src VERDATE
CHANGELOG doc INSTALL Makefile ROADMAP SUBVERS VERSION
contrib examples LICENSE README scripts tests
//编译安装
[root@haproxy haproxy-2.3.0]# make clean //清除编译缓存
[root@haproxy haproxy-2.3.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@haproxy haproxy-2.3.0]# make install PREFIX=/usr/local/haproxy
略...
[root@haproxy haproxy-2.3.0]# ls
BRANCHES CONTRIBUTING haproxy LICENSE README scripts tests
CHANGELOG doc include MAINTAINERS reg-tests src VERDATE
contrib examples INSTALL Makefile ROADMAP SUBVERS VERSION
[root@haproxy haproxy-2.3.0]# cp haproxy /usr/sbin/
- 配置各个负载的内核参数
[root@haproxy ~]# echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
[root@haproxy ~]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[root@haproxy ~]# sysctl -p //重新读取文件生效
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
- 提供配置文件
[root@haproxy ~]# mkdir /etc/haproxy
[root@haproxy ~]# cat > /etc/haproxy/haproxy.cfg <<EOF
> #--------------全局配置----------------
> global
> log 127.0.0.1 local0 info //记录本机日志到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 //后端RS
> mode http //负载
> log global
> option dontlognull //不记录空日志
> option httpclose
> option httplog
> #option forwardfor
> option redispatch
> balance roundrobin //负载算法rr
> timeout connect 10s //超时时间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 172.16.103.130:80 check inter 2000 fall 5
> #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
> EOF
- 修改配置文件
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
#---------------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.152.134:80 check inter 2000 fall 5 //修改为RS1的IP
server web02 192.168.152.135:80 check inter 2000 fall 5 //修改为RS2的IP
#server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
- haproxy.service文件编写
[root@haproxy ~]# cat > /usr/lib/systemd/system/haproxy.service <<EOF
> [Unit]
> Description=HAProxy Load Balancer //服务说明
> After=syslog.target network.target //在syslog后面启动
>
> [Service]
>ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
> ExecStart=/usr/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@haproxy ~]# systemctl daemon-reload //重新加载文件
- 启用日志
[root@haproxy ~]# vim /etc/rsyslog.conf
# Save boot messages also to boot.log
local0.* /var/log/haproxy.log //添加此行
local7.* /var/log/boot.log
[root@haproxy ~]# systemctl restart rsyslog //重启服务
- 启动haproxy服务
[root@haproxy ~]# systemctl start haproxy
[root@haproxy ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8189 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
- 配置haproxy负载均衡https
//配改配置
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
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
#---------------web设置-----------------------
listen webcluster
bind 0.0.0.0:443 //端口改为443
mode tcp //模式改为tcp
#option httpchk GET /index.html
log global
maxconn 3000
balance roundrobin
cookie SESSION_COOKIE insert indirect nocache
server web01 192.168.152.134:443 check inter 2000 fall 5 //端口改为443
server web02 192.168.152.135:443 check inter 2000 fall 5 //端口改为443
#server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
[root@haproxy ~]# systemctl start haproxy //重启服务
[root@haproxy ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:443 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8189 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
- 访问验证
- haproxy网络管理页面