docker-compose搭建nacos集群

本文介绍如何使用docker-compose在Linux环境中配置Nacos高可用集群。详细步骤包括创建必要目录、配置custom.properties及nacos-hostname.env文件、搭建Docker网络、编写docker-compose.yml文件并配置nginx负载均衡。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


        本文是通过docker-compose在linux配置nacos高可用集群。前提条件是系统中需要安装docker和docker-compose。

配置配置

创建目录

mkdir -p data/cluster-logs/nacos1;
mkdir -p data/cluster-logs/nacos2;
mkdir -p data/cluster-logs/nacos3;
mkdir -p data/init.d;
mkdir -p env/;

配置custom.properties

        在data/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

配置nacos配置信息

        在env目录下配置nacos-hostname.env,MYSQL_SERVICE_HOST中填写当前服务对应的ip。

#nacos dev env
PREFER_HOST_MODE=hostname
NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848
MYSQL_SERVICE_HOST=$host
MYSQL_SERVICE_DB_NAME=nacos
MYSQL_SERVICE_PORT=3306
MYSQL_SERVICE_USER=root
MYSQL_SERVICE_PASSWORD=123456
JVM_XMS=512m
JVM_XMX=512m
JVM_XMN=256m
JVM_MS=64m
JVM_MMS=128m

        在env目录下配置mysql.env。

MYSQL_ROOT_PASSWORD=123456
MYSQL_DATABASE=nacos
MYSQL_USER=root
MYSQL_PASSWORD=123456

创建指定网络

docker network create alex_net

配置docker-compose.yml

version: "3"
services:
  nacos1:
    hostname: nacos1
    container_name: nacos1
    image: nacos/nacos-server
    volumes:
      - ./data/init.d/custom.properties:/home/nacos/init.d/custom.properties
    ports:
      - 8811:8848
      - 9811:9848
      - 9812:9849
    env_file:
      - ./env/nacos-hostname.env
    restart: always
    networks:
      - alex_net
    healthcheck:
      test: ["CMD-SHELL", "echo 'ruok' | curl -s telnet://localhost:8848 || exit 1"]
    deploy:
      resources:
        limits:
          memory: 512M
          cpus: 50m
  nacos2:
    hostname: nacos2
    image: nacos/nacos-server
    container_name: nacos2
    volumes:
      - ./data/init.d/custom.properties:/home/nacos/init.d/custom.properties
    ports:
      - 8822:8848
      - 9822:9848
      - 9823:9849
    env_file:
      - ./env/nacos-hostname.env
    restart: always
    networks:
      - alex_net
    healthcheck:
      test: ["CMD-SHELL", "echo 'ruok' | curl -s telnet://localhost:8848 || exit 1"]
    deploy:
      resources:
        limits:
          memory: 512M
          cpus: 50m
  nacos3:
    hostname: nacos3
    image: nacos/nacos-server
    container_name: nacos3
    volumes:
      - ./data/init.d/custom.properties:/home/nacos/init.d/custom.properties
    ports:
      - 8833:8848
      - 9833:9848
      - 9834:9849
    env_file:
      - ./env/nacos-hostname.env
    restart: always
    networks:
      - alex_net
    healthcheck:
      test: ["CMD-SHELL", "echo 'ruok' | curl -s telnet://localhost:8848 || exit 1"]
    deploy:
      resources:
        limits:
          memory: 512M
          cpus: 50m
  nacos-nginx:
    networks:
      - alex_net
    container_name: nacos-nginx
    image: nginx
    volumes:
      - $PWD/nginx/nginx.conf:/etc/nginx/nginx.conf
    ports:
      - 8844:80
    depends_on:
      nacos1:
        condition: service_healthy
      nacos2:
        condition: service_healthy
      nacos3:
        condition: service_healthy
networks:
  alex_net:
    external: true

配置nginx的nginx.conf

        在nginx/目录下配置nginx.conf文件。

# 用户组
user  nginx;

# 工作进程数
worker_processes  1;

# 日志路径和日志级别
error_log  /var/log/nginx/error.log warn;

# 进程文件路径
pid        /var/run/nginx.pid;


events {
    # 设置网路连接序列化
    accept_mutex on;
    
    # 一个进程是否同时接受多个网络连接
    multi_accept on;
    
    # 事件驱动模型
    use epoll;
    
    # 最大连接数
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    # 自定义服务日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    # 是否开启服务日志
    access_log  /var/log/nginx/access.log  main;

    # 是否开启高效文件传输模式
    sendfile        on;
    #tcp_nopush     on;

    # 长连接超时时间
    keepalive_timeout  65;

    # 响应客户端的超时时间
    send_timeout 75;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    #这里配置nacos的ip:端口,因为nginx和nacos在同一个网络下,这里可以用服务名访问
    upstream nacosUrl {
        server nacos1:8848 weight=1 max_fails=2 fail_timeout=10s;
        server nacos2:8848 weight=1 max_fails=2 fail_timeout=10s;
        server nacos3:8848 weight=1 max_fails=2 fail_timeout=10s;
    }

    server{
        listen 80; #nginx监听的端口
        server_name 10.10.20.100; #ip

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        location /nacos {
            proxy_pass http://nacosUrl/nacos;
            #proxy_set_header Host $host;
            #proxy_set_header X-Real-IP $remote_addr;
            #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #proxy_set_header REMOTE-HOST $remote_addr;
            #add_header X-Cache $upstream_cache_status;
            #add_header Cache-Control no-cache;
        }
    }
}

目录的效果图

tree

        最后目录的效果图,如下:

.
├── data
│   ├── cluster-logs
│   │   ├── nacos1
│   │   ├── nacos2
│   │   ├── nacos3
│   │   └── test_nacos
│   └── init.d
│       └── custom.properties
├── docker-compose.yml
├── env
│   ├── mysql.env
│   └── nacos-hostname.env
└── nginx
    └── nginx.conf

启动docker-compose

docker-compose up -d

集群重启脚本

echo "重启nacos集群库开始"
docker rm -f nacos1

docker rm -f nacos2

docker rm -f nacos3

docker rm -f nacos-nginx

docker-compose up -d

echo "重启nacos集群结束"

总结

        至此,通过docker-comopose搭建nacos集群成功。通过访问如下url:http://ip:8848/nacos、http://ip:8811/nacos、http://ip:8822/nacos、http://ip:8833/nacos,判断是否启动nacos成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值