测试环境
准备工作 | IP地址/容器的名称 |
---|---|
docker 主机 | 192.168.1.10 |
tomcat:latest | tomcat1 |
tomcat:latest | tomcat2 |
nginx:latest | nginx |
搭建过程
准备好Nginx 和 Tomcat 镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest a92c139758db 2 years ago 558 MB
nginx latest 3f8a4339aadd 2 years ago 108 MB
创建共享卷用于容器和主机之间的映射目录
*在启动之前,我们先创建容器和主机之间的映射的共享卷*
[root@localhost ~]# mkdir www1
[root@localhost ~]# mkdir www2
[root@localhost ~]# cd www1
[root@localhost www1]# vi index.jsp //创建文件,写入如下内容
<html>
<head>
<title>Tomcat1</title>
</head>
<body>
<%
out.println("this is Tomcat1");
%>
</body>
</html>
[root@localhost www1]# cd ..
[root@localhost ~]# cd www2 //进入另外一个目录
[root@localhost www2]# vi index.jsp //写入如下内容
<html>
<head>
<title>Tomcat2</title>
</head>
<body>
<%
out.println("this is Tomcat2");
%>
</body>
</html>
启动Tomcat镜像为容器
[root@localhost ~]# docker run -itd --name tomcat1 -P -v /root/www1:/usr/share/tomcat/webapps/ROOT tomcat:latest
[root@localhost ~]# docker run -itd --name tomcat2 -P -v /root/www2:/usr/share/tomcat/webapps/ROOT tomcat:latest
让宿主机的www1目录和www2目录映射到Tomcat的网站发布目录中去
-v /root/www1:/usr/share/tomcat/webapps/ROOT
-P 指定随机端口
[root@localhost ~]# docker ps //查看运行状态
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
425d605672c3 tomcat:latest "catalina.sh run" 5 seconds ago Up 4 seconds 0.0.0.0:32771->8080/tcp tomcat2
cd9ca608a5be tomcat:latest "catalina.sh run" 11 seconds ago Up 11 seconds 0.0.0.0:32770->8080/tcp tomcat1
访问测试
注:如果这里访问不了,可能是因为Tomcat没有启动我们可以执行以下操作:
[root@localhost ~]# docker exec -it tomcat2 /bin/bash //进入容器
root@425d605672c3:/usr/local/tomcat# cd bin/
root@425d605672c3:/usr/local/tomcat/bin# startup.sh //开启服务即可
Tomcat started.
启动Nginx镜像为容器
[root@localhost ~]# docker run -itd --name nginx -p 80:80 nginx:latest //启动Nginx镜像
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f850a7b10728 nginx:latest "nginx -g 'daemon ..." 2 seconds ago Up 1 second 0.0.0.0:80->80/tcp nginx
在宿主机上编辑一个nginx.conf 文件(这个文件可自行下载nginx复制而得)
[root@localhost ~]# vi 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 {
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;
#主要添加如下内容即可
upstream tomcat {
server 192.168.1.10:32770; //指定Tomcat服务的IP地址以及端口号
server 192.168.1.10:32771;
}
server {
listen 80;
server_name 192.168.1.10;
location / {
proxy_pass http://tomcat; //调用Tomcat服务 设置Tomcat的代理
}
}
}
以为修改好了以后,我们将这个文件复制到Nginx容器里面然后重启容器即可
[root@localhost ~]# docker cp nginx.conf f850a7b10728:/etc/nginx/nginx.conf
[root@localhost ~]# docker restart f850a7b10728
效果验证
然后不断刷新(Nginx会在Tomcat1和Tomcat2之间来回进行切换,达到负载分担效果)