二进制安装nacos单点和集群

二进制安装nacos单点和集群

Nacos部署

1. Nacos单机部署(版本2.5.4)

1.1 快速部署文档,官方参考文档

  • Nacos2.x 版本官方文档:https://nacos.io/zh-cn/docs/v2/quickstart/quick-start.html
  • Nacos Server下载和发布历史:https://nacos.io/download/nacos-server/?spm=5238cd80.47ee59c.0.0.189fcd36171OyJ
  • 2.5.4版本,依赖jdk8
  • 3.2.4版本,依赖jdk17

1.2 安装jdk

根据nacos版本进行选择。这里计划安装nacos2.5.4,故安装jdk8。

上传jdk的二进制包,通过 install_jdk.sh 脚本安装,注意安装路径。

mkdir /opt/local  
ls install_jdk.sh jdk-8u391-linux-x64.tar.gz  
sh install_jdk.sh && bash  
java -version

1.3 安装mysql 8.0(可选)

上传mysql的二进制包,通过offline_install_mysql57or80.sh脚本安装,注意安装路径。

[root@server ~]# ls offline_install_mysql57or80.sh mysql-8.0.42-linux-glibc2.28-x86_64.tar.xz mysql-8.0.42-linux-glibc2.28-x86_64.tar.xz  offline_install_mysql57or80.sh[root@server ~]# sh offline_install_mysql57or80.sh 开始安装 MySQL 数据库...安装系统依赖...解压 MySQL 安装包...生成 /etc/my.cnf ...初始化数据库...注册 MySQL 服务...启动 MySQL 服务...Starting MySQL. SUCCESS! 设置 root 密码...启用密码复杂度策略组件...数据库安装完成                                             [  OK  ]  
----------------------------------------  安装目录 : /opt/local/mysql  数据目录 : /opt/mysql/data  配置文件 : /etc/my.cnf  root密码 : Root135-/----------------------------------------[root@server ~]# ps -ef | grep mysqlroot       15817       1  0 15:40 pts/0    00:00:00 /bin/sh /opt/local/mysql/bin/mysqld_safe --datadir=/opt/mysql/data --pid-file=/opt/mysql/data/mysql.pidmysql      15989   15817  1 15:40 pts/0    00:00:00 /opt/local/mysql/bin/mysqld --basedir=/opt/local/mysql --datadir=/opt/mysql/data --plugin-dir=/opt/localmysql/lib/plugin --user=mysql --log-error=/opt/mysql/data/mysql.log --pid-file=/opt/mysql/data/mysql.pid --socket=/opt/mysql/data/mysql.sockroot       16048    1819  0 15:41 pts/0    00:00:00 grep --color=auto mysql

验证mysql:

source /etc/profile.d/mysql.sh  
# 查看服务状态service mysqld status  
# 登录测试mysql -uroot -p'Root135-/' -S /opt/mysql/data/mysql.sock -e "SELECT VERSION();"  
# 查看数据目录mysql -uroot -p'Root135-/' -S /opt/mysql/data/mysql.sock \  -e "SHOW VARIABLES LIKE 'datadir';"  
# 查看密码策略mysql -uroot -p'Root135-/' -S /opt/mysql/data/mysql.sock \  -e "SHOW VARIABLES LIKE 'validate_password%';"

1.4 安装nacos

1、下载并解压nacos安装包

# wget https://github.com/alibaba/nacos/releases/download/2.5.4/nacos-server-2.5.4.zipls nacos-server-2.5.4.zipunzip nacos-server-2.5.4.zip -d /opt/local/chown -R root. /opt/local/nacos/ll -d /opt/local/nacos/

2、导入nacos的初始化表结构到mysql数据库,需要提前创建好数据库。表结构在nacos-server的包里面。

[root@server ~]# cd /opt/local/nacos[root@server nacos]# ls conf/mysql-schema.sql conf/mysql-schema.sql[root@server nacos]# mysql -uroot -p -S /opt/mysql/data/mysql.sock Enter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 8Server version: 8.0.42 MySQL Community Server - GPL  
Copyright (c) 2000, 2025, Oracle and/or its affiliates.  
Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.  
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
mysql> create database nacos;Query OK, 1 row affected (0.00 sec)  
mysql> use nacos;Database changed  
mysql> source conf/mysql-schema.sql;  
mysql> show tables;+----------------------+| Tables_in_nacos      |+----------------------+| config_info          || config_info_gray     || config_tags_relation || group_capacity       || his_config_info      || permissions          || roles                || tenant_capacity      || tenant_info          || users                |+----------------------+10 rows in set (0.00 sec)  
mysql> CREATE USER 'nacos'@'%' IDENTIFIED BY 'Nacos135-/';Query OK, 0 rows affected (0.01 sec)  
mysql> GRANT ALL PRIVILEGES ON nacos.* TO 'nacos'@'%';Query OK, 0 rows affected (0.00 sec)  
mysql> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.00 sec)  
mysql> SHOW GRANTS FOR 'nacos'@'%';+--------------------------------------------------+| Grants for nacos@%                               |+--------------------------------------------------+| GRANT USAGE ON *.* TO `nacos`@`%`                || GRANT ALL PRIVILEGES ON `nacos`.* TO `nacos`@`%` |+--------------------------------------------------+2 rows in set (0.00 sec)  
mysql> exitBye

3、配置nacos

# 建议提前备份配置文件cd /opt/local/nacos/confcp application.properties application.properties.bakvim /opt/local/nacos/conf/application.properties  
# 修改以下配置server.servlet.contextPath=/nacos        # 默认上下文路径server.error.include-message=ALWAYS      # 默认即可server.port=8848                         # 默认端口nacos.inetutils.ip-address=192.168.0.21   # 修改,强制对外暴露的ip地址,多网卡需要配置。建议配置。  
# 配置连接mysql数据库,数据库需要提前导入数据mysql-schema.sqlspring.sql.init.platform=mysql   # 加此行db.num=1                         # 加此行db.url.0=jdbc:mysql://192.168.0.21:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=truedb.user.0=nacosdb.password.0=Nacos135-/  
# 配置登录验证# 生成token值,至少32位。#[root@server ~]# openssl rand -base64 32#5YhA2MSZzlSIeBKQdK1ZikALwn8waCRDFPP6cSKkT9M=nacos.core.auth.system.type=nacosnacos.core.auth.enabled=truenacos.core.auth.server.identity.key=dK1ZikALwn8w     # 不是账号nacos.core.auth.server.identity.value=dK1ZikALwn8w   # 不是密码nacos.core.auth.plugin.nacos.token.secret.key=5YhA2MSZzlSIeBKQdK1ZikALwn8waCRDFPP6cSKkT9M=  
# 完整访问路径:http://ip地址:8848/nacos

4、启动和关闭Nacos

(1)启动服务

# Linux/Unix/Mac系统[root@server ~]# cd /opt/local/nacos/[root@server /opt/local/nacos# ./bin/startup.sh -m standalone    # 启动单机版nacosnacos is starting with standalonenacos is starting,you can check the /opt/local/nacos/logs/start.out  
################ 检查nacos启动情况 ########################ps -ef | grep nacosnetstat -antup | grep 8848[root@server nacos]# ss -ntlp | grep "pid=17162"LISTEN 0      128                *:9848             *:*    users:(("java",pid=17162,fd=118)) LISTEN 0      128                *:9849             *:*    users:(("java",pid=17162,fd=119)) LISTEN 0      128                *:7848             *:*    users:(("java",pid=17162,fd=120)) LISTEN 0      100                *:8848             *:*    users:(("java",pid=17162,fd=188))   
# 7848 # Jraft请求服务端端口,用于处理服务端间的Raft相关请求# 8848 # 主端口,客户端、控制台及OpenAPI所使用的HTTP端口。# 9848 # 客户端gRPC请求服务端端口,用于客户端向服务端发起连接和请求。# 9849 # 服务端gRPC请求服务端端口,用于服务间同步等。  
# ubuntu系统bash startup.sh -m standalone  
# windows系统startup.cmd -m standalone

(2)关闭服务

# Linux/Unix/Mac系统sh shutdown.sh  
# windows系统shutdown.cmd

(3)由service管理nacos服务,开机自启。

[root@server ~]# vim /etc/systemd/system/nacos.service[Unit]Description=Nacos ServerAfter=network.target  
[Service]Type=forkingEnvironment="JAVA_HOME=/opt/local/jdk"WorkingDirectory=/opt/local/nacosExecStart=/opt/local/nacos/bin/startup.sh -m standaloneExecReload=/bin/kill -s HUP $MAINPIDExecStop=/bin/kill -s QUIT $MAINPIDPrivateTmp=true  
[Install]WantedBy=multi-user.target[root@server ~]# systemctl daemon-reload[root@server ~]# systemctl enable --now nacos.service

1.5 访问nacos

浏览器访问:http://192.168.0.21:8848/nacos

默认用户名和密码都是nacos,建议修改密码,建议创建只读用户和读写用户。

2. Nacos集群部署(版本2.5.4)

2.1 nacos集群架构

使用自定义域名+nginx反向代理nacos集群方式。

2.2 nacos集群部署过程

1、假设已部署3个单点nacos,均未启动nacos服务。部署步骤见上方单机部署。

2、配置集群配置文件

cat > /opt/local/nacos/conf/cluster.conf<<EOF192.168.0.21:8848192.168.0.22:8848192.168.0.23:8848EOF

3、默认是以集群方式启动nacos服务,设置开机自启。不要加 -m standalone

# /opt/local/nacos/bin/startup.sh# nacos正在以集群方式启动nacos is starting with clusternacos is starting,you can check the /opt/local/nacos/logs/start.out  
# 推荐使用cat >/etc/systemd/system/nacos.service<<EOF[Unit]Description=Nacos ServerAfter=network.target  
[Service]Type=forkingEnvironment="JAVA_HOME=/opt/local/jdk"WorkingDirectory=/opt/local/nacosExecStart=/opt/local/nacos/bin/startup.shExecReload=/bin/kill -s HUP \$MAINPIDExecStop=/bin/kill -s QUIT \$MAINPIDPrivateTmp=true  
[Install]WantedBy=multi-user.targetEOF  
systemctl daemon-reloadsystemctl enable --now nacos.servicesystemctl status nacos.service

4、检查后端nacos服务可用性

# 检查服务和端口ps -ef | grep nacosss -ntlp | grep 8848  
# 检查webhttp://192.168.0.21:8848/nacoshttp://192.168.0.22:8848/nacoshttp://192.168.0.23:8848/nacos

2.3 配置haproxy代理(二选一)

1、编译安装haproxy

(1)下载并解压haproxywget https://www.haproxy.org/download/2.9/src/haproxy-2.9.15.tar.gztar xzf haproxy-2.9.15.tar.gz  
(2)查看安装依赖内核版本cd haproxy-2.9.15/cat INSTALLuname -r  
(3)安装依赖包yum install make gcc gcc-c++ openssl-devel -y  
(4)编译安装haproxymake TARGET=linux5100 PREFIX=/opt/local/haproxymake install PREFIX=/opt/local/haproxy  
(5)查看haproxy安装情况cd /opt/local/haproxy/

2、自行编写haproxy.cfg,代理nacos集群。需要代理nacos的8848和9848端口

cat >/opt/local/haproxy/haproxy.cfg<<EOF# 全局配置global    # 最大连接数    maxconn 10240    # 运行用户/组(和服务文件保持一致)    user root    group root    # 后台运行    daemon    # 日志配置(可选,如需日志需先配置rsyslog)    log 127.0.0.1 local0 info  
# 默认配置(仅基础配置,不指定mode,让各模块自行声明)defaults    log global    timeout connect 10s    timeout client 30s    timeout server 30s    maxconn 10000  
# Nacos集群HTTP代理配置(8848→18848)frontend nacos-frontend    bind *:18848    mode http  # 显式声明HTTP模式    default_backend nacos-backend  
backend nacos-backend    mode http    balance roundrobin    option httpchk GET /nacos    timeout check 5s    server nacos-node1 192.168.0.21:8848 check inter 3s rise 2 fall 3    server nacos-node2 192.168.0.22:8848 check inter 3s rise 2 fall 3    server nacos-node3 192.168.0.23:8848 check inter 3s rise 2 fall 3  
# 代理 Nacos 2.x gRPC 端口 9848(TCP 模式,9848→19848)frontend nacos-grpc-frontend    bind *:19848    mode tcp  # 显式声明TCP模式    option tcplog  # 开启TCP日志,便于排查    default_backend nacos-grpc-backend  
backend nacos-grpc-backend    mode tcp    balance roundrobin    timeout connect 10s  # TCP连接超时延长到10s(默认1s太短)    timeout check 15s    # 健康检查超时延长到15s    server nacos-node1 192.168.0.21:9848 check inter 5s rise 2 fall 3    server nacos-node2 192.168.0.22:9848 check inter 5s rise 2 fall 3    server nacos-node3 192.168.0.23:9848 check inter 5s rise 2 fall 3  
# HAProxy监控页面listen stats    bind *:8888    mode http    stats enable    stats uri /haproxy-stats    stats auth admin:5YhA2MSZzlSIe    stats hide-version    # 新增:监控页面自动刷新(5秒)    #stats refresh 5sEOF

3、开启haproxy日志

# 配置日志收集,用于收集haproxy日志。[root@server ~]# vim /etc/rsyslog.conf$ModLoad imudp$UDPServerRun 514  
local0.*   /var/log/haproxy.log[root@server ~]# systemctl restart rsyslog

4、管理haproxy服务

# 1、启动haproxycp -a /opt/local/haproxy/sbin/haproxy /usr/sbin/ln -s /opt/local/haproxy/haproxy.cfg /etc/haproxy.cfghaproxy -f /etc/haproxy.cfg  
# 2、查看haproxy进程和占用端口号netstat -antup | grep -E '18848|8888'  ps -ef | grep haproxy  
# 3、强制关闭haproxykill -9 pid  
# 4、设置开机自启chmod +x /etc/rc.d/rc.localecho "sleep 5 && /usr/sbin/haproxy -f /etc/haproxy.cfg" >> /etc/rc.d/rc.localcat /etc/rc.d/rc.local

5、配置本地域名解析,方便后续变更。

cat /etc/hostsecho "192.168.0.11 www.nacos.com" >> /etc/hosts  
# windows添加,修改host文件C:\Windows\System32\drivers\etc

6、访问nacos集群

http://www.nacos.com:18848/nacos

7、访问haproxy状态页:http://www.nacos.com:8888/haproxy-stats,需要输入账号密码。

2.4 配置nginx代理(二选一)

编译安装nginx

1、添加nginx配置

# 1、在nginx.conf的http块上面加一段stream,需要stream四层转发模块的支持。# TCP/UDP 代理层(代理 9848)stream {  proxy_connect_timeout 3s;  # nacos的grpc协议配置  upstream nacoscluster-grpc{    # nacos2版本,grpc端口与要比主端口多1000    server 192.168.0.21:9848 weight=5 max_fails=3 fail_timeout=30s;    server 192.168.0.22:9848 weight=5 max_fails=3 fail_timeout=30s;    server 192.168.0.23:9848 weight=5 max_fails=3 fail_timeout=30s;  }  server{    listen 19848;    proxy_pass nacoscluster-grpc;  }}  

# 新增配置文件 nacos.confupstream nacosserver{     hash $remote_addr consistent;     server 192.168.0.21:8848 weight=5 max_fails=3 fail_timeout=30s;     server 192.168.0.22:8848 weight=5 max_fails=3 fail_timeout=30s;     server 192.168.0.23:8848 weight=5 max_fails=3 fail_timeout=30s;}  
server {        listen  18848;        server_name localhost;  
        location / {                root    html;                index   index.html index.htm;        }  
        location /nacos/ {                proxy_pass http://nacosserver/nacos/;        }}

2、重新加载nginx配置

3、访问nacos集群

http://192.168.0.11:18848/nacos/#/login

4、观察后端服务日志/opt/local/nacos/logs/access_log*,看请求是否有转发到每个后端nacos服务。

评论