构建haproxy镜像(基于alpine系统)

拉取alpine系统镜像

[root@localhost ~]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
Digest: sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300
Status: Image is up to date for alpine:latest
docker.io/library/alpine:latest

[root@localhost docker]# tree haproxy/
haproxy/
├── conf
│   └── RSS
├── Dockerfile
└── files
    ├── entrypoint.sh
    ├── haproxy-2.4.9.tar.gz
    ├── haproxy.cfg
    ├── install.sh

编写dockerfile

[root@localhost docker]# vim haproxy/Dockerfile 
FROM alpine
  
LABEL MAINTAINER='neawalke 123456789@qq.com'

ENV version 2.4.9
ENV PATH /usr/local/harpoxy/sbin:$PATH

ADD files/haproxy-${version}.tar.gz /tmp/
ADD files/install.sh /tmp/
COPY files/entrypoint.sh /


RUN /tmp/install.sh


EXPOSE 80 8189
ENTRYPOINT ["/entrypoint.sh"]
~                               

安装脚本

[root@localhost docker]# 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
apk del gcc make
mkdir /usr/local/haproxy/conf
rm -rf  /tmp/install.sh

启动脚本

[root@localhost ~]# vim /docker/haproxy/files/entrypoint.sh
#!/bin/sh

cat > /usr/local/haproxy/conf/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 in $(cat /tmp/RSS);do
cat >> /usr/local/haproxy/conf/haproxy.cfg << EOF
    server web$count $rs:80 check inter 2000 fall 5
EOF
let count++
done
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg -db
~                                                                               
# 构建镜像
[root@localhost docker]# docker build -t haproxy:v5.0 haproxy
 
#启动页面容器
[root@localhost ~]# docker run -d --name web1 neawalke/httpd:latest 
6a81a9963d31c1cecd4cf715985b3b3da1e596a41195edf854ce108c92cf6a4e
[root@localhost ~]# docker run -d --name web2 neawalke/nginx:v2.0 
6c698a936d98a19dbd004181b51e25a03eb10f3066091a1c8adf922a77e99725

[root@localhost ~]# cat /docker/haproxy/conf/RSS 
172.17.0.2
172.17.0.3

#启动容器
[root@localhost docker]# docker run -it --name test1 -p 80:80 -v /docker/haproxy/conf:/tmp haproxy:v5.0

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值