[root@localhost docker]# tree haproxy/
haproxy/
├── conf
│ └── RSS
├── Dockerfile
└── files
├── haproxy-2.4.9.tar.gz
├── haproxy.cfg
├── install.sh
├── rsyslog.conf
├── start.sh
└── sysctl.conf
编写dockerfile
[root@localhost docker]# vim haproxy/Dockerfile
FROM centos
LABEL MAINTAINER='neawalke 123456789@qq.com'
ENV version 2.4.9
ENV web1_ip ""
ADD files/haproxy-${version}.tar.gz /usr/src/
ADD files/haproxy.cfg /etc/haproxy/
ADD files/rsyslog.conf /etc/
ADD files/start.sh /usr/bin/
ADD files/sysctl.conf /etc/
ADD files/install.sh /tmp/
ADD conf/RSS /tmp/RSS/
RUN ["/bin/bash","-c","/tmp/install.sh"]
EXPOSE 80 8189
CMD ["/usr/bin/start.sh"]
提供执行安装脚本
[root@localhost docker]# vim 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 list all &&
yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel &&
useradd -r -M -s /sbin/nologin haproxy &&
cd /usr/src/haproxy-${version} && make clean &&
make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_PCRE=1 USE_SYSTEMD=1 &&
make install PREFIX=/usr/local/haproxy &&
yum -y remove make gcc gcc-c++ &&
rm -rf /usr/src/{haproxy-${version}.tar.gz, haproxy-${version}}
[root@localhost docker]# chmod +x haproxy/files/install.sh
#启动容器脚本
[root@localhost docker]# vim haproxy/files/start.sh
#!/bin/bash
IFS=$'n'
OLDIFS="$IFS"
for RS in $(cat /tmp/RSS/RSS)
do
echo " server ${RS}:80 check inter 2000 fall 5" >> /etc/haproxy/haproxy.cfg
done
/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg
[root@localhost docker]# cat haproxy/conf/RSS
web01 172.17.0.3
web02 172.17.0.4
构建镜像
[root@localhost docker]# docker build -t neawalke/haproxy:v2.0 haproxy/
Sending build context to Docker daemon 3.618MB
Step 1/12 : FROM centos
---> 5d0da3dc9764
Step 2/12 : LABEL MAINTAINER='neawalke 123456789@qq.com'
---> Using cache
---> b55330fdb823
Step 3/12 : ENV version 2.4.9
---> Using cache
---> 1cff2a4d16cf
Step 4/12 : ADD files/haproxy-${version}.tar.gz /usr/src/
---> Using cache
---> 35dc3f973b92
Step 5/12 : ADD files/haproxy.cfg /etc/haproxy/
---> Using cache
---> fd4556794da8
Step 6/12 : ADD files/rsyslog.conf /etc/
---> Using cache
---> 6b407163413f
Step 7/12 : ADD files/start.sh /usr/bin/
---> Using cache
---> 2e5e72137472
Step 8/12 : ADD files/sysctl.conf /etc/
---> Using cache
---> 445cb99df008
Step 9/12 : ADD files/install.sh /tmp/
---> Using cache
---> 4913b8031adf
Step 10/12 : RUN ["/bin/bash","-c","/tmp/install.sh"]
---> Using cache
---> aacaf4bbdecb
Step 11/12 : EXPOSE 80 8189
---> Using cache
---> e4e1d9864614
Step 12/12 : CMD ["/usr/bin/start.sh"]
---> Using cache
---> e0cc01fedede
Successfully built e0cc01fedede
Successfully tagged neawalke/haproxy:v2.0
通过haproxy镜像启动容器
[root@localhost docker]# docker run -it --name web --rm -v /docker/haproxy/conf:/tmp/RSS neawalke/haproxy:v2.0 [NOTICE] (8) : New worker #1 (10) forked [WARNING] (10) : Server webcluster/web01 is DOWN, reason: Layer4 timeout, check duration: 2002ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue. [WARNING] (10) : Server webcluster/web02 is DOWN, reason: Layer4 timeout, check duration: 2003ms. 0 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue. [NOTICE] (10) : haproxy version is 2.4.9-f8dcd9f [NOTICE] (10) : path to executable is /usr/local/haproxy/sbin/haproxy [alert] (10) : proxy 'webcluster' has no server available!
启动两台测试页面容器
[root@localhost ~]# docker run -it --rm neawalke/httpd:latest AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message [root@localhost ~]# docker run -it --rm neawalke/nginx:v2.0
访问效果
[root@localhost docker]# curl 172.17.0.2It works!
[root@localhost docker]# curl 172.17.0.2Welcome to nginx! Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.Thank you for using nginx.



