Haproxy镜像制作
下载haproxy
/opt/dockerfile/web/nginx-ubuntu# docker pull haproxy:2.1.3
进到haproxy容器
/opt/dockerfile/web/nginx-ubuntu# docker run --rm -it haproxy:2.1.3 bash
查看版本:Debian
b49633f4394:/# cat /etc/issue
Debian GNU/Linux 10 \n \l
#退出
b49633f4394:/# exit
创建目录
/opt/dockerfile/web/nginx-ubuntu# cd /opt/dockerfile/web/
#基于centos做haproxy镜像
/opt/dockerfile/web# mkdir haproxy-centos
/opt/dockerfile/web# cd haproxy-centos
Dockerfile
/opt/dockerfile/web/haproxy-centos# vim Dockerfile
FROM centos-base:7.7.1908
#维护者信息
LABEL maintainer="stao <clark_0932@qq.com>"
#安装基础命令
RUN yum install -y 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
ADD haproxy-2.0.30.tar.gz /usr/local/src/
#编译安装
RUN cd /usr/local/src/haproxy-2.0.30 && 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_haproxy.sh
EXPOSE 80 9999
CMD ["/usr/bin/run_haproxy.sh"]
上传文件 haproxy-2.0.30.tar.gz、HAproxy官网(www.haproxy.org)
/opt/dockerfile/web/haproxy-centos# ll
total 2680
drwxr-xr-x 2 root root 4096 Dec 27 01:18 ./
drwxr-xr-x 8 root root 4096 Dec 27 01:08 ../
-rw-r--r-- 1 root root 341 Dec 27 01:18 Dockerfile
-rw-r--r-- 1 root root 2728444 Dec 27 01:18 haproxy-2.0.30.tar.gz
haproxy.cfg
/opt/dockerfile/web/haproxy-centos# cat 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:123456
listen web_port
bind 0.0.0.0:80
mode http
log global
balance roundrobin
server web1 192.168.37.101:8080 check inter 30000 fall 2 rise 5
server web2 192.168.37.102:8080 check inter 30000 fall 2 rise 5
/opt/dockerfile/web/haproxy-centos# vim run_haproxy.sh
#!/bin/bash
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
tail -f /etc/hosts
添加权限
/opt/dockerfile/web/haproxy-centos# chmod a+x run_haproxy.sh
/opt/dockerfile/web/haproxy-centos# docker build -t haproxy:2.0.30-centos .
#进入容器
/opt/dockerfile/web/haproxy-centos# docker run -it -p 80:80 -p 9999:9999 haproxy:2.0.30-centos
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 c060e66a25cb
新窗口
~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy 2.0.30-centos 323abdc0063a 7 minutes ago 962MB
nginx 1.18.0-ubuntu 2dea393466ff 19 hours ago 384MB
nginx 1.18.0-alpine c7f50cb1e0ed 20 hours ago 245MB
nginx-v1 1.18.0-alpine 8e8dc97f717b 34 hours ago 241MB
tomcat-linux00 app2 36429591d7cf 36 hours ago 1.02GB
tomcat-linux00 app1 c7577cde6aff 37 hours ago 1.02GB
centos-tomcat 8.5.51 2adbba39b781 37 hours ago 1GB
centos-jdk 8u351 932a0c002b15 37 hours ago 988MB
centos-base 7.7.1908 08fec43ccc9f 45 hours ago 612MB
ubuntu 18.04 251b86c83674 2 weeks ago 63.1MB
alpine latest 49176f190c7e 4 weeks ago 7.05MB
haproxy 2.1.3 7dd3479a7f03 2 years ago 92.5MB
centos 7.7.1908 08d05d1d5859 3 years ago 204MB
#运行容器
~# docker run -d -it -p 8080:8080 tomcat-linux00:app1
dc688ed375254966380849508e3be6c1e7dca5deadfbf7a3b166040f425b8f92
再次启动服务
/opt/dockerfile/web/haproxy-centos# docker run -it -p 80:80 -p 9999:9999 haproxy:2.0.30-centos
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 38956d87a4c0
把linux00:app2打包、并拷贝到102
/opt/dockerfile/web/haproxy-centos# docker save tomcat-linux00:app2 > /opt/app2.tar.gz
/opt/dockerfile/web/haproxy-centos# scp /opt/app2.tar.gz 192.168.37.102:/opt/
102
导入镜像
~# docker load -i /opt/app2.tar.gz
~# docker run -d -it -p 8080:8080 tomcat-linux00:app2
101
~# cd /opt/dockerfile/web/nginx-alpine/
/opt/dockerfile/web/nginx-alpine# vim nginx.conf
···
upstream tomcat {
server 192.168.37.101:8080;
server 192.168.37.102:8080;
}
server {
#下面信息要写到server中
location /linux00 {
proxy_pass http://tomcat;
}
···
/opt/dockerfile/web/nginx-alpine# bash build-command.sh
/opt/dockerfile/web/nginx-alpine# docker run -d -it -p 81:80 nginx:1.18.0-alpine
再次把镜像导出、拷贝到102
/opt/dockerfile/web/nginx-alpine# docker save nginx:1.18.0-alpine > /opt/nginx.tar.gz
/opt/dockerfile/web/nginx-alpine# scp /opt/nginx.tar.gz 192.168.37.102:/opt/
102
导入
~# docker load -i /opt/nginx.tar.gz
~# docker run -d -it -p 81:80 nginx:1.18.0-alpine
101
/opt/dockerfile/web/haproxy-centos# pwd
/opt/dockerfile/web/haproxy-centos
/opt/dockerfile/web/haproxy-centos# cat haproxy.cfg
···
#修改以下两行、端口号即可
server web1 192.168.37.101:81 check inter 30000 fall 2 rise 5
server web2 192.168.37.102:81 check inter 30000 fall 2 rise 5
···
/opt/dockerfile/web/haproxy-centos# vim build-command.sh
#!/bin/bash
docker build -t haproxy:2.0.30-centos .
/opt/dockerfile/web/haproxy-centos# bash build-command.sh
/opt/dockerfile/web/haproxy-centos# docker run -it -p 80:80 haproxy:2.0.30-centos
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 68d3043ae032
此时访问可以80端口