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

  • Post author:
  • Post category:其他


拉取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

在这里插入图片描述

在这里插入图片描述



版权声明:本文为NeaWalke原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。