haproxy2.1.3-编译安装 centos7

  1. haproxy2.1.3 下载

  2. 安装依赖:
    yum install -y openssl openssl-devel systemd-devel.x86_64

  3. 解压源码:
    tar -zxf haproxy-2.1.3.tar.gz

  4. 编译安装:

    cd haproxy-2.1.3/
    make TARGET=linux-glibc   USE_OPENSSL=1 USE_SYSTEMD=1 USE_PCRE=1  USE_ZLIB=1  && make install
    
    • 开启https:USE_OPENSSL=1

    • 指定为systemd模式: USE_SYSTEMD=1

    • 注册到系统服务:

      cp contrib/systemd/haproxy.service.in /usr/lib/systemd/system/haproxy.service
      
      vim /usr/lib/systemd/system/haproxy.service
      

      修改路径

      [Service]
      ExecStartPre=/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg   -c -q
      ExecStart=/usr/local/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg  -p /run/haproxy.pid
      ExecReload=/bin/kill -USR2 $MAINPID
      
    • 开启haproxy日志: vim /etc/rsyslog.conf

      # Provides UDP syslog reception
      #解开注释
      $ModLoad imudp
      #解开注释
      $UDPServerRun 514
      #添加
      local0.*                                      /var/log/haproxy.log
      

      编辑rsyslog

      vim /etc/sysconfig/rsyslog
      SYSLOGD_OPTIONS="-r -m 0 -c 2"
      
    • 添加启动用户

      useradd -r haproxy
      usermod -G haproxy haproxy
      
    • 创建配置文件及启动目录:

      mkdir -p /var/lib/haproxy
      touch /var/lib/haproxy/stats
      mkdir /etc/haproxy
      vim /etc/haproxy/haproxy.cfg
      

      默认不生成 Example:

      global
          log 127.0.0.1   local0 info
          chroot      /var/lib/haproxy
          pidfile     /var/run/haproxy.pid
          maxconn     1020   # See also: ulimit -n
          user        haproxy
          group       haproxy
          daemon
          # turn on stats unix socket
          stats socket /var/lib/haproxy/stats.sock mode 600 level admin
          stats timeout 2m
      defaults
          mode    tcp
          log     global
          option  dontlognull
          option  redispatch
          timeout http-request      10s
          retries                   3
          timeout queue             45s
          timeout connect           10s
          timeout client            1m
          timeout server            1m
          timeout http-keep-alive   10s
          timeout check             10s
          maxconn                   1020
      listen stats
          mode http
          bind 0.0.0.0:8089
          stats enable
          stats uri     /admin?stats
          stats realm   Haproxy\ Statistics
          stats auth    admin:admin
          stats admin if TRUE
      
      frontend mysql
          description "MySQL"
          bind *:3306
          mode tcp
          log global
          default_backend mysql
      
      backend mysql
          description "MySQL"
          balance leastconn
          server mysql1 192.168.88.8:3307 check
          server mysql2 192.168.88.10:3307 check
      

      检验haproxy.cfg 是否正确: haproxy -c -f /etc/haproxy/haproxy.cfg
      待出现 Configuration file is valid 说明配置有效

    • 重启服务

      systemctl restart haproxy
      systemctl restart rsyslog
      ## 查看日志
      tail /var/log/haproxy.log