Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

分享:使用nginx做nacos集群负载均衡 #9004

Open
qq670176019 opened this issue Aug 21, 2022 · 16 comments
Open

分享:使用nginx做nacos集群负载均衡 #9004

qq670176019 opened this issue Aug 21, 2022 · 16 comments
Labels
area/Document Category issues or prs related to document. good first issue Good for newcomers

Comments

@qq670176019
Copy link

1、分享的目的
网络鱼龙混杂,鱼目混珠,内容良莠不齐,大家为了所谓的积分,不顾三七二十一搬运别人的,有的甚至一字不差照搬不误,误人子弟!关于这个使用nginx做nacos集群复杂均衡问题,我搜了好多,都是劣等的教程,终于在一个角落看到了解决办法,看到也有很多朋友之前问过这种问题,现在一起分享一下吧
2、要解决的问题
有N(N>=3)台机器要组建nacos集群:
192.168.190.128:8848; 192.168.190.129:8848; 192.168.190.130:8848;
普通方法是,在server-addr中使用逗号隔开几个nacos集群地址,但是此种问题的缺点在于,集群扩建时,需要重新打包代码、重启才可以,但是很明显这种方式不是很友好。
那就需要想一个办法,使用nginx负载均衡来做,不再废话,下边直接上教程供参考:
3、解决办法
1)准备三台机器,并配置好各自的nacos
cluster.conf:
192.168.190.128:8848 192.168.190.129:8848 192.168.190.130:8848
startup.sh:
export MODE="cluster"
启动文件中默认的内存占用2G 2G 1G,可以按需调整,注意最后一个要比前边的小,一般是小一半
application.properties:
只修改了如下:
spring.datasource.platform=mysql db.url.0=jdbc:mysql://192.168.190.1:3306/config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC db.user.0=root db.password.0=12345678
2)设置nginx负载均衡
安装nginx时,注意要安装上stream模块,即,在./configure 阶段,--with-stream
配置文件:
`
#user nginx nginx;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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"';
client_max_body_size 300m;
#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;
upstream cluster{
	server 192.168.190.128:8848;
	server 192.168.190.129:8848;
	server 192.168.190.130:8848;
}
server {
    listen       7847;
    server_name  localhost;
	charset utf-8;
    location / {
         proxy_pass http://cluster/;             
    }
	#access_log  /data/nginx/logs/nacos.log;
	#error_log  /data/nginx/logs/nacos.log;
}

}

nacos的grpc协议配置

stream {
# 负载均衡配置(TCP长连接配置)
upstream lb-nocos-tcp{
server 192.168.190.128:9848 weight=1;
server 192.168.190.129:9848 weight=1;
server 192.168.190.130:9848 weight=1;
}
server {
listen 8847;
proxy_pass lb-nocos-tcp;
}
}
`
由于nginx放在了三台nacos机器中的一台上,所以把监听的端口进行了修改。
nginx配置的关键就在于,gRPC通讯部分,nacos会在默认的端口上偏移1000进行通讯,但是网络上很多教程根本就没有提过这个stream,这才是关键问题。
在这里,我用7847对应nacos各个机器的8848,偏移1000,用8847对应各个机器的9848
3)程序中使用
server-addr: 192.168.190.128:7847
如果多环境,可以在跟pom中配置profiles,然后各个项目中引用。

至此,结束,如果各位还有更好的方案或者优化,希望能够共享一下

@karsonto
Copy link
Contributor

网络上大多数是基于nacos1.x版本进行配置,2.x版本很少提及,谢谢分享👍

@KomachiSion KomachiSion added the good first issue Good for newcomers label Aug 22, 2022
@simpleLiYu
Copy link

网络上大多数是基于nacos1.x版本进行配置,2.x版本很少提及,谢谢分享👍

你好,请问下如果我是用三台服务器做集群有几个问题咨询一下:

  1. cluster.conf文件中使用公网ip还是内网ip
  2. nginx upstream代理使用的是公网ip还是内网ip
  3. nginx grpc协议配置 stream代理使用的是公网ip还是内网ip

目前我是参考网上说的使用方式:

  1. cluster.conf文件中使用公网ip,并且在启动文件startup.sh加上-Dnacos.server.ip=对应服务器公网IP
  2. nginx upstream代理使用的是公网ip
  3. nginx grpc协议配置 stream代理使用的是公网ip
    结果是集群列表里面有公网IP有内网IP而且有的是上线有的是下线很乱

@qq670176019
Copy link
Author

都是用局域网IP,局域网多快,没必要去外网绕一圈

@KomachiSion
Copy link
Collaborator

KomachiSion commented Sep 6, 2022

@qq670176019 I think you can make this share as an blog article, and submit it into nacos.io.

@qq670176019
Copy link
Author

@qq670176019 I think you can make this share as an blog article, and submit it into nacos.io.

主要我的水平实在是差,也只能是发出来大家看看,可能还会有认知问题等

@KomachiSion
Copy link
Collaborator

@qq670176019 I think you can make this share as an blog article, and submit it into nacos.io.

主要我的水平实在是差,也只能是发出来大家看看,可能还会有认知问题等

我认为这不是问题,你可以在你的文章中梳理你遇到的问题,然后你是怎么排查到这个问题的, 最后是按照这个方法解决的。 写这样一篇博客发到官网就可以了, 和水平什么的没有关系。

这样和你有相同的问题的同学就可以找到博客,然后尝试解决了。 文章里可以留这个issue的连接,作者部分可以留邮箱,如果大家又问题就邮件或者issue里继续讨论就好。

只放issue里一来背景不明确,二来曝光不够。

@KomachiSion KomachiSion added the area/Document Category issues or prs related to document. label Sep 22, 2022
@shixueshang
Copy link

3台机器还需要开放 9848 9849端口吗

@reminisences
Copy link

3台机器还需要开放 9848 9849端口吗

我之前排查日志报错的时候开了4个端口才解决:8848/9848/7848/8849, 几乎都开了,没看nacos通讯机制,仅从报错来看需要开这些端口.

@yuchang01
Copy link

nginx http2 代理 nacos 2.x 时为啥会连接中断?有知道的吗?

@DaiYuanchuan
Copy link

同样的情况,nacos2.1.2
一开始连接是没有问题的 ,但它总会在一个时间段内报 连接失败的 错误

这是我一开始使用nginx配置的
image
一开始是可以连接的,可是用着用着就会报连接异常,过一段时间又会自动恢复,我开始以为是我的nginx不支持长连接,或者是nginx是问题,所以改成了下面的,使用ip直接连接到nacos
image

但是我依旧在日志里面找到了连接异常的问题
image

找到了日志时间节点对应的nacos节点的日志,那一个时间段内,nacos在大量的报错
image

过了那个时间段,它又会恢复正常 ......

@lhztop
Copy link

lhztop commented Apr 23, 2023

我问一下,cluster模式下,每个实例用的数据库都是同一个吗?
我配置成同一个数据库,经常出问题,也不知道是哪里出问题了

@qq670176019
Copy link
Author

qq670176019 commented Apr 23, 2023 via email

@DaiYuanchuan
Copy link

我问一下,cluster模式下,每个实例用的数据库都是同一个吗? 我配置成同一个数据库,经常出问题,也不知道是哪里出问题了

理论上是的,不然可能数据不同步?

@hanzongbo
Copy link

Nacos开启鉴权功能后,用Nginx代理Nacos集群,upstream 和 stream都配置了,配置确认无误,项目中server-addr用Nginx代理地址报403:NacosException: http error, code=403,msg=user not found!,,username和password那写了,如果server-addr:用的是Nacos地址不用代理是正常的,有遇到过的吗

@qq670176019
Copy link
Author

qq670176019 commented Aug 8, 2023 via email

@zhou-lang
Copy link

1、分享的目的 网络鱼龙混杂,鱼目混珠,内容良莠不齐,大家为了所谓的积分,不顾三七二十一搬运别人的,有的甚至一字不差照搬不误,误人子弟!关于这个使用nginx做nacos集群复杂均衡问题,我搜了好多,都是劣等的教程,终于在一个角落看到了解决办法,看到也有很多朋友之前问过这种问题,现在一起分享一下吧 2、要解决的问题 有N(N>=3)台机器要组建nacos集群: 192.168.190.128:8848; 192.168.190.129:8848; 192.168.190.130:8848; 普通方法是,在server-addr中使用逗号隔开几个nacos集群地址,但是此种问题的缺点在于,集群扩建时,需要重新打包代码、重启才可以,但是很明显这种方式不是很友好。 那就需要想一个办法,使用nginx负载均衡来做,不再废话,下边直接上教程供参考: 3、解决办法 1)准备三台机器,并配置好各自的nacos cluster.conf: 192.168.190.128:8848 192.168.190.129:8848 192.168.190.130:8848 startup.sh: export MODE="cluster" 启动文件中默认的内存占用2G 2G 1G,可以按需调整,注意最后一个要比前边的小,一般是小一半 application.properties: 只修改了如下: spring.datasource.platform=mysql db.url.0=jdbc:mysql://192.168.190.1:3306/config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC db.user.0=root db.password.0=12345678 2)设置nginx负载均衡 安装nginx时,注意要安装上stream模块,即,在./configure 阶段,--with-stream 配置文件: ` #user nginx nginx; worker_processes 1;

#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include 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"';
client_max_body_size 300m;
#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;
upstream cluster{
	server 192.168.190.128:8848;
	server 192.168.190.129:8848;
	server 192.168.190.130:8848;
}
server {
    listen       7847;
    server_name  localhost;
	charset utf-8;
    location / {
         proxy_pass http://cluster/;             
    }
	#access_log  /data/nginx/logs/nacos.log;
	#error_log  /data/nginx/logs/nacos.log;
}

}

nacos的grpc协议配置

stream { # 负载均衡配置(TCP长连接配置) upstream lb-nocos-tcp{ server 192.168.190.128:9848 weight=1; server 192.168.190.129:9848 weight=1; server 192.168.190.130:9848 weight=1; } server { listen 8847; proxy_pass lb-nocos-tcp; } } ` 由于nginx放在了三台nacos机器中的一台上,所以把监听的端口进行了修改。 nginx配置的关键就在于,gRPC通讯部分,nacos会在默认的端口上偏移1000进行通讯,但是网络上很多教程根本就没有提过这个stream,这才是关键问题。 在这里,我用7847对应nacos各个机器的8848,偏移1000,用8847对应各个机器的9848 3)程序中使用 server-addr: 192.168.190.128:7847 如果多环境,可以在跟pom中配置profiles,然后各个项目中引用。

至此,结束,如果各位还有更好的方案或者优化,希望能够共享一下

你好,我这边有几个问题请教一下。
《 nginx配置的关键就在于,gRPC通讯部分,nacos会在默认的端口上偏移1000进行通讯,但是网络上很多教程根本就没有提过这个stream,这才是关键问题。 在这里,我用7847对应nacos各个机器的8848,偏移1000,用8847对应各个机器的9848 3)程序中使用 server-addr: 192.168.190.128:7847 如果多环境,可以在跟pom中配置profiles,然后各个项目中引用。》这是你的原文。
我的问题是: 你说的是偏移1000,是nacos自动偏移的吧? nginx用不用跟着一起设置,我看你http的使用nginx的7847反向代理到nacos的8848端口。 使用nginx的8847反向代理到nacos的9848端口。程序中使用 server-addr: 192.168.190.128:7847。

问题1:程序直接使用nginx的7847 ( server-addr: 192.168.190.128:7847),代理到的到底是nacos的8848还是9848
问题2:如果我的nginx因为安全要求,只能开80和443端口来应对http和https协议,我能用nginx的80端口来反向代理grpc的9848端口吗?
问题3: 如果问题1成立。 现在nacos集群是2.x版本,客户端有1.x,也有2.x。我如果想用两个域名来做反向代理的话。nginx这样设置可以不可以:
upstream nacos-8848 {
server ip1:8848 weight=10;
server ip2:8848 weight=10;
server ip3:8848 weight=10;
}
server {
listen 80;
server_name nacos1111.prod.com;
proxy_pass http://nacos-8848/;
}

upstream nacos-9848 {

server ip1:9848 weight=10;
server ip2:9848 weight=10;
server ip3:9848 weight=10;
}
server {
listen 80;
server_name nacos2221.prod.com;
proxy_pass http://nacos-9848/;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/Document Category issues or prs related to document. good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests