用Dockerfile方式使用alpine系统制作haproxy镜像
Dockerfile目录结构
[root@localhost ~]
haproxy/
├── Dockerfile
└── files
├── haproxy-2.5.0.tar.gz
├── install.sh
├── start.sh
└── sysctl.conf
1 directory, 5 files
编写Dockerfile
[root@localhost ~]
FROM alpine
LABEL MAINTAINER wjj200112 1234567890@qq.com
ENV version 2.5.0
ADD files/haproxy-${version}.tar.gz /usr/src
ADD files/install.sh /tmp/
RUN ["/bin/bash","-c","/tmp/install.sh"]
ENTRYPOINT /tmp/haproxy.sh
[root@localhost ~]
[root@localhost ~]
FROM alpine
LABEL MAINTAINER wjj200112 1234567890@qq.com
ENV version 2.5.0
ADD files/haproxy-${version}.tar.gz /usr/src
ADD files/install.sh /scripts/
ADD files/start.sh /scripts/
ADD files/sysctl.conf /scripts/
RUN /scripts/install.sh
EXPOSE 80 8189
CMD /scripts/start.sh
提供Dockerfile所需的文件
安装脚本
[root@localhost ~]
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 /usr/src/haproxy-${version}
make TARGET=linux-musl USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1
make install PREFIX=/usr/local/haproxy
cp haproxy /usr/sbin/
mkdir -p /usr/local/haproxy/conf/
apk del gcc make
rm -rf /usr/src/haproxy-${version} /var/cache/*
启动脚本
[root@localhost ~]
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
server=1
for RS in `cat /RS/RS.txt`
do
cat >> /usr/local/haproxy/conf/haproxy.cfg <<EOF
server web$server $RS:80 check inter 2000 fall 5
EOF
let server++
done
haproxy -f /usr/local/haproxy/conf/haproxy.cfg
/bin/sh
构建haproxy镜像
[root@localhost ~]
Sending build context to Docker daemon 3.812MB
Step 1/10 : FROM alpine
---> c059bfaa849c
Step 2/10 : LABEL MAINTAINER wjj200112 1234567890@qq.com
---> Using cache
---> 895c9a229550
Step 3/10 : ENV version 2.5.0
---> Using cache
---> b0acbcf67d13
Step 4/10 : ADD files/haproxy-${version}.tar.gz /usr/src
---> Using cache
---> be9133f11324
Step 5/10 : ADD files/install.sh /scripts/
---> Using cache
---> 9f7dad6ec8fc
Step 6/10 : ADD files/start.sh /scripts/
---> Using cache
---> b1c30a0fff5d
Step 7/10 : ADD files/sysctl.conf /scripts/
---> Using cache
---> 5da396d055b7
Step 8/10 : RUN /scripts/install.sh
---> Using cache
---> d0b711f373a5
Step 9/10 : EXPOSE 80 8189
---> Using cache
---> fd2d46ebc681
Step 10/10 : CMD /scripts/start.sh
---> Using cache
---> 78f79b448d6a
Successfully built 78f79b448d6a
Successfully tagged haproxy:v0.2
[root@localhost ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy v0.2 78f79b448d6a 13 minutes ago 82.9MB
创建RS文件
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
172.17.0.2
172.17.0.3
基于新镜像创建haproxy容器
[root@localhost ~]
ff034ca5c1a5f9d9e5dba90a0c8d4272d4f2931a9774a06282f5402156955137
[root@localhost ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff034ca5c1a5 haproxy:v0.2 "/bin/sh -c /scripts…" 13 seconds ago Up 12 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8189->8189/tcp, :::8189->8189/tcp haproxy
[root@localhost ~]
/
/
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:8189 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
访问测试
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4fa9ff6ec05f8bba1696c97fdd717026.png)