Dockerfile使用alpine系统写haproxy

Dockerfile使用alpine系统写haproxy,第1张

Dockerfile使用alpine系统写haproxy

文章目录
    • 创建httpd、nginx容器
    • 目录结构
    • Dockerfile文件
    • 安装脚本
    • 传输配置文件和启动服务的脚本
    • 创建haproxy容器
    • 页面访问

创建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:  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:  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:  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:  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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5659484.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存