树形结构
[root@localhost ~]# tree /haproxy/
/haproxy/
├── Dockerfile
└── files
├── haproxy-2.4.0.tar.gz
├── haproxy.cfg
├── install.sh
└── start.sh
编写
Dockerfile
[root@localhost ~]# cat /haproxy/Dockerfile
FROM centos
LABEL MAINTAINER='harry1004 2252628021@qq.com'
ENV version 2.4.0
WORKDIR /usr/local/
ADD files/haproxy-${version}.tar.gz /usr/src
ADD files/haproxy.cfg /etc/haproxy/conf/
ADD files/install.sh /opt/
ADD files/start.sh /opt/
RUN ["/bin/bash","-c","/opt/install.sh"]
EXPOSE 80 8189
CMD ["/opt/start.sh"]
haproxy.cfg
[root@localhost ~]# cat /haproxy/files/haproxy.cfg
#--------------全局配置----------------
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
server web01 172.17.0.2:80 check inter 2000 fall 5
server web02 172.17.0.3:80 check inter 2000 fall 5
install.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 -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 $(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 && \
cp haproxy /usr/sbin/
echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
start.sh
[root@localhost ~]# cat /haproxy/files/start.sh
#!/bin/sh
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/conf/haproxy.cfg
/bin/bash
制作haproxy镜像
[root@localhost ~]# docker build -t haproxy:v1.0 /haproxy/
Removing intermediate container 18cbfe41f7b4
---> 0a26b5290d03
Step 10/11 : EXPOSE 80 8189
---> Running in f4908f8df88c
Removing intermediate container f4908f8df88c
---> f7094ba823a4
Step 11/11 : CMD ["/opt/start.sh"]
---> Running in 6a2bcda74609
Removing intermediate container 6a2bcda74609
---> 78e89178e709
Successfully built 78e89178e709
Successfully tagged haproxy:v1.0
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy v1.0 1fd78a36c18a About a minute ago 578MB
harry1004/httpd v1.0 1717eddae199 50 minutes ago 702MB
busybox latest ffe9d497c324 3 days ago 1.24MB
harry1004/nginx v0.2 1afdce5b37fc 8 days ago 549MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost ~]# docker run -itd --name haproxy -p 2000:80 haproxy:v1.0
00f7ad4d8212e9bffbecf5e0c678dc8195860aa58eb6342d6cc8e1cfd45e4357
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
00f7ad4d8212 haproxy:v1.0 "/opt/start.sh" About a minute ago Up About a minute 8189/tcp, 0.0.0.0:2000->80/tcp, :::2000->80/tcp haproxy
be5ed37ec6ce harry1004/httpd:v1.0 "/usr/local/apache/b…" 50 minutes ago Up 50 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp httpd
dac3c1c718b5 1afdce5b37fc "/start.sh" 58 minutes ago Up 58 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp centos02
[root@00f7ad4d8212 local]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8189 0.0.0.0:*
访问效果