Centos7下利用Yum安装配置haproxy

in 互联网技术 with 0 comment  访问: 896 次

利用Yum安装haproxy

添加安装源参考:Centos下添加阿里的yum安装源

[root@nock ~]# yum -y install haproxy
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 haproxy.x86_64.0.1.5.18-9.el7 将被 安装
--> 解决依赖关系完成
...............
Running transaction
  正在安装    : haproxy-1.5.18-9.el7.x86_64                                                                                                                                               1/1 
  验证中      : haproxy-1.5.18-9.el7.x86_64                                                                                                                                               1/1 

已安装:
  haproxy.x86_64 0:1.5.18-9.el7                                                                                                                                                               

完毕

修改配置文件

备份默认配置文件:

[root@nock ~]# cd /etc/haproxy/
[root@nock ~]# mv haproxy.cfg haproxy.cfg.bak

添加代理配置:

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

# Global settings
#---------------------------------------------------------------------
global
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

defaults
    mode                    http
    mode                    tcp
    log                     global
    option                  httplog
    option                  tcplog
    option                  dontlognull
    option                  http-server-close
    # option forwardfor     except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000


frontend web-http
        bind            0.0.0.0:81
        log-format %ci:%cp\ [%t]\ %ft\ %b/%s\ %Tq/%Tw/%Tc/%Tr/%Tt\ %ST\ %B\ %CC\ \%CS\ %tsc\ %ac/%fc/%bc/%sc/%rc\ %sq/%bq\ %hr\ %hs\ %{+Q}r
        mode            http
        default_backend http-servers

backend  http-servers
        mode            http
        option httplog
        log global
        balance roundrobin
        cookie SERVERID insert nocache indirect
        server app1  10.0.1.176:80 check port 80


listen mysql-3307
        bind 0.0.0.0:6380
        log-format %ci:%cp\ [%t]\ %ft\ %b/%s\ %Tw/%Tc/%Tt\ %B\ %ts\ \%ac/%fc/%bc/%sc/%rc\ %sq/%bq
        option tcplog
        log global
        mode tcp
        balance leastconn
        maxconn 40000  
        #tcp-request content accept if { src -f /etc/haproxy/white_ip_list }
        #tcp-request content reject
        server rs1 10.0.1.176:6379

自定义日志格式参考: http://cbonte.github.io/haproxy-dconv/1.5/configuration.html#8.2.4

Tips: 10.0.1.175 代理 10.0.1.76 , 因为1.175的80端口和6379端口都已经使用,我就用其他端口了

配置haproxy日志

[root@nock ~]# vim /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS=""  ===>  SYSLOGD_OPTIONS="-c 2 -r -m 0"

[root@nock ~]# mkdir -pv /data/logs/haproxy
[root@nock ~]# vim /etc/rsyslog.conf
#$ModLoad imudp  ===>  $ModLoad imudp       # 去掉注释#号
#$UDPServerRun 514 ===>  $UDPServerRun 514  # 去掉注释#号

# haproxy log 添加如下内容和本行
local2.*                                                /data/logs/haproxy/haproxy.log

[root@nock ~]# systemctl restart rsyslog.service
[root@nock ~]# systemctl status rsyslog.service
● rsyslog.service - System Logging Service
   Loaded: loaded (/usr/lib/systemd/system/rsyslog.service; enabled; vendor preset: enabled)
   Active: active (running) since 二 2019-12-03 17:39:49 UTC; 3s ago
 Main PID: 10358 (rsyslogd)
   CGroup: /system.slice/rsyslog.service
           └─10358 /usr/sbin/rsyslogd -n -c 2 -r -m 0

启动和测试haproxy

[root@nock ~]# systemctl start haproxy.service
[root@nock ~]# systemctl status haproxy.service
● haproxy.service - HAProxy Load Balancer
   Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled)
   Active: active (running) since 二 2019-12-03 17:45:26 UTC; 2s ago
 Main PID: 10780 (haproxy-systemd)
   CGroup: /system.slice/haproxy.service
           ├─10780 /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
           ├─10781 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
           └─10782 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds

12月 03 17:45:26 nock systemd[1]: Started HAProxy Load Balancer.
12月 03 17:45:26 nock haproxy-systemd-wrapper[10780]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds

测试:
http-proxy.jpg

[root@76 html]# /data/app/redis/bin/redis-cli -h 10.0.1.175 -p 6380 info Replication
# Replication
role:slave
master_host:10.0.1.175
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:1771485
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

访问logs:

[root@nockhaproxy]# tail -f /data/logs/haproxy/haproxy.log 
Dec  3 18:02:07 localhost haproxy[11953]: 10.0.3.5:49675 [03/Dec/2019:18:02:07.427] web-http http-servers/app1 0/0/0/1/1 304 142 - \- --NI 1/1/0/1/0 0/0 "GET /index.html HTTP/1.1"
Dec  3 18:04:25 localhost haproxy[11953]: 10.0.1.176:29219 [03/Dec/2019:18:04:25.849] mysql-3307 mysql-3307/rs1 1/0/1 2114 -- 0/0/0/0/0 0/0
Dec  3 18:04:25 localhost haproxy[11953]: 10.0.1.176:29219 [03/Dec/2019:18:04:25.849] mysql-3307 mysql-3307/rs1 1/0/1 2114 -- 0/0/0/0/0 0/0
Dec  3 18:04:33 localhost haproxy[11953]: 10.0.1.176:29220 [03/Dec/2019:18:04:33.350] mysql-3307 mysql-3307/rs1 1/0/1 373 -- 0/0/0/0/0 0/0
Dec  3 18:04:33 localhost haproxy[11953]: 10.0.1.176:29220 [03/Dec/2019:18:04:33.350] mysql-3307 mysql-3307/rs1 1/0/1 373 -- 0/0/0/0/0 0/0
WeZan
Responses