欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker+Nginx+Tomcat配置简单的负载均衡

程序员文章站 2024-02-21 10:19:58
...

环境说明

虚拟机 Centos7 IP 192.168.1.144
[[email protected] ~]# uname -a
Linux master 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

Nginx安装

[[email protected] ~]# yum install nginx
[[email protected] ~]# nginx -v
nginx version: nginx/1.12.2

Docker安装

[[email protected] ~]#yum install -y yum-utils device-mapper-persistent-data lvm2
[[email protected] ~]#yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[[email protected] ~]#yum -y install docker-ce
[[email protected] ~]#systemctl start docker
[[email protected] ~]# docker version
Client:
 Version:           18.09.4
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        d14af54266
 Built:             Wed Mar 27 18:34:51 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.4
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.8
  Git commit:       d14af54
  Built:            Wed Mar 27 18:04:46 2019
  OS/Arch:          linux/amd64
  Experimental:     false

Docker运行tomcat实例

[[email protected] ~]# docker pull tomcat
[[email protected] ~]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
tomcat               latest              f1332ae3f570        5 days ago          463MB
#创建docker连接目录
[[email protected] ~]# mkdir 4480
[[email protected] ~]# mkdir 5580
#运行4480实例
[[email protected] ~]# docker run --name tomcat44 -p 4480:8080 -v /root/4480:/usr/local/tomcat/webapps/ROOT -d tomcat
#运行5580实例
[[email protected] ~]# docker run --name tomcat55 -p 5580:8080 -v /root/5580:/usr/local/tomcat/webapps/ROOT -d tomcat
#检查
[[email protected] ~]# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp6       0      0 :::5580                 :::*                    LISTEN      31560/docker-proxy  
tcp6       0      0 :::4480                 :::*                    LISTEN      31385/docker-proxy  
#创建index文件
[[email protected] ~]# cd /root/4480
[[email protected] 4480]# echo 4480 > index.html
[[email protected] ~]# cd /root/5580
[[email protected] 5580]# echo 5580 > index.html

测试

访问 http://192.168.1.144:4480/
4480
访问 http://192.168.1.144:5580/
5580

Nginx 负载均衡

[[email protected] ~]# vi /etc/nginx/nginx.conf

在http节点中(大概40行 :40)增加如下内容:

       server {
                listen 80;
                server_name 192.168.1.144;
                location  / {
                        proxy_pass http://tomcat;
                }
        }
        upstream tomcat{
                server localhost:4480 weight=5;
                server localhost:5580 weight=5;
        }

#启动Ngixn
[[email protected] ~]# systemctl start nginx

测试

访问 http://192.168.1.144
4480或5580
这时刷新就会发现内容会在4480和5580之间切换。
这样就达到了负载均衡。

相关标签: 负载均衡