Loading

Docker中nginx+tomcat实现负载均衡

拉取tomcat镜像

docker pull tomcat 

运行两个tomcat容器

docker run -d -p 8088:8080 --name tomcat8088 tomcat
docker run -d -p 8089:8080 --name tomcat8089 tomcat

修改下index.jsp把两个tomcat区别开来

docker ps
docker exec -it 01c626690608 /bin/bash cd webapps/ROOT/ echo "<h1>Apache Tomcat 8088</h1>" >index.jsp
cat index.jsp
exit
docker restart 01c626690608
echo "test" >index.jsp  替换index.jsp的内容
echo "test" >>index.jsp 在文件末尾追加内容

另外一个也是一样的

获取nginx镜像

docker pull nginx
创建目录
mkdir -p /data/nginx/{conf,conf.d,html,logs}

 

创建配置文件nginx.conf
vim /data/nginx/conf/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 auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # 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 default_server;
        listen       [::]:80 default_server;
        server_name  192.168.1.4;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        proxy_pass http://pic; 
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

    upstream pic{
                server 192.168.1.4:8088 weight=5;
                server 192.168.1.4:8089 weight=5;
    }

}

创建nginx容器
docker run --name mynginx -d -p 80:80  -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /data/nginx/logs:/var/log/nginx -d docker.io/nginx

 

访问页面

 

--

 


至此nginx负载均衡已完成。


posted @ 2019-08-16 17:07  Reasonzzy  阅读(626)  评论(0编辑  收藏  举报