1. 结构目录
[root@localhost ~]# tree haproxy/
haproxy/
├── dockerfile
└── files
├── haproxy-2.5.0.tar.gz
├── haproxy.cfg
├── install.sh
└── start.sh
1 directory, 5 files
[root@localhost ~]#
[root@localhost ~]# cd haproxy/
[root@localhost haproxy]# ls
dockerfile files
[root@localhost haproxy]# cat dockerfile
FROM centos
LABEL MAINTAINER='pengyudong 1@2.com@qq.com'
ENV version 2.5.0
ADD files/haproxy-${version}.tar.gz /usr/src
ADD files/haproxy.cfg /etc/haproxy/
ADD files/install.sh /tmp/
ADD files/start.sh /usr/local/
RUN ["/bin/bash","-c","/tmp/install.sh"]
EXPOSE 80 8189
WORKDIR /usr/local/haproxy
CMD ["/usr/local/start.sh"]
[root@localhost haproxy]#
[root@localhost haproxy]# cd files/
[root@localhost files]# ls
haproxy-2.5.0.tar.gz haproxy.cfg install.sh start.sh
[root@localhost files]# vim start.sh
[root@localhost files]# cat start.sh
#!/bin/sh
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
/bin/bash
[root@localhost files]# cat 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==2{print $2}' /etc/os-release).repo
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/haproxy-${version}
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-*
[root@localhost files]# cat 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.3:80 check inter 2000 fall 5
server web02 172.17.0.4:80 check inter 2000 fall 5
[root@localhost files]#
2. 构建镜像
[root@localhost ~]# docker build -t pengyudong/haproxy:v1.0 haproxy/
root@localhost ~]# docker images
pengyudong/haproxy v1.0 8819e319cf32 19 minutes ago 418MB
[root@localhost ~]#
3. 创建容器
[root@localhost ~]# docker run -itd --name haproxy -p 8080:80 haproxy:v1.0
[root@localhost ~]# docker exec -it haproxy /bin/bash
[root@ce02ae7f027e haproxy]# ss -antl
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:*
[root@ce02ae7f027e haproxy]#
[root@localhost ~]# docker run -itd --name httpd pengyudong/httpd
462bd9babdcdac7f2abbb6da551858466659602bb3031129bb5b5de6e71853eb
[root@localhost ~]# docker run -itd --name nginx nginx
0dffef28f17692db84e93c7caece9fbfe303d66b042bc1f40f13e0304c09227f
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0dffef28f176 nginx "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 80/tcp nginx
462bd9babdcd pengyudong/httpd "/usr/local/apache/b…" 8 minutes ago Up 8 minutes 80/tcp httpd
ce02ae7f027e pengyudong/haproxy:v1.0 "/usr/bin/start.sh" 22 minutes ago Up 22 minutes 8189/tcp, 0.0.0.0:8080->80/tcp, :::8080->80/tcp haproxy
[root@localhost ~]#