[root@localhost ~]# cd haproxy
[root@localhost haproxy]# cat Dockerfile
FROM centos
ENV version 2.4.0
WORKDIR /usr/local
ADD files/haproxy${version}.tar.gz /usr/local/
ADD files/haproxy.sh /usr/local
ADD files/haproxy.cfg /etc/haproxy/
ADD files/run_haproxy.sh /usr/local
ADD files/sysctl.conf /etc/
RUN ["/bin/bash","-c","/usr/local/haproxy.sh"]
EXPOSE 80
CMD ["/usr/local/run_haproxy.sh"]
haproxy配置文件
[root@localhost ~]# cd haproxy
[root@localhost haproxy]# ls files/
haproxy2.4.0.tar.gz haproxy.cfg haproxy.sh run_haproxy.sh sysctl.conf
[root@localhost haproxy]# cat files/haproxy.sh
#!/bin/bash
yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
useradd -r -M -s /sbin/nologin haproxy
cd /usr/local
tar xf haproxy2.4.0.tar.gz
cd haproxy-2.4.0
make clean
make -j $(grep 'processor' /proc/cpuinfo |wc -l) \
TARGET=linux-glibc \
USE_OPENSSL=1 \
USE_ZLIB=1 \
USE_PCRE=1 \
USE_SYSTEMD=1
make install PREFIX=/usr/local/haproxy
cp haproxy /usr/sbin/
配置各个负载的内核参数配置
[root@localhost files]# ls
haproxy-2.4.0.tar.gz haproxy.sh sysctl.conf
haproxy.cfg run_haproxy.sh
[root@localhost haproxy]# cat files/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
提供haproxy配置文件
[root@localhost haproxy]# cat files/haproxy.cfg
#--------------全局配置----------------
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 web1 172.17.0.3:80 check inter 3000 fall 5
server web2 172.17.0.4:80 check inter 3000 fall 5