集群-haproxy+apache

Haproxy配置文件

由于CentOS7之前版本自带的lua版本比较低并不符合HAProxy要求的lua最低版本(5.3)的要求,因此需要编译安装较新版本的lua环境,然后才能编译安装HAProxy

  • HAProxy 的配置文件haproxy.cfg由两大部分组成,分别是global和proxies部分

global(全局配置)

  • 配置进程及安全配置相关的参数
  • 性能调整相关参数
  • Debug参数
## global
maxconn 100000             #每个haproxy进程的最大并发连接数
chroot /apps/haproxy       #把haproxy锁定一个工作目录
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin   #指定socket文件路径及文件的权限
user haproxy
group haproxy
#uid 99
#gid 99                   
daemon                     #以守护进程方式启动
nbproc 4                   #haproxy工作进程数量;与CPU核心数量相对应
cpu-map 1 0                #第一个工作进程绑定在第0核CPU上
cpu-map 2 1                #第二个工作进程绑定在第1核CPU上
cpu-map 3 2
cpu-map 4 3
maxconn                   #每个haproxy进程的最大并发连接数
maxsslconn                #每个haproxy进程ssl最大连接数,用于haproxy配置了证书的场景下
maxconnrate               #每个进程每秒创建的最大连接数量
pidfile /var/lib/haproxy/haproxy.pid
spread-checks 3           #后端server状态检查,随机提前或延迟百分比时间,建议2-5(20%-50%)之间;设置当前检测时间为3s,则会提前3s的20%-50%,或者延后3s的20%-50%;把同时探测的压力分散开
log 127.0.0.1 local3 info  #生成的日志发送给本机的syslog服务器;最多可以定义两个;定义的是local3设备,所以需要在syslog服务器的配置文件中进行定义;vim /etc/rsyslog.conf;添加local3.*/var/log/haproxy.log,记录local3设备的所有级别的日志到指定文件中;并且需要开启$ModLoad imtcp、$InputTCPServerRun 514这两行,使用TCP协议进行日志转发;systemctl restart rsyslog

proxies(代理配置)

  • default []默认配置
  • frontend 前端配置
  • backend 后端配置
  • listen 将frontend和backend合并在一起配置
  • name字段只能使用"-","_","."和":",并且严格区分大小写
##default
option redispatch     #当server Id(real server)对应的服务器挂掉后,强制定向到其他健康的服务器  
option abortonclose   #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接;一般长时间处理的服务器(连接数据库等),需要关闭该选项
option http-keep-alive  #开启与客户端的会话保持,在不超时的情况下,再次访问就不需要TCP三次握手建立连接
option forwardfor     #透传客户端真实IP至后端web服务器
mode http             #haproxy默认工作类型,基于应用层;如果后端服务器有自己的应用层协议,可单独在后端代理上定义协议类型;工作中一般为TCP
timeout connect 120s  #客户端请求到后端server的最长连接等待时间(haproxy与后端服务器TCP三次握手之前)
timeout server 600s   #客户端请求到后端服务端的超时超时时长(TCP之后);如果在600s后端服务器还未给haproxy返回,则会报502(后端服务器超时)
timeout client 600s   #与客户端的最长非活动时间;600s内客户端没有向haproxy请求资源,则haproxy将会主动切断与客户端的连接
timeout http-keep-alive 120s    #session 会话保持超时时间,范围内会转发到相同的后端服务器
timeout check   5s    #对后端服务器的检测超时时间
##frontend
frontend yewu-service-80                     #前端的名字一般为:业务-服务-端口号   
bind 192.168.38.37:80,192.168.38.37:81   #指定haproxy监听的IP及端口,可以是多个IP;也可以写成,IP:80-89,或者是sock文件
mode tcp                                 #指定负载均衡协议类型
use_backend yewu-service-80-nodes        #调用后端服务器组的名称
##backend
server     #定义后端real server
check      #对指定real server进行健康状态检查,默认不开启
addr IP    #可指定的健康状态监测IP
port num   #指定的健康状态监测端口;一般先对后端重要的服务的端口进行检查,如果重要服务的端口存在,则进行后端服务器的负载均衡;一般反向代理的IP和端口号与重要服务的IP不在同一个网段;如nginx与php,先检查后端nginx服务器的php的9000端口是否存在,存在才反向代理后端的nginx服务器
inter num   #健康状态检查间隔时间,默认2000ms
fall num  #后端服务器失效检查次数,默认为3,失败3次,从后端服务器组里面踢出去;1次失败有可能是网络问题,所以设置为检测失败3次;检测的时候, 用户的请求也会往服务器进行转发,所以失败检测次数不能太多;被踢出去后,用户的请求将不会往该服务器转发
rise num  #后端服务器从下线恢复检查次数,默认为2;后端服务器宕机,恢复后,检查成功2次后,重新添加到haproxy反向代理的服务器组里面
weight    #默认为1,最大值为256,0表示不参与负载均衡
backup    #将后端服务器标记为备份状态;当所有服务器都宕机时,backup服务器才会启动,相当于say sorry服务器
disabled   #将后端服务器标记为不可用状态
redirect prefix http://www.xxx.net/   #将请求临时重定向至其它URL,只适用于http模式
maxconn <maxconn>:当前,后端server的最大并发连接数
backlog <backlog>:当server的连接数达到上限后的后援队列长度
##listen
listen yewu-service-80      #指定名称,业务-服务-端口号
2  bind 192.168.38.37:80      #指定haproxy所监听的IP及端口号
3  mode http                  #指定反向代理的协议类型
4  option forwardfor          #把用户的IP透传给后端服务器
5  server web1 192.168.38.27:80 weight 1 check inter 3000 fall 3 rise 5     #指定后端服务器的名称、IP、端口号等

haproxy+apache实现负载均衡

环境描述

主机 服务
vm2(192.168.225.128) apache
vm4(192.168.225.131) apache
vm5(192.168.225.134) haproxy

apache部署

##vm2,vm4的httpd配置相同
[root@vm2 ~]# vim /etc/httpd/conf/httpd.conf 
Include conf.modules.d/*.conf
ServerName www.wisan.com:80
DocumentRoot "/var/www/html"
IncludeOptional conf.d/*.conf

[root@vm2 ~]# vim /etc/httpd/conf.d/vhost.conf <VirtualHost *:80>
    ServerName www.wisan.com
    DocumentRoot "/var/www/html/wisan"
    ErrorLog "/var/log/httpd/error_log"
    CustomLog "/var/log/httpd/access_log" common
</VirtualHost>

[root@vm2 ~]# vim /etc/httpd/conf.modules.d/00-ssl.conf 
LoadModule ssl_module modules/mod_ssl.so

[root@vm2 ~]# vim /etc/httpd/conf.d/ssl.conf
Listen 443 https
DocumentRoot "/var/www/html/wisan"
ServerName www.fanfan.com:443
SSLCertificateFile /etc/httpd/ssl/httpd.crt
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key

[root@vm2 ~]# ls /etc/httpd/ssl/
httpd.crt  httpd.csr  httpd.key

[root@vm2 ~]# cat /var/www/html/wisan/index.html 
rs1
[root@vm4 ~]# cat /var/www/html/wisan/index.html 
rs2

Haproxy编译安装

配置内核参数

[root@vm5 ~]# vim /etc/sysctl.conf 
[root@vm5 ~]# sysctl -p 
net.ipv4.ip_nonlocal_bind = 1        ##用于开启绑定非本机ip的功能
net.ipv4.ip_forward = 1

创建Haproxy用户

[root@vm5 ~]# useradd -r -s /sbin/nologin -M haproxy
[root@vm5 ~]# id haproxy
uid=995(haproxy) gid=992(haproxy) groups=992(haproxy)

安装依赖包

[root@vm5 ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel

编译安装

[root@vm5 opt]# tar xf haproxy-2.3.0.tar.gz 
[root@vm5 opt]# ls
haproxy-2.3.0  haproxy-2.3.0.tar.gz
[root@vm5 opt]# cd haproxy-2.3.0
[root@vm5 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@vm5 haproxy-2.3.0]# make clean
## 编译
[root@vm5 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@vm5 haproxy-2.3.0]# make install PREFIX=/usr/local/haproxy
[root@vm5 sbin]# ln -sv /usr/local/haproxy/sbin/haproxy /usr/sbin/
[root@vm5 ~]# tree /usr/local/haproxy/
/usr/local/haproxy/
├── doc
│   └── haproxy
│       ├── 51Degrees-device-detection.txt
│       ├── architecture.txt
│       ├── close-options.txt
│       ├── configuration.txt
│       ├── cookie-options.txt
│       ├── DeviceAtlas-device-detection.txt
│       ├── intro.txt
│       ├── linux-syn-cookies.txt
│       ├── lua.txt
│       ├── management.txt
│       ├── netscaler-client-ip-insertion-protocol.txt
│       ├── network-namespaces.txt
│       ├── peers.txt
│       ├── peers-v2.0.txt
│       ├── proxy-protocol.txt
│       ├── regression-testing.txt
│       ├── seamless_reload.txt
│       ├── SOCKS4.protocol.txt
│       ├── SPOE.txt
│       └── WURFL-device-detection.txt
├── sbin
│   └── haproxy
└── share
    └── man
        └── man1
            └── haproxy.1

生成启动文件

[root@vm5 ~]# 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@vm5 ~]# systemctl daemon-reload

创建配置文件

mkdir /etc/haproxy

cat > /etc/haproxy/haproxy.cfg <<EOF
#--------------全局配置----------------
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.225.128:80 check inter 2000 fall 5
    server web02 192.168.225.131:80 check inter 2000 fall 5
    #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
EOF

重启用rsyslog日志

[root@vm5 haproxy]# vim  /etc/rsyslog.conf
local0.*      /var/log/haproxy.log
[root@vm5 haproxy]# systemctl restart rsyslog

启动haproxy

[root@vm5 ~]# systemctl restart haproxy
[root@vm5 ~]# 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:8189                      0.0.0.0:*           
LISTEN        0             128                         0.0.0.0:80                        0.0.0.0:*           
LISTEN        0             128                            [::]:22                           [::]:*    

验证http服务负载均衡

修改haproxy.cfg配置文件

[root@vm5 haproxy]# vim /etc/haproxy/haproxy.cfg
...................
listen webcluster
    bind 0.0.0.0:80
#   bind 0.0.0.0:443
    mode http
    #option httpchk GET /index.html
    log global
    maxconn 3000
    balance roundrobin
    cookie SESSION_COOKIE insert indirect nocache
    server web01 192.168.225.128:80 check inter 2000 fall 5
    server web02 192.168.225.131:80 check inter 2000 fall 5

重启haproxy服务

[root@vm5 haproxy]# systemctl restart haproxy
[root@vm5 haproxy]# ss -antl 
State         Recv-Q        Send-Q                Local Address:Port                 Peer Address:Port        
LISTEN        0             128                         0.0.0.0:8189                      0.0.0.0:*           
LISTEN        0             128                         0.0.0.0:5355                      0.0.0.0:*           
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                            [::]:5355                         [::]:*           
LISTEN        0             128                            [::]:22     

测试

  • vm3(192.168.225.130)进行测试
[root@vm3 ~]# curl -i http://192.168.225.134
HTTP/1.1 200 OK
date: Thu, 12 Nov 2020 14:48:51 GMT
server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips
last-modified: Wed, 11 Nov 2020 15:05:33 GMT
etag: "4-5b3d6208a6394"
accept-ranges: bytes
content-length: 4
content-type: text/html; charset=UTF-8
set-cookie: SESSION_COOKIE=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/
cache-control: private
connection: close

rs1
[root@vm3 ~]# curl -i http://192.168.225.134
HTTP/1.1 200 OK
date: Thu, 12 Nov 2020 14:48:57 GMT
server: Apache/2.4.37 (centos) OpenSSL/1.1.1c
last-modified: Wed, 11 Nov 2020 15:40:46 GMT
etag: "4-5b3d69e6f3b14"
accept-ranges: bytes
content-length: 4
content-type: text/html; charset=UTF-8
set-cookie: SESSION_COOKIE=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/
cache-control: private
connection: close

rs2

验证Https服务负载均横

修改haproxy.cfg配置文件

[root@vm5 haproxy]# vim /etc/haproxy/haproxy.cfg 
........................
listen webcluster
#    bind 0.0.0.0:80
    bind 0.0.0.0:443
    mode tcp
    #option httpchk GET /index.html
    log global
    maxconn 3000
    balance roundrobin
    cookie SESSION_COOKIE insert indirect nocache
#    server web01 192.168.225.128:80 check inter 2000 fall 5
#    server web02 192.168.225.131:80 check inter 2000 fall 5
    server web01 192.168.225.128:443 check inter 2000 fall 5
    server web02 192.168.225.131:443 check inter 2000 fall 5

重启haproxy服务

[root@vm5 haproxy]# systemctl restart haproxy
[root@vm5 haproxy]# ss -antl 
State         Recv-Q        Send-Q                Local Address:Port                 Peer Address:Port        
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                         0.0.0.0:5355                      0.0.0.0:*           
LISTEN        0             128                        0.0.0.0:22                        0.0.0.0:*           
LISTEN        0             128                            [::]:5355                         [::]:*           
LISTEN        0             128                            [::]:22                           [::]:*           
[root@vm5 haproxy]# 

测试

[root@vm3 ~]# curl -ik https://192.168.225.134
HTTP/1.1 200 OK
Date: Thu, 12 Nov 2020 14:53:54 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips
Last-Modified: Wed, 11 Nov 2020 15:05:33 GMT
ETag: "4-5b3d6208a6394"
Accept-Ranges: bytes
Content-Length: 4
Content-Type: text/html; charset=UTF-8

rs1
[root@vm3 ~]# curl -ik https://192.168.225.134
HTTP/1.1 200 OK
Date: Thu, 12 Nov 2020 14:53:58 GMT
Server: Apache/2.4.37 (centos) OpenSSL/1.1.1c
Last-Modified: Wed, 11 Nov 2020 15:40:46 GMT
ETag: "4-5b3d69e6f3b14"
Accept-Ranges: bytes
Content-Length: 4
Content-Type: text/html; charset=UTF-8

rs2

haproxy web界面

  • haproxy.cfg配置文件
listen admin_stats
    bind 0.0.0.0:8189
    stats enable
    mode http
    log global
    stats uri /haproxy_stats        ##访问时要加上URI(/haproxy_stats)
    stats realm Haproxy\ Statistics
    stats auth admin:admin          ##登录的账号和密码
    #stats hide-version
    stats admin if TRUE
    stats refresh 30s

posted @ 2020-11-13 09:29  小芃总  阅读(362)  评论(0编辑  收藏  举报