Dockerfile基于alpine构建haproxy
编写Dockerfile
FROM alpine
LABEL MAINTAINER="wjm 1@2.com"
ENV version 2.5.0
ENV PATH /usr/local/haproxy/sbin:$PATH
ADD files/haproxy-${version}.tar.gz /tmp/
ADD files/install.sh /tmp/
ADD files/haproxycfg.sh /tmp/
RUN /tmp/install.sh
ENTRYPOINT /tmp/haproxycfg.sh
配置文件
haproxycfg.sh 配置文件脚本
[root@localhost haproxy]# cd files/
[root@localhost files]# ls
haproxy-2.5.0.tar.gz haproxycfg.sh install.sh sysctl.conf
[root@localhost files]# cat haproxycfg.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_ip in $(cat /tmp/RSs.txt);do
cat >> /etc/haproxy/haproxy.cfg <<EOF
server web$count $rs_ip:80 check inter 2000 fall 5
EOF
let count++
done
haproxy -f /etc/haproxy/haproxy.cfg -db //-f指定配置文件位置。-db关闭后台模式 也就是前台运行
install.sh 安装脚本
[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
echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
cp haproxy /usr/sbin/
mkdir /etc/haproxy
apk del gcc make
rm -rf /tmp/haproxy-2.5.0/ /tmp/install.sh
指定RS IP的文件
[root@localhost haproxy]# cat RSs.txt
172.17.0.3
172.17.0.4
172.17.0.5
构建镜像
[root@localhost ~]# docker build -t haproxy:v4.0 haproxy/
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy v4.0 62b77794bfbc About an hour ago 23MB
创建容器
[root@localhost ~]# docker run -d --name apache httpd
7ab5c3bf0cfb2a0a6b4887264a33c5a981c8c58e61d14f928becfc5451dd54a9
[root@localhost ~]# docker run -d --name nginx nginx
204f91f1ad5602eb0425a568aa7df17d59c7be064600d3984c6262d2bd14f2bd
[root@localhost ~]# docker run -d --name haproxy-1 -p 80:80 -p 8189:8189 -v /root/haproxy/RSs.txt :/tmp haproxy:v4.0
b3d081e8dee7e99d0eb4bd517114445002c0be8819cffe036b02fb6dd5871de8
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b3d081e8dee7 haproxy:v4.0 "/bin/sh -c /tmp/hap…" 3 minutes ago Up 5 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp haproxy-1
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
访问 测试