Docker-Haproxy构建镜像
1、需求
基于基础的 tomcat镜像,运行两个专用的tomcat项目,使用haproxy代理webapp1,webapp2的项目
2、编写Dockerfile
2.1、创建Dockerfile专用目录
mkdir /docker/images/haproxy -p && cd /docker/images/haproxy
2.2、下载软件包
curl -O https://www.haproxy.org/download/2.4/src/haproxy-2.4.2.tar.gz
2.3、定制配置文件haproxy.cfg
cat << 'CAT_END' > haproxy.cfg global chroot /usr/local/haproxy/ maxconn 4096 uid 99 gid 99 daemon nbproc 1 pidfile /usr/local/haproxy/haproxy.pid log 127.0.0.1 local3 info defaults log global mode http option http-keep-alive option forwardfor retries 3 maxconn 4096 timeout connect 120s timeout client 120s timeout server 120s listen status bind 0.0.0.0:9999 mode http log global stats enable stats uri /haproxy-stats stats auth haadmin:123456 listen web_port bind 0.0.0.0:80 mode http log global balance roundrobin server web1 192.168.10.19:8080 check inter 2000 fall 3 rise 5 server web2 192.168.10.19:8081 check inter 2000 fall 3 rise 5 CAT_END
2.4、编写haproxy运行脚本
cat << 'CAT_END' > entrypoint.sh !/bin/bash /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg tail -f /etc/hosts CAT_END
2.5、定制Dockerfile
cat << 'CAT_END' > Dockerfile FROM ubuntu:latest MAINTAINER cyc author ADD haproxy-2.4.2.tar.gz /usr/local/src RUN apt-get update && apt install make gcc build-essential libssl-dev zlib1g-dev libpcre3 libpcre3-dev libsystemd-dev libreadline-dev -y && \ cd /usr/local/src/haproxy-2.4.2 && \ 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-2.4.2 && make install PREFIX=/usr/local/haproxy-2.4.2 && \ ln -s /usr/local/haproxy-2.4.2 /usr/local/haproxy && rm -rf /usr/local/src/haproxy* && \ apt-get autoclean && apt-get clean && apt-get autoremove ADD haproxy.cfg /etc/haproxy/haproxy.cfg ADD entrypoint.sh /root/entrypoint.sh EXPOSE 80 9999 CMD ["bash","/root/entrypoint.sh"] CAT_END
2.6、构建镜像
docker build -t haproxy:v2.4.2 .
3、使用新镜像启动一个容器,查看效果
3.1、启动容器
docker run -d -p 80:80 -p 9999:9999 haproxy:v2.4.2
3.2、查看运行状态
]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0a58f3a11f30 haproxy:v2.4.2 "bash /root/entrypoi…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:9999->9999/tcp, :::9999->9999/tcp sweet_dijkstra b35d16d291e6 tomcat-webapp2:v0.1 "bash /root/entrypoi…" 47 minutes ago Up 47 minutes 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp agitated_blackburn be6fb6f5c9fe tomcat-webapp1:v0.1 "bash /root/entrypoi…" 54 minutes ago Up 54 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp pensive_rhodes
3.3、访问测试
3.4、查看健康检查状态