//结构
[root@localhost haproxyalpine]# tree
.
├── Dockerfile
└── files
├── haproxy-2.5.0.tar.gz
├── haproxycfg.sh
├── install.sh
└── sysctl.conf
编写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
//dockerfile
[root@localhost haproxy]# cat Dockerfile
FROM alpine
LABEL MAINTAINER=xaw@qq.com
ENV version 2.5.0
ADD files/haproxy-${version}.tar.gz /tmp/
ADD files/install.sh /tmp/
ADD files/haproxycfg.sh /tmp/
ADD files/sysctl.conf /tmp/
RUN /tmp/install.sh
ENTRYPOINT /tmp/haproxycfg.sh
[root@localhost files]# cat haproxy.sh
#!/bin/sh
cat > /etc/haproxy/haproxy.cfg <<EOF
#--------------全局配置----------------
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
EOF
count=1
for RS in 'cat /RS/RS.txt';do
cat >> /etc/haproxy/haproxy.cfg <<EOF
server web$count $RS:80 check inter 2000 fall 5
EOF
let count++
done
haproxy -f /etc/haproxy/haproxy.cfg -db
//记得给X权限
[root@localhost files]# cat install.sh
#!/bin/sh
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/' /etc/apk/repositories
apk update
adduser -S -H -s /sbin/nologin haproxy
addgroup haproxy
apk add --no-cache -U make gcc pcre-dev bzip2-dev openssl-dev elogind-dev libc-dev dahdi-tools dahdi-tools-dev libexecinfo libexecinfo-dev ncurses-dev zlib-dev zlib
cd /tmp/haproxy-2.5.0
make TARGET=linux-musl USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1
make install PREFIX=/usr/local/haproxy
cp haproxy /usr/sbin/
mkdir /etc/haproxy
apk del gcc make
rm -rf /tmp/haproxy-2.5.0 /tmp/install.sh
//主机文件
[root@localhost ~]# mkdir RS
[root@localhost ~]# touch RS.txt
[root@localhost ~]# vim RS.txt
172.17.0.2
172.17.0.3
构建镜像
[root@localhost haproxy]# docker build -t haproxy:0.4 /haproxy/
//查看大小
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy 0.4 bb2af435dd0d 9 seconds ago 82.6MB
//创建容器
[root@localhost ~]# docker run -d --name haproxy -p 8080:80 -v /RS:/RS haproxy:0.4
72b42a58a8ec2cf5e58882d6bb2d88de26555070ec572e571f77b3ce8dc2d775
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
72b42a58a8ec haproxy:0.4 "/scripts/haproxy.sh" 9 seconds ago Up 7 seconds 8189/tcp, 0.0.0.0:8080->80/tcp, :::8080->80/tcp haproxy
[root@localhost ~]# docker exec -it haproxy /bin/bash
[root@72b42a58a8ec /]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8189 0.0.0.0:*
[root@72b42a58a8ec /]#
//创建两个容器实现负载均衡,一台nginx一台httpd
[root@localhost ~]# docker run -d --name apache httpd
7ab5c3bf0cfb2a0a6b4887264a33c5a981c8c58e61d14f928becfc5451dd54a9
[root@localhost ~]# docker run -d --name nginx nginx
204f91f1ad5602eb0425a568aa7df17d59c7be064600d3984c6262d2bd14f2bd
//查看容器
[root@localhost haproxy]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f106173e3b69 haproxy "/scripts/haproxy.sh" About a minute ago Up About a minute 8189/tcp, 0.0.0.0:8080->80/tcp, :::8080->80/tcp haproxy999
204f91f1ad56 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp nginx
7ab5c3bf0cfb httpd "httpd-foreground" About a minute ago Up About a minute 80/tcp apache