一、系统环境
| IP地址 | 操作系统 | Selinux |
|---|
| 192.168.2.104 | Rocky10 | 已关闭 |
| 192.168.2.105 | Rocky10 | 已关闭 |
| 192.168.2.106 | Rocky10 | 已关闭 |
| 执行后续操作 | | |
1
2
3
4
5
6
| #打开端口转发
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
sysctl -p
#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
|
二、安装Docker
在三台服务器上运行下列命令,用来安装docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| # 安装 Docker (如果尚未安装)
# 1. 卸载旧版本(如果有)
dnf remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 2. 安装必要工具
dnf install -y device-mapper-persistent-data lvm2
# 3. 添加阿里云 Docker 源
dnf config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 4. 替换为阿里云地址(由于 Rocky 10 兼容性,有时需要手动指定变量)
sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+g' /etc/yum.repos.d/docker-ce.repo
# 5. 安装 Docker 引擎
dnf makecache
dnf install -y docker-ce docker-ce-cli containerd.io
|
配置Docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| # 创建配置目录
# 修改后的 /etc/docker/daemon.json
cat <<EOF > /etc/docker/daemon.json
{
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://dockerproxy.com"
],
"bip": "10.50.0.1/24",
"default-address-pools": [
{
"base": "10.60.0.0/16",
"size": 24
}
]
}
EOF
# 重启 Docker 生效
systemctl daemon-reload
systemctl restart docker
|
三、部署Mysql集群
3.1、启动Mysql
因Nacos需要Mysql数据库,在192.168.2.104和105上部署一套Mysql集群,使用Mysql8.0
然后在192.168.2.105的/opt/mysql目录下创建docker-compose.yml文件,写入如下内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| # /opt/mysql/docker-compose.yml
services:
mysql-master:
image: mysql:8
container_name: mysql-master
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: Test123456!
command:
- --server-id=1
- --log-bin=mysql-bin
- --gtid_mode=ON
- --enforce-gtid-consistency=ON
- --mysql-native-password=ON
volumes:
- ./data:/var/lib/mysql
|
然后在192.168.2.106的/opt/mysql目录下创建docker-compose.yml文件,写入如下内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| # /opt/mysql/docker-compose.yml
services:
mysql-slave:
image: mysql:8
container_name: mysql-slave
restart: always
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: Test123456!
command:
- --server-id=2
- --log-bin=mysql-bin
- --gtid_mode=ON
- --enforce-gtid-consistency=ON
- --mysql-native-password=ON
volumes:
- ./data:/var/lib/mysql
|
在两台服务器上执行
启动mysql,查看日志,系统是否正常启动
1
2
| docker logs mysql-master
docker logs mysql-slave
|
3.2、配置Mysql集群
使用下列命令进入mysql-master,创建用于同步的账号
1
2
3
4
5
6
7
8
9
| docker exec -it mysql-master mysql -uroot -p'Test123456!'
#执行语句创建同步用的账号
CREATE USER 'repl'@'192.168.2.106' IDENTIFIED WITH mysql_native_password BY 'Test123456!';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.2.106';
FLUSH PRIVILEGES;
#创建一个nacos用户
CREATE USER 'nacos'@'%' IDENTIFIED WITH mysql_native_password BY 'Test123456!';
GRANT ALL PRIVILEGES ON nacos_config.* TO 'nacos'@'%';
FLUSH PRIVILEGES;
|
进入mysql-slave,创建用于同步的账号
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| docker exec -it mysql-slave mysql -uroot -p'Test123456!'
#停止现有复制线程(如果是新实例则无所谓,但习惯上先停)
STOP REPLICA;
#配置指向 Master
CHANGE REPLICATION SOURCE TO
SOURCE_HOST='192.168.2.105',
SOURCE_USER='repl',
SOURCE_PASSWORD='Test123456!',
SOURCE_PORT=3306,
SOURCE_AUTO_POSITION=1; -- 关键:开启 GTID 自动定位
#启动同步
START REPLICA;
#查看同步状态
SHOW REPLICA STATUS\G
#如果显示如下则成功
#**`Replica_IO_Running: Yes`**:IO 线程正常,正在实时读取 Master 的日志。
#**`Replica_SQL_Running: Yes`**:SQL 线程正常,正在本地执行拿到的日志。
#**`Seconds_Behind_Source: 0`**:同步延迟为 0 秒,表示 Slave 已经完全追上了 Master 的进度。
#**`Retrieved_Gtid_Set` 和 `Executed_Gtid_Set`**:都有值且包含 Master 的 UUID,说明 GTID 模式已经生效,数据一致性有保障。
|
四、配置Nacos集群
4.1、初始化Nacos数据库
首先创建用户,并执行初始化脚本,在数据库的master节点上执行
1
2
3
4
5
6
7
8
| CREATE DATABASE `nacos_config` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
#使用如下命令拷贝初始化脚本,并导入
# 先临时跑个 nacos 容器
docker run -d --name nacos-tmp nacos/nacos-server:latest
# 拷贝 SQL 文件到当前目录
docker cp nacos-tmp:/home/nacos/conf/mysql-schema.sql ./nacos-db.sql
# 删除临时容器
docker rm -f nacos-tmp
|
检查两个数据库里面是否已经同步好数据库
4.2、启动Nacos
在三个节点分别执行
1
2
| mkdir -p /opt/nacos/logs
cd /opt/nacos
|
在三个节点的/opt/nacos下创建docker-compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| services:
nacos:
image: nacos/nacos-server:latest
container_name: nacos-server
restart: always
network_mode: "host"
environment:
- MODE=cluster
- PREFER_HOST_MODE=ip
# 替换为你的三台机器实际 IP
- NACOS_SERVERS=192.168.2.105:8848 192.168.2.106:8848 192.168.2.104:8848
# 数据库配置(指向你的 Master 节点)
- SPRING_DATASOURCE_PLATFORM=mysql
- MYSQL_SERVICE_HOST=192.168.2.105
- MYSQL_SERVICE_DB_NAME=nacos_config
- MYSQL_SERVICE_PORT=3306
- MYSQL_SERVICE_USER=nacos
- MYSQL_SERVICE_PASSWORD=Test123456!
# Nacos 2.2.0+ 必须配置的鉴权(Key 必须是 Base64,长度 > 32 字符)
- NACOS_AUTH_ENABLE=true
- NACOS_AUTH_TOKEN=VGhpcy1pcy1teS1jdXN0b20tc2VjcmV0LWtleS0yMDI2LTAzLTIyLTA4
- NACOS_AUTH_IDENTITY_KEY=nacos
- NACOS_AUTH_IDENTITY_VALUE=nacos
volumes:
- ./logs:/home/nacos/logs
|
在三个节点执行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| docker compose up -d
#查看日志
#如果有下列输出即可
,--.
,--.'|
,--,: : | Nacos Console 3.2.0-BETA
,`--.'`| ' : ,---. Running in cluster mode, All function modules
| : : | | ' ,'\ .--.--. Port: 8080
: | \ | : ,--.--. ,---. / / | / / ' Pid: 1
| : ' '; | / \ / \. ; ,. :| : /`./ Console: http://192.168.2.105:8080/index.html
' ' ;. ;.--. .-. | / / '' | |: :| : ;_
| | | \ | \__\/: . .. ' / ' | .; : \ \ `. https://nacos.io
' : | ; .' ," .--.; |' ; :__| : | `----. \
| | '`--' / / ,. |' | '.'|\ \ / / /`--' /
' : | ; : .' \ : : `----' '--'. /
; |.' | , .-./\ \ / `--'---'
'---' `--`---' `----'
2026-03-22 20:20:00,715 INFO Nacos Console is starting...
2026-03-22 20:20:00,810 INFO Nacos Console started successfully in 1126 ms
|
根据提示,在浏览器输入http://192.168.2.104:8080/index.html及其他两个节点的地址,使用用户名nacos密码nacos进入系统即可
五、配置nginx
在192.168.2.107(另外一个服务器,这样不会造成端口冲突)上创建目录,用于配置nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
| mkdir -p /opt/nginx/conf /opt/nginx/logs
#创建配置文件 /opt/nginx/conf/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
# 1. 核心步骤:处理 Nacos 2.x 的 gRPC 流量 (9848 端口)
stream {
upstream nacos-grpc {
server 192.168.2.105:9848;
server 192.168.2.106:9848;
server 192.168.2.107:9848;
}
server {
listen 9848;
proxy_pass nacos-grpc;
}
}
# 2. 处理 Nacos 的 HTTP 流量 (8848 端口)
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
upstream nacos-cluster {
server 192.168.2.105:8848;
server 192.168.2.106:8848;
server 192.168.2.107:8848;
}
server {
listen 80;
server_name localhost;
location /nacos {
proxy_pass http://nacos-cluster/nacos;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 根路径重定向到 nacos 控制台
location / {
rewrite ^/$ /nacos/index.html permanent;
}
}
}
#在/opt/nginx下配置docker-compose.yml
services:
nginx:
image: nginx:latest
container_name: nginx-nacos-lb
restart: always
# 使用 host 模式可以简化 9848 端口的 TCP 转发压力
network_mode: "host"
volumes:
- ./conf/nginx.conf:/etc/nginx/nginx.conf:ro
- ./logs:/var/log/nginx
#启动compose
docker compose up -d
|
检查http://192.168.2.107