一、安装前准备
官网下载安装包:http://www.haproxy.org/download/1.8/src/haproxy-1.8.20.tar.gz
[root@centos src]$cd /usr/local/src/ [root@centos src]$ls haproxy-1.8.20.tar.gz [root@centos src]$yum install -y gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools vim iotop bc zip unzip zlib-devel lrzsz tree screen lsof tcpdump wget ntpdate
二、开始安装
[root@centos src]$tar xvf haproxy-1.8.20.tar.gz [root@centos src]$cd haproxy-1.8.20/ [root@centos haproxy-1.8.20]$make ARCH=x86_64 TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/usr/local/haproxy [root@centos haproxy-1.8.20]$ make install PREFIX=/usr/local/haproxy install -d "/usr/local/haproxy/sbin" install haproxy "/usr/local/haproxy/sbin" install -d "/usr/local/haproxy/share/man"/man1 install -m 644 doc/haproxy.1 "/usr/local/haproxy/share/man"/man1 install -d "/usr/local/haproxy/doc/haproxy" for x in configuration management architecture peers-v2.0 cookie-options lua WURFL-device-detection proxy-protocol linux-syn-cookies network-namespaces DeviceAtlas-device-detection 51Degrees-device-detection netscaler-client-ip-insertion-protocol peers close-options SPOE intro; do \ install -m 644 doc/$x.txt "/usr/local/haproxy/doc/haproxy" ; \ done [root@centos haproxy-1.8.20]$cp haproxy /usr/sbin/
三、创建启动脚本
[root@centos ~]$vim /usr/lib/systemd/system/haproxy.service [Unit] Description=HAProxy Load Balancer After=syslog.target network.target [Service] #支持多配置文件读取,类似于配置文件的include功能 ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid ExecReload=/bin/kill -USR2 $MAINPID [Install] WantedBy=multi-user.target
四、创建目录和用户
[root@centos ~]$mkdir /etc/haproxy [root@centos ~]$useradd -r -s /sbin/nologin haproxy [root@centos ~]$mkdir /var/lib/haproxy [root@centos ~]$chown -R haproxy.haproxy /var/lib/haproxy/ [root@centos ~]$cat /etc/haproxy/haproxy.cfg global log 127.0.0.1 local2 chroot /usr/local/haproxy pidfile /run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats mode 600 level admin defaults mode http log global option httplog 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 # haproxy.cfg文件中定义了chroot、pidfile、user、group等参数,如果系统没有相应的资源会导致haproxy无法启动,具体参考日志文件/var/log/messages
五、启动haproxy
[root@centos ~]$systemctl start haproxy [root@centos ~]$systemctl enable haproxy Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service. [root@centos ~]$ps -ef | grep haproxy root 8492 1 0 20:48 ? 00:00:00 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid haproxy 8495 8492 0 20:48 ? 00:00:00 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid root 8606 7171 0 20:53 pts/0 00:00:00 grep --color=auto haproxy