基于Alpine 编写Haproxy的Dockerfile
Dockerfile编写
目录
[root@localhost haproxy]
/root/haproxy
[root@localhost haproxy]
.
├── Dockerfile
├── entrypoint.sh
├── files
│ ├── haproxy-2.4.0.tar.gz
│ └── install.sh
└── haproxy_config
└── RSs.txt
2 directories, 5 files
Dockerfile文件
[root@localhost haproxy]
FROM alpine
ENV version 2.4.0
ENV PATH /usr/local/haproxy/sbin:$PATH
COPY files/ /tmp/
COPY entrypoint.sh /
RUN /tmp/install.sh
EXPOSE 80 8189
WORKDIR /usr/local/haproxy
ENTRYPOINT ["/entrypoint.sh"]
安装服务脚本
[root@localhost haproxy]
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/
tar xf haproxy-${version}.tar.gz
cd haproxy-${version}
make clean
make -j $(nproc) \
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
mkdir /usr/local/haproxy/conf
apk del gcc make
rm -rf /tmp/* /var/cache/*
开启脚本
[root@localhost haproxy]
cat > /usr/local/haproxy/conf/haproxy.cfg << EOF
global
log 127.0.0.1 local0 info
maxconn 20480
pidfile /var/run/haproxy.pid
user haproxy
group haproxy
daemon
defaults
mode http
log global
option dontlognull
option httpclose
option httplog
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 admin if TRUE
stats refresh 30s
listen webcluster
bind 0.0.0.0:80
mode http
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 >> /usr/local/haproxy/conf/haproxy.cfg << EOF
server web$count $rs_ip:80 check inter 2000 fall 5
EOF
let count++
done
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg -db
用来指定RS IP的文件
[root@localhost haproxy]
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6
基于Dockerfile创建镜像
[root@localhost ~]
[root@localhost ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
jiejiehao/haproxy v3 f6ad2b54e8fd 12 minutes ago 54.1MB
基于镜像开启容器
[root@localhost ~]
3abf4b594a59934064177a8afae50c4a066052668bdd6486ce9a196524d10126
[root@localhost ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3abf4b594a59 jiejiehao/haproxy:v3 "/entrypoint.sh" 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8189->8189/tcp, :::8189->8189/tcp haproxy01
创建用来测试的web容器
[root@localhost ~]
f887b6e2b72eb1197973a0bf42c13ef8ad362a492f5f454f2e79cbb5ded0366c
[root@localhost ~]
5a8610dc9a7c2ea7e8b012774d74f094c44a1ed9322bd5c79c48b83d9ba28302
[root@localhost ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5a8610dc9a7c nginx "/docker-entrypoint.…" 6 seconds ago Up 5 seconds 80/tcp nginx01
f887b6e2b72e httpd "httpd-foreground" 17 seconds ago Up 16 seconds 80/tcp httpd01
3abf4b594a59 jiejiehao/haproxy:v3 "/entrypoint.sh" 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8189->8189/tcp, :::8189->8189/tcp haproxy01
访问测试

测试用来指定RS IP的文件是否可用
[root@localhost haproxy_config]
/root/haproxy/haproxy_config
[root@localhost haproxy_config]
RSs.txt
[root@localhost haproxy_config]
[root@localhost haproxy_config]
192.168.1.3
192.168.1.4
[root@localhost haproxy_config]
haproxy01
