1. 安装docker
第一篇已经介绍了如何安装docker服务
2. 拉取nacos镜像
2.1 搜索有哪些nacos镜像
>docker search nacos
注意:此操作可能会出现无法从dockerhub获取镜像的问题,这时可以配置更多的加速镜像地址,(参考网址:镜像地址文章1,镜像地址文章2),此方法可能无效于docker search,但是应该可以使用docker pull
编辑: /etc/docker/daemon.json(没有就新建一个即可),输入以下内容:
{
"registry-mirrors":[
"https://hub-mirror.c.163.com",
"https://registry.docker-cn.com",
"https://docker.mirrors.ustc.edu.cn",
"https://mirror.ccs.tencentyun.com",
"https://docker.nju.edu.cn",
"https://docker.sunzishaokao.com",
"https://docker.xuanyuan.me/",
"https://docker.1ms.run"]
}
2.2 拉取镜像
>docker pull nacos/nacos-server
默认拉取最新的nacos版本(拉取指定版本:docker pull nacos/nacos-server:v2.2.1)
[root@linux2 docker]# docker pull nacos/nacos-server
Using default tag: latest
WARNING: ⚠️ Failed to pull manifest by the resolved digest. This registry does not
appear to conform to the distribution registry specification; falling back to
pull by tag. This fallback is DEPRECATED, and will be removed in a future
release. Please contact admins of https://docker.sunzishaokao.com/. ⚠️latest: Pulling from nacos/nacos-server
1f3e46996e29: Pull complete
61362ea6747a: Pull complete
a13dd2d57588: Pull complete
be4672c5f484: Pull complete
abd4b6771bd0: Pull complete
5ee1b9e34ab2: Pull complete
8a70fb770a9d: Pull complete
Digest: sha256:b8fd27b7402aa83ff958105ce70bad60126e6e16ee3cd170caa99ee27267c442
Status: Downloaded newer image for nacos/nacos-server:latest
docker.io/nacos/nacos-server:latest
2.3 配置挂载目录
>mkdir -p /data/nacos/log # 用于存放日志文件
>mkdir -p /data/nacos/data # 用于存放nacos数据
2.4 启动容器
docker run --name nacos -d -p 8848:8848 -e MODE=standalone nacos/nacos-server
说明:
(1) 启动nacos
(2) 宿主8848 -> 容器8848
(3) 启动模式: standalone(单机)
2.5 迁移容器内的配置到宿主机
>docker cp nacos:/home/nacos/conf/ /data/nacos/conf # 用于存放nacos配置文件位置
>docker cp nacos:/home/nacos/logs/ /data/nacos/logs
>docker cp nacos:/home/nacos/data/ /data/nacos/data
2.6 创建nacos使用的mysql数据库
>create database `nacos-config` # 创建库
将/data/nacos/conf/mysql-schema.sql在nacos-config库中执行
2.7 配置nacos
>vi /data/nacos/conf/application.properties
# spring
server.servlet.contextPath=/nacos
server.contextPath=/nacos
server.port=8848
# server.tomcat.accesslog.max-days=30
# server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i %{Request-Source}i
# server.tomcat.accesslog.enabled=false
# default current work dir
# server.tomcat.basedir=file:.
#*************** Config Module Related Configurations ***************#
### Deprecated configuration property, it is recommended to use `spring.sql.init.platform` replaced.
spring.datasource.platform=mysql
spring.sql.init.platform=mysql
nacos.cmdb.dumpTaskInterval=3600
nacos.cmdb.eventTaskInterval=10
nacos.cmdb.labelTaskInterval=300
nacos.cmdb.loadDataAtStart=false
db.num=1
# 这里必须为公网或服务器内网地址,我这里是服务器的内网地址,容器内部没有mysql,绝对不能使用 127.0.0.1和localhost# 如果nacos启动失败,Nacos Server did not start because dumpservice bean construction failure : No DataSource set
# 加上 &serverTimezone=UTC ,再不行就加上 &allowPublicKeyRetrieval=true# 将connectTimeout 和 socketTimeout 分别加个0,避免出现超时异常
db.url.0=jdbc:mysql://192.168.188.10:3306/nacos-config?characterEncoding=utf8&connectTimeout=10000&socketTimeout=30000&autoReconnect=true&useUnicode=true&useSSL=false
db.user.0=root
db.password.0=xiaohouzi### The auth system to use, currently only 'nacos' and 'ldap' is supported:
# 鉴权类型,默认为nacos
nacos.core.auth.system.type=nacos# 是否开启鉴权功能,默认为false
nacos.core.auth.enabled=true# Base64加密前密码 TcmxJw05k$-_zcx.)8EtFC^D^F1W!IPr
# Base64加密后密码 VGNteEp3MDVrJC1femN4Lik4RXRGQ15EXkYxVyFJUHI=
# 加密网站:https://www.qqxiuzi.cn/bianma/base64.htm
# 自定义密钥,在自定义密钥时,推荐将配置项设置为Base64编码的字符串,且原始密钥长度不得低于32字符。同nacos.core.auth.plugin.nacos.token.secret.key
nacos.core.auth.default.token.secret.key=VGNteEp3MDVrJC1femN4Lik4RXRGQ15EXkYxVyFJUHI=### worked when nacos.core.auth.system.type=nacos
### The token expiration in seconds:
# 用户登陆临时accessToken的过期时间,默认18000
nacos.core.auth.plugin.nacos.token.expire.seconds=18000
### The default token:# 默认鉴权插件用于生成用户登陆临时accessToken所使用的密钥,在2.2.0.1后无默认值,必须执行此变更,否则无法启动;其他版本为建议设置。
nacos.core.auth.plugin.nacos.token.secret.key=VGNteEp3MDVrJC1femN4Lik4RXRGQ15EXkYxVyFJUHI=
### Turn on/off caching of auth information. By turning on this switch, the update of auth information would have a 15 seconds delay.
# nacos.core.auth.caching.enabled=${NACOS_AUTH_CACHE_ENABLE:false}# 关闭使用user-agent判断服务端请求并放行鉴权的功能
nacos.core.auth.enable.userAgentAuthWhite=false# 用于替换useragent白名单的身份识别key,不可为空,2.2.1后无默认值
nacos.core.auth.server.identity.key=nacosKey
# 用于替换useragent白名单的身份识别value,不可为空,2.2.1后无默认值
nacos.core.auth.server.identity.value=nacosValue## spring security config
### turn off security
nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**
# metrics for elastic search
management.metrics.export.elastic.enabled=false
management.metrics.export.influx.enabled=false
nacos.naming.distro.taskDispatchThreadCount=10
nacos.naming.distro.taskDispatchPeriod=200
nacos.naming.distro.batchSyncKeyCount=1000
nacos.naming.distro.initDataRatio=0.9
nacos.naming.distro.syncRetryDelay=5000
nacos.naming.data.warmup=true
2.8 停止、删除之前的nacos容器,并启动新的nacos
>docker stop nacos
>docker rm nacos
启动新的nacos,并映射卷
docker run -d --name nacos \
-p 8848:8848 \
-p 9848:9848 \
-p 9849:9849 \
--env MODE=standalone \
--env NACOS_AUTH_ENABLE=true \
-v /data/nacos/conf/:/home/nacos/conf \
-v /data/nacos/logs:/home/nacos/logs \
-v /data/nacos/data:/home/nacos/data \
nacos/nacos-server
说明:
docker run -d --name nacos \ # -d 表示运行在后台,--name 指定名称为nacos
-p 8848:8848 \ # 前者为暴露给外部访问的端口,后者为nacos容器端口
-p 9848:9848 \ # 9848是nacos2.0.0版本以上必须要加上端口映射
-p 9849:9849 \ # 9849是nacos2.0.0版本以上必须要加上端口映射
--env MODE=standalone \ # nacos以单机版启动,默认为cluster(集群)
--env NACOS_AUTH_ENABLE=true \ #如果使用官方镜像,请在启动docker容器时,添加如下环境变量
-v /data/nacos/conf/:/home/nacos/conf \ #配置文件目录映射
-v /data/nacos/logs:/home/nacos/logs \ #日志文件目录映射
-v /data/nacos/data:/home/nacos/data \ # 数据文件目录映射
nacos/nacos-server # 指定 docker nacos 版本
2.9 验证
访问:http://{ip}:8848/nacos
使用: nacos/nacos登录即可
进去后修改密码即可。可以看到我的密码加密如下(users表)
nacos $2a$10$KssGtd/rGiQ9A9i3vtF4pOc4NdXH.RD1EsOBmDh9mMvcOfw9Df9g. 1