基于Alpine 编写Haproxy的Dockerfile

Posted 1314444

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于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==5print $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以上是关于基于Alpine 编写Haproxy的Dockerfile的主要内容,如果未能解决你的问题,请参考以下文章

Docker下alpine镜像安装Latex工具心得

无法在基于 Alpine 的 Docker 容器中运行 Play 框架应用程序

Docker inside Docker 基于 Alpine Linux

基于alpine java的docker容器查看堆栈信息

为 Spring Boot 应用程序基于“openjdk:8-jdk-alpine”构建 docker 映像时无法运行“./mvnw clean install”

docker制作nginx+nginx-module-vts镜像基于alpine镜像