docker-compose搭建nacos单机集群环境

1.配置网络(复用已有网络)

1.1 创建docker虚拟网络

# 创建一个名为tars的桥接(bridge)虚拟网络,网关172.26.0.1,网段为172.25.0.0
docker network create -d bridge --subnet=172.26.0.0/16 --gateway=172.26.0.1 clickhouse

2.启停命令

docker-compose.yml路径下:

构建开启: docker-compose up -d

停止: docker-compose stop

开启:docker-compose start

3. 依赖mysql

  • 复用已有的mysql5.7容器,新建nacos数据库,用户。mysql容器启动脚本如下:
sudo docker run -itd --net=clickhouse -e TZ="Asia/Shanghai" -e MYSQL_ROOT_PASSWORD="root" --ip="172.26.0.8" -p 3306:3306 -v /opt/DockerData/mysql:/var/lib/mysql --name=docker_mysql mysql:5.7
  • 导入数据表:https://github.com/alibaba/nacos/tree/develop/distribution/conf/nacos-mysql.sql
      MYSQL_SERVICE_HOST=172.26.0.8
      MYSQL_SERVICE_DB_NAME=nacos_devtest
      MYSQL_SERVICE_PORT=3306
      MYSQL_SERVICE_USER=nacos
      MYSQL_SERVICE_PASSWORD=nacos
#登录mysql
mysql -uroot -proot
#创建数据库
create database nacos_devtest;
use mysql;
#创建用户
CREATE USER 'nacos'@'%' identified by 'nacos';
#赋予权限
grant all privileges on nacos_devtest.* to 'nacos'@'%' with GRANT OPTION;
flush privileges;

4. 集群规划

容器ip 端口 节点类型 节点名
172.26.0.130 8848、9555 nacos nacos1
172.26.0.131 8848 nacos nacos2
172.26.0.132 8848 nacos nacos3
172.26.0.8 3306 mysql docker_mysql,复用已有容器

5. 编写 docker-compose.yml

version: "3.7"
services:
  nacos1:
    image: nacos/nacos-server:latest
    container_name: nacos1
    networks:
      clickhouse:
        ipv4_address: 172.26.0.130
    volumes:
      - /f/DockerData/nacos1:/home/nacos/logs
    ports:
      - "8848:8848"
      - "9555:9555"
    environment:
      NACOS_AUTH_ENABLE: 'true'
      NACOS_SERVERS: 172.26.0.130:8848,172.26.0.131:8848,172.26.0.132:8848
      MYSQL_SERVICE_HOST: 172.26.0.8
      MYSQL_SERVICE_DB_NAME: nacos_devtest
      MYSQL_SERVICE_PORT: 3306
      MYSQL_SERVICE_USER: nacos
      MYSQL_SERVICE_PASSWORD: nacos
    # restart: on-failure
  nacos2:
    image: nacos/nacos-server:latest
    container_name: nacos2
    networks:
      clickhouse:
        ipv4_address: 172.26.0.131
    volumes:
      - /f/DockerData/nacos2:/home/nacos/logs
    ports:
      - "8849:8848"
    environment:
      NACOS_AUTH_ENABLE: 'true'
      NACOS_SERVERS: 172.26.0.130:8848,172.26.0.131:8848,172.26.0.132:8848
      MYSQL_SERVICE_HOST: 172.26.0.8
      MYSQL_SERVICE_DB_NAME: nacos_devtest
      MYSQL_SERVICE_PORT: 3306
      MYSQL_SERVICE_USER: nacos
      MYSQL_SERVICE_PASSWORD: nacos
    # restart: always
  nacos3:
    image: nacos/nacos-server:latest
    container_name: nacos3
    networks:
      clickhouse:
        ipv4_address: 172.26.0.132
    volumes:
      - /f/DockerData/nacos3:/home/nacos/logs
    ports:
      - "8850:8848"
    environment:
      NACOS_AUTH_ENABLE: 'true'
      NACOS_SERVERS: 172.26.0.130:8848,172.26.0.131:8848,172.26.0.132:8848
      MYSQL_SERVICE_HOST: 172.26.0.8
      MYSQL_SERVICE_DB_NAME: nacos_devtest
      MYSQL_SERVICE_PORT: 3306
      MYSQL_SERVICE_USER: nacos
      MYSQL_SERVICE_PASSWORD: nacos
    # restart: always
networks:
  clickhouse:
    external: true

7. 登录ui配置

  • 登录 http://127.0.0.1:8848/nacos/ nacos/nacos
  • 服务地址:127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850
  • 开启了鉴权,参考:https://nacos.io/zh-cn/docs/auth.html

其他

  1. https://nacos.io/zh-cn/docs/quick-start-docker.html
  2. https://github.com/nacos-group/nacos-spring-project

你可能感兴趣的:(docker-compose搭建nacos单机集群环境)