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/;
在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
在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
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文件。
# 用户组
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 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成功。