docker-compose 部署nacos单机/集群

7 篇文章 0 订阅

单机模式

docker-compose.yml

version: '3'
services:
  mysql-master:
    image: mysql:5.7
    container_name: mysql-master
    restart: always
    privileged: true
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=886291
      - character-set-server=utf8mb4
      - collation-server=utf8mb4_unicode_ci
      - default-authentication-plugin=mysql_native_password
    volumes:
      - /root/mysql/master/conf/my.cnf:/etc/mysql/my.cnf
      - /root/mysql/master/logs:/var/log/mysql
      - /root/mysql/master/data:/var/lib/mysql
  nacos1:
    image: nacos/nacos-server:1.2.1
    container_name: nacos1
    restart: always
    privileged: true
    depends_on:
      - mysql-master
    environment:
      PREFER_HOST_MODE: 10.10.9.155 #如果支持主机名可以使用hostname,否则使用ip,默认也是ip
      SPRING_DATASOURCE_PLATFORM: mysql #数据源平台 仅支持mysql或不保存empty
      # 单节点模式
      #MODE: standalone
      #集群中其它节点[ip1:port ip2:port ip3:port]
	  NACOS_SERVERS: 10.10.9.155:8848 10.10.9.156:8848 10.10.9.157:8848
      MYSQL_SERVICE_HOST: 172.24.0.4
      MYSQL_SERVICE_DB_NAME: nacos
      MYSQL_SERVICE_PORT: 3306
      MYSQL_SERVICE_USER: root
      MYSQL_SERVICE_PASSWORD: 886291
      NACOS_APPLICATION_PORT: 8848
      NACOS_SERVER_IP: 10.10.9.155
      NACOS_APPLICATION_PORT: 8848
      JVM_XMS: 1024m
      JVM_XMX: 1024m
      JVM_MMS: 320m
      TOMCAT_ACCESSLOG_ENABLED: false
    volumes:
      - /root/nacos/standalone-logs/:/home/nacos/logs
      - /root/nacos/plugins/:/home/nacos/plugins
      - /root/nacos/conf/application.properties:/home/nacos/conf/application.properties
    ports:
      - "8848:8848"

其它节点将nacos1修改为nacos2等

创建nacos目录

drwxr-xr-x 2 root root 4096 Jan 13 10:24 conf
drwxr-xr-x 2 root root 4096 Dec  3 16:51 plugins
drwxr-xr-x 2 root root 4096 Jan 13 10:25 standalone-logs

在conf创建application.properties

#
# Copyright 1999-2018 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#*************** Spring Boot Related Configurations ***************#
### Default web context path:
server.servlet.contextPath=/nacos
### Default web server port:
server.port=8848

#*************** Network Related Configurations ***************#
### If prefer hostname over ip for Nacos server addresses in cluster.conf:
# nacos.inetutils.prefer-hostname-over-ip=false

### Specify local server's IP:
# nacos.inetutils.ip-address=


#*************** Config Module Related Configurations ***************#
### If user MySQL as datasource:
# spring.datasource.platform=mysql

### Count of DB:
# db.num=1

### Connect URL of DB:
# db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
# db.user=nacos
# db.password=nacos


#*************** Naming Module Related Configurations ***************#
### Data dispatch task execution period in milliseconds:
# nacos.naming.distro.taskDispatchPeriod=200

### Data count of batch sync task:
# nacos.naming.distro.batchSyncKeyCount=1000

### Retry delay in milliseconds if sync task failed:
# nacos.naming.distro.syncRetryDelay=5000

### If enable data warmup. If set to false, the server would accept request without local data preparation:
# nacos.naming.data.warmup=true

### If enable the instance auto expiration, kind like of health check of instance:
# nacos.naming.expireInstance=true

nacos.naming.empty-service.auto-clean=true
nacos.naming.empty-service.clean.initial-delay-ms=50000
nacos.naming.empty-service.clean.period-time-ms=30000


#*************** CMDB Module Related Configurations ***************#
### The interval to dump external CMDB in seconds:
# nacos.cmdb.dumpTaskInterval=3600

### The interval of polling data change event in seconds:
# nacos.cmdb.eventTaskInterval=10

### The interval of loading labels in seconds:
# nacos.cmdb.labelTaskInterval=300

### If turn on data loading task:
# nacos.cmdb.loadDataAtStart=false


#*************** Metrics Related Configurations ***************#
### Metrics for prometheus
#management.endpoints.web.exposure.include=*

### Metrics for elastic search
management.metrics.export.elastic.enabled=false
#management.metrics.export.elastic.host=http://localhost:9200

### Metrics for influx
management.metrics.export.influx.enabled=false
#management.metrics.export.influx.db=springboot
#management.metrics.export.influx.uri=http://localhost:8086
#management.metrics.export.influx.auto-create-db=true
#management.metrics.export.influx.consistency=one
#management.metrics.export.influx.compressed=true


#*************** Access Log Related Configurations ***************#
### If turn on the access log:
# 关闭tomcat访问日志,防止造成磁盘不足
server.tomcat.accesslog.enabled=false

### The access log pattern:
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i

### The directory of access log:
server.tomcat.basedir=


#*************** Access Control Related Configurations ***************#
### If enable spring security, this option is deprecated in 1.2.0:
#spring.security.enabled=false

### The ignore urls of auth, is deprecated in 1.2.0:
nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**

### The auth system to use, currently only 'nacos' is supported:
nacos.core.auth.system.type=nacos

### If turn on auth system:
nacos.core.auth.enabled=false

### The token expiration in seconds:
nacos.core.auth.default.token.expire.seconds=18000

### The default token:
nacos.core.auth.default.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789

### 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=true


#*************** Istio Related Configurations ***************#
### If turn on the MCP server:
nacos.istio.mcp.server.enabled=false



###*************** Add from 1.3.0 ***************###


#*************** Core Related Configurations ***************#

### set the WorkerID manually
# nacos.core.snowflake.worker-id=

### Member-MetaData
# nacos.core.member.meta.site=
# nacos.core.member.meta.adweight=
# nacos.core.member.meta.weight=

### MemberLookup
### Addressing pattern category, If set, the priority is highest
# nacos.core.member.lookup.type=[file,address-server,discovery]
## Set the cluster list with a configuration file or command-line argument
# nacos.member.list=192.168.16.101:8847?raft_port=8807,192.168.16.101?raft_port=8808,192.168.16.101:8849?raft_port=8809
## for DiscoveryMemberLookup
# If you want to use cluster node self-discovery, turn this parameter on
# nacos.member.discovery=false
## for AddressServerMemberLookup
# Maximum number of retries to query the address server upon initialization
# nacos.core.address-server.retry=5

#*************** JRaft Related Configurations ***************#

### Sets the Raft cluster election timeout, default value is 5 second
# nacos.core.protocol.raft.data.election_timeout_ms=5000
### Sets the amount of time the Raft snapshot will execute periodically, default is 30 minute
# nacos.core.protocol.raft.data.snapshot_interval_secs=30
### Requested retries, default value is 1
# nacos.core.protocol.raft.data.request_failoverRetries=1
### raft internal worker threads
# nacos.core.protocol.raft.data.core_thread_num=8
### Number of threads required for raft business request processing
# nacos.core.protocol.raft.data.cli_service_thread_num=4
### raft linear read strategy, defaults to index
# nacos.core.protocol.raft.data.read_index_type=ReadOnlySafe
### rpc request timeout, default 5 seconds
# nacos.core.protocol.raft.data.rpc_request_timeout_ms=5000

集群

准备三台服务器192.168.1.155:8848, 192.168.1.156:8848, 192.168.1.157:8848;

docker-compose.yml

在三台服务器分别创建docker-compose.yml文件,注意配置PREFER_HOST_MODE为各自节点的IP。

version: "3"
services:
  nacos3:
    image: nacos/nacos-server:1.2.1
    container_name: nacos3
    restart: always
    privileged: true
    environment:
      PREFER_HOST_MODE: 192.168.1.155 #如果支持主机名可以使用hostname,否则使用ip,默认也是ip
      SPRING_DATASOURCE_PLATFORM: mysql #数据源平台 仅支持mysql或不保存empty
      MYSQL_SERVICE_HOST: 192.168.1.151
      MYSQL_SERVICE_DB_NAME: nacos
      MYSQL_SERVICE_PORT: 3306
      MYSQL_SERVICE_USER: root
      MYSQL_SERVICE_PASSWORD: 886291
      NACOS_SERVER_IP: 10.10.9.157
      NACOS_APPLICATION_PORT: 8848
      #集群中其它节点[ip1:port ip2:port ip3:port]
      NACOS_SERVERS: 192.168.1.155:8848 192.168.1.156:8848 192.168.1.157:8848
      JVM_XMS: 1024m
      JVM_XMX: 1024m
    volumes:
      - /root/nacos/standalone-logs/:/home/nacos/logs
      - /root/nacos/plugins/:/home/nacos/plugins
      - /root/nacos/conf/application.properties:/home/nacos/conf/application.properties
    ports:
      - "8848:8848"

conf

创建/root/nacos/conf/application.properties文件内容如下:

server.servlet.contextPath=/nacos
server.port=8848
spring.datasource.platform=mysql

db.num=1
# 这里要对应ip,以及对应的数据库
db.url.0=jdbc:mysql://192.168.1.151:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=886291

nacos.cmdb.dumpTaskInterval=3600
nacos.cmdb.eventTaskInterval=10
nacos.cmdb.labelTaskInterval=300
nacos.cmdb.loadDataAtStart=false

management.metrics.export.elastic.enabled=false
management.metrics.export.influx.enabled=false
# 关闭tomcat访问日志,防止造成磁盘不足
server.tomcat.accesslog.enabled=false
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i
nacos.security.ignore.urls=/,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/login,/v1/
console/health/**,/v1/cs/**,/v1/ns/**,/v1/cmdb/**,/actuator/**,/v1/console/server/**
nacos.naming.distro.taskDispatchThreadCount=1
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
nacos.naming.expireInstance=true

测试

分别启动三个节点上的容器,浏览器输入链接: http://ip:8848/nacos
后进入集群管理菜单→节点管理,可以看到三个节点的信息。
在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值