1、需求:
使用haproxy代理两个服务(web服务或者是mysql服务)
2、编写Dockerfile
2.1、创建Dockerfile专用目录
mkdir /docker/images/haproxy -p && cd /docker/images/haproxy
2.2、下载软件包到Dockerfile专用目录
curl -O https://www.haproxy.org/download/2.4/src/haproxy-2.4.2.tar.gz
或者下载这个 https://download.csdn.net/download/cyw8998/89069508
2.3、定制配置文件haproxy.cfg
bash
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.0.9:8090 check inter 2000 fall 3 rise 5
server web2 192.168.0.9:8090/Zm check inter 2000 fall 3 rise 5
listen mysql-cluster
bind 0.0.0.0:3306
mode tcp
balance roundrobin
option mysql-check
server mysql1 192.168.0.9:13306 check weight 2 inter 2000 rise 2 fall 5
server mysql2 192.168.0.9:13307 check weight 2 inter 2000 rise 2 fall 5
CAT_END
2.4、编写haproxy运行脚本
bash
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
bash
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 3306 9999
CMD ["bash","/root/entrypoint.sh"]
CAT_END
2.6、构建镜像
docker build -t haproxy:v2.4.2 .
3.1、启动容器
docker run -d -p 13301:3306 -p 9999:9999 haproxy:v2.4.2