Docker 3.2.7:基于 Dockerfile 制作 HAProxy 镜像

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

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值