Dockerfile基于alpine镜像部署haproxy
制作haproxy镜像
[root@docker ~]# tree /haproxy/
/haproxy/
├── dockerfile
├── entrypoint.sh
└── files
├── haproxy-2.5.0.tar.gz
├── haproxy.cfg
├── install.sh
└── start.sh
dockerfile
[root@docker haproxy]# cat dockerfile
FROM alpine
LABEL MAINTAINER='aoman xx@163.com'
ENV version 2.5.0
ADD files/haproxy-${version}.tar.gz /tmp/
ADD files/install.sh /tmp/
COPY entrypoint.sh /
RUN /tmp/install.sh
WORKDIR /usr/local/haproxy
ENTRYPOINT ["/entrypoint.sh"]
install.sh
[root@docker haproxy]# cat files/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-${version}
make clean && \
make TARGET=linux-musl
USE_OPENSSL=1
USE_ZLIB=1
USE_PCRE=1
make install PREFIX=/usr/local/haproxy
cp haproxy /usr/sbin/
echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
mkdir -p /usr/local/haproxy/conf
apk del gcc make
rm -rf /tmp/* /var/cache/*
entrypoint.sh
[root@docker ~]# cat /haproxy/entrypoint.sh
#!/bin/sh
cat > /usr/local/haproxy/conf/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
Snu=1
for rs_ip in $(cat /tmp/sip.txt);do
cat >> /usr/local/haproxy/conf/haproxy.cfg <<EOF
server web$Snu $rs_ip:80 check inter 2000 fall 5
EOF
let Snu++
done
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg -db
sip文件
[root@localhost ~]# cat /config/sip.txt
192.168.100.3
192.168.100.4
构建镜像
[root@docker haproxy]# docker build -t xm17671855780/haproxy:v2.1 /haproxy/
Sending build context to Docker daemon 3.815MB
Step 1/9 : FROM alpine
---> c059bfaa849c
Step 2/9 : LABEL MAINTAINER='aoman xx@163.com'
---> Using cache
---> fd9d523e916d
Step 3/9 : ENV version 2.5.0
---> Using cache
---> 24d940e74a8a
Step 4/9 : ADD files/haproxy-${version}.tar.gz /tmp/
---> Using cache
---> 6b04f9699c64
Step 5/9 : ADD files/install.sh /tmp/
---> 89391b244bd1
Step 6/9 : COPY entrypoint.sh /
---> 9869dab4fc55
Step 7/9 : RUN /tmp/install.sh
---> Running in d500b9be9104
Executing busybox-1.34.1-r3.trigger
OK: 35 MiB in 45 packages
Removing intermediate container d500b9be9104
---> 2582c0bbcbfe
Step 8/9 : WORKDIR /usr/local/haproxy
---> Running in 380ff4c5c4c0
Removing intermediate container 380ff4c5c4c0
---> 71086666d3c8
Step 9/9 : ENTRYPOINT /entrypoint.sh
---> Running in 9fef96b53128
Removing intermediate container 9fef96b53128
---> c9714f7b9d8d
Successfully built c9714f7b9d8d
Successfully tagged xm17671855780/haproxy:v2.1
创建haproxy、httpd、httpd2容器
[root@docker haproxy]# docker run -dit --name haproxy10 -v /config/:/tmp -p 80:80 -p 8189:8189 c9714f7b9d8d
1a71d4044d7723c201ad14187eb6eeb1ac0770f5e3e6cf76fad5bcde3a48e6ca
[root@docker haproxy]# docker exec -it haproxy10 /bin/sh
/usr/local/haproxy # cd conf/
/usr/local/haproxy/conf # ls
haproxy.cfg
[root@docker haproxy]# docker run -dit --name httpd a563454c4a5f
04430cc193f232625ea55d8887dddb6d0fe8141a3ff7803b393e6267967a0dc3
[root@docker haproxy]# docker exec -it httpd2 /bin/bash
[root@6503eba781e3 src]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
98: eth0@if99: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@6503eba781e3 src]# echo "two" > /usr/local/apache/htdocs/index.html
[root@6503eba781e3 src]#
[root@docker haproxy]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6503eba781e3 a563454c4a5f "/usr/local/apache/b…" 3 seconds ago Up 2 seconds 80/tcp, 443/tcp httpd2
04430cc193f2 a563454c4a5f "/usr/local/apache/b…" 8 seconds ago Up 7 seconds 80/tcp, 443/tcp httpd
1a71d4044d77 c9714f7b9d8d "/bin/sh -c /entrypo…" 11 minutes ago Up 11 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8189->8189/tcp, :::8189->8189/tcp haproxy10
查看效果