dockerfile基于alpine构建haproxy

1. 结构目录

[root@localhost ~]# tree haproxy/
haproxy/
├── dockerfile
└── files
    ├── env.txt
    ├── haproxy-2.5.0.tar.gz
    ├── haproxycfg.sh
    ├── install.sh
    └── sysctl.conf

1 directory, 6 files
[root@localhost ~]# 

2. 编写dockerfile

[root@localhost ~]# cd haproxy/
[root@localhost haproxy]# cat dockerfile 
FROM alpine

LABEL MAINTAINER "pyd 1@2qq.com"

ENV version 2.5.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 haproxy]# 

3. 配置文件

[root@localhost haproxy]# cd files/
[root@localhost files]# ls
env.txt  haproxy-2.5.0.tar.gz  haproxycfg.sh  install.sh  sysctl.conf

//RS ip
[root@localhost files]# cat  env.txt 
RSs=172.17.0.2 172.17.0.3

//内核参数
[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]# 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

//haproxy配置文件
[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]# 

4. 构建镜像

[root@localhost haproxy]# docker build -t haproxy:v1.0 .
.........忽略N行
(1/10) Purging gcc (10.3.1_git20211027-r0)
(2/10) Purging binutils (2.37-r3)
(3/10) Purging libatomic (10.3.1_git20211027-r0)
(4/10) Purging libgomp (10.3.1_git20211027-r0)
(5/10) Purging libgphobos (10.3.1_git20211027-r0)
(6/10) Purging make (4.3-r0)
(7/10) Purging mpc1 (1.2.1-r0)
(8/10) Purging mpfr4 (4.1.0-r0)
(9/10) Purging isl22 (0.22-r0)
(10/10) Purging gmp (6.2.1-r0)
Executing busybox-1.34.1-r3.trigger
OK: 35 MiB in 45 packages
Removing intermediate container 7d2ec6aa8327
 ---> c9813eff9a55
Step 9/9 : ENTRYPOINT /tmp/haproxycfg.sh
 ---> Running in 54c3e78ce2d5
Removing intermediate container 54c3e78ce2d5
 ---> bc383740a37a
Successfully built bc383740a37a
Successfully tagged haproxy:v1.0

[root@localhost haproxy]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
haproxy      v1.0      bc383740a37a   2 minutes ago   85.1MB
alpine       latest    c059bfaa849c   2 weeks ago     5.59MB
[root@localhost haproxy]# 

6. 创建容器

//创建apache和nginx容器
[root@localhost haproxy]# docker run -d --name apache httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
e5ae68f74026: Pull complete 
bc36ee1127ec: Pull complete 
d3576f2b6317: Pull complete 
f1aa5f54b226: Pull complete 
aa379c0cedc2: Pull complete 
Digest: sha256:fba8a9f4290180ceee5c74638bb85ff21fd15961e6fdfa4def48e18820512bb1
Status: Downloaded newer image for httpd:latest
dc5872009f97f546fbd54856d7a40c65ff13c1c60af0122470a18a357cb36cea
[root@localhost haproxy]# docker run -d --name nginx nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
e5ae68f74026: Already exists 
21e0df283cd6: Pull complete 
ed835de16acd: Pull complete 
881ff011f1c9: Pull complete 
77700c52c969: Pull complete 
44be98c0fab6: Pull complete 
Digest: sha256:9522864dd661dcadfd9958f9e0de192a1fdda2c162a35668ab6ac42b465f0603
Status: Downloaded newer image for nginx:latest
1f4fed0191c810938072da3c6bfc70d2a73ea7ff3b4bcbf7144ce6a51896405b
[root@localhost haproxy]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS     NAMES
1f4fed0191c8   nginx     "/docker-entrypoint.…"   23 seconds ago       Up 22 seconds       80/tcp    nginx
dc5872009f97   httpd     "httpd-foreground"       About a minute ago   Up About a minute   80/tcp    apache
[root@localhost haproxy]# 

//查看apache ip
[root@localhost haproxy]# docker inspect apache 
......省略N行
                "NetworkID": "18039c5e5ccec90bd886bf585700422392db62dbd2258ca378cf4eee1abc3f9f",
                    "EndpointID": "00bf38a60b64a858bbd35e445cc76e51ad70c58e6c11504d6c2a991c81b829af",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
......省略N行
//查看nginx ip
[root@localhost haproxy]# docker inspect nginx
......省略N行
                "NetworkID": "18039c5e5ccec90bd886bf585700422392db62dbd2258ca378cf4eee1abc3f9f",
                    "EndpointID": "0c34b972284c0dca798fa15173aee2c2da333b90f484bf161f3f68ab8cff2fbd",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
......省略N行
//填写RS ip
[root@localhost haproxy]# cat files/env.txt 
RSs=172.17.0.2 172.17.0.3
[root@localhost haproxy]# 

//利用haproxyv:1.0镜像创建容器
[root@localhost haproxy]# docker run -d --name haproxy -p 6666:80 --env-file files/env.txt haproxy:v1.0 
dc201555c9810abebb6037319db127d99db72cf64161e1fbefc6c5f3c7eefb78
[root@localhost haproxy]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                   NAMES
dc201555c981   haproxy:v1.0   "/bin/sh -c /tmp/hap…"   8 seconds ago    Up 7 seconds    0.0.0.0:6666->80/tcp, :::6666->80/tcp   haproxy
1f4fed0191c8   nginx          "/docker-entrypoint.…"   10 minutes ago   Up 10 minutes   80/tcp                                  nginx
dc5872009f97   httpd          "httpd-foreground"       10 minutes ago   Up 10 minutes   80/tcp                                  apache
[root@localhost haproxy]# docker port haproxy 
80/tcp -> 0.0.0.0:6666
80/tcp -> :::6666
[root@localhost haproxy]# 

7. 访问

[root@localhost ~]# curl 192.168.8.130:6666
<html><body><h1>It works!</h1></body></html>
[root@localhost ~]# curl 192.168.8.130:6666
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost ~]# 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彭宇栋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值