Dockerfile基于alpine镜像构建haproxy

Dockerfile基于alpine镜像构建haproxy

文件结构

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

Dockerfile


[root@localhost haproxy]# cat Dockerfile 
FROM alpine 
LABEL MAINTAINER="yyy 123456789@com"

ENV version 2.4.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


配置文件和安装脚本

[root@localhost files]# vim haproxycfg.sh
[root@localhost files]# chmod +x haproxycfg.sh 
[root@localhost files]# cat 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 files]# vim sysctl.conf 
[root@localhost files]# cat sysctl.conf 
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

[root@localhost files]# vim install.sh 
[root@localhost files]# chmod +x install.sh 
[root@localhost files]# cat 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
cp haproxy  /usr/sbin/
mkdir /etc/haproxy
apk del gcc make
rm -rf /tmp/haproxy-$version/ /tmp/install.sh

[root@localhost files]# ls
haproxy-2.4.0.tar.gz  haproxycfg.sh  install.sh  sysctl.conf

测试

//创建镜像
[root@localhost ~]# docker build -t haproxy:v0.3 haproxy
[root@localhost ~]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED          SIZE
haproxy         v0.3      0140afce2d43   29 seconds ago   83.8MB
haproxy         v0.1      949e1a86fb6b   39 hours ago     578MB
yzy0923/httpd   v0.01     a88b702deaee   3 days ago       701MB
busybox         latest    d23834f29b38   12 days ago      1.24MB
alpine          latest    c059bfaa849c   2 weeks ago      5.59MB
centos          8         5d0da3dc9764   2 months ago     231MB
centos          latest    5d0da3dc9764   2 months ago     231MB

//创建容器
[root@localhost haproxy]# docker run -d --name haproxy -p 80:80 -e RSs="172.17.0.3 172.17.0.4" haproxy:v0.3
2e7cf1e4a7b627e1af648197515bf1cf4b80675d549ff283133e269b64829b63
[root@localhost haproxy]# docker run -d --name http httpd
2a9df974dc4ab3f5b0351c9dd1b36b60dbc5079d31d95404f92983bd7ce8518a
[root@localhost haproxy]# docker run -d --name nginx nginx
7d6d2c004eb144d9375838806c0dee59c0b9f0dd3d067126bab35f0596debb08

[root@localhost haproxy]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS                               NAMES
7d6d2c004eb1   nginx          "/docker-entrypoint.…"   54 seconds ago       Up 52 seconds       80/tcp                              nginx
2a9df974dc4a   httpd          "httpd-foreground"       About a minute ago   Up About a minute   80/tcp                              http
2e7cf1e4a7b6   haproxy:v0.3   "/bin/sh -c /tmp/hap…"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp, :::80->80/tcp   haproxy


在这里插入图片描述

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值