自定义haproxy镜像
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
本篇博客基于自定义的CentOS镜像来自定义haproxy服务(编译安装笔记https://www.cnblogs.com/yinzhengjie/p/12113144.html)的。
一.编写Dockerfile相关文件
1>.编写编译脚本
[root@docker101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/dockerfile/web/haproxy total 4 -rw-r--r-- 1 root root 467 Jan 24 11:01 build-command.sh [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# chmod +x /yinzhengjie/softwares/dockerfile/web/haproxy/build-command.sh [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/dockerfile/web/haproxy total 4 -rwxr-xr-x 1 root root 467 Jan 24 11:01 build-command.sh [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/dockerfile/web/haproxy/build-command.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2020-01-18 #FileName: docker-build.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: Build jdk base Script #Copyright (C): 2020 All rights reserved #******************************************************************** docker image build -t centos-haproxy:v1.8.20 . [root@docker101.yinzhengjie.org.cn ~]#
2>.下载haproxy安装包
[root@docker101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/dockerfile/web/haproxy/ total 4 -rwxr-xr-x 1 root root 467 Jan 24 11:01 build-command.sh [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# ll total 2036 -rw-r--r-- 1 root root 2083917 Jan 24 06:12 haproxy-1.8.20.tar.gz [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# cp haproxy-1.8.20.tar.gz /yinzhengjie/softwares/dockerfile/web/haproxy/ [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/dockerfile/web/haproxy/ total 2040 -rwxr-xr-x 1 root root 467 Jan 24 11:01 build-command.sh -rw-r--r-- 1 root root 2083917 Jan 24 11:05 haproxy-1.8.20.tar.gz [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]#
3>.编写haproxy的配置文件
[root@docker101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/dockerfile/web/haproxy/haproxy.cfg -rw-r--r-- 1 root root 796 Jan 24 12:11 /yinzhengjie/softwares/dockerfile/web/haproxy/haproxy.cfg [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/dockerfile/web/haproxy/haproxy.cfg global maxconn 100000 chroot /yinzhengjie/softwares/haproxy stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin uid 99 gid 99 daemon pidfile /yinzhengjie/softwares/haproxy/run/haproxy.pid defaults option http-keep-alive option forwardfor option redispatch option abortonclose maxconn 100000 mode http timeout connect 300000ms timeout client 300000ms timeout server 300000ms listen status_page bind 0.0.0.0:8888 stats enable stats uri /haproxy-status stats auth admin:yinzhengjie stats realm "Welcome to the haproxy load balancer status page of YinZhengjie" listen WEB_PORT_80 bind 0.0.0.0:80 mode http server web01 172.200.3.101:8080 check inter 3000 fall 3 rise 5 [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]#
4>.编写运行haproxy的脚本
[root@docker101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/dockerfile/web/haproxy/run_haproxy.sh -rw-r--r-- 1 root root 833 Jan 24 11:32 /yinzhengjie/softwares/dockerfile/web/haproxy/run_haproxy.sh [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# chmod +x /yinzhengjie/softwares/dockerfile/web/haproxy/run_haproxy.sh [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/dockerfile/web/haproxy/run_haproxy.sh -rwxr-xr-x 1 root root 833 Jan 24 11:32 /yinzhengjie/softwares/dockerfile/web/haproxy/run_haproxy.sh [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/dockerfile/web/haproxy/run_haproxy.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2020-01-18 #FileName: docker-build.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: Run haproxy Script #Copyright (C): 2020 All rights reserved #******************************************************************** #指定haproxy的配置文件并运行haproxy服务 haproxy -f /etc/haproxy/haproxy.cfg #温馨提示,我们使用"tail -f"命令作为容器的守护进程,这样就可以在容器内重启haproxy服务,但是使用"tail -f"查看的文件尽量不要是日志文件哟,因为我们执行该命令的目的并不是为了查看日志,如果查看日志的话可能会带来不必要的IO损耗. tail -f /etc/hosts [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]#
5>.启动tomcat容器
此过程可参考我之前的笔记:
https://www.cnblogs.com/yinzhengjie/p/12230043.html
二.编写Dockerfile并编译成镜像
1>.编写Dockerfile文件
[root@docker101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/dockerfile/web/haproxy/Dockerfile -rw-r--r-- 1 root root 1301 Jan 24 12:16 /yinzhengjie/softwares/dockerfile/web/haproxy/Dockerfile [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/dockerfile/web/haproxy/Dockerfile #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-25 #Blog: http://www.cnblogs.com/yinzhengjie #Description: YinZhengjie's Nginx Dockerfile #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** #指定咱们自己制作的基础镜像,该镜像已经安装了一些常用命令 FROM centos-base:7.6.1810 #指定镜像维护者的信息. MAINTAINER Jason.Yin y1053419035@qq.com #解压haproxy文件,ADD指令会自动帮咱们解压haproxy-1.8.20.tar.gz该文件 ADD haproxy-1.8.20.tar.gz /usr/local/src #编译安装haproxy服务 RUN cd /usr/local/src/haproxy-1.8.20 && make ARCH=x86_64 TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/yinzhengjie/softwares/haproxy && make install PREFIX=/yinzhengjie/softwares/haproxy && cp haproxy /usr/sbin/ && mkdir /yinzhengjie/softwares/haproxy/run
#后台启动haproxy服务 COPY haproxy.cfg /etc/haproxy/haproxy.cfg #将运行haproxy的脚本添加到指定的环境变量中 copy run_haproxy.sh /usr/bin/run_haproxy.sh #运行haproxy CMD ["/usr/bin/run_haproxy.sh"] [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]#
2>.运行编译脚本
[root@docker101.yinzhengjie.org.cn ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE tomcat-app01 v0.1 bf45c22f2d5b 17 hours ago 983MB tomcat-base 8.5.50 9ff79f369094 47 hours ago 968MB jdk-base 1.8.0_231 0f63a97ddc85 2 days ago 953MB centos-base 7.6.1810 b4931fd9ace2 2 days ago 551MB centos centos7.6.1810 f1cb7c7d58b7 10 months ago 202MB [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# [root@docker101.yinzhengjie.org.cn ~]# cd /yinzhengjie/softwares/dockerfile/web/haproxy/ [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/haproxy]# [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/haproxy]# ll total 2052 -rwxr-xr-x 1 root root 467 Jan 24 11:01 build-command.sh -rw-r--r-- 1 root root 1372 Jan 24 11:23 Dockerfile -rw-r--r-- 1 root root 2083917 Jan 24 11:05 haproxy-1.8.20.tar.gz -rw-r--r-- 1 root root 809 Jan 24 11:23 haproxy.cfg -rwxr-xr-x 1 root root 833 Jan 24 11:32 run_haproxy.sh [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/haproxy]# [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/haproxy]# ./build-command.sh
3>.启动容器
[root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/haproxy]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE centos-haproxy v1.8.20 1858fe05d96f 3 minutes ago 606MB tomcat-app01 v0.1 bf45c22f2d5b 18 hours ago 983MB tomcat-base 8.5.50 9ff79f369094 2 days ago 968MB jdk-base 1.8.0_231 0f63a97ddc85 2 days ago 953MB centos-base 7.6.1810 b4931fd9ace2 2 days ago 551MB centos centos7.6.1810 f1cb7c7d58b7 10 months ago 202MB [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/haproxy]# [root@docker101.yinzhengjie.org.cn /yinzhengjie/softwares/dockerfile/web/haproxy]# docker container run -it --rm -p 80:80 -p 8888:8888 centos-haproxy:v1.8.20 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.3 35292b8538d1
三.验证haproxy功能
1>.web服务验证成功
2>.访问haproxy的状态页