Dockerfile基于alpine构建haproxy

结构

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

1 directory, 4 files

拉取镜像

[root@localhost ~]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
59bf1c3509f3: Pull complete 
Digest: sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
haproxy      v0.5      0190b2adbaac   45 hours ago   495MB
busybox      latest    ffe9d497c324   5 days ago     1.24MB
httpd        v1.0      3471d3329f64   6 days ago     721MB
nginx        latest    f652ca386ed1   10 days ago    141MB
alpine       latest    c059bfaa849c   2 weeks ago    5.59MB
centos       latest    5d0da3dc9764   2 months ago   231MB

编写Dockerfile

[root@localhost ~]# cat haproxy/Dockerfile 
#基础镜像
FROM alpine

#作者信息
LABEL MAINTAINER 'lxx'

#环境变量
ENV version 2.5.0

#传输文件
ADD files/haproxy-${version}.tar.gz /tmp/
ADD files/install.sh /tmp/
ADD files/haproxycfg.sh /tmp/
#安装
RUN /tmp/install.sh

#暴露端口
EXPOSE 80 8189

#启动命令
ENTRYPOINT /tmp/haproxycfg.sh

相关文件

配置文件

//利用脚本传递变量的方式进行生成
[root@localhost ~]# cat haproxy/Dockerfile 
#基础镜像
FROM alpine

#作者信息
LABEL MAINTAINER 'lxx'

#环境变量
ENV version 2.5.0

#传输文件
ADD files/haproxy-${version}.tar.gz /tmp/
ADD files/install.sh /tmp/
ADD files/haproxycfg.sh /tmp/
#安装
RUN /tmp/install.sh

#暴露端口
EXPOSE 80 8189

#启动命令
ENTRYPOINT /tmp/haproxycfg.sh


[root@localhost ~]# cat haproxy/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 ~]# cat haproxy/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-${version}
make TARGET=linux-musl USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1 
make install PREFIX=/usr/local/haproxy
echo 'net.ipv4.ip_nonlocal_bind = 1' >>  /etc/sysctl.conf 
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
cp haproxy  /usr/sbin/
mkdir /etc/haproxy
apk del gcc make
rm -rf /tmp/haproxy-2.5.0/ /tmp/install.sh

构建镜像

[root@localhost ~]# docker build -t haproxy:alpine haproxy/
Sending build context to Docker daemon  3.811MB
Step 1/7 : FROM alpine
 ---> c059bfaa849c
Step 2/7 : LABEL MAINTAINER 'lxx'
 ---> Using cache
 ---> 48c0ed157160
Step 3/7 : ENV VERSION 2.5.0
 ---> Using cache
 ---> 192037a94321
Step 4/7 : ADD files  /tmp/
 ---> 4f7b431d675b
Step 5/7 : RUN /tmp/install.sh
...OK: 35 MiB in 45 packages
Removing intermediate container 1c3be38bebd7
 ---> c3b767a99948
Step 6/7 : EXPOSE 80 8189
 ---> Running in 3b70ad06302f
Removing intermediate container 3b70ad06302f
 ---> c2a90a72f583
Step 7/7 : ENTRYPOINT /tmp/haproxycfg.sh
 ---> Running in ca8e1f33e965
Removing intermediate container ca8e1f33e965
 ---> 86f0df80b75e
Successfully built 86f0df80b75e
Successfully tagged haproxy:alpine

测试

//启2个上篇文章的httpd容器
[root@localhost ~]# docker start 3b9401d15c79 
3b9401d15c79
[root@localhost ~]# docker start c72fd66f9fe7
c72fd66f9fe7
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE        COMMAND               CREATED        STATUS          PORTS                               NAMES
3b9401d15c79   httpd:v1.0   "/scripts/start.sh"   45 hours ago   Up 11 seconds   0.0.0.0:82->80/tcp, :::82->80/tcp   web2
c72fd66f9fe7   httpd:v1.0   "/scripts/start.sh"   45 hours ago   Up 3 seconds                                        web1

[root@localhost ~]# docker inspect  web1
//查看ip
...略
Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
...

//启动容器传递ip,映射80端口
[root@localhost ~]# docker run -itd --name haproxy-1 -p 80:80 -e RSs="172.17.0.3 172.17.0.2" haproxy:alpine


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值