nacos集群搭建
[TOC]
- 机器规划清单
IP地址 | 角色 | nacos端口 | 操作系统 | 数据目录 |
---|---|---|---|---|
10.30.3.231 | 节点1 | 8848 | CentOS 7.9 x64 | /data/nacos_8848 |
10.30.3.232 | 节点2 | 8848 | CentOS 7.9 x64 | /data/nacos_8848 |
10.30.3.233 | 节点3 | 8848 | CentOS 7.9 x64 | /data/nacos_8848 |
搭建nacos集群
1.通用配置
- 1.1.所有节点:安装jdk 1.8.0_391(注:nacos依赖jdk1.8及以上版本)
curl -sL http://iso.sqlfans.cn/jdk/install_jdk_8u391.sh | bash
source /etc/bashrc
java -version
- 1.2.所有节点:调整防火墙,否则节点之间无法通讯会导致集群端口无法监听
/usr/sbin/iptables -F
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service
- 1.3.mysql节点:创建nacos数据库和账号,导入初始化脚本(参考
.conf/mysql-schema.sql
)
curl -sL http://iso.sqlfans.cn/linux/nacos-schema-2.2.3.sql -o /opt/nacos-schema-2.2.3.sql
mysql -uroot -p -S /tmp/mysql_3306.sock -e"CREATE DATABASE IF NOT EXISTS nacos DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
mysql -uroot -p -S /tmp/mysql_3306.sock -e"CREATE USER IF NOT EXISTS 'user_nacos'@'%' IDENTIFIED BY 'KKi3Vr4aHB_SkmX2';"
mysql -uroot -p -S /tmp/mysql_3306.sock -e"GRANT ALL PRIVILEGES ON nacos.* TO 'user_nacos'@'%';"
mysql -uroot -p -S /tmp/mysql_3306.sock -e"FLUSH PRIVILEGES;"
mysql -uroot -p -S /tmp/mysql_3306.sock nacos -e"source /opt/nacos-schema-2.2.3.sql;"
2.搭建nacos集群
- 2.1.所有节点:下载 nacos 2.2.3
mkdir -p /data/
curl -L http://iso.sqlfans.cn/linux/nacos-server-2.2.3.tar.gz -o /opt/nacos-server-2.2.3.tar.gz
tar -zxf /opt/nacos-server-2.2.3.tar.gz -C /opt/
mv /opt/nacos /data/nacos_8848
- 2.2.所有节点:配置nacos集群的节点信息,示例节点
10.30.3.231、10.30.3.232、10.30.3.233
echo "10.30.3.231:8848" > /data/nacos_8848/conf/cluster.conf
echo "10.30.3.232:8848" >> /data/nacos_8848/conf/cluster.conf
echo "10.30.3.233:8848" >> /data/nacos_8848/conf/cluster.conf
chmod 755 /data/nacos_8848/conf/cluster.conf
- 2.3.所有节点:开启鉴权登录(注意:Nacos 2.2.0.1以前的版本默认开启auth鉴权)
sed -i 's/^nacos.core.auth.system.type.*/nacos.core.auth.system.type=nacos/' /data/nacos_8848/conf/application.properties
sed -i 's/^nacos.core.auth.enabled.*/nacos.core.auth.enabled=true/' /data/nacos_8848/conf/application.properties
sed -i 's/^nacos.core.auth.server.identity.key.*/nacos.core.auth.server.identity.key=nacos/' /data/nacos_8848/conf/application.properties
sed -i 's/^nacos.core.auth.server.identity.value.*/nacos.core.auth.server.identity.value=nacos/' /data/nacos_8848/conf/application.properties
sed -i 's/^nacos.core.auth.plugin.nacos.token.secret.key.*/nacos.core.auth.plugin.nacos.token.secret.key=VGhpc0lzTXlDdXN0b21TZWNyZXRLZXkwMTIzNDU2Nzg=/' /data/nacos_8848/conf/application.properties
cat /data/nacos_8848/conf/application.properties | grep "^nacos.core.auth.default.token.secret.key" || echo "nacos.core.auth.default.token.secret.key=" >> /data/nacos_8848/conf/application.properties
sed -i 's/^nacos.core.auth.default.token.secret.key.*/nacos.core.auth.default.token.secret.key=/' /data/nacos_8848/conf/application.properties
cat /data/nacos_8848/conf/application.properties | grep "^nacos.core.auth"
- 2.4.所有节点:集群模式必须配置mysql信息,示例mysql节点为
10.30.3.234
cat /data/nacos_8848/conf/application.properties | grep "^spring.datasource.platform=" || echo "spring.datasource.platform=mysql" >> /data/nacos_8848/conf/application.properties
cat /data/nacos_8848/conf/application.properties | grep "^db.num=" || echo "db.num=1" >> /data/nacos_8848/conf/application.properties
cat /data/nacos_8848/conf/application.properties | grep "^db.url.0=" || echo "db.url.0=jdbc:mysql://10.30.3.234:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC" >> /data/nacos_8848/conf/application.properties
cat /data/nacos_8848/conf/application.properties | grep "^db.user.0=" || echo "db.user.0=user_nacos" >> /data/nacos_8848/conf/application.properties
cat /data/nacos_8848/conf/application.properties | grep "^db.password.0=" || echo "db.password.0=KKi3Vr4aHB_SkmX2" >> /data/nacos_8848/conf/application.properties
cat /data/nacos_8848/conf/application.properties | egrep "(^spring.datasource.platform|^db.num|^db.url.0|^db.user|^db.password.0)"
- 2.5.所有节点:启动nacos(7848即刻监听,而8848/9848/9849待半数以上节点启动后才被监听),若8848端口未监听,请检查下防火墙
#.以root用户启动
sh /data/nacos_8848/bin/startup.sh -p embedded
sleep 5 && netstat -lnpt | egrep "(7848|8848|9848|9849)"
#.以appadmin用户启动
# groupadd appadmin
# useradd appadmin -g appadmin -d /home/appadmin -s /bin/bash
# echo 'Admin_147' | passwd appadmin --stdin
# cat /etc/sudoers | grep appadmin || echo "appadmin ALL=NOPASSWD:ALL" >> /etc/sudoers
# chown -R appadmin.appadmin /data/nacos_8848
# su - appadmin -c "source /etc/bashrc && sh /data/nacos_8848/bin/startup.sh -p embedded" > /dev/null
- 2.6.所有节点:加入开机启动
cat /etc/rc.local | grep nacos || echo 'sh /data/nacos_8848/bin/startup.sh -p embedded' >> /etc/rc.local
- 2.7.任意节点:登录nacos页面控制台,修复初始弱密码,比如
10.30.3.231
地址:http://10.30.3.231:8848/nacos
账号:nacos
nacos初始密码为:nacos
nacos重置密码为:pFDQqSOhZcH_G3Cs
- 2.8.所有节点:优化jvm最大内存(建议512m)
cat /data/nacos_8848/bin/startup.sh | grep "Xm"
遇到的问题
场景1:如何彻底卸载nacos
- 所有节点:杀掉进程、删除目录、清除开机启动
cd /opt/
ps -ef | grep nacos | grep -v grep | awk '{print $2}' | xargs kill -9 2> /dev/null
rm -rf /data/nacos_8848
sed -i '/nacos/d' /etc/rc.local
- mysql节点:删库、删账号
mysql -uroot -p -S /tmp/mysql_3306.sock -e"drop database if exists nacos;"
mysql -uroot -p -S /tmp/mysql_3306.sock -e"drop user 'user_nacos'@'%';"
场景2:启动nacos未监听8848端口
- 症状:搭建 nacos 集群,在启动 nacos 进程之后,只监听了
7848
端口,而8848
端口未监听 - 解决:执行
/usr/sbin/iptables -F
关掉防火墙
场景3:启动nacos报错 Error creating bean with name embeddedDumpService
- 症状:搭建 nacos 集群,启动 nacos 失败,启动日志报错如下:
[root@localhost ~]# cat /data/nacos_8848/logs/start.out | egrep "(Error|ERROR|errCode)"
2023-08-24 19:48:55,891 ERROR Startup errors :
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'memoryMonitor' defined in URL [jar:file:/data/nacos_8848/target/nacos-server.jar!/BOOT-INF/lib/nacos-config-2.2.3.jar!/com/alibaba/nacos/config/server/monitor/MemoryMonitor.class]: Unsatisfied dependency expressed through constructor parameter 0;
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'asyncNotifyService': Unsatisfied dependency expressed through field 'dumpService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embeddedDumpService': Invocation of init method failed; nested exception is ErrCode:500, ErrMsg:Nacos Server did not start because dumpservice bean construction failure :
errCode: 500, errMsg: java.lang.NullPointerException
- 解决:集群模式必须配置mysql信息,否则会报上述错误(单节点可以不用配置mysql信息)