docker-compose
使用单机编排工具docker-compose部署haproxy+nginx+tomcat的动静分离站点
架构
环境
root@harbor:haproxy#cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.5 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.5 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
root@harbor:~#docker version
Client: Docker Engine - Community
Version: 19.03.15
API version: 1.40
Go version: go1.13.15
Git commit: 99e3ed8
Built: Sat Jan 30 03:11:43 2021
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.15
API version: 1.40 (minimum version 1.12)
Go version: go1.13.15
Git commit: 99e3ed8
Built: Sat Jan 30 03:18:13 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.3.9
GitCommit: ea765aba0d05254012b0b9e595e995c09186427f
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
root@harbor:~#python -V
Python 2.7.17
root@harbor:~#docker-compose version
docker-compose version 1.25.3, build d4d1b42b
docker-py version: 4.1.0
CPython version: 3.7.5
OpenSSL version: OpenSSL 1.1.0l 10 Sep 2019
harbor
搭建过程具体请参考我的另一篇博客
https://blog.csdn.net/qq_43652666/article/details/123422169
root@harbor:haproxy#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
harbor.kktb.org/web/haproxy 2022-07-1910-3540 1edf69786e84 18 minutes ago 601MB
harbor.kktb.org/web/nginx-tomcat 2022-07-18213931 d6f57855f7fa 13 hours ago 557MB
harbor.kktb.org/web/tomcat/tomcat-app2 2022-07-16-17-35-20 aba3ab3a4dff 2 days ago 496MB
harbor.kktb.org/web/tomcat/tomcat-app1 2022-07-16-17-31-44 8c42eb73325e 2 days ago 496MB
harbor.kktb.org/web/tomcat-base v8.5.452022-07-16-17-01-48 bde317b698d0 2 days ago 482MB
harbor.kktb.org/web/jdk-base time2022-07-16-16-30-23 c5c312a617dd 2 days ago 468MB
harbor.kktb.org/web/tomcat-base 2022-07-16-13-53-40 e96098a13f02 2 days ago 437MB
harbor.kktb.org/web/jdk-base 2022-07-16-13-39-14 30027436dd97 2 days ago 423MB
harbor.kktb.org/base/centos centos7.9.2009 eeb6ee3f44bd 10 months ago 204MB
dockerfile
haproxy
root@harbor:~#tree /opt/dockerfile/web/haproxy/
/opt/dockerfile/web/haproxy/
├── build_command.sh
├── Dockerfile
├── haproxy-2.4.10.tar.gz
├── haproxy.cfg
├── lua-5.4.4.tar.gz
└── run_haproxy.sh
0 directories, 6 files
root@harbor:~#cat /opt/dockerfile/web/haproxy/Dockerfile
FROM harbor.kktb.org/base/centos:centos7.9.2009
RUN yum install -y gcc make readline-devel openssl-devel pcre-devel systemd-devel
ADD lua-5.4.4.tar.gz /usr/local/src
RUN cd /usr/local/src/lua-5.4.4 && make linux test
ADD haproxy-2.4.10.tar.gz /usr/local/src
RUN cd /usr/local/src/haproxy-2.4.10 && make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_LUA=1 LUA_INC=/usr/local/src/lua-5.4.4/src/ LUA_LIB=/usr/local/src/lua-5.4.4/src/ \
&& make install PREFIX=/apps/haproxy
RUN mkdir /var/lib/haproxy && mkdir /apps/haproxy/run
RUN cp /apps/haproxy/sbin/haproxy /usr/sbin/
ADD haproxy.cfg /etc/haproxy/
ADD run_haproxy.sh /usr/bin
EXPOSE 80 9999
CMD ["/usr/bin/run_haproxy.sh"]
root@harbor:~#cat /opt/dockerfile/web/haproxy/run_haproxy.sh
#!/bin/bash
haproxy -f /etc/haproxy/haproxy.cfg
tail -f /etc/hosts
root@harbor:~#cat /opt/dockerfile/web/haproxy/build_command.sh
#!/bin/bash
docker build -t harbor.kktb.org/web/haproxy:`date +%F%H-%M%S` .
root@harbor:~#cat /opt/dockerfile/web/haproxy/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 /var/lib/haproxy/haproxy.pid
log 127.0.0.1 local3 info
defaults
option http-keep-alive
option forwardfor
maxconn 100000
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_80
bind 0.0.0.0:80
mode http
log global
balance roundrobin
server web1 service-nginx-web:80 check inter 3000 fall 2 rise 5
listen web_port_443
bind 0.0.0.0:443
mode http
log global
balance roundrobin
server web1 service-nginx-web:443 check inter 3000 fall 2 rise 5
nginx
root@harbor:haproxy#tree /opt/dockerfile/web/nginx/
/opt/dockerfile/web/nginx/
├── build-command.sh
├── dockerfile
├── nginx.conf
└── nginx.conf.bak
0 directories, 4 files
root@harbor:haproxy#cat /opt/dockerfile/web/nginx/dockerfile
FROM centos:centos7.9.2009
RUN yum install wget gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools epel-release -y
WORKDIR /usr/local/src
RUN wget http://nginx.org/download/nginx-1.18.0.tar.gz
RUN tar xf nginx-1.18.0.tar.gz
RUN cd nginx-1.18.0 && ./configure --prefix=/usr/local/nginx --with-http_sub_module && make && make install
RUN cd /usr/local/nginx && mkdir /data/nginx/html/{static,myapp} -p
ADD nginx.conf /usr/local/nginx/conf/nginx.conf
RUN useradd nginx -s /sbin/nologin
RUN ln -sv /usr/local/nginx/sbin/nginx /usr/bin/nginx
RUN echo "/data/nginx/html/static/index.html" > /data/nginx/html/static/index.html \
&& echo "/data/nginx/html/myapp/index.html" > /data/nginx/html/myapp/index.html \
&& echo "/data/nginx/html/index.html" > /data/nginx/html/index.html
#RUN echo "dockerfile nginx page" > /usr/local/nginx/html/index.html
EXPOSE 80 443
CMD ["nginx"]
root@harbor:haproxy#cat /opt/dockerfile/web/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes 1;
error_log /usr/local/nginx/logs/error.log;
pid /usr/local/nginx/logs/nginx.pid;
daemon off;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /usr/local/nginx/logs/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /usr/local/nginx/conf/mime.types;
default_type application/octet-stream;
upstream myapp {
server service-tomcat-app1:8080;
server service-tomcat-app2:8080;
}
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/local/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root /data/nginx/html;
index index.html;
}
location /static {
root /data/nginx/html;
index index.html;
}
location /myapp {
proxy_pass http://myapp;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
jdk
root@harbor:haproxy#tree /opt/dockerfile/web/jdk/
/opt/dockerfile/web/jdk/
├── build-command.sh
├── Dockerfile
├── Dockerfile.bak
├── jdk-8u291-linux-x64.tar.gz
└── profile
0 directories, 5 files
root@harbor:haproxy#cat /opt/dockerfile/web/jdk/Dockerfile
# JDK baseimage
FROM ubuntu:18.04
# systemctl config
# apt source
RUN sed -i s@/archive.ubuntu.com/@/mirrors.tuna.tsinghua.edu.cn/@g /etc/apt/sources.list
RUN sed -i s@/security.ubuntu.com/@/mirrors.tuna.tsinghua.edu.cn/@g /etc/apt/sources.list
RUN apt clean && apt update
# timezone
# alpine
# ENV TZ Asia/Shanghai
# RUN apk add tzdata && cp /usr/share/zoneinfo/${TZ} /etc/localtime \
# && echo ${TZ} > /etc/timezone \
# && apk del tzdata
# debian
# ENV TZ=Asia/Shanghai \
# DEBIAN_FRONTEND=noninteractive
# RUN ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime \
# && echo ${TZ} > /etc/timezone \
# && dpkg-reconfigure --frontend noninteractive tzdata \
# && rm -rf /var/lib/apt/lists/*
# ubuntu
ENV TZ=Asia/Shanghai \
DEBIAN_FRONTEND=noninteractive
RUN apt install -y tzdata \
&& ln -fns /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo ${TZ} > /etc/timezone \
&& dpkg-reconfigure --frontend noninteractive tzdata \
&& rm -rf /var/lib/apt/lists/*
# centos
# ENV TZ Asia/Shanghai
# RUN ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime \
# && echo ${TZ} > /etc/timezone
ADD jdk-8u291-linux-x64.tar.gz /usr/local/src/
RUN ln -sv /usr/local/src/jdk1.8.0_291 /usr/local/jdk
ADD profile /etc/profile
ENV JAVA_HOME /usr/local/jdk
ENV JRE_HOME ${JAVA_HOME}/jre
ENV CLASSPATH ${JAVA_HOME}/lib/:${JRE_HOME}/lib/
ENV PATH $PATH:${JAVA_HOME}/bin
tomcat
root@harbor:web#tree tomcat/
tomcat/
├── apache-tomcat-8.5.45.tar.gz
├── build-command.sh
├── Dockerfile
├── tomcat-app1
│ ├── build-command.sh
│ ├── Dockerfile
│ ├── myapp
│ │ └── index.html
│ └── run_tomcat.sh
└── tomcat-app2
├── build-command.sh
├── Dockerfile
├── myapp
│ └── index.html
└── run_tomcat.sh
4 directories, 11 files
root@harbor:tomcat#cat /opt/dockerfile/web/tomcat/Dockerfile
# tomcat base
FROM harbor.kktb.org/web/jdk-base:time2022-07-16-16-30-23
# env
ENV LANG en_US.UTF-8
ENV TERM xterm
ENV TOMCAT_MAJOR_VERSION 8
ENV TOMCAT_MINOR_VERSION 8.5.45
ENV CATALINA_HOME /apps/tomcat
ENV APP_DIR ${CATALINA_HOME}/webapps
# tomcat
RUN mkdir /apps
ADD apache-tomcat-8.5.45.tar.gz /apps
RUN ln -sv /apps/apache-tomcat-8.5.45 /apps/tomcat
tomcat-app1
root@harbor:tomcat-app1#vim Dockerfile
# tomcat app1
FROM harbor.kktb.org/web/tomcat-base:v8.5.452022-07-16-17-01-48
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
RUN groupadd www;useradd www -g www
RUN chown -R www.www /apps/
ADD myapp/* /apps/tomcat/webapps/myapp/
EXPOSE 8080 8009
CMD ["/apps/tomcat/bin/run_tomcat.sh"]
root@harbor:tomcat-app1#cat run_tomcat.sh
#!/bin/bash
echo "1.1.1.1 abc.test.com" >> /etc/hosts
echo "nameserver 223.6.6.6" >> /etc/resolv.conf
su www -c "/apps/tomcat/bin/catalina.sh start"
su www -c "tail -f /etc/hosts"
root@harbor:tomcat-app1#cat build-command.sh
#!/bin/bash
docker build -t harbor.kktb.org/web/tomcat/tomcat-app1:`date +%F-%H-%M-%S` .
tomcat-app2同1
docker-compose
volume
root@harbor:haproxy#cat /data/nginx/html/static/index.html
volume /data/nginx/html/static/index.html
root@harbor:haproxy#cat /data/tomcat/webapps/showhost.jsp
<%@ page import="java.util.Enumeration" %>
<br />
host:
<% try{out.println(""+java.net.InetAddress.getLocalHost().getHostName());}catch(Exception e){} %>
<br />
remoteAddr: <%=request.getRemoteAddr()%>
<br />
remoteHost: <%=request.getRemoteHost()%>
<br />
sessionId: <%=request.getSession().getId()%>
<br />
serverName: <%=request.getServerName()%>
<br />
<%request.getSession().setAttribute("t1","t2");%>
<%
Enumeration en = request.getHeaderNames();
while(en.hasMoreElements()){
String hd = en.nextElement().toString();
out.println(hd+":"+request.getHeader(hd));
out.println("<br/>");
}
%>
root@harbor:haproxy#cat /opt/compose/haproxy/docker-compose.yml
service-haproxy:
image: harbor.kktb.org/web/haproxy:2022-07-1910-3540
container_name: haproxy
expose:
- 80
- 443
- 9999
ports:
- "80:80"
- "443:443"
- "9999:9999"
links:
- service-nginx-web
service-nginx-web:
image: harbor.kktb.org/web/nginx-tomcat:2022-07-18213931
container_name: nginx-web3
volumes:
- /data/nginx/html/static:/data/nginx/html/static
expose:
- 80
- 443
# ports:
#- "8800:80"
#- "8443:443"
links:
- service-tomcat-app1
- service-tomcat-app2
service-tomcat-app1:
image: harbor.kktb.org/web/tomcat/tomcat-app1:2022-07-16-17-31-44
container_name: tomcat-app1
volumes:
- /data/tomcat/webapps/:/apps/tomcat/webapps/myapp
# expose:
# - 8080
#ports:
#- "8801:8080"
service-tomcat-app2:
image: harbor.kktb.org/web/tomcat/tomcat-app2:2022-07-16-17-35-20
container_name: tomcat-app2
volumes:
- /data/tomcat/webapps/:/apps/tomcat/webapps/myapp
# expose:
#- 8080
#ports:
#- "8802:8080"
root@harbor:haproxy#pwd
/opt/compose/haproxy
root@harbor:haproxy#docker-compose up -d
tomcat-app1 is up-to-date
tomcat-app2 is up-to-date
Recreating nginx-web3 ... done
Recreating haproxy ... done
测试
haproxy状态页
静态页面测试
volume挂在卷测试
动态页面测试
负载均衡测试
有问题欢迎评论留言