3.2.7:基于 Dockerfile 制作 HAProxy 镜像
准备基础镜像
使用之前制作的 centos-init:8.3.2011 作为基础镜像;
编写 Dockerfile
root@Images-Server:~# cd /root/Dockerfile/Services/HAProxy/
root@Images-Server:~/Dockerfile/Services/HAProxy# vim Dockerfile
# HAProxy Dockerfile
#
FROM 192.168.1.121/centos/centos-init:8.3.2011
LABEL maintainer="yqc<20251839@qq.com>"
ADD haproxy-1.8.20.tar.gz /usr/local/src
RUN yum -y install gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools vim iotop bc zip unzip zlib-devel lrzsz tree lsof tcpdump wget \
&& cd /usr/local/src/haproxy-1.8.20 \
&& make ARCH=x86_64 TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/usr/local/haproxy \
&& make install PREFIX=/usr/local/haproxy \
&& cp haproxy /usr/sbin/ \
&& mkdir /etc/haproxy /var/lib/haproxy \
&& groupadd haproxy -g 99 \
&& useradd haproxy -s /sbin/nologin -u 99 -g 99 \
&& chown haproxy.haproxy /var/lib/haproxy/ -R
ADD haproxy.cfg /etc/haproxy/haproxy.cfg
ADD run_haproxy.sh /usr/bin/run_haproxy.sh
CMD ["/usr/bin/run_haproxy.sh"]
准备相关文件
准备 haproxy 源码包、配置文件、启动脚本;
配置文件:
注释 daemon,前台启动 haproxy;pidfile 也不需要了;
#daemon #pidfile /run/haproxy.pid
root@Images-Server:~/Dockerfile/Services/HAProxy# cat haproxy.cfg
global
maxconn 100000
uid 99
gid 99
#daemon
nbproc 1
#pidfile /run/haproxy.pid
log 127.0.0.1 local3 info
chroot /usr/local/haproxy
stats socket /var/lib/haproxy/haproxy.socket mode 600 level admin
defaults
option redispatch
option abortonclose
option http-keep-alive
option forwardfor
maxconn 100000
mode http
timeout connect 10s
timeout client 20s
timeout server 30s
timeout check 5s
listen stats
bind :9999
stats enable
stats hide-version
stats uri /haproxy-status
stats realm HAPorxy\ Stats\ Page
stats auth haadmin:123456
stats auth admin:123456
stats refresh 30s
stats admin if TRUE
启动脚本:
添加执行权限;
root@Images-Server:~/Dockerfile/Services/HAProxy# vim run_haproxy.sh
#!/bin/bash
#
/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg
root@Images-Server:~/Dockerfile/Services/HAProxy# chmod +x run_haproxy.sh
准备完毕:
root@Images-Server:~/Dockerfile/Services/HAProxy# ll
total 2056
drwxr-xr-x 2 root root 4096 Feb 1 17:56 ./
drwxr-xr-x 6 root root 4096 Feb 1 16:48 ../
-rw-r--r-- 1 root root 684 Feb 1 17:56 Dockerfile
-rw-r--r-- 1 root root 2083917 Apr 29 2020 haproxy-1.8.20.tar.gz
-rw-r--r-- 1 root root 618 Feb 1 17:51 haproxy.cfg
-rwxr-xr-x 1 root root 63 Feb 1 17:53 run_haproxy.sh*
构建镜像
编写镜像构建脚本,完成镜像构建和自动推送:
root@Images-Server:~/Dockerfile/Services/HAProxy# vim docker_build.sh
#!/bin/bash
docker build -t 192.168.1.121/haproxy/haproxy:1.8.20 .
docker push 192.168.1.121/haproxy/haproxy:1.8.20
执行构建脚本:
root@Images-Server:~/Dockerfile/Services/HAProxy# bash docker_build.sh
验证镜像
启动 haproxy 容器,验证状态页:
root@Images-Server:~# docker run -itd -p 9999:9999 --name haproxy1 192.168.1.121/haproxy/centos-haproxy:1.8.20
root@Images-Server:~# ss -tnlp |grep 9999
LISTEN 0 20480 *:9999 *:* users:(("docker-proxy",pid=8911,fd=4))
root@Images-Server:~# docker ps -f name=haproxy1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d9868fae80db 192.168.1.121/haproxy/centos-haproxy:1.8.20 "/usr/bin/run_haprox…" 2 minutes ago Up About a minute 0.0.0.0:9999->9999/tcp h访问aproxy1
访问 http://192.168.1.111:9999/haproxy-status