dockerfile生产案例-构建haproxy镜像

生产案例:构建haproxy镜像

准备相关文件
#准备haproxy源码文件
[root@ubuntu1804 ~]#mkdir -p  /data/dockerfile/web/haproxy/2.1.2-centos7 
[root@ubuntu1804 ~]#cd /data/dockerfile/web/haproxy/2.1.2-centos7
[root@ubuntu1804 2.1.2-centos7]#wget http://www.haproxy.org/download/2.1/src/haproxy-2.1.2.tar.gz

#准备haproxy启动脚本
[root@ubuntu1804 2.1.2-centos7]#vim run_haproxy.sh
[root@ubuntu1804 2.1.2-centos7]#cat run_haproxy.sh
#!/bin/bash
haproxy -f /etc/haproxy/haproxy.cfg
tail -f /etc/hosts
准备haproxy配置文件
#准备haproxy配置文件
[root@ubuntu1804 2.1.2-centos7]#cat haproxy.cfg 
global
chroot /apps/haproxy
#stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
uid 99
gid 99
daemon
nbproc 1
pidfile /apps/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 10.0.0.101:8080 check inter 3000 fall 2 rise 5
 server web2 10.0.0.102:8080 check inter 3000 fall 2 rise 5 
准备Dockerfile
[root@ubuntu1804 2.1.2-centos7]#pwd
/data/dockerfile/web/haproxy/2.1.2-centos7

[root@ubuntu1804 haproxy]# cat Dockerfile 
#Haproxy Base Image
FROM centos7-base:v1
LABEL maintainer="wangxiaochun <root@wangxiaochun.com>"

ADD haproxy-2.1.2.tar.gz /usr/local/src/ 

RUN cd /usr/local/src/haproxy-2.1.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=/apps/haproxy /
    && make install PREFIX=/apps/haproxy /
    && ln -s /apps/haproxy/sbin/haproxy  /usr/sbin/ /
    && mkdir /apps/haproxy/run /
    && rm -rf /usr/local/src/haproxy*  

ADD haproxy.cfg /etc/haproxy/
ADD run_haproxy.sh /usr/bin

EXPOSE 80 9999
CMD ["run_haproxy.sh"]
准备构建脚本构建haproxy镜像
[root@ubuntu1804 2.1.2-centos7]#vim build.sh 
[root@ubuntu1804 2.1.2-centos7]#cat  build.sh 
#!/bin/bash
docker build -t haproxy-centos7:2.1.2 .

[root@ubuntu1804 2.1.2-centos7]#ls
build.sh  Dockerfile  haproxy-2.1.2.tar.gz  haproxy.cfg  run_haproxy.sh

[root@ubuntu1804 2.1.2-centos7]#bash build.sh 
[root@ubuntu1804 2.1.2-centos7]#docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
haproxy-centos7     2.1.2               5eccdb29a058        26 minutes ago      428MB
nginx-ubuntu1804    1.16.1              19efdd23ac87        15 hours ago        378MB
alpine-nginx        1.16.1              978a43bbb61d        16 hours ago        211MB
nginx-alpine        1.16.1              978a43bbb61d        16 hours ago        211MB
alpine-base         3.11                b162eecf4da9        17 hours ago        182MB
tomcat-web          app2                0e1760fe79a6        37 hours ago        838MB
tomcat-web          app1                76016219a0ca        37 hours ago        838MB
tomcat-base         v8.5.50             8d5395cb72c4        38 hours ago        824MB
centos7-jdk         8u212               e0fe770a7ccd        39 hours ago        809MB
centos7-base        v1                  34ab3afcd3b3        40 hours ago        403MB
alpine              3.11                e7d92cdc71fe        12 days ago         5.59MB
alpine              latest              e7d92cdc71fe        12 days ago         5.59MB
ubuntu              18.04               ccc6e87d482b        2 weeks ago         64.2MB
ubuntu              bionic              ccc6e87d482b        2 weeks ago         64.2MB
centos              centos7.7.1908      08d05d1d5859        2 months ago        204MB
从镜像启动容器
[root@ubuntu1804 2.1.2-centos7]#docker run -d -p 80:80 -p 9999:9999 haproxy-centos7:2.1.2 
e0a7c827cb5fdd5a630f7dfe58b1f60822da18929a4dfeccb7490fb78403e3df
2.5.6 在另外两台主机启动容器
#导出本地相关镜像
[root@ubuntu1804 ~]#docker save centos7-base:v1 > /data/centos7-base.tar.gz
[root@ubuntu1804 ~]#docker save centos7-jdk:8u212 > /data/centos7-jdk.tar.gz
[root@ubuntu1804 ~]#docker save tomcat-base:v8.5.50 > /data/tomcat-base.tar.gz
[root@ubuntu1804 ~]#docker save tomcat-web:app1 > /data/tomcat-web-app1.tar.gz
[root@ubuntu1804 ~]#docker save tomcat-web:app2 > /data/tomcat-web-app2.tar.gz
[root@ubuntu1804 ~]#ls /data
centos7-base.tar.gz  centos7-jdk.tar.gz  dockerfile  tomcat-base.tar.gz  tomcat-web-app1.tar.gz  tomcat-web-app2.tar.gz

#将镜像复制到另外两台主机
[root@ubuntu1804 ~]#scp /data/*.gz 10.0.0.101:/data/
[root@ubuntu1804 ~]#scp /data/*.gz 10.0.0.102:/data/   

#在另外两台主机上执行下面操作导入镜像
[root@ubuntu1804 ~]#ls /data
centos7-base.tar.gz  lost+found          tomcat-web-app1.tar.gz
centos7-jdk.tar.gz   tomcat-base.tar.gz  tomcat-web-app2.tar.gz
[root@ubuntu1804 ~]#for i in /data/*.gz;do docker load -i $i;done

#在另外两台主机上创建相关容器
[root@ubuntu1804 ~]#docker run -d -p 8080:8080 tomcat-web:app1 
781681e73333396b23f404e70d0c781ab464a8e9b578f41c153583d23bd76a46
[root@ubuntu1804 ~]#docker run -d -p 8080:8080 tomcat-web:app2
81fa01a688cb72cf397a5da46acc89a51f2a2f8de3a0072565d701625c43540a
web访问验证
[root@ubuntu1804 2.1.2-centos7]#curl http://10.0.0.100/app/
Tomcat Page in app1
[root@ubuntu1804 2.1.2-centos7]#curl http://10.0.0.100/app/
Tomcat Page in app2
[root@ubuntu1804 2.1.2-centos7]#docker exec -it e0a7c827cb5 bash
[root@e0a7c827cb5f /]# netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:9999            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN     
[root@e0a7c827cb5f /]# vim /etc/haproxy/haproxy.cfg 
[root@e0a7c827cb5f /]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.2  11700  2428 ?        Ss   11:01   0:00 /bin/bash /usr/bin/run_haproxy.sh
nobody        7  0.0  7.1 181076 70324 ?        Ss   11:01   0:00 haproxy -f /etc/haproxy/haproxy.cfg
root          8  0.0  0.0   4416   772 ?        S    11:01   0:00 tail -f /etc/hosts
root          9  0.1  0.3  12488  3696 pts/0    Ss   11:02   0:00 bash
root         54  0.0  0.3  51764  3448 pts/0    R+   11:06   0:00 ps aux

dockerfile生产案例-构建haproxy镜像插图
dockerfile生产案例-构建haproxy镜像插图(1)

#在第二台主机上停止容器
[root@ubuntu1804 ~]#docker stop 81fa01a688cb
[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
81fa01a688cb        tomcat-web:app2     "/apps/tomcat/bin/ru…"   28 minutes ago      Exited (137) 39 seconds ago 
#观察状态页,发现后端服务器down

dockerfile生产案例-构建haproxy镜像插图(2)

#在第二台主机上恢复启动容器
[root@ubuntu1804 ~]#docker start 81fa01a688cb
81fa01a688cb
[root@ubuntu1804 ~]#docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                              NAMES
81fa01a688cb        tomcat-web:app2     "/apps/tomcat/bin/ru…"   30 minutes ago      Up 14 seconds       8009/tcp, 0.0.0.0:8080->8080/tcp   optimistic_shirley
#再次观察状态页,发现后端服务器上线

dockerfile生产案例-构建haproxy镜像插图(3)

本文链接:http://www.yunweipai.com/34848.html

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/52669.html

(0)
上一篇 2021年8月6日
下一篇 2021年8月6日

相关推荐

发表回复

登录后才能评论