Dockerfile使用alpine系统写haproxy

导读:本篇文章讲解 Dockerfile使用alpine系统写haproxy,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

创建httpd、nginx容器

[root@localhost ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED          SIZE
httpd              v0.1      3709a35b5387   15 minutes ago   423MB
nginx              v0.1      45d2af2e6e0f   27 minutes ago   549MB
centos             latest    5d0da3dc9764   2 months ago     231MB

// httpd
[root@localhost ~]# docker run -d --name httpd httpd:v0.1
c21c60460234f56d6c79a179bece1de6e8e02e8fffbb3b06d1c4137c08a8f680

[root@localhost ~]# docker exec -it httpd /bin/bash
[root@c21c60460234 apache]# ss -antl
State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process                                                      
LISTEN 0      128           0.0.0.0:80          0.0.0.0:*                                                                 
[root@c21c60460234 apache]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
25: eth0@if26: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever


// nginx 
[root@localhost ~]# docker run -itd --name nginx nginx:v0.1 /start.sh
2c56de84ec9129a5c40000785a3ee23c39636046a1fea485991465db0410d398
[root@localhost ~]# docker exec -it nginx /bin/bash
[root@2c56de84ec91 /]# ss -antl
State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process                                                      
LISTEN 0      128           0.0.0.0:80          0.0.0.0:*                                                                 
[root@2c56de84ec91 /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
31: eth0@if32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

目录结构

[root@localhost ~]# tree /opt/haproxy/
/opt/haproxy/
|-- Dockerfile
|-- config.sh
`-- files
    |-- haproxy-2.5.0.tar.gz
    `-- install.sh

1 directory, 4 files

// 变量文件
[root@localhost ~]# tree /config_ip/
/config_ip/
`-- RSS.txt

0 directories, 1 file

[root@localhost ~]# cat /config_ip/RSS.txt 
172.17.0.3
172.17.0.4

Dockerfile文件

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

// 作者信息
LABEL MAINTAINER "gaofan1225 123@qq.com"

// 定义变量
ENV VERSION 2.5.0
ENV PATH /usr/local/haproxy/sbin:$PATH

// 传输安装包和文件
ADD files/haproxy-${VERSION}.tar.gz /tmp/
ADD files/install.sh /tmp/
COPY config.sh /

// 安装脚本
RUN /tmp/install.sh

// 暴露端口
EXPOSE 80 443 8189

// 切换目录
WORKDIR /usr/local/haproxy

// 启动
ENTRYPOINT ["/config.sh"]

安装脚本

[root@localhost opt]# cat haproxy/files/install.sh 
#!/bin/sh

sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.co
m/' /etc/apk/repositories	// 替换成国内源
apk update					// 更新
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 && \			// 下载依赖包
adduser -S -H -s /sbin/nologin haproxy
addgroup haproxy			// 创建系统用户和组
cd /tmp/haproxy-${VERSION} && make clean && \	// 编译安装
        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
mkdir /usr/local/haproxy/conf
apk del make gcc
rm -rf /var/cache/* /tmp/*		// 删除安装完成之后不需要的文件

传输配置文件和启动服务的脚本

[root@localhost opt]# cat haproxy/config.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 rss_ip in $(cat /tmp/RSS.txt);do
cat >> /usr/local/haproxy/conf/haproxy.cfg << EOF
server web$count $rss_ip:80 check inter 2000 fall 5
EOF
let count++
done

/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg -db

创建haproxy容器

[root@localhost opt]# docker build -t haproxy:v1.0 haproxy/
Successfully built 0ab0f867da04
Successfully tagged haproxy:v1.0

[root@localhost opt]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED         SIZE
haproxy            v1.0      0ab0f867da04   4 minutes ago   65.8MB
httpd              v0.1      3709a35b5387   39 hours ago    423MB
nginx              v0.1      45d2af2e6e0f   39 hours ago    549MB
alpine             latest    c059bfaa849c   2 weeks ago     5.59MB


[root@localhost opt]# docker run -itd --name haproxy -p 80:80 -p 8189:8189 -v /config_ip:/tmp haproxy:v1.0
c2fe4a6ee874a10409532ef4beaeb63c23d6b89b6522467a18083ae2abbb5bb5

[root@localhost opt]# docker ps
CONTAINER ID   IMAGE          COMMAND                 CREATED         STATUS         PORTS                                                                                   NAMES
c2fe4a6ee874   haproxy:v1.0   "/config.sh"            8 seconds ago   Up 7 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8189->8189/tcp, :::8189->8189/tcp, 443/tcp   haproxy
2c56de84ec91   nginx:v0.1     "/start.sh"             5 hours ago     Up 5 hours                                                                                             nginx
c21c60460234   httpd:v0.1     "httpd -D FOREGROUND"   5 hours ago     Up 5 hours     80/tcp, 443/tcp                                                                         httpd

[root@localhost opt]# docker exec -it haproxy /bin/sh
/usr/local/haproxy # cd /tmp/
/tmp # ls
RSS.txt
/tmp # cat RSS.txt 
172.17.0.3
172.17.0.4

页面访问

在这里插入图片描述
在这里插入图片描述
默认账户密码是 admin
在这里插入图片描述
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/5577.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!