1 nacos服务准备工作

1.1 官网下载nacos(如2.0.3版本)

https://github.com/alibaba/nacos/releases/tag/2.0.3

1.2 解压nacos(如2.0.3版本)

tar -xvf nacos-server-2.0.3.tar.gz

1.3 配置nacos数据源(使用外部存储,如mysql)

cd /app/nacos/conf

vi application.properties

1.4 配置nacos集群节点

cp /app/nacos/conf/cluster.conf.example /app/nacos/conf/cluster.conf

vi cluster.conf

# 配置三个或以上nacos集群节点地址(IP:PORT)

#it is ip

#example

192.168.16.101:8848

192.168.16.102:8848

192.168.16.103:8848

2 配置nacos多节点

准备三个或以上nacos节点服务器,以及一台nacos代理服务器(nginx+haproxy)

nacos集群节点的服务配置一样,均按照1中的1.3和1.4配置,或者直接复制一个已经配置好的节点

3 管理nacos集群节点

官网推荐使用VIP/nginx代理nacos的控制台和grpc通信集群节点,如下图,由于nginx的短链接的限制,这里采用nginx代理nacos的控制台节点(http代理) 和 haproxy代理nacos的grpc通讯节点(tcp)

nacos linux 部署 please set java_home variable linux配置nacos集群_nginx

3.1 nginx代理nacos控制台节点

在nginx.conf中的http配置块中增加如下内容

#nacos控制台代理
upstream nacoscluster{
       #nacos2.0初始启动端口
       server *.*.*.*:8848 weight=1;
       server *.*.*.*:8848 weight=1;
       server *.*.*.*:8848 weight=1;
}
server {
     listen 8848;
     location / {
         root  /nacos/;
         proxy_pass http://nacoscluster;
     }
}

 

 

3.2 haproxy代理配置nacos grpc通信节点

3.2.1 下载解压haproxy

tar -zxvf /app/haproxy-2.4.7.tar.gz  #解压

 

3.2.2 进入haproxy-2.4.7命令

tar /app/haproxy-2.4.7

 

3.2.3 编译配置

cd /app/haproxy-2.4.7

make TARGET=custom ARCH=x86_64 PREFIX=/app/haproxy

 

3.2.4 安装

make install PREFIX=/app/haproxy

 

3.2.5 配置haproxy

cd /app/haproxy

vi haproxy.cfg

global                                                                              #全局参数的设置

    log         127.0.0.1 local0                                                    #log语法:log <address_1>[max_level_1] # 全局的日志配置,使用log关键字,指定使用127.0.0.1上的syslog服务中的local0日志设备,记录日志等级为info的日志

    nbproc      1                                                                   #监控进程个数

    maxconnrate 300                                                                 #进程每秒所能创建的最大连接数

    maxcomprate 300                                                                 #压缩速率

    maxsessrate 500                                                                 #进程每秒能创建的会话数量

    chroot      /app/haproxy                                                        #当前工作目录

    pidfile     /app/haproxy.pid                                                    #当前进程id文件

    maxconn     4000                                                                #进程所能接收的最大并发连接数

    user        haproxy                                                             #启动用户名

    group       haproxy                                                             #启动用户组

    daemon                                                                          #以守护进程方式运行haproxy

    stats       socket /app/haproxy/stats                                           #开启统计socket

defaults

    mode                    tcp                                                    #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK

    log                     global                                                  #应用全局的日志配置    

    option                  dontlognull                                             #启用该项,日志中将不会记录空连接。所谓空连接就是在上游的负载均衡器或者监控系统为了探测该  服务是否存活可用时,需要定期的连接或者获取某一固定的组件或页面,或者探测扫描端口是否在监 听或开放等动作被称为空连接;官方文档中标注,如果该服务上游没有其他的负载均衡器的话,建议 不要使用该参数,因为互联网上的恶意扫描或其他动作就不会被记录下来

    option                  redispatch                                              #当使用了cookie时,haproxy将会将其请求的后端服务器的serverID插入到cookie中,以保证 会话的SESSION持久性;而此时,如果后端的服务器宕掉了, 但是客户端的cookie是不会刷新 的,如果设置此参数,将会将客户的请求强制定向到另外一个后端server上,以保证服务的正常。

    option                  http-use-htx                                            #启用http2

    option                  logasap                                                 #传输大文件时提前记录日志

    option                  tcplog                                                 #启用日志记录HTTP请求,默认haproxy日志记录是不记录HTTP请求日志

    retries                 3                                                       #定义连接后端服务器的失败重连次数,连接失败次数超过此值后将会将对应后端服务器标记为不可用

    timeout queue           1m                                                      #一个请求在队列里的超时时间

    timeout connect         10s                                                     #连接超时

    timeout client          1m                                                      #客户端超时

    timeout server          1m                                                      #服务器端超时

    timeout http-keep-alive 100s                                                    #设置http-keep-alive的超时时间

    timeout check           10s                                                     #检测超时

    #option http-server-close                                                        #每次请求完毕后主动关闭http通道

    #option forwardfor       except 127.0.0.0/8                                      #如果服务器上的应用程序想记录发起请求的客户端的IP地址, 需要在HAProxy上 配置此选项,  这样  HAProxy会把客户端的IP信息发送给服务器,在HTTP请求中添加"X-Forwarded-For"字 段。  启用  X-Forwarded-For,在requests头部插入客户端IP发送给后端的server,使后端server获 取到客户端的真实IP。        

    #timeout http-request    10s                                                     #http请求超时时间    

    #maxconn                 3000                                                    #每个进程可用的最大连接数

listen admin_stats

    stats                   enable                                                  #启动管理后台

    bind                    0.0.0.0:12888                                           #监控端口设置    

    mode                  http                                                    #管理控制台模式

    log                     global                                                  #日志配置

    maxconn                 10                                                      #最大连接数

    stat uri                /admin                                                  #登录监控子路径配置

    stat realm              welcome\    Haproxy                                     #登录提示信息

    stat auth               haproxy:haproxy                                         #监控的账号密码

    stat admin              if TRUE                                                 #启用管理员模式

    stat refresh            30s                                                     #监控刷新时间

    stat                    hide-version                                            #隐藏页面版本号

    option                httplog                                                 #http日志记录    

frontend nacos_cluster                                                              #代理集群配置(名称自定义)

    bind                    :9848                                                   #代理端口

    mode                   tcp                                                    #代理模式

    log                     global                                                  #日志配置

    maxconn                 8000                                                    #最大连接数    

    default_backend         nacos_cluster_nodes                                     #代理节点名称。此处将对于的请求转发给后端

backend nacos_cluster_nodes                                                         #集群节点(名称自定义), 需要与frontend的配置项default_backend值一致

    mode                    tcp                                                    #代理模式

    #balance                 roundrobin                                              #负载均衡算法

    server                  nacos1 *.*.*.*:9848  check                          #集群节点

    server                  nacos2 *.*.*.*:9848  check                          #集群节点

    server                  nacos3 *.*.*.*:9848  check                        #集群节点

 

3.2.6 添加用户haproxy和用户组haproxy

useradd haproxy

 

3.2.7 启动

cd /app/haproxy/sbin

./haproxy -f /app/haproxy/haproxy.cfg

 

3.2.8 验证haproxy启动

http://ip:12888/admin

haproxy haproxy

其中端口12888和用户名密码haproxy都是通过haproxy.cfg配置文件处理

 

 

4 常见问题

nginx代理问题

1 HTTP Status 400 – Bad Request

确认负载均衡配置变量是否包含了下划线,如nacos_cluster,此时应改成nacoscluster

2 UNAVAILABLE: HTTP status code 502

需要将haproxy.cfg中的http和httplog内容(listen admin_stats下的除外)替换成tcp和tcplog

 

 

nginx 增加tcp支持

nginx默认安装的时候没有加载stream模块

需要重新对源文件进行编译、安装,通过添加--with-stream(tcp) 和--with-http_v2_module(grpc)参数指定安装stream模块

./configure  --prefix=指定安装路径  --with-stream 

# ./configure --prefix=/app/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module  --with-http_flv_module  --with-http_mp4_module --with-http_gzip_static_module --with-stream_ssl_module --with-stream --with-http_v2_module

make & make install

 

再次检查nginx.conf配置文件,确认配置无语法错误后,再次尝试启动服务。

nginx -t 检查配置文件是否正确

nginx -c 指定启动的配置文件

 

 

 

nacos状态查看

 

查看nacos服务个数

ps -ef|grep nacos|grep -v grep |wc -l

//查看所有端口连接数 

netstat -nat |grep "TIME_WAIT" | wc -l

//查看某个端口的状态

netstat -anlp|grep 9848 |grep  TIME_WAIT |wc -l

netstat -anlp|grep 9848 |grep  ESTABLISHED |wc -l

//查看服务器上各个状态的统计数量:

netstat -ant | awk '/^tcp/ {++y[$NF]} END {for(w in y) print w, y[w]}'

// 单独查看TIME_WAIT

ss -nat | grep TIME-WAIT