Docker+Nginx+tomcat模拟tomcat集群负载均衡

Docker+Nginx+tomcat模拟tomcat集群负载均衡_第1张图片

启动docker服务,拉取tomcat,nginx镜像

本文是基于 win10 环境进行集成,至于怎么在win10环境上体验docker,可自行百度,本文不在阐述。

安装好docker服务之后,启动docker服务,运行如下命令,拉取tomcat,nginx镜像:

docker pull hub.c.163.com/library/tomcat:latest

docker pull hub.c.163.com/library/nginx:latest

镜像拉取成功之后,运行 docker images命令查看拉取的镜像:

λ docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

redis latest c5355f8853e4 4 weeks ago 107MB

hub.c.163.com/library/ubuntu latest ccc7a11d65b1 8 months ago 120MB

hub.c.163.com/library/tomcat latest 72d2be374029 8 months ago 292MB

hub.c.163.com/library/nginx latest 46102226f2fd 12 months ago 109MB

启动tomcat容器,挂载本地目录

docker run -it -p 58080:8080 -v E:local_data:/data --name tomcat_02 hub.c.163.com/library/tomcat /bin/bash

运行上述命令,启动容器,将宿主机的 E:local_data 挂载到容器的 /data 目录下。将打包好的war包放在 E:local_data目录下,使用如下命令将war包复制到 tomcat 的 webapps 下,并启动tomcat。

cp /data/nginx_tomcat-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/sh /usr/local/tomcat/bin/startup.sh

重复上述操作,连续启动三个tomcat容器,如下所示:

λ docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

175d6f2be242 hub.c.163.com/library/tomcat "/bin/bash" 7 hours ago Up 7 hours 0.0.0.0:48080->8080/tcp tomcat_03

11d5b4487c66 hub.c.163.com/library/tomcat "/bin/bash" 7 hours ago Up 7 hours 0.0.0.0:38080->8080/tcp tomcat_02

c77cd2b2a6e9 hub.c.163.com/library/tomcat "/bin/bash" 7 hours ago Up 7 hours 0.0.0.0:58080->8080/tcp tomcat_01

启动nginx并配置

docker run -p 80:80 --privileged=true -v E:my_nginxconfig ginx.conf:/etc/nginx/nginx.conf --name mynginx hub.c.163.com/library/nginx

将nginx的80端口映射到宿主机的80端口,将宿主机的 E:my_nginxconfig ginx.conf 目录挂载到容器的 /etc/nginx/nginx.conf 目录下,用自定义 nginx.conf配置文件覆盖默认配置文件。

配置文件如下所示:

user nginx;

worker_processes 1;

error_log /var/log/nginx/error.log warn;

pid /var/run/nginx.pid;

events {

worker_connections 1024;

}

http {

server {

listen 80;

location / {

proxy_pass http://blance;

}

}

upstream blance{

server 172.17.0.2:8080;

server 172.17.0.3:8080;

server 172.17.0.4:8080;

}

include /etc/nginx/mime.types;

default_type application/octet-stream;

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 /var/log/nginx/access.log main;

sendfile on;

#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

include /etc/nginx/conf.d/*.conf;

}

其中,以下为在默认配置内容中新增的内容

server {

listen 80;

location / {

proxy_pass http://blance;

}

}

upstream blance{

server 172.17.0.2:8080;

server 172.17.0.3:8080;

server 172.17.0.4:8080;

}

运行启动命令,启动nginx容器。

查看所有启动容器,测试是否均衡负载

运行 docker ps 命令查看所有正在运行的容器:

λ docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

b0d477164bed hub.c.163.com/library/nginx "nginx -g 'daemon of…" 35 minutes ago Up 35 minutes 0.0.0.0:80->80/tcp mynginx

175d6f2be242 hub.c.163.com/library/tomcat "/bin/bash" 7 hours ago Up 7 hours 0.0.0.0:48080->8080/tcp tomcat_03

11d5b4487c66 hub.c.163.com/library/tomcat "/bin/bash" 7 hours ago Up 7 hours 0.0.0.0:38080->8080/tcp tomcat_02

c77cd2b2a6e9 hub.c.163.com/library/tomcat "/bin/bash" 7 hours ago Up 7 hours 0.0.0.0:58080->8080/tcp tomcat_01

浏览器访问 127.0.0.1:80 ,进入tomcat 欢迎页面。

多次访问 127.0.0.1/nginx_tomcat-0.0.1-SNAPSHOT/test/loadBalancing 链接,会打印请求到的服务的ip及端口,

java代码如下:

/**

  • Created by Haiyoung on 2018/5/1.

*/@RestController@RequestMapping( value = "/test")public class LoadBalancing {

@RequestMapping( value = "/loadBalancing")

public String loadBalancingTest(HttpServletRequest request) throws UnknownHostException {

String ip = java.net.InetAddress.getLocalHost().getHostAddress();

int post = request.getLocalPort();

System.out.println(ip + ":" + post);

return ip + ":" + post;

}}

请求结果如下所示:

172.17.0.2:8080

172.17.0.3:8080

172.17.0.4:8080

172.17.0.2:8080

172.17.0.3:8080

172.17.0.4:8080

172.17.0.2:8080

可见,nginx实现了对请求的负载均衡。

你可能感兴趣的:(Docker+Nginx+tomcat模拟tomcat集群负载均衡)