基于Alpine 编写Haproxy的Dockerfile

基于Centos编写Haproxy的Dockerfile

//创建dockerfile文件目录以及脚本文件
[root@localhost ~]# mkdir -p haproxy/files
[root@localhost ~]# ls haproxy/
files
[root@localhost ~]# touch haproxy/Dockerfile
[root@localhost ~]# touch haproxy/files/install.sh
[root@localhost ~]# touch haproxy/entrypoint.sh

//项目结构
[root@localhost ~]# tree haproxy/
haproxy/
|-- Dockerfile
|-- entrypoint.sh
`-- files
    |-- haproxy-2.4.0.tar.gz
    `-- install.sh

1 directory, 4 files

//添加脚本权限
[root@localhost ~]# chmod +x haproxy/entrypoint.sh 
[root@localhost ~]# chmod +x haproxy/files/install.sh

[root@localhost ~]# ls -l haproxy/entrypoint.sh 
-rwxr-xr-x. 1 root root 1624 Dec 12 18:21 haproxy/entrypoint.sh
[root@localhost ~]# ls -l haproxy/files/install.sh 
-rwxr-xr-x. 1 root root 1032 Dec 12 18:16 haproxy/files/install.sh

//结构(在根目录下)
[root@localhost /]# tree haproxy_config/
haproxy_config/
`-- RSs.txt

0 directories, 1 file

[root@localhost /]# cat haproxy_config/RSs.txt 
172.17.0.3
172.17.0.4
172.17.0.5
172.17.0.6

//创建两台装容器(一台httpd,一台nginx,用来测试)
[root@localhost ~]# docker run --name web1 -d httpd 
e84f1c748e99d47bbf856a5403ec1cb3bb301e6a98daaa0d1e32f786699823a1

[root@localhost ~]# docker inspect web05
                    "Gateway": "172.17.0.2",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:05",
                    "DriverOpts": null
                }
            }
        }
    }
]


[root@localhost ~]# docker run --name web2 -d nginx 
4d9789e30cacde1baa2bbe45d9141a085dc8e7213bb0ee7c33712dd8a645088b
[root@localhost ~]# docker inspect web06

                    "IPAddress": "172.17.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:06",
                    "DriverOpts": null
                }
            }
        }
    }
]

//测试一下
[root@localhost ~]# curl 172.17.0.3
<html><body><h1>It works!</h1></body></html>


[root@localhost ~]# curl 172.17.0.4
<!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>


//编写dockerfile
[root@localhost ~]# cat haproxy/Dockerfile 
FROM centos
LABEL MAINTAINER='1314444 123@qq.com'

ENV version 2.4.0
ENV PATH /usr/local/haproxy/sbin:$PATH
COPY files /usr/src/
COPY entrypoint.sh /
RUN ["/bin/bash","-c","/usr/src/install.sh"]

EXPOSE 80 8189
WORKDIR /usr/local/haproxy
ENTRYPOINT ["/entrypoint.sh"]



//编写安装脚本
[root@localhost ~]# cat haproxy/files/install.sh 
#!/bin/bash
rm -rf /etc/yum.repos.d/*  
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-$(awk -F'"' 'NR==5{print $2}'  /etc/os-release).repo 
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo 
yum clean all && yum makecache 
echo "alias ls='ls --color'" >> ~/.bashrc
source ~/.bashrc
yum -y install make gcc gcc-c++ pcre-devel bzip2-devel openssl-devel systemd-devel
useradd -r -M -s /sbin/nologin haproxy 
cd /usr/src
tar xf haproxy-${version}.tar.gz
cd haproxy-${version}  
make clean 
make -j $(nproc)  \
    TARGET=linux-glibc  \
    USE_OPENSSL=1  \
    USE_ZLIB=1  \
    USE_PCRE=1  \
    USE_SYSTEMD=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
sysctl  -p
mkdir  /usr/local/haproxy/conf
echo 'local0.* /var/log/haproxy.log' >>  /etc/rsyslog.conf
yum -y remove gcc gcc-c++ make
rm -rf /usr/src/*  /var/cache/*


//编写开启脚本
[root@localhost ~]# cat haproxy/entrypoint.sh 
#!/bin/bash

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_ip in $(cat /tmp/RSs.txt);do
cat >> /usr/local/haproxy/conf/haproxy.cfg << EOF
    server web$count $rs_ip:80 check inter 2000 fall 5
EOF
let count++
done
haproxy -f /usr/local/haproxy/conf/haproxy.cfg -db

//构建haproxy镜像
[root@localhost ~]# docker build -t 1314444/haproxy:v0.1 haproxy
Sending build context to Docker daemon  3.604MB
Step 1/10 : FROM centos
 ---> 5d0da3dc9764
Step 2/10 : LABEL MAINTAINER='1314444 123@qq.com'
 ---> Using cache
 ---> a8b67caa2102
Step 3/10 : ENV version 2.4.0
 ---> Using cache
 ---> c48a871bda67
Step 4/10 : ENV PATH /usr/local/haproxy/sbin:$PATH
 ---> Using cache
 ---> df0ccbe70aba
Step 5/10 : COPY files /usr/src/
 ---> Using cache
 ---> a8b7c9abce9e
Step 6/10 : COPY entrypoint.sh /
 ---> Using cache
 ---> 1cd3b00edf13
Step 7/10 : RUN ["/bin/bash","-c","/usr/src/install.sh"]
 ---> Using cache
 ---> 89c76dd3e051
Step 8/10 : EXPOSE 80 8189
 ---> Using cache
 ---> af832f2ffd4a
Step 9/10 : WORKDIR /usr/local/haproxy
 ---> Using cache
 ---> 66adeda8f354
Step 10/10 : ENTRYPOINT ["/entrypoint.sh"]
 ---> Using cache
 ---> f50226955caa
Successfully built f50226955caa
Successfully tagged 1314444/haproxy:v0.1


//查看镜像
[root@localhost ~]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED              SIZE
1314444/haproxy   v0.1      f50226955caa   3 minutes ago        381MB

//基于新镜像创建haproxy容器
[root@localhost ~]# docker run -d --name haproxy -p 80:80 -p 8189:8189 -v /haproxy_config/:/tmp 1314444/haproxy:v0.1
9bfc78e5dc8c30892f46beb6599d8e4a79843c107310a1ca892d3eea1bd54258

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                                                                          NAMES
504afeb4a2e3   nginx       "/docker-entrypoint.…"   24 minutes ago   Up 24 minutes   80/tcp                                                                         web2
94447b9531c3   httpd       "httpd-foreground"       24 minutes ago   Up 24 minutes   80/tcp                                                                         web1
9bfc78e5dc8c   1314444/haproxy:v0.1   "/entrypoint.sh"         23 seconds ago   Up 22 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8189->8189/tcp, :::8189->8189/tcp   haproxy


[root@localhost ~]# docker exec -it haproxy /bin/bash
[root@9bfc78e5dc8c haproxy]# pwd
/usr/local/haproxy
[root@9bfc78e5dc8c haproxy]# ss -anlt
State           Recv-Q          Send-Q                   Local Address:Port                   Peer Address:Port          Process          
LISTEN          0               128                            0.0.0.0:8189                        0.0.0.0:*                              
LISTEN          0               128                            0.0.0.0:80                          0.0.0.0:*                              

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

用户:admin
密码:admin

在这里插入图片描述
http://IP:8189/haproxy_stats
在这里插入图片描述
上传镜像仓库

[root@localhost ~]# docker login
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@localhost ~]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED        SIZE
1314444/haproxy   v0.1      f50226955caa   2 hours ago    381MB

[root@localhost ~]# docker push 1314444/haproxy:v0.1 
The push refers to repository [docker.io/1314444/haproxy]
888b23fa1cd4: Pushed 
1454c74ea402: Pushed 
e573f0878e77: Pushed 
74ddd0ec08fa: Mounted from 1314444/httpd 
v0.1: digest: sha256:92fb4f3019f78407e6af14ea3f3f7873c5b2dbca1f13ef837ed325220707ce23 size: 1159

基于Alpine 编写Haproxy的Dockerfile(精简版)

//创建dockerfile文件目录以及脚本文件
[root@localhost ~]# mkdir -p  haproxy/files
[root@localhost ~]# ls haproxy/
files
[root@localhost ~]# touch haproxy/Dockerfile
[root@localhost ~]# touch haproxy/files/install.sh
[root@localhost ~]# touch haproxy/entrypoint.sh

//项目结构
[root@localhost ~]# tree haproxy/
haproxy/
|-- Dockerfile
|-- entrypoint.sh
`-- files
    |-- haproxy-2.4.0.tar.gz
    `-- install.sh

1 directory, 4 files

//添加脚本权限
[root@localhost ~]# chmod +x haproxy/entrypoint.sh 
[root@localhost ~]# chmod +x haproxy/files/install.sh

[root@localhost ~]# ls -l haproxy/entrypoint.sh 
-rwxr-xr-x. 1 root root 1624 Dec 12 18:21 haproxy/entrypoint.sh
[root@localhost ~]# ls -l haproxy/files/install.sh 
-rwxr-xr-x. 1 root root 1032 Dec 12 18:16 haproxy/files/install.sh

//创建两台装容器(一台httpd,一台nginx,用来测试)
[root@localhost ~]# docker run --name web1 -d httpd 
e84f1c748e99d47bbf856a5403ec1cb3bb301e6a98daaa0d1e32f786699823a1

[root@localhost ~]# docker inspect web05
                    "Gateway": "172.17.0.2",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:05",
                    "DriverOpts": null
                }
            }
        }
    }
]


[root@localhost ~]# docker run --name web2 -d nginx 
4d9789e30cacde1baa2bbe45d9141a085dc8e7213bb0ee7c33712dd8a645088b
[root@localhost ~]# docker inspect web06

                    "IPAddress": "172.17.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:06",
                    "DriverOpts": null
                }
            }
        }
    }
]

//测试一下
[root@localhost ~]# curl 172.17.0.3
<html><body><h1>It works!</h1></body></html>


[root@localhost ~]# curl 172.17.0.4
<!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>


//编写dockerfile
[root@localhost ~]# cat haproxy/Dockerfile 
FROM alpine
LABEL MAINTAINER='1314444 123@qq.com'
ENV version 2.4.0
ENV PATH /usr/local/haproxy/sbin:$PATH
COPY files/ /tmp/
COPY entrypoint.sh /
RUN /tmp/install.sh
EXPOSE 80 8189
WORKDIR /usr/local/haproxy
ENTRYPOINT ["/entrypoint.sh"]


//编写安装脚本
[root@localhost ~]# 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/
tar xf haproxy-${version}.tar.gz
cd haproxy-${version}  
make clean 
make -j $(nproc)  \
    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 gcc make
rm -rf /tmp/*  /var/cache/*


//编写开启脚本
[root@localhost ~]# cat haproxy/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_ip in $(cat /tmp/RSs.txt);do
cat >> /usr/local/haproxy/conf/haproxy.cfg << EOF
    server web$count $rs_ip:80 check inter 2000 fall 5
EOF
let count++
done
haproxy -f /usr/local/haproxy/conf/haproxy.cfg -db

//构建haproxy镜像
[root@localhost ~]# docker build -t 1314444/haproxy:v0.2 haproxy
Sending build context to Docker daemon  3.604MB
Step 1/10 : FROM centos
 ---> 5d0da3dc9764
Step 2/10 : LABEL MAINTAINER='1314444 123@qq.com'
 ---> Using cache
 ---> a8b67caa2102
Step 3/10 : ENV version 2.4.0
 ---> Using cache
 ---> c48a871bda67
Step 4/10 : ENV PATH /usr/local/haproxy/sbin:$PATH
 ---> Using cache
 ---> df0ccbe70aba
Step 5/10 : COPY files /usr/src/
 ---> Using cache
 ---> a8b7c9abce9e
Step 6/10 : COPY entrypoint.sh /
 ---> Using cache
 ---> 1cd3b00edf13
Step 7/10 : RUN ["/bin/bash","-c","/usr/src/install.sh"]
 ---> Using cache
 ---> 89c76dd3e051
Step 8/10 : EXPOSE 80 8189
 ---> Using cache
 ---> af832f2ffd4a
Step 9/10 : WORKDIR /usr/local/haproxy
 ---> Using cache
 ---> 66adeda8f354
Step 10/10 : ENTRYPOINT ["/entrypoint.sh"]
 ---> Using cache
 ---> f50226955caa
Successfully built f50226955caa
Successfully tagged 1314444/haproxy:v0.2

//结构(在根目录下)
[root@localhost /]# tree haproxy_config/
haproxy_config/
`-- RSs.txt

0 directories, 1 file

//指定有多少个RS
[root@localhost /]# cat haproxy_config/RSs.txt 
172.17.0.3
172.17.0.4
172.17.0.5
172.17.0.6
//查看镜像
[root@localhost ~]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED              SIZE
1314444/haproxy   v0.2		26c06bc7a73e   About a minute ago   54.1MB
1314444/haproxy   v0.1      f50226955caa   3 minutes ago        381MB

//基于新镜像创建haproxy容器
[root@localhost ~]# docker run -d --name haproxy -p 80:80 -p 8189:8189 -v /haproxy_config/:/tmp 1314444/haproxy:v0.2
01c48ac2db525d9e0834f2da9c47b24a9008eb3926ff78c7b2fc458f948ce786

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                                                                          NAMES
4654204f99cb   nginx       "/docker-entrypoint.…"   29 minutes ago   Up 29 minutes   80/tcp                                                                         web2
ddc140e4adfa   httpd       "httpd-foreground"       29 minutes ago   Up 29 minutes   80/tcp                                                                         web1
01c48ac2db52   1314444/haproxy:v0.2   "/entrypoint.sh"         23 seconds ago   Up 22 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8189->8189/tcp, :::8189->8189/tcp   haproxy


[root@localhost ~]# docker exec -it haproxy /bin/sh
[root@01c48ac2db52 haproxy]# pwd
/usr/local/haproxy
[root@01c48ac2db52 haproxy]#apk add iproute2
[root@01c48ac2db52 haproxy]# ss -anlt
State           Recv-Q          Send-Q                   Local Address:Port                   Peer Address:Port          Process          
LISTEN          0               128                            0.0.0.0:8189                        0.0.0.0:*                              
LISTEN          0               128                            0.0.0.0:80                          0.0.0.0:*                              

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值