单机部署
1- 拉取镜像
docker pull nacos/nacos-server:v2.2.0
2- 准备挂载的配置文件目录和日志目录
- 日志目录(空目录):./nacos/logs
- 配置文件:./nacos/conf/application.properties
从官网下载 nacos 压缩包:Release 2.2.0 (Dec 14, 2022) · alibaba/nacos · GitHub,将得到的 application.properties 放到上一步的 ./nacos/conf 目录下,用于下一步的挂载。
a. 配置端口
b. 取消配置的注释并修改为自己的数据库配置
#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
spring.datasource.platform=mysql
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:mysql://192.168.157.142:3306/nacos-220?characterEncoding=utf8&connectTimeout=10000&socketTimeout=30000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=1234
3- 运行容器
docker run --name nacos-server \
-p 8848:8848 \
-p 9848:9848 \
-p 9849:9849 \
--privileged=true \
--restart=always \
-e JVM_XMS=256m \
-e JVM_XMX=256m \
-e MODE=standalone \
-e PREFER_HOST_MODE=hostname \
-v ./nacos/logs:/home/nacos/logs \
-v ./nacos/conf/application.properties:/home/nacos/conf/application.properties \
-d nacos/nacos-server:v2.2.0
集群部署
1- 准备三台主机,ip分别为
- 192.168.3.211
- 192.168.3.212
- 192.168.3.213
2- 关闭防火墙(三台)
systemctl stop firewalld
systemctl disable firewalld
3- 禁用selinux(三台)
vim /etc/selinux/config
# 将 SELNINUX=enforcing 改为 disable
SELINUX=disable
Docker compose
编写docker-compose脚本文件
节点1(nacos-1)
目录结构
.
├── nacos-cluster
│ ├── logs
│ └── nacos-cluster-1.yml
└── nacos-mysql
├── data
└── Dockerfile
1- Dockerfile
FROM mysql:8.0.28
ADD https://raw.githubusercontent.com/alibaba/nacos/develop/distribution/conf/mysql-schema.sql /docker-entrypoint-initdb.d/nacos-mysql.sql
RUN chown -R mysql:mysql /docker-entrypoint-initdb.d/nacos-mysql.sql
EXPOSE 3306
2- nacos-cluster-1.yml
version: "3.8"
services:
nacos-1:
container_name: nacos-1
image: nacos/nacos-server:v2.2.0
volumes:
- ./logs:/home/nacos/logs
ports:
- "8848:8848"
- "9848:9848"
- "9849:9849"
environment:
MODE: cluster
#NACOS_SERVER_IP: 192.168.3.211
NACOS_SERVERS: 192.168.3.211:8848 192.168.3.212:8848 192.168.3.213:8848
SPRING_DATASOURCE_PLATFORM: mysql
MYSQL_SERVICE_HOST: 192.168.3.211
MYSQL_SERVICE_PORT: 3306
MYSQL_SERVICE_DB_NAME: nacos-220
MYSQL_SERVICE_USER: root
MYSQL_SERVICE_PASSWORD: 1234
MYSQL_SERVICE_DB_PARAM: characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
JVM_XMS: 256m
JVM_XMX: 512m
JVM_XMN: 256m
depends_on:
mysql:
condition: service_healthy
mysql:
container_name: mysql
build:
context: ../nacos-mysql
dockerfile: Dockerfile
image: mysql:8.0.28
privileged: true
environment:
MYSQL_ROOT_PASSWORD: 1234
MYSQL_DATABASE: nacos-220
#MYSQL_USER: baiye
#MYSQL_PASSWORD: by1234
volumes:
- ../nacos-mysql/data:/var/lib/mysql
ports:
- "3306:3306"
healthcheck:
test: [ "CMD", "mysqladmin" ,"ping", "-h", "192.168.3.211" ]
interval: 5s
timeout: 10s
retries: 10
3- 运行
docker-compose -f nacos-cluster-1.yml up -d
节点2(nacos-2)
目录结构
.
└── nacos-cluster
├── logs
└── nacos-cluster-2.yml
1- nacos-cluster-2.yml
version: "3.8"
services:
nacos-2:
image: nacos/nacos-server:v2.2.0
container_name: nacos-2
volumes:
- ./logs:/home/nacos/logs
ports:
- "8848:8848"
- "9848:9848"
- "9849:9849"
environment:
MODE: cluster
#ACOS_SERVER_IP: 192.168.3.212
NACOS_SERVERS: 192.168.3.211:8848 192.168.3.212:8848 192.168.3.213:8848
SPRING_DATASOURCE_PLATFORM: mysql
MYSQL_SERVICE_HOST: 192.168.3.211
MYSQL_SERVICE_PORT: 3306
MYSQL_SERVICE_DB_NAME: nacos-220
MYSQL_SERVICE_USER: root
MYSQL_SERVICE_PASSWORD: 1234
MYSQL_SERVICE_DB_PARAM: characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
JVM_XMS: 256m
JVM_XMX: 512m
JVM_XMN: 256m
节点3(nacos-3)
目录结构
.
└── nacos-cluster
├── logs
└── nacos-cluster-3.yml
1- nacos-cluster-3.yml
version: "3.8"
services:
nacos-3:
image: nacos/nacos-server:v2.2.0
container_name: nacos-3
volumes:
- ./logs:/home/nacos/logs
ports:
- "8848:8848"
- "9848:9848"
- "9849:9849"
environment:
MODE: cluster
#NACOS_SERVER_IP: 192.168.3.213
NACOS_SERVERS: 192.168.3.211:8848 192.168.3.212:8848 192.168.3.213:8848
SPRING_DATASOURCE_PLATFORM: mysql
MYSQL_SERVICE_HOST: 192.168.3.211
MYSQL_SERVICE_PORT: 3306
MYSQL_SERVICE_DB_NAME: nacos-220
MYSQL_SERVICE_USER: root
MYSQL_SERVICE_PASSWORD: 1234
MYSQL_SERVICE_DB_PARAM: characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
JVM_XMS: 256m
JVM_XMX: 512m
JVM_XMN: 256m
Docker run
将下面的命令**分别在三台主机上面运行(可修改 --name 的值便于区分不同节点)**即可到达和 docker compose 相同的效果,但是运行前需要先安装 mysql
docker run -it -d \
-e MODE=cluster \
-e NACOS_SERVERS="192.168.3.211:8848 192.168.3.212:8848 192.168.3.213:8848" \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=192.168.3.211 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_DB_NAME=nacos-220 \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=1234 \
-e MYSQL_SERVICE_DB_PARAM="characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true" \
-e JVM_XMS=256m \
-e JVM_XMX=512m \
-e JVM_XMN=256m \
-p 8848:8848 \
-p 9848:9848 \
-p 9849:9849 \
--name nacos-1 \
--restart=always \
nacos/nacos-server:v2.2.0
Nginx 配置
1- 拉取镜像
docker pull nginx
2- 准备需要挂载的文件
# 运行一个 nginx 镜像
docker run -it -d --name=temp -p 8080:80 nginx
# 拷贝文件到主机,用于后面的挂载
docker cp temp:/usr/share/nginx/html ./
docker cp temp:/etc/nginx ./
3- 编辑配置文件
vim ./nginx/nginx.conf
在 http 中添加 nacos 配置,具体参考图片
#下面是 Naocs 集群的配置
upstream nacoscluster {
server 192.168.3.211:8848;
server 192.168.3.212:8848;
server 192.168.3.213:8848;
}
server {
listen 8848; # 此处的 8848 可自定义
server_name 192.168.3.208;
location /nacos/ {
proxy_pass http://nacoscluster/nacos/;
}
}
在 http 上一行 添加 nacosGrpc 配置(Naocs 2.x 版本之前无需添加下面的配置),具体参考图片
# 下面是 Nacos 的集群的配置
stream {
upstream nacosGrpc {
server 192.168.3.211:9848;
server 192.168.3.212:9848;
server 192.168.3.213:9848;
}
server {
listen 9848; # 此处的 9848 在自定义基础上加1000,即 8848 + 1000
proxy_pass nacosGrpc;
}
}
4- 运行镜像
注意:此处一定要将 nacos 的访问端口暴露出去,我的访问端口是配置文件中自定义的 8848
docker run -it -d --name=nacos-nginx --restart=always -p 18008:80 -p 8848:8848 -v ./content:/usr/share/nginx/html -v ./nginx:/etc/nginx nginx
成功访问
访问地址:http://192.168.3.208:8848/nacos/
Nacos 报错总结
docker 启动 nacos 报错
报错信息:Error creating bean with name ‘memoryMonitor’ defined in URL [jar:file:/home/nacos/target/nacos-server
报错信息:ErrMsg:Nacos Server did not start because dumpservice bean construction failure :No DataSource set
两种解决办法:
-
nacos 启动失败,是因为 mysql 没有被唤醒,我们可以尝试用数据库连接工具连接 mysql ,从而唤醒mysql,最后重启 nacos 即可解决问题。
-
指定加载的先后顺序,只有当 mysql 健康后才开始运行 nacos,需要我们在 nacos 中添加下面的脚本
healthcheck: test: curl --fail http://192.168.3.220:18848/nacos/actuator/health || exit 1 interval: 10s timeout: 5s retries: 3
推荐使用第二种解决办法,为 MySQL 进行检查,Nacos 只有在 MySQL 健康检查通过后才开始启动