docker+Tomcat+Nginx实现负载均衡(也可以说是灾备)

环境准备:docker环境,两个tomcat,一个nginx,再加上之前配置的redis同步了session,这个配置就可以实现负载均衡了,我是用来灾备了,这篇文章主要讲述nginx+tomcat实现负载均衡,其他的教程参考之前的文章。

docker安装教程:https://blog.csdn.net/buyaopingbixiazai/article/details/89291116

docker安装配置tomcat:https://blog.csdn.net/buyaopingbixiazai/article/details/89344211

docker安装配置redis:https://blog.csdn.net/buyaopingbixiazai/article/details/89303538

docker利用redis+tomcat实现session共享:https://blog.csdn.net/buyaopingbixiazai/article/details/89342942

docker安装配置nginx:https://blog.csdn.net/buyaopingbixiazai/article/details/89389335

开始本篇:docker利用nginx+tomcat实现负载均衡

1.修改配置文件

我这里是两个tomcat,一主一备,主(1982.168.29.24:8089),备(192.168.29.24:8090),nginx放在192.168.29.24。

所以第一步先在配置文件里面添加如下内容:

upstream myproject {
            server 192.168.29.24:8089;
            server 192.168.29.24:8090 backup;
     }

第二步配置缺省访问路径:

server {
        listen 80;
        server_name nginx2tomcat1;   
    location / {
         proxy_pass http://myproject;
    }
   
    }

附上我的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;

	upstream myproject {
            server 192.168.29.24:8089;
            server 192.168.29.24:8090 backup;
     }
	
	
	
    keepalive_timeout  65;

    #gzip  on;
    server {
        listen 80;
        server_name nginx2tomcat1;
   	
	
	location / {
	 	proxy_pass http://myproject;
	}
	
	
	
    }
    include /etc/nginx/conf.d/*.conf;
}

2.重启

重启nginx:    docker restart 容器名称(容器ID)

3.验证

首先访问192.168.29.24,这时候应该访问到的是192.168.29.24:8089,登陆之后查看你页面,然后手动关掉8089端口的服务(docker stop jax-tomcat1),再次访问192.168.29.24,看到进入了192.168.29.24:8089的系统,测试成功!

3.1首次访问192.168.29.24并登录

docker+Tomcat+Nginx实现负载均衡(也可以说是灾备)_第1张图片

3.2关闭8089端口的服务器,


[root@localhost ~]# docker stop jax-tomcat1
jax-tomcat1
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
7f9f5c1e142f        tomcatwcy           "catalina.sh run"        2 days ago          Up 15 minutes       80/tcp, 0.0.0.0:8090->8080/tcp   jax-tomcat2
d2cec1acd172        redis:latest        "docker-entrypoint.s…"   3 days ago          Up 7 hours          0.0.0.0:6379->6379/tcp           jax_redis
4068c1c383f1        tomcat:7.0.92       "catalina.sh run"        3 weeks ago         Up 7 hours          0.0.0.0:8455->8080/tcp           checktomcat
6f9726681e83        mysql:5.7.19        "docker-entrypoint.s…"   3 weeks ago         Up 7 hours          0.0.0.0:3306->3306/tcp           mysql
5eb927494115        nginx               "nginx -g 'daemon of…"   5 weeks ago         Up 7 hours          0.0.0.0:80->80/tcp               nginx_test
f42ef6cc1aca        tomcat:7.0.92       "catalina.sh run"        5 weeks ago         Up 7 hours          0.0.0.0:8083->8080/tcp           tomcat3

3.3再次访问192.168.29.24

docker+Tomcat+Nginx实现负载均衡(也可以说是灾备)_第2张图片

说明:根据标记的地方即可区分是两个项目

你可能感兴趣的:(linux,docker,nginx)