Dockerfile制作基于alpine的haproxy镜像

Dockerfile制作基于alpine的haproxy镜像,第1张

Dockerfile制作基于alpine的haproxy镜像 Dockerfile制作基于alpine的haproxy镜像

使用docker pull拉下来的alpine镜像

工作目录

[root@docker haproxy]# tree
.
├── Dockerfile
└── files
    ├── entrypoint.sh
    ├── haproxy-2.5.0.tar.gz
    ├── haproxy.cfg
    ├── haproxyconf.sh
    ├── install.sh
    └── sysctl.conf

编写Dockerfile文件

FROM alpine
  
LABEL MAINTAINER "sun 112233@abc.com"

ENV version 2.5.0

ADD files/haproxy-${version}.tar.gz /tmp/
ADD files/install.sh /tmp/
ADD files/sysctl.conf /tmp/
ADD files/entrypoint.sh /
ADD files/haproxy.cfg /etc/haproxy/haproxy.cfg

RUN /tmp/install.sh

ENTRYPOINT /entrypoint.sh

编写安装脚本

[root@docker haproxy]# vim 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-2.5.0
make TARGET=linux-musl USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1
make install PREFIX=/usr/local/haproxy
cp haproxy  /usr/sbin/
mkdir /etc/haproxy
apk del gcc make
rm -rf /tmp/*

编辑entrypoint.sh文件

[root@docker haproxy]# vim files/entrypoint.sh
#!/bin/sh
  
count=1
for rs_ip in $RSIP;do
cat >> /etc/haproxy/haproxy.cfg < 

提供sysctl.conf文件

[root@docker haproxy]# vim files/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

提供配置文件

[root@docker haproxy]# vim files/haproxy.cfg 
#--------------全局配置----------------
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

制作镜像

[root@docker haproxy]# docker build -t 93quan/haproxy:v1-alpine .
[root@docker haproxy]# docker images
REPOSITORY       TAG         IMAGE ID       CREATED          SIZE
93quan/haproxy   v1-alpine   adef9baa8629   10 seconds ago   85.1MB

用不同的index.html运行两个httpd容器进行测试

[root@docker ~]# mkdir -p test/{html1,html2}
[root@docker ~]# vim test/html1/index.html
this web1
[root@docker ~]# vim test/html2/index.html
this web2
[root@docker ~]# docker run -d -v /root/test/html1:/usr/local/apache/htdocs --name web1 93quan/httpd:v4
59b355019a073fc73558fba3992a867c7f373f5052bf18003e352df68b845492
[root@docker ~]# docker run -d -v /root/test/html2:/usr/local/apache/htdocs --name web2 93quan/httpd:v4
30dd9da62cfcd150bc4bb83a5a9860676012541b4f640be99efc98153496f327
[root@docker ~]# docker ps
ConTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS     NAMES
30dd9da62cfc   93quan/httpd:v4   "/usr/local/apache/b…"   15 seconds ago   Up 14 seconds   80/tcp    web2
59b355019a07   93quan/httpd:v4   "/usr/local/apache/b…"   24 seconds ago   Up 23 seconds   80/tcp    web1
[root@docker ~]# curl 172.17.0.2
this web1
[root@docker ~]# curl 172.17.0.3
this web2

创建容器测试

[root@docker ~]# docker run -itd --rm -p 80:80 -eRSIP="172.17.0.2 172.17.0.3" 93quan/haproxy:v1-alpine
8e36be749ead7ab62a0f77b4f8d00336a745079bc7931d90bc7fbdf443ba1eab
[root@docker ~]# docker ps
ConTAINER ID   IMAGE                      COMMAND                  CREATED          STATUS          PORTS                               NAMES
8e36be749ead   93quan/haproxy:v1-alpine   "/bin/sh -c /entrypo…"   40 seconds ago   Up 39 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   strange_jepsen
30dd9da62cfc   93quan/httpd:v4            "/usr/local/apache/b…"   7 minutes ago    Up 7 minutes    80/tcp                              web2
59b355019a07   93quan/httpd:v4            "/usr/local/apache/b…"   7 minutes ago    Up 7 minutes    80/tcp                              web1

浏览器访问测试

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5669695.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存