制作 haproxy 镜像:
- 进入目录
mkdir -pv /opt/dockerfile/app/haproxy
cd /opt/dockerfile/app/haproxy
- 准备 Dockerfile:
vim Dockerfile
#Haproxy Base Image
FROM centos
ADD haproxy-2.0.5.tar.gz /usr/local/src/
RUN yum install gcc gcc-c++ glibc glibc-devel pcre \
pcre-devel openssl openssl-devel systemd-devel \
net-tools vim iotop bc zip unzip zlib-devel lrzsz \
tree screen lsof tcpdump wget ntpdate –y \
&& cd /usr/local/src/haproxy-2.0.5 \
&& make ARCH=x86_64 TARGET=linux-glibc \
USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 \
USE_SYSTEMD=1 USE_CPU_AFFINITY=1 \
PREFIX=/usr/local/haproxy \
&& make install PREFIX=/usr/local/haproxy \
&& cp haproxy /usr/sbin/ \
&& mkdir /usr/local/haproxy/run
ADD haproxy.cfg /etc/haproxy/
ADD run_haproxy.sh /usr/bin
RUN chmod +x /usr/bin/run_haproxy.sh
EXPOSE 80 9999
CMD ["/usr/bin/run_haproxy.sh"]
-
准备 haproxy 源码文件:
haproxy-2.0.5.tar.gz
-
准备
run_haproxy.sh
脚本
vim run_haproxy.sh
#!/bin/bash
haproxy -f /etc/haproxy/haproxy.cfg
tail -f /etc/hosts
- 准备 haproxy 配置文件:
vim haproxy.cfg
global
chroot /usr/local/haproxy
#stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
uid 99
gid 99
daemon
nbproc 1
pidfile /usr/local/haproxy/run/haproxy.pid
log 127.0.0.1 local3 info
defaults
option http-keep-alive
option forwardfor
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
listen stats
mode http
bind 0.0.0.0:9999
stats enable
log global
stats uri /haproxy-status
stats auth haadmin:q1w2e3r4ys
listen web_port
bind 0.0.0.0:80
mode http
log global
balance roundrobin
server web1 192.168.99.21:8888 check inter 3000 fall 2 rise 5
server web2 192.168.99.22:8889 check inter 3000 fall 2 rise 5
- 准备构建脚本:
docker build -t haproxy:v1 .
- 从镜像启动容器:
docker run -it -d -p 80:80 -p 9999:9999 haproxy:v1
-
web 访问验证
-
访问tomcat业务app1