首页 Docker教程Docker compse实战案例-实现单机版的Haproxy+Nginx+Tomcat

docker-容器编排管理工具docker compse应用

docker-容器资源限制

运维派隶属马哥教育旗下专业运维社区,是国内成立最早的IT运维技术社区,欢迎关注公众号:yunweipai
领取学习更多免费Linux云计算、Python、Docker、K8s教程关注公众号:马哥linux运维

实战案例:实现单机版的Haproxy+Nginx+Tomcat

编写 docker-compose.yml 文件,实现单机版本的 nginx+tomcat 的动静分离 web
站点,要求从 nginx 作为访问入口,当访问指定 URL 的时候转发至 tomcat 服务
器响应

Docker compse实战案例-实现单机版的Haproxy+Nginx+Tomcat插图

制作haproxy镜像
编辑Dockerfile文件
[root@ubuntu1804 ~]#cat /data/dockerfile/web/haproxy/2.1.2-centos7-base/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"]
前台启动脚本
[root@ubuntu1804 ~]#cat /data/dockerfile/web/haproxy/2.1.2-centos7-base/run_haproxy.sh
#!/bin/bash
haproxy -f /etc/haproxy/haproxy.cfg
tail -f /etc/hosts
haproxy参数文件
[root@ubuntu1804 ~]#cat /data/dockerfile/web/haproxy/2.1.2-centos7-base/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 nginx-web service-nginx-web:80 check inter 3000 fall 2 rise 5
 #service-nginx-web是docker-compose.yml文件中使用的地址
镜像build和上传harbor的脚本
[root@ubuntu1804 ~]#cat /data/dockerfile/web/haproxy/2.1.2-centos7-base/build.sh 
#!/bin/bash
#
docker build -t 10.0.0.102/example/haproxy-centos7-base:2.1.2 .
docker push 10.0.0.102/example/haproxy-centos7-base:2.1.2 
准备压缩包及其他文件
[root@ubuntu1804 ~]#tree /data/dockerfile/web/haproxy/2.1.2-centos7-base/
/data/dockerfile/web/haproxy/2.1.2-centos7-base/
├── build.sh
├── Dockerfile
├── haproxy-2.1.2.tar.gz
├── haproxy.cfg
└── run_haproxy.sh

0 directories, 5 files
执行构建镜像并上传barbor
[root@ubuntu1804 ~]#cd /data/dockerfile/web/haproxy/2.1.2-centos7-base/
[root@ubuntu1804 2.1.2-centos7-base]#bash build.sh 
Sending build context to Docker daemon   2.67MB
Step 1/8 : FROM centos7-base:v1
 ---> 34ab3afcd3b3
Step 2/8 : LABEL maintainer="wangxiaochun <root@wangxiaochun.com>"
 ---> Using cache
 ---> 6206742174bc
Step 3/8 : ADD haproxy-2.1.2.tar.gz /usr/local/src/
 ---> Using cache
 ---> 21a146f4e75d
Step 4/8 : 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*
 ---> Using cache
 ---> 014f0f0b3569
Step 5/8 : ADD haproxy.cfg /etc/haproxy/
 ---> Using cache
 ---> 878e8db33c8d
Step 6/8 : ADD run_haproxy.sh /usr/bin
 ---> Using cache
 ---> 1177ed360989
Step 7/8 : EXPOSE 80 9999
 ---> Using cache
 ---> 8c2c602c1a56
Step 8/8 : CMD ["run_haproxy.sh"]
 ---> Using cache
 ---> 03e8086d441d
Successfully built 03e8086d441d
Successfully tagged 10.0.0.102/example/haproxy-centos7-base:2.1.2
The push refers to repository [10.0.0.102/example/haproxy-centos7-base]
fea7d7539b84: Layer already exists 
118f303dfb57: Layer already exists 
2748cd88bb93: Layer already exists 
a5dbda9ecfbf: Layer already exists 
2073413aebd6: Layer already exists 
6ec9af97c369: Layer already exists 
034f282942cd: Layer already exists 
2.1.2: digest: sha256:5df06247a5bd187894dd1c705f374568ecb4996e8d38bea71fd0bc679ab6ae13 size: 1785
准备nginx镜像
准备Dockfile文件
[root@ubuntu1804 ~]#cat /data/dockerfile/web/nginx/1.16.1-centos7-base/Dockerfile 
FROM centos7-base:v1

LABEL maintainer="wangxiaochun <root@wangxiaochun.com>"

ADD nginx-1.16.1.tar.gz /usr/local/src

RUN cd /usr/local/src/nginx-1.16.1 \
    && ./configure --prefix=/apps/nginx \
    && make && make install \
    && rm -rf /usr/local/src/nginx-1.16.1*     \
    && useradd -r -s /sbin/nologin nginx 

COPY nginx.conf /apps/nginx/conf/

ADD  app.tar.gz /apps/nginx/html/

EXPOSE 80 443

CMD ["/apps/nginx/sbin/nginx"]
准备nginx配置文件
[root@ubuntu1804 ~]#cat /data/dockerfile/web/nginx/1.16.1-centos7-base/nginx.conf
user  nginx;
worker_processes  1;
daemon off;
events {
    worker_connections  1024;
}
http {
    upstream tomcat {
        server service-tomcat-app1:8080;  #service-tomcat-app1为docker-compse.yml指定的名称
        server service-tomcat-app2:8080;
    }
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /app {
            proxy_pass http://tomcat ;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
镜像build和上传harbor的脚本
[root@ubuntu1804 ~]#cat  /data/dockerfile/web/nginx/1.16.1-centos7-base/build.sh 
#!/bin/bash
#
docker build -t 10.0.0.102/example/nginx-centos7-base:1.6.1 . 
docker push 10.0.0.102/example/nginx-centos7-base:1.6.1
准备软件包和其它文件
[root@ubuntu1804 ~]#tree /data/dockerfile/web/nginx/1.16.1-centos7-base/
/data/dockerfile/web/nginx/1.16.1-centos7-base/
├── app.tar.gz
├── build.sh
├── Dockerfile
├── nginx-1.16.1.tar.gz
└── nginx.conf

0 directories, 5 files
[root@ubuntu1804 ~]#
执行构建镜像并上传harbor
[root@ubuntu1804 ~]#cd /data/dockerfile/web/nginx/1.16.1-centos7-base/
[root@ubuntu1804 1.16.1-centos7-base]#bash build.sh 
Sending build context to Docker daemon  1.041MB
Step 1/8 : FROM centos7-base:v1
 ---> 34ab3afcd3b3
Step 2/8 : LABEL maintainer="wangxiaochun <root@wangxiaochun.com>"
 ---> Using cache
 ---> 6206742174bc
Step 3/8 : ADD nginx-1.16.1.tar.gz /usr/local/src
 ---> Using cache
 ---> 1c2d1cbbf34d
Step 4/8 : RUN cd /usr/local/src/nginx-1.16.1     && ./configure --prefix=/apps/nginx     && make && make install     && rm -rf /usr/local/src/nginx-1.16.1*         && useradd -r -s /sbin/nologin nginx
 ---> Using cache
 ---> 019823fa30bf
Step 5/8 : COPY nginx.conf /apps/nginx/conf/
 ---> Using cache
 ---> 9583c76d6c9d
Step 6/8 : ADD  app.tar.gz /apps/nginx/html/
 ---> Using cache
 ---> 4bc0528bb32e
Step 7/8 : EXPOSE 80 443
 ---> Using cache
 ---> 547fb553f9d9
Step 8/8 : CMD ["/apps/nginx/sbin/nginx"]
 ---> Using cache
 ---> f6aa9591a2b0
Successfully built f6aa9591a2b0
Successfully tagged 10.0.0.102/example/nginx-centos7-base:1.6.1
The push refers to repository [10.0.0.102/example/nginx-centos7-base]
4591f99cd5a6: Layer already exists 
27bdf47a2126: Layer already exists 
6aa26a3d91e1: Layer already exists 
93ae7d74d90e: Layer already exists 
2073413aebd6: Layer already exists 
6ec9af97c369: Layer already exists 
034f282942cd: Layer already exists 
1.6.1: digest: sha256:2eb7525c623ecf34bc2046888029cdb0ef01d866a0687ca7afd4dfd36c16a863 size: 1786
[root@ubuntu1804 1.16.1-centos7-base]#
准备tomcat镜像

此处略,参看前面的第2.4节

参考下面文件列表:

[root@ubuntu1804 ~]#tree /data/dockerfile/
/data/dockerfile/
├── system
│   ├── alpine
│   │   ├── build.sh
│   │   ├── Dockerfile
│   │   └── repositories
│   ├── centos
│   │   ├── build.sh
│   │   ├── Dockerfile
│   │   ├── Dockerfile.bak
│   │   └── Dockerfile.bak2
│   ├── debian
│   └── ubuntu
└── web
    ├── apache
    ├── haproxy
    │   └── 2.1.2-centos7-base
    │       ├── build.sh
    │       ├── Dockerfile
    │       ├── haproxy-2.1.2.tar.gz
    │       ├── haproxy.cfg
    │       └── run_haproxy.sh
    ├── jdk
    │   ├── build.sh
    │   ├── Dockerfile
    │   ├── jdk-8u212-linux-x64.tar.gz
    │   └── profile
    ├── nginx
    │   ├── 1.16.1-alpine
    │   │   ├── build.sh
    │   │   ├── Dockerfile
    │   │   ├── index.html
    │   │   ├── nginx-1.16.1.tar.gz
    │   │   └── nginx.conf
    │   ├── 1.16.1-centos7
    │   │   ├── build.sh
    │   │   ├── Dockerfile
    │   │   ├── index.html
    │   │   ├── nginx-1.16.1.tar.gz
    │   │   └── nginx.conf
    │   ├── 1.16.1-centos7-base
    │   │   ├── app.tar.gz
    │   │   ├── build.sh
    │   │   ├── Dockerfile
    │   │   ├── nginx-1.16.1.tar.gz
    │   │   └── nginx.conf
    │   └── 1.16.1-ubuntu1804
    │       ├── build.sh
    │       ├── Dockerfile
    │       ├── index.html
    │       ├── nginx-1.16.1.tar.gz
    │       ├── nginx.conf
    │       └── sources.list
    └── tomcat
        ├── tomcat-app1
        │   ├── app
        │   │   └── index.jsp
        │   ├── app.tar.gz
        │   ├── build.sh
        │   ├── Dockerfile
        │   ├── run_tomcat.sh
        │   └── server.xml
        ├── tomcat-app2
        │   ├── app
        │   │   ├── index.html
        │   │   └── index.jsp
        │   ├── app.tar.gz
        │   ├── build.sh
        │   ├── Dockerfile
        │   ├── run_tomcat.sh
        │   └── server.xml
        └── tomcat-base-8.5.50
            ├── apache-tomcat-8.5.50.tar.gz
            ├── build.sh
            └── Dockerfile

21 directories, 53 files
[root@ubuntu1804 ~]#
查看harbor上的相关镜像

Docker compse实战案例-实现单机版的Haproxy+Nginx+Tomcat插图(1)

编辑docker compose文件及环境准备
编辑docker compose文件
[root@ubuntu1804 ~]#cat /data/docker-compose/docker-compose.yml 
service-haproxy:
  image: 10.0.0.102/example/haproxy-centos7-base:2.1.2
  container_name: haproxy
  expose:
    - 80
    - 9999
  ports:
    - "80:80"
    - "9999:9999"
  links:
    - service-nginx-web

service-nginx-web:
  image: 10.0.0.102/example/nginx-centos7-base:1.6.1
  container_name: nginx-web 
  volumes:
    - /data/nginx:/apps/nginx/html/
  links:
    - service-tomcat-app1
    - service-tomcat-app2

  expose:
   - 80 
   - 443
#  ports:
#    - "80:80"
#    - "443:443"

service-tomcat-app1:
  image: 10.0.0.102/example/tomcat-web:app1
  container_name: tomcat-app1
  expose:
    - 8080
#  ports:
#    - "8081:8080"

service-tomcat-app2:
  image: 10.0.0.102/example/tomcat-web:app2
  container_name: tomcat-app2
  expose:
    - 8080
#  ports:
#    - "8082:8080"
[root@ubuntu1804 ~]#
启动容器
[root@ubuntu1804 ~]#cd /data/docker-compose/
[root@ubuntu1804 docker-compose]#docker-compose up -d 
Pulling service-tomcat-app1 (10.0.0.102/example/tomcat-web:app1)...
app1: Pulling from example/tomcat-web
f34b00c7da20: Pull complete
544476d462f7: Pull complete
39345915aa1b: Pull complete
4b792f2bae38: Pull complete
4439447a3522: Pull complete
fe34d2ec1dd0: Pull complete
b8487ca03126: Pull complete
5a475b7d8b1a: Pull complete
df8703d3d2dd: Pull complete
f0da1ffa7aa7: Pull complete
80fd4c70e670: Pull complete
c2a0247d7bfa: Pull complete
b0977ed809cd: Pull complete
Digest: sha256:e0aba904df6095ea04c594d6906101f8e5f4a6ceb0a8f9b24432c47698d0caa8
Status: Downloaded newer image for 10.0.0.102/example/tomcat-web:app1
Pulling service-tomcat-app2 (10.0.0.102/example/tomcat-web:app2)...
app2: Pulling from example/tomcat-web
f34b00c7da20: Already exists
544476d462f7: Already exists
39345915aa1b: Already exists
4b792f2bae38: Already exists
4439447a3522: Already exists
fe34d2ec1dd0: Already exists
b8487ca03126: Already exists
5a475b7d8b1a: Already exists
df8703d3d2dd: Already exists
f0da1ffa7aa7: Already exists
80fd4c70e670: Already exists
1a55cb76a801: Pull complete
565ab795f82a: Pull complete
Digest: sha256:c4d6f166c3933f6c1ba59c84ea0518ed653af25f28b87981c242b0deff4209bb
Status: Downloaded newer image for 10.0.0.102/example/tomcat-web:app2
Pulling service-nginx-web (10.0.0.102/example/nginx-centos7-base:1.6.1)...
1.6.1: Pulling from example/nginx-centos7-base
f34b00c7da20: Already exists
544476d462f7: Already exists
39345915aa1b: Already exists
cf9a24457402: Pull complete
15ef14db18ed: Pull complete
fc0bcf215997: Pull complete
2808e0476ed2: Pull complete
Digest: sha256:2eb7525c623ecf34bc2046888029cdb0ef01d866a0687ca7afd4dfd36c16a863
Status: Downloaded newer image for 10.0.0.102/example/nginx-centos7-base:1.6.1
Pulling service-haproxy (10.0.0.102/example/haproxy-centos7-base:2.1.2)...
2.1.2: Pulling from example/haproxy-centos7-base
f34b00c7da20: Already exists
544476d462f7: Already exists
39345915aa1b: Already exists
70a1e528112f: Pull complete
d240b62890cd: Pull complete
4223b7054cb3: Pull complete
7d2e1b713699: Pull complete
Digest: sha256:5df06247a5bd187894dd1c705f374568ecb4996e8d38bea71fd0bc679ab6ae13
Status: Downloaded newer image for 10.0.0.102/example/haproxy-centos7-base:2.1.2
Creating tomcat-app1 ... done
Creating tomcat-app2 ... done
Creating nginx-web   ... done
Creating haproxy     ... done
[root@ubuntu1804 docker-compose]#curl 127.0.0.1/app/
Tomcat Page in app1
[root@ubuntu1804 docker-compose]#curl 127.0.0.1/app/
Tomcat Page in app2
[root@ubuntu1804 docker-compose]#curl 127.0.0.1
Docker compose test page
验证容器启动成功
[root@ubuntu1804 ~]#curl http://10.0.0.101/
Docker compose test page
[root@ubuntu1804 ~]#curl http://10.0.0.101/app/
Tomcat Page in app1
[root@ubuntu1804 ~]#curl http://10.0.0.101/app/
Tomcat Page in app2

访问haroxy 管理界面:

Docker compse实战案例-实现单机版的Haproxy+Nginx+Tomcat插图(2)

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

docker-容器编排管理工具docker compse应用

docker-容器资源限制

网友评论comments

发表评论

邮箱地址不会被公开。

暂无评论

Copyright © 2012-2021 YUNWEIPAI.COM - 运维派 京ICP备16064699号-6
扫二维码
扫二维码
返回顶部