准备两台web测试容器
[root@localhost ~]# docker run -d --name httpd01 httpd
[root@localhost ~]# docker run -d --name nginx01 nginx
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4bfa1460b519 httpd "httpd-foreground" 8 hours ago Up 17 minutes 80/tcp httpd01
6ef1eb31ccc1 nginx "/docker-entrypoint.…" 8 hours ago Up 17 minutes 80/tcp nginx01
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4bfa1460b519 httpd "httpd-foreground" 8 hours ago Up 18 minutes 80/tcp httpd01
6ef1eb31ccc1 nginx "/docker-entrypoint.…" 8 hours ago Up 18 minutes 80/tcp nginx01
#查看web容器ip
[root@localhost ~]# docker inspect httpd01
....
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "4de407f35a9d087fb19cc8d41c59cffb38428758b65720a439b25854efe564a6",
"EndpointID": "fa39faa24a01a4b45e25eaeffb5b25c63cee4e7b8df3c89abf4d977ad21269c6",
"Gateway": "192.168.1.1",
"IPAddress": "192.168.1.2",
"IPPrefixLen": 24,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:c0:a8:01:02",
"DriverOpts": null
....
[root@localhost ~]# docker inspect nginx01
....
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "4de407f35a9d087fb19cc8d41c59cffb38428758b65720a439b25854efe564a6",
"EndpointID": "f9e25b56f6fe886a4042a3b319f642af9e2ecadbb3bcd932de0e3005dbf8a65b",
"Gateway": "192.168.1.1",
"IPAddress": "192.168.1.3",
"IPPrefixLen": 24,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:c0:a8:01:03",
"DriverOpts": null
#本地访问测试
[root@localhost ~]# curl 192.168.1.2
<html><body><h1>It works!</h1></body></html>
[root@localhost ~]# curl 192.168.1.3
<!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 haproxy]# tree
.
├── Dockerfile
└── files
├── haproxy-2.4.0.tar.gz
└── start.sh
1 directory, 3 files
[root@localhost ~]# cd haproxy/
[root@localhost haproxy]# cat Dockerfile
FROM centos
ENV PATH /usr/local/haproxy/sbin:$PATH
ENV version 2.4.0
COPY files /usr/src
ADD files/start.sh /root/
RUN useradd -r -M -s /sbin/nologin haproxy && \
yum -y install openssl make gcc pcre-devel bzip2-devel openssl-devel systemd-devel make && \
cd /usr/src && tar xf haproxy-${version}.tar.gz && cd /usr/src/haproxy-$version && \
make clean && \
make -j $(grep 'processor' /proc/cpuinfo |wc -l) \
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 && \
mkdir /etc/haproxy && \
yum -y remove gcc gcc-c++ make && \
rm -rf /usr/src/* /var/cache/*
EXPOSE 80 8189
CMD /root/start.sh $ips
[root@localhost haproxy]# cat files/start.sh
#!/bin/bash
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
i=0
while [ $# -gt 0 ]; do
let i++
cat >> /etc/haproxy/haproxy.cfg << EOF
server web0$i ${1}:80 check inter 2000 fall 5
EOF
shift;
done
/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid && \
rm -f /root/start.sh
#生成镜像
[root@localhost haproxy]# pwd
/root/haproxy
[root@localhost haproxy]# docker build -t jiejiehao/haproxy:v1 /root/haproxy/
[root@localhost haproxy]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
jiejiehao/haproxy v1 86469fcbf43d 18 minutes ago 379MB
#生成容器
[root@localhost haproxy]# docker run -d --name haproxy -e ips="192.168.1.2 192.168.1.3" -p 80:80 -p 8189:8189 jiejiehao/haproxy:v1
daabc0a2a0bfc3c24c9712a9567f2238e9470be83fc776ff51b6000a3e6d34ae
[root@localhost haproxy]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
daabc0a2a0bf jiejiehao/haproxy:v1 "/bin/sh -c '/root/s…" 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8189->8189/tcp, :::8189->8189/tcp haproxy
4bfa1460b519 httpd "httpd-foreground" 8 hours ago Up 13 minutes 80/tcp httpd01
6ef1eb31ccc1 nginx "/docker-entrypoint.…" 8 hours ago Up 13 minutes 80/tcp nginx01
测试
访问本机,不断刷新页面
登录 haproxy web界面
http:ip:8189/haproxy_stats
用户名和密码都是admin