结构目录
[root@localhost ~]# tree haproxy/
haproxy/
├── Dockerfile
└── files
├── haproxy-2.4.8.tar.gz
├── haproxy.cfg
├── install.sh
├── run_haproxy.sh
└── sysctl.conf
[root@localhost ~]# mkdir haproxy
[root@localhost haproxy]# mkdir files
[root@localhost haproxy]# vim Dockerfile
[root@localhost haproxy]# cat Dockerfile
FROM centos
LABEL MAINTAINER="yaya 1870648704 @qq.com"
ENV version 2.4.8
WORKDIR /usr/local/
ADD files/haproxy-${version}.tar.gz /usr/src
ADD files/install.sh /usr/local
ADD files/haproxy.cfg /etc/haproxy/
ADD files/run_haproxy.sh /usr/local
ADD files/sysctl.conf /etc/
RUN ["/bin/bash","-c","/usr/local/install.sh"]
EXPOSE 80 8189
CMD ["/usr/local/run_haproxy.sh"]
[root@localhost haproxy]# cd files/
[root@localhost files]# ll
总用量 3516
-rw-r--r-- 1 root root 3599555 12月 10 21:02 haproxy-2.4.8.tar.gz
[root@localhost files]# vim install.sh
[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 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/* /var/cache/*
[root@localhost files]#
[root@localhost files]# cat sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
[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
制作haproxy镜像
[root@localhost haproxy]# docker build -t weixiaoya/haproxy:v0.1 haproxy/
Sending build context to Docker daemon 3.609MB
Step 1/12 : FROM centos
---> 5d0da3dc9764
Step 2/12 : LABEL MAINTAINER="yaya 1870648704 @qq.com"
---> Running in 26b8ff9b8ceb
Removing intermediate container 26b8ff9b8ceb
---> 51deb2caf18b
Step 3/12 : ENV version 2.4.8
---> Running in f433067e9ae9
Removing intermediate container f433067e9ae9
---> 7929ed7010f7
Step 4/12 : WORKDIR /usr/local/
---> Running in 643c1758d996
Removing intermediate container 643c1758d996
---> 8a7c14d4f5b7
Step 5/12 : ADD files/haproxy-${version}.tar.gz /usr/src
······················
---> Running in f01ab7688994
Removing intermediate container f01ab7688994
---> 72c546d2fe87
Successfully built 72c546d2fe87
Successfully tagged weixiaoya/haproxy:v0.1
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
weixiaoya/haproxy v0.1 72c546d2fe87 About a minute ago 390MB
weixiaoya/httpd latest 0315b95b2dad 42 hours ago 701MB
weixiaoya/yaya v0.1 835f5837f57d 46 hours ago 231MB
weixiaoya/httpd v0.2 230991a0d197 3 days ago 701MB
weixiaoya/httpd v0.1 24270cd5ab1a 4 days ago 712MB
nginx latest f652ca386ed1 8 days ago 141MB
busybox latest d23834f29b38 11 days ago 1.24MB
centos latest 5d0da3dc9764 2 months ago 231MB
创建容器
[root@localhost ~]# docker run -itd --name haproxy -p 8080:80 72c546d2fe87
911034dfd352f5d969a236730bf4a7f82ffa10b05dabd700588a18b10c04083c
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
911034dfd352 72c546d2fe87 "/usr/local/run_hapr…" 5 seconds ago Up 3 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp haproxy
[root@localhost haproxy]# docker exec -it haproxy /bin/bash
[root@911034dfd352 ~]# 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
[root@localhost haproxy]# docker run -itd --name httpd 0315b95b2dad
636b41c7296b518a13b23b18b03a82887e2fa355b69e0a442e70a85f98d158b7
[root@localhost haproxy]# docker run -itd --name nginx nginx
40608264bc298da20ba7260713bea44d9f9dd88017441e113a7806f369f0e51e
[root@localhost haproxy]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
40608264bc29 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp nginx
0315b95b2dad httpd "/usr/local/apache/b…" 4 minutes ago Up 4 minutes 80/tcp, 443/tcp httpd
911034dfd352 72c546d2fe87 "/usr/local/run_hapr…" 19 minutes ago Up 19 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp haproxy
访问测试