docker实现nginx tocmat的负载均衡

简介: 应用场景当一个应用部署在tomcat后,发现访问量越来越多,服务器完全无法承受住压力,导致系统卡顿延时等,此时需要通过nginx技术将应用压力进行平均分配到其他服务器,将多台服务器一起提供资源,通过nginx来协调资源进行负载均衡。

应用场景

当一个应用部署在tomcat后,发现访问量越来越多,服务器完全无法承受住压力,导致系统卡顿延时等,此时需要通过nginx技术将应用压力进行平均分配到其他服务器,将多台服务器一起提供资源,通过nginx来协调资源进行负载均衡。

操作步骤

1. 目标与准备

目标:使用docker部署nginx+tomcat,nginx实现负载均衡。

准备:
docker主机,192.168.199.32 (docker2)
tomcat容器1:tomcat01
tomcat容器2:tomcat03
nginx容器:nginx_test

2. 镜像下载

docker主机pull镜像文件,需要tomcat和nginx。
 # docker pull docker.io/tomcat
 # docker pull docker.io/malderhout/tomcat
 # docker pull nginx

下载两个不同版本的tomcat镜像文件,只是为了后期更好的验证nginx的负载均衡。

用# docker search tomcat命令搜索tomcat的镜像,进行选择,选择一个tomcat7,一个tomcat8。

这里写图片描述

3. 部署2个tomcat容器

 # docker run -d -ti --name tomcat01 docker.io/tomcat
 # docker run -d -ti --name tomcat03 docker.io/malderhout/tomcat
查看两个容器是否已开启:
 # docker ps

这里写图片描述

tomcat容器默认启用8080端口。

4. 部署nginx容器,并修改配置文件,commit新镜像。

启动nginx容器,并进入到容器:
 # docker run -ti docker.io/nginx /bin/bash
进入容器中,进行修改配置文件:
 # cd /etc/nginx/
 # cd conf.d/
 # vi default.conf
   bash: vi: command not found
提示没有vi命令,执行
 # apt-get update
 # apt-get install vim
编辑default.conf文件,如下:
 # vim default.conf
upstream web_app{
server tomcat01:8080 weight=1 max_fails=2 fail_timeout=30s;
server tomcat03:8080 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 80;
server_name nginx_test localhost;
index index.jsp index.html index.htm;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://web_app;
root /usr/share/nginx/html;
index index.html index.htm;
}
location ~* \.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /usr/share/nginx/html;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
#location ~ /\.ht {
# deny all;
#}
}

该配置文件为网上找的,如果有好的配置文件可以替换。 注意,配置文件引用的为容器的name,而不是ip地址,好处就是不用关心ip变动。nginx_test ,为nginx容器name,一会需要创建。

手动执行nginx,看是否报错,如果报错进行调试,直至没有报错。
 # nginx
 # ps -ef

这里写图片描述

复制这个临时容器的id:0b85b3e2edda
退出临时容器,使用刚创建的临时容器commit新的镜像,镜像名:nginx_tomcat

 # docker commit 0b85b3e2edda nginx_tomcat
 # docker images

这里写图片描述

使用nginx_tomcat部署nginx_test容器:

 # docker run -d -p 80:80 --name nginx_test nginx_tomcat nginx -g “daemon off;”
 # docker ps

这里写图片描述

5. 验证

浏览器打开: http://192.168.199.32
连续打开2次,发现第一个为tomcat8.0界面,第二个为tomcat7.0界面。

这里写图片描述

这里写图片描述

相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
17天前
|
负载均衡 算法 应用服务中间件
面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
字节跳动面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
32 0
|
24天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
99 0
|
1月前
|
前端开发 应用服务中间件 nginx
使用Docker快速搭建Web服务器Nginx
本文指导如何使用Docker快速搭建Nginx服务器。首先,通过`docker pull`命令获取Nginx镜像,然后以容器形式运行Nginx并映射端口。通过挂载目录实现本地文件与容器共享,便于自定义网页。使用`docker ps`检查运行状态,访问IP:8088确认部署成功。最后,介绍了停止、删除Nginx容器的命令,强调Docker简化了服务器部署和管理。
50 0
|
1天前
|
运维 应用服务中间件 nginx
【docker】记录一次nginx启动失败的检测
【docker】记录一次nginx启动失败的检测
11 1
|
7天前
|
负载均衡 应用服务中间件 nginx
Nginx 负载均衡
Nginx 负载均衡
21 2
|
21天前
|
应用服务中间件 nginx Docker
docker实现Nginx
通过以上步骤,你可以使用Docker来快速搭建并运行Nginx服务器,而且可以根据需要进行自定义配置和持久化数据。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
17 1
|
1月前
|
NoSQL 关系型数据库 MySQL
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
221 0
|
1月前
|
负载均衡 应用服务中间件 nginx
|
1月前
|
前端开发 应用服务中间件 nginx
Docker安装nginx和基本配置
Docker安装nginx和基本配置
136 0
|
4月前
|
JavaScript 应用服务中间件 nginx