docker安装nacos

nacos

最近尝试着将项目由springcloud + netflix重构为springcloud alibaba,需要安装一些组件,感觉安装太麻烦,版便尝试着使用docker来部署,发现挺方便,记录一下。

单机版

docker直接运行

1)准备工作

# 创建文件夹存放一些配置文件
mkdir -p /home/docker/nacos
cd /home/docker/nacos

2)创建配置文件
vim nacos-standlone-mysql.env

PREFER_HOST_MODE=hostname
MODE=standalone
SPRING_DATASOURCE_PLATFORM=mysql
MYSQL_SERVICE_HOST=192.168.0.130
MYSQL_SERVICE_DB_NAME=nacos_config
MYSQL_SERVICE_PORT=3306
MYSQL_SERVICE_USER=root
MYSQL_SERVICE_PASSWORD=123456

JVM_XMS=512m
JVM_XMX=512m
JVM_XMN=256m
JVM_MS=32m
JVM_MMS=80m
# 保存退出 :wq

vim custom.properties

#spring.security.enabled=false
#management.security=false
#security.basic.enabled=false
#nacos.security.ignore.urls=/**
#management.metrics.export.elastic.host=http://localhost:9200
# 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
# 保存退出

3)运行

# 注意使用绝对路径
docker run --name standalone-mysql-5.7 -d \
--env-file /home/docker/nacos/nacos-standlone-mysql.env \
-p 8848:8848 \
-p 9555:9555 \
-v /home/docker/nacos/standalone-logs/:/home/nacos/logs \
-v /home/docker/nacos/custom.properties:/home/nacos/init.d/custom.properties \
--restart=on-failure \
nacos/nacos-server:1.2.0 

docker-compose运行

官方提供的方式就是docker-compose的方式
nacos docker 连接
1)下载git到本地

# 运行
git clone https://github.com/nacos-group/nacos-docker.git
#切换到1.2.0的分支
git checkout 1.2.0 

2)修改配置文件
vim nacos-docker/example/standalone-mysql-5.7.yaml

version: "2"
services:
  nacos:
    image: nacos/nacos-server:1.2.0
    container_name: nacos-standalone-mysql
    env_file:
      - ../env/nacos-standlone-mysql.env
    volumes:
      - ./standalone-logs/:/home/nacos/logs
      - ./init.d/custom.properties:/home/nacos/init.d/custom.properties
    ports:
      - "8848:8848"
      - "9555:9555"
    restart: on-failure

vim ../env/nacos-standlone-mysql.env

PREFER_HOST_MODE=hostname
MODE=standalone
SPRING_DATASOURCE_PLATFORM=mysql
MYSQL_SERVICE_HOST=192.168.0.130 # mysql的ip
MYSQL_SERVICE_DB_NAME=nacos_config # mysql的库名
MYSQL_SERVICE_PORT=3306
MYSQL_SERVICE_USER=root
MYSQL_SERVICE_PASSWORD=123456

# 修改JVM参数,nacos集群模式默认内存 java  -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m ,这里需要修改内存配置(根据你的硬件配置调整)
JVM_XMS=512m
JVM_XMX=512m
JVM_XMN=256m
JVM_MS=32m
JVM_MMS=80m

# 保存退出

vim ./init.d/custom.properties

#spring.security.enabled=false
#management.security=false
#security.basic.enabled=false
#nacos.security.ignore.urls=/**
#management.metrics.export.elastic.host=http://localhost:9200
# 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

3)启动

# 先切换目录
cd nacos-docker/example
# 启动
docker-compose -f standalone-mysql-5.7.yaml up -d
docker ps

集群

1)拉取代码

# 没有的话就安装
yum insyall -y git

git clone https://github.com/nacos-group/nacos-docker.git
#切换到1.2.0的分支
git checkout 1.2.0 
# 查看当前分支
[root@VM_0_16_centos nacos-docker]# git branch -v
* 1.2.0  a24dd38 Fix auth not enabled #79
  master a24dd38 Fix auth not enabled #79

2)修改配置文件
注意:这里会用host方式运行
2.1)nacos-hostname.env
先备份旧文件:cp env/nacos-hostname.env env/nacos-hostname.env.bak
vim env/nacos-hostname.env

###开始
#nacos dev env
PREFER_HOST_MODE=hostname
NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848 
MYSQL_SERVICE_HOST=192.168.0.130 # mysql的ip
MYSQL_SERVICE_DB_NAME=nacos_config # mysql的库名
MYSQL_SERVICE_PORT=3306
MYSQL_SERVICE_USER=root
MYSQL_SERVICE_PASSWORD=123456

# 修改JVM参数,nacos集群模式默认内存 java  -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m ,一般情况下三台机子+mysql需要8G以上内存才够用,这里需要修改内存配置
JVM_XMS=512m
JVM_XMX=512m
JVM_XMN=256m
JVM_MS=32m
JVM_MMS=80m
###结束

2.2)cluster-hostname.yaml
先备份旧文件:cp example/cluster-hostname.yaml example/cluster-hostname.yaml.bak
vim example/cluster-hostname.yaml
删除内嵌的mysql及其依赖(可选,如果你不需要使用自己的mysql就不用删除)

version: "3"
services:
  nacos1:
    hostname: nacos1
    container_name: nacos1
    image: nacos/nacos-server:1.2.0 # 注意所有的nacos版本由latest更换为1.2.0
    volumes:
      - ./cluster-logs/nacos1:/home/nacos/logs
      - ./init.d/custom.properties:/home/nacos/init.d/custom.properties
    ports:
      - "8848:8848"
      - "9555:9555"
    env_file:
      - ../env/nacos-hostname.env
    restart: always

  nacos2:
    hostname: nacos2
    image: nacos/nacos-server:1.2.0
    container_name: nacos2
    volumes:
      - ./cluster-logs/nacos2:/home/nacos/logs
      - ./init.d/custom.properties:/home/nacos/init.d/custom.properties
    ports:
      - "8849:8848"
    env_file:
      - ../env/nacos-hostname.env
    restart: always

  nacos3:
    hostname: nacos3
    image: nacos/nacos-server:1.2.0
    container_name: nacos3
    volumes:
      - ./cluster-logs/nacos3:/home/nacos/logs
      - ./init.d/custom.properties:/home/nacos/init.d/custom.properties
    ports:
      - "8850:8848"
    env_file:
      - ../env/nacos-hostname.env
    restart: always

2.3)创建数据库表
执行下一步之前先在你的mysql中创建nacos_config数据库,然后运行sql文件

https://github.com/alibaba/nacos/blob/develop/distribution/conf/nacos-mysql.sql

3)运行

#很重要
cd example # 进入当前文件夹
docker-compose -f ./cluster-hostname.yaml up -d
docker ps 

# 删除
docker-compose -f ./cluster-hostname.yaml stop
posted @ 2020-05-28 16:56  bartggg  阅读(764)  评论(0编辑  收藏  举报