【6.4】服务器安装 Docker Swarm中搭建 Nacos 集群

搭建单节点nacos 请看这里

以下是nacos 集群搭建方法一

# 节点一
docker run -d \
 -e PREFER_HOST_MODE=hostname \
 -e MODE=cluster \
 -e NACOS_SERVER_PORT=8948 \
 -e NACOS_SERVER_IP=192.168.0.105 \
 -e NACOS_SERVERS="192.168.0.105:8948 192.168.0.103:8948 192.168.0.103:8949" \
 -e SPRING_DATASOURCE_PLATFORM=mysql \
 -e MYSQL_SERVICE_HOST=192.168.0.105 \
 -e MYSQL_SERVICE_PORT=4002 \
 -e MYSQL_SERVICE_USER=root \
 -e MYSQL_SERVICE_PASSWORD=admin \
 -e MYSQL_SERVICE_DB_NAME=nacos \
 -p 8948:8848 --net=swarm_test \
 --name nacos1 \
 nacos/nacos-server:latest

# 节点二
docker run -d \
  -e PREFER_HOST_MODE=hostname \
 -e MODE=cluster \
 -e NACOS_SERVER_PORT=8948 \
 -e NACOS_SERVERS="192.168.0.105:8948 192.168.0.103:8948 192.168.0.103:8949" \
 -e SPRING_DATASOURCE_PLATFORM=mysql \
 -e MYSQL_SERVICE_HOST=192.168.0.105 \
 -e MYSQL_SERVICE_PORT=4002 \
 -e MYSQL_SERVICE_USER=root \
 -e MYSQL_SERVICE_PASSWORD=admin \
 -e MYSQL_SERVICE_DB_NAME=nacos \
 -e NACOS_SERVER_IP=192.168.0.103 \
 -p 8948:8848 --net=swarm_test \
 --name nacos2 \
 nacos/nacos-server:latest

# 节点三
docker run -d \
  -e PREFER_HOST_MODE=hostname \
 -e MODE=cluster \
 -e NACOS_SERVER_PORT=8949 \
 -e NACOS_SERVERS="192.168.0.105:8948 192.168.0.103:8948 192.168.0.103:8949" \
 -e SPRING_DATASOURCE_PLATFORM=mysql \
 -e MYSQL_SERVICE_HOST=192.168.0.105 \
 -e MYSQL_SERVICE_PORT=4002 \
 -e MYSQL_SERVICE_USER=root \
 -e MYSQL_SERVICE_PASSWORD=admin \
 -e MYSQL_SERVICE_DB_NAME=nacos \
 -e NACOS_SERVER_IP=192.168.0.103 \
 -p 8949:8848 --net=swarm_test \
 --name nacos3 \
 nacos/nacos-server:latest
image.png

方法二
用户密码参考docker-compose-swarm-nacos.yml
nacos-mysql.sql

#docker-compose-swarm-nacos.yml
#node.id为docker swarm 节点id
version: "3.5"
services:
  nacos1:
    hostname: nacos1
    container_name: nacos1
    image: nacos/nacos-server:latest
    ports:
      - "8848:8848"
    environment:
      MODE: cluster
      PREFER_HOST_MODE: hostname
      NACOS_SERVERS: nacos1:8848 nacos2:8848 nacos3:8848
      MYSQL_SERVICE_HOST: 192.168.0.105  #修改成实际的数据库ip
      MYSQL_SERVICE_DB_NAME: nacos  #修改成实际的dbname
      MYSQL_SERVICE_PORT: 4002          #修改成对应的端口
      MYSQL_SERVICE_USER: root     #修改成对应的用户名
      MYSQL_SERVICE_PASSWORD: admin     #修改成对应的密码
    restart: always
    networks:
      - swarm_test
    deploy:
      placement:
        constraints:
          - node.id == ultdmk211olun9ycczhnk3ttj  #根据实际情况修改
  
  nacos2:
    hostname: nacos2
    image: nacos/nacos-server:latest
    container_name: nacos2
    ports:
      - "8849:8848"
    environment:
      MODE: cluster
      PREFER_HOST_MODE: hostname
      NACOS_SERVERS: nacos1:8848 nacos2:8848 nacos3:8848
      MYSQL_SERVICE_HOST: 192.168.0.105  #修改成实际的数据库ip
      MYSQL_SERVICE_DB_NAME: nacos  #修改成实际的dbname
      MYSQL_SERVICE_PORT: 4002          #修改成对应的端口
      MYSQL_SERVICE_USER: root     #修改成对应的用户名
      MYSQL_SERVICE_PASSWORD: admin     #修改成对应的密码
    restart: always
    networks:
      - swarm_test
    deploy:
      placement:
        constraints:
          - node.id == m2lf0h6b2jjy3to5i5zsyeb1q     #根据实际情况修改
  nacos3:
    hostname: nacos3
    image: nacos/nacos-server:latest
    container_name: nacos3
    ports:
      - "8850:8848"
    environment:
      MODE: cluster
      PREFER_HOST_MODE: hostname
      NACOS_SERVERS: nacos1:8848 nacos2:8848 nacos3:8848
      MYSQL_SERVICE_HOST: 192.168.0.105  #修改成实际的数据库ip
      MYSQL_SERVICE_DB_NAME: nacos  #修改成实际的dbname
      MYSQL_SERVICE_PORT: 4002          #修改成对应的端口
      MYSQL_SERVICE_USER: root     #修改成对应的用户名
      MYSQL_SERVICE_PASSWORD: admin     #修改成对应的密码
    restart: always
    networks:
      - swarm_test
    deploy:
      placement:
        constraints:
          - node.id == m2lf0h6b2jjy3to5i5zsyeb1q      #根据实际情况修改
networks:
  swarm_test:
    external: true

(三) 编辑nginx代理nacos的访问,

点波关注 系统搭建(docker)

你可能感兴趣的:(【6.4】服务器安装 Docker Swarm中搭建 Nacos 集群)