2019.10.8 基于 Haproxy 构建负载均衡集群

1、安装配置Haproxy

 安装Haproxy依赖包及源码包编译安装

[root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel bzip2-devel

[root@localhost ~]# rz
z waiting to receive.**B0100000023be50
[root@localhost ~]# tar xf haproxy-1.4.24.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/haproxy-1.4.24/
[root@localhost haproxy-1.4.24]# make TARGET=linux26 && make install

建立haproxy的配置目录及文件

[root@localhost haproxy-1.4.24]# mkdir /etc/haproxy
[root@localhost haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/

 haproxy 配置文件修改
[root@localhost haproxy-1.4.24]# cd
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg

# this config needs haproxy-1.1.28 or haproxy-1.2.1
 
global
#   log 127.0.0.1   local0
#   log 127.0.0.1   local1 notice
    log /dev/log    local0 info
    log /dev/log    local0 notice
    maxconn 4096
    uid 99
    gid 99
    daemon
 
defaults
    log global
    mode    http
    option  httplog
    retries 3
    maxconn 4096
    contimeout  5000
    clitimeout  50000
    srvtimeout  50000
 
listen  webcluster 0.0.0.0:80
    option  httpchk GET /index.html
    balance roundrobin
    server  inst1 192.168.200.103:80 check inter 2000 fall 3
    server  inst1 192.168.200.104:80 check inter 2000 fall 3
 
listen admin_stats
    bind 0.0.0.0:8000
    mode http
    option httplog
    maxconn 100
    stats refresh 30s
    stats uri /stats
    stats realm Crushlinux\ Haproxy
        stats auth admin:admin
    stats hide-version
准备服务自启动脚本

[root@localhost ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@localhost ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@localhost ~]# chmod +x /etc/init.d/haproxy
[root@localhost ~]# /etc/init.d/haproxy start
Starting haproxy (via systemctl): [ 确定 ]

如果再次修改配置文件应该重启

[root@localhost ~]# /etc/init.d/haproxy restart

2、安装配置Web服务Nginx 

首先搭建Nginx1

[root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel openssl-devel

[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# rz
z waiting to receive.**B0100000023be50

[root@localhost ~]# tar xf nginx-1.15.9.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.15.9/
[root@localhost nginx-1.15.9]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install

[root@localhost nginx-1.15.9]# cd /usr/local/nginx/html/
[root@localhost html]# echo "server 192.168.200.113" > index.html
[root@localhost html]# /usr/local/nginx/sbin/nginx
[root@localhost html]# netstat -anpt |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7233/nginx: master

[root@localhost html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/                 //可以加一个软链接

[root@localhost html]# nginx                                 //直接nginx启动服务

搭建nginx2, 同nginx1搭建方式是一样的。唯一不同的是:

[root@localhost html]# echo "server 192.168.200.114" > index.html

3、客户端访问测试:
用浏览器打开 http://192.168.200.111
打开一个新的浏览器再次访问 http://192.168.200.111

4、Haproxy 日志

 修改Haproxy配置文件中关于日志配置选项

[root@localhost ~]# vim /etc/haproxy/haproxy.cfg 

log /dev/log    local0 info
log /dev/log    local0 notice

[root@localhost ~]# service haproxy restart
Restarting haproxy (via systemctl): [ 确定 ]

这两行配置放到global选项中,主要是将Haproxy的info和notice日志分别记录到不同的日志文件中

 修改rsyslog配置

[root@localhost ~]# vim /etc/rsyslog.d/haproxy.conf

if ($programname == 'haproxy' and $syslogserverity-text == 'info') then -/var/log/haproxy/haproxy-info.log
&~
if ($programname == 'haproxy' and $syslogserverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log
&~ 

重启rsyslog服务

[root@localhost ~]# service rsyslog restart
Redirecting to /bin/systemctl restart rsyslog.service

查看日志文件是否创建成功

[root@localhost ~]# ls -l /var/log/haproxy/haproxy-info.log
ls: 无法访问/var/log/haproxy/haproxy-info.log: 没有那个文件或目录
[root@localhost ~]# ls -l /var/log/haproxy/haproxy-notice.log
ls: 无法访问/var/log/haproxy/haproxy-notice.log: 没有那个文件或目录

去网页上输入192.168.200.111:8000/stats

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
计算机