Dockerfile使用alpine系统制作haproxy镜像

Dockerfile使用alpine系统制作haproxy镜像

Dockerfile目录结构

[root@localhost haproxyalpine]# tree
.
├── Dockerfile
└── files
    ├── haproxy-2.5.0.tar.gz
    ├── haproxycfg.sh
    ├── install.sh
    └── sysctl.conf

Dockerfile

[root@localhost haproxyalpine]# cat Dockerfile 
FROM alpine

LABEL MAINTAINER "best 123321@qqq.com"

ENV version 2.5.0

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

RUN /tmp/install.sh
ENTRYPOINT /tmp/haproxycfg.sh

安装haproxy脚本

[root@localhost haproxyalpine]# 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-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/haproxy-2.5.0  /tmp/install.sh

配置文件

[root@localhost haproxyalpine]# cat files/haproxycfg.sh 
#!/bin/sh
cat > /etc/haproxy/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
count=1
for rs_ip in $RSs;do
cat >> /etc/haproxy/haproxy.cfg <<EOF
    server web$count $rs_ip:80 check inter 2000 fall 5
EOF
let count++
done

haproxy -f  /etc/haproxy/haproxy.cfg -db

制作镜像

[root@localhost haproxyalpine]# ls
Dockerfile  files
[root@localhost haproxyalpine]# docker build -t haproxy:v3.0 .
[root@localhost haproxyalpine]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED         SIZE
haproxy            v3.0      bb2af435dd0d   9 seconds ago   85.1MB

启动容器

[root@localhost ~]# docker run -d --name apache best2001/httpd:v1.0
[root@localhost ~]# docker run -d --name nginx best2001/nginx:v0.3
[root@localhost haproxyalpine]# docker run -d --name haproxy -p 80:80 -e RSs="172.17.0.4 172.17.0.5" haproxy:v3.0
f9d6e427f11d65eb0117835dafdc490ef043e301a8af3f71888967e2969acbac

[root@localhost haproxyalpine]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                               NAMES
f9d6e427f11d   haproxy:v3.0          "/bin/sh -c /tmp/hap…"   4 minutes ago    Up 4 minutes    0.0.0.0:80->80/tcp, :::80->80/tcp   haproxy
d824b80e750e   best2001/nginx:v0.3   "/usr/local/nginx/sb…"   18 minutes ago   Up 18 minutes                                       nginx
daa852698965   best2001/httpd:v1.0   "/usr/local/apache/b…"   20 minutes ago   Up 20 minutes   80/tcp                              apache

浏览器访问测试

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枯木逢秋࿐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值