popyone
发布于 2024-02-22 / 13 阅读
0
0

部署Nacos 2.3.0 负载均衡集群

服务器

192.168.211.200
192.168.211.201
192.168.211.202

1、安装nacos运行环境 - java17

下载openjdk17

https://www.openlogic.com/openjdk-downloads?field_java_parent_version_target_id=807&field_operating_system_target_id=426&field_architecture_target_id=391&field_java_package_target_id=396

mkdir /usr/local/src/openjdk -p
mkdir /usr/lib/jvm
cd /usr/local/src/openjdk
wget https://builds.openlogic.com/downloadJDK/openlogic-openjdk/17.0.10+7/openlogic-openjdk-17.0.10+7-linux-x64.tar.gz
tar zxvf openlogic-openjdk-17.0.10+7-linux-x64.tar.gz -C /usr/lib/jvm/

添加jdk环境变量

vim /etc/profile

......
# jdk17
export JAVA_HOME=/usr/lib/jvm/openlogic-openjdk-17.0.10+7-linux-x64
export CLASSPATH=$JAVA_HOME/lib:$CLASSPATH
PATH=$JAVA_HOME/bin:$PATH

使配置生效

source /etc/profile

root@debian:/usr/lib/jvm# java --version
openjdk 17.0.10 2024-01-16
OpenJDK Runtime Environment OpenLogic-OpenJDK (build 17.0.10+7-adhoc.root.jdk17u)
OpenJDK 64-Bit Server VM OpenLogic-OpenJDK (build 17.0.10+7-adhoc.root.jdk17u, mixed mode, sharing)

2、安装nacos

① 下载解压nacos-server二进制包

mkdir /usr/local/src/nacos
wget https://github.com/alibaba/nacos/releases/download/2.3.0/nacos-server-2.3.0.tar.gz -O /usr/local/src/nacos/nacos-server-2.3.0.tar.gz
tar zxvf /usr/local/src/nacos/nacos-server-2.3.0.tar.gz -C /opt/

② 创建nacos存储目录

useradd nacos
mkdir /data/nacos/data -p
mkdir /data/nacos/logs
chown nacos: /data/nacos -R
chown nacos: /opt/nacos -R

创建目录软连接

ln -s /data/nacos/data /opt/nacos/data
ln -s /data/nacos/logs /opt/nacos/logs

④、修改application.properties,配置ip和数据库连接,开启鉴权(根据需要,增加连接超时时间)

vim /opt/nacos/conf/application.properties

server.servlet.contextPath=/nacos
server.port=8848
nacos.inetutils.ip-address=192.168.211.200
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://192.168.211.200:3306/nacos?characterEncoding=utf8&connectTimeout=5000&socketTimeout=8000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
db.user.0=nacos
db.password.0=123456
......

### The directory of access log:
server.tomcat.basedir=/opt/nacos
......

### If turn on auth system:
# nacos.core.auth.enabled=false
nacos.core.auth.enabled=true
......

### Since 1.4.1, worked when nacos.core.auth.enabled=true and nacos.core.auth.enable.userAgentAuthWhite=false.
### The two properties is the white list for auth and used by identity the request from other server.
nacos.core.auth.server.identity.key=eTUlN2tidExAazd0MVQqMUwycUtwZTl0ZGc3MzBlbGM=
nacos.core.auth.server.identity.value=I3BNeWE3NWNtQzlKMnZRSnpsa1lDd0JHMk5PQXpFWlo=
......

### The default token (Base64 String):
nacos.core.auth.plugin.nacos.token.secret.key=TFR1RDRUc25OQjB4QmZwbzg3cCFoIUVRZlZnd3F2WW0=
....
  • 三个服务器的nacos.core.auth.server.identity.keynacos.core.auth.server.identity.valuenacos.core.auth.plugin.nacos.token.secret.key保持一致,32字符转为Base64

⑤ 修改cluster.conf,配置nacos集群ip和端口

vim /opt/nacos/conf/cluster.conf

192.168.211.200:8848
192.168.211.201:8848
192.168.211.202:8848

⑥ 导入数据库结构

mysql-schema.sql

⑦ 启动nacos

cd /opt/nacos
su nacos -c "bin/startup.sh"
(单机模式 bin/startup.sh -m standalone)
  • 如果内存不够,可以通过修改调整bin/startup.sh中的JAVA_OPT参数。
  • 系统中如有多个java,可以将nacos的java环境变量配置到bin/startup.sh的起始位置。

⑧ 在三台服务器上安装haproxy,配置负载均衡

Nacos2.0版本相比1.X新增了gRPC的通信方式,因此需要增加2个端口。新增端口是在配置的主端口(server.port)基础上,进行一定偏移量自动生成。

端口 与主端口的偏移量 描述
9848 1000 客户端gRPC请求服务端端口,用于客户端向服务端发起连接和请求
9849 1001 服务端gRPC请求服务端端口,用于服务间同步等
7848 -1000 Jraft请求服务端端口,用于处理服务端间的Raft相关请求

使用VIP/nginx请求时,需要配置成TCP转发,不能配置http2转发,否则连接会被nginx断开。 9849和7848端口为服务端之间的通信端口,请勿暴露到外部网络环境和客户端测。

apt install haproxy
vim /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend nacos_8848
    mode                 tcp
    bind                 *:18848
    option               tcplog
    default_backend      nacos_8848

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend nacos_8848
    mode        tcp
    balance     leastconn
    server  nacos1 192.168.211.200:8848 check port 8848 inter 2000 rise 2 fall 3
    server  nacos2 192.168.211.201:8848 check port 8848 inter 2000 rise 2 fall 3
    server  nacos3 192.168.211.202:8848 check port 8848 inter 2000 rise 2 fall 3

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend nacos_9848
    mode                 tcp
    bind                 *:19848
    option               tcplog
    default_backend      nacos_9848

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend nacos_9848
    mode        tcp
    balance     leastconn
    server  nacos1 192.168.211.200:9848 check port 8848 inter 2000 rise 2 fall 3
    server  nacos2 192.168.211.201:9848 check port 8848 inter 2000 rise 2 fall 3
    server  nacos3 192.168.211.202:9848 check port 8848 inter 2000 rise 2 fall 3

⑨ 打开防火墙端口

防火墙需要开启4个端口:

  • 通过haproxy代理的对客户端端口18848和19848(代理端口8848和9848)。
  • 服务端通信端口9849和7848。

⑩ 最后可以安装keepalived做服务高可用,这里省略


评论