vlambda博客
学习文章列表

运维(26) docker-compose部署Nacos2.0.3集群


一、前言

本文将基于以下环境部署Nacos2.0.3集群

  1. CentOS Linux release 7.6.1810 (Core)

  2. Docker version 20.10.5, build 55c4c88

  3. docker-compose version 1.28.6, build 5db8d86f

tips: 本文仅基于单台服务器环境部署示例

二、docker-compose部署Nacos2.0.3集群

可参考 https://gitee.com/zhengqingya/docker-compose

运维(26) docker-compose部署Nacos2.0.3集群

# 准备
git clone https://gitee.com/zhengqingya/docker-compose.git
cd docker-compose/Liunx

# 运行 -- tips:需要修改相关配置信息再运行
docker-compose -f docker-compose-nacos-cluster-2.0.3.yml -p nacos_cluster_v2.0.3 up -d

运维(26) docker-compose部署Nacos2.0.3集群运维(26) docker-compose部署Nacos2.0.3集群运维(26) docker-compose部署Nacos2.0.3集群

  1. nginx ip:8880/nacos

  2. nacos-server-1 ip:8848/nacos

  3. nacos-server-2 ip:18848/nacos

  4. nacos-server-3 ip:28848/nacos

三、相关配置文件

1、docker-compose-nacos-cluster-2.0.3.yml

多机器环境下,每台机器分别部署一个节点即可~

# docker-compose参考:https://github.com/nacos-group/nacos-docker/blob/master/example/cluster-hostname.yaml
# Nacos2.0.0兼容性文档:https://nacos.io/zh-cn/docs/2.0.0-compatibility.html
version: '3'

# 网桥 -> 方便相互通讯
networks:
nacos:
ipam:
driver: default
config:
- subnet: "172.22.0.0/24"

services:
# nginx
nacos-nginx:
image: registry.cn-hangzhou.aliyuncs.com/zhengqing/nginx:1.21.1 # 镜像`nginx:1.21.1`
container_name: nacos-nginx # 容器名为'nacos-nginx'
restart: unless-stopped # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
volumes: # 数据卷挂载路径设置,将本机目录映射到容器目录
- "./nacos_cluster_2.0.3/nginx/conf/nginx.conf:/etc/nginx/nginx.conf"
- "./nacos_cluster_2.0.3/nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf"
- "./nacos_cluster_2.0.3/nginx/html:/usr/share/nginx/html"
- "./nacos_cluster_2.0.3/nginx/log:/var/log/nginx"
environment: # 设置环境变量,相当于docker run命令中的-e
TZ: Asia/Shanghai
LANG: en_US.UTF-8
ports: # 映射端口
- "8880:8880"
networks:
nacos:
ipv4_address: 172.22.0.80
depends_on:
- nacos-server-1
# nacos节点1
nacos-server-1:
image: registry.cn-hangzhou.aliyuncs.com/zhengqing/nacos-server:2.0.3 # 原镜像`nacos/nacos-server:2.0.3`
container_name: nacos-server-1 # 容器名为'nacos-server-1'
hostname: nacos-server-1 # 指定容器hostname
restart: unless-stopped # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
env_file:
- ./nacos_cluster_2.0.3/env/nacos-hostname.env
volumes: # 数据卷挂载路径设置,将本机目录映射到容器目录
- "./nacos_cluster_2.0.3/nacos_1_logs:/home/nacos/logs"
- "./nacos_cluster_2.0.3/init.d/custom.properties:/home/nacos/init.d/custom.properties"
- "./nacos_cluster_2.0.3/conf/application.properties:/home/nacos/conf/application.properties"
- "./nacos_cluster_2.0.3/conf/cluster.conf:/home/nacos/conf/cluster.conf"
environment: # 设置环境变量,相当于docker run命令中的-e
- NACOS_SERVERS=nacos-server-1:8848,nacos-server-2:8848,nacos-server-3:8848 # nacos cluster地址
- PREFER_HOST_MODE=hostname
# - NACOS_SERVER_IP=nacos-server-1 # 多网卡下的自定义nacos服务器IP
ports: # 映射端口 (Nacos2.0版本相比1.X新增了gRPC的通信方式,因此需要增加2个端口:9848、9849)
- "7848:7848"
- "8848:8848"
# 以 8848 为基础偏移 1000 和 1001,不能乱映射,不然服务会连不上 Nacos
- "9848:9848"
- "9849:9849"
- "9555:9555"
networks:
nacos:
ipv4_address: 172.22.0.11
depends_on:
- nacos-server-2
mem_limit: 2000m # 最大使用内存
# nacos节点2
nacos-server-2:
image: registry.cn-hangzhou.aliyuncs.com/zhengqing/nacos-server:2.0.3 # 原镜像`nacos/nacos-server:2.0.3`
container_name: nacos-server-2 # 容器名为'nacos-server-2'
hostname: nacos-server-2 # 指定容器hostname
restart: unless-stopped # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
env_file:
- ./nacos_cluster_2.0.3/env/nacos-hostname.env
volumes: # 数据卷挂载路径设置,将本机目录映射到容器目录
- "./nacos_cluster_2.0.3/nacos_2_logs:/home/nacos/logs"
- "./nacos_cluster_2.0.3/init.d/custom.properties:/home/nacos/init.d/custom.properties"
- "./nacos_cluster_2.0.3/conf/application.properties:/home/nacos/conf/application.properties"
- "./nacos_cluster_2.0.3/conf/cluster.conf:/home/nacos/conf/cluster.conf"
environment: # 设置环境变量,相当于docker run命令中的-e
- NACOS_SERVERS=nacos-server-1:8848,nacos-server-2:8848,nacos-server-3:8848 # nacos cluster地址
- PREFER_HOST_MODE=hostname
# - NACOS_SERVER_IP=nacos-server-2 # 多网卡下的自定义nacos服务器IP
ports: # 映射端口 (Nacos2.0版本相比1.X新增了gRPC的通信方式,因此需要增加2个端口:9848、9849)
- "17848:7848"
- "18848:8848"
- "19848:9848"
- "19849:9849"
- "19555:9555"
networks:
nacos:
ipv4_address: 172.22.0.22
depends_on:
- nacos-server-3
mem_limit: 2000m # 最大使用内存
# nacos节点3
nacos-server-3:
image: registry.cn-hangzhou.aliyuncs.com/zhengqing/nacos-server:2.0.3 # 原镜像`nacos/nacos-server:2.0.3`
container_name: nacos-server-3 # 容器名为'nacos-server-3'
hostname: nacos-server-3 # 指定容器hostname
restart: unless-stopped # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
env_file:
- ./nacos_cluster_2.0.3/env/nacos-hostname.env
volumes: # 数据卷挂载路径设置,将本机目录映射到容器目录
- "./nacos_cluster_2.0.3/nacos_3_logs:/home/nacos/logs"
- "./nacos_cluster_2.0.3/init.d/custom.properties:/home/nacos/init.d/custom.properties"
- "./nacos_cluster_2.0.3/conf/application.properties:/home/nacos/conf/application.properties"
- "./nacos_cluster_2.0.3/conf/cluster.conf:/home/nacos/conf/cluster.conf"
environment: # 设置环境变量,相当于docker run命令中的-e
- NACOS_SERVERS=nacos-server-1:8848,nacos-server-2:8848,nacos-server-3:8848 # nacos cluster地址
- PREFER_HOST_MODE=hostname
# - NACOS_SERVER_IP=nacos-server-3 # 多网卡下的自定义nacos服务器IP
ports: # 映射端口 (Nacos2.0版本相比1.X新增了gRPC的通信方式,因此需要增加2个端口:9848、9849)
- "27848:7848"
- "28848:8848"
- "29848:9848"
- "29849:9849"
- "29555:9555"
networks:
nacos:
ipv4_address: 172.22.0.33
mem_limit: 2000m # 最大使用内存

2、nacos-hostname.env

#nacos dev env
# 如果支持主机名可以使用hostname,否则使用ip,默认也是ip
PREFER_HOST_MODE=hostname
# cluster模式启动
MODE=cluster
# nacos cluster地址
#NACOS_SERVERS=www.zhengqingya.com:8848,www.zhengqingya.com:18848,www.zhengqingya.com:28848
# 多网卡下的自定义nacos服务器IP
#NACOS_SERVER_IP=www.zhengqingya.com

# 数据源平台 仅支持mysql或不保存empty
SPRING_DATASOURCE_PLATFORM=mysql

# TODO 修改mysql连接信息
# 注:这里不能为`127.0.0.1`或`localhost`方式!!!
MYSQL_SERVICE_HOST=www.zhengqingya.com
MYSQL_SERVICE_DB_NAME=nacos_config # 所需sql脚本位于 `nacos_xxx/nacos-mysql.sql`
MYSQL_SERVICE_PORT=3306
MYSQL_SERVICE_USER=root
MYSQL_SERVICE_PASSWORD=root
# mysql5.7 连接参数
MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false
# mysql8 连接参数
#MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai

# TODO 修改JVM调优参数
JVM_XMS=512m #-Xms default :2g
JVM_XMX=512m #-Xmx default :2g
JVM_XMN=256m #-Xmn default :1g
JVM_MS=128m #-XX:MetaspaceSize default :128m
JVM_MMS=128m #-XX:MaxMetaspaceSize default :320m
NACOS_DEBUG=n #是否开启远程debug,y/n,默认n
TOMCAT_ACCESSLOG_ENABLED=false #是否开始tomcat访问日志的记录,默认false

3、cluster.conf

nacos-server-1:8848
nacos-server-2:8848
nacos-server-3:8848

4、nginx.conf

user  nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
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;

# upstream模块实现负载均衡 -- tips:不能使用下划线(ex:nacos_cluster)
upstream nacos-cluster {
server www.zhengqingya.com:8848 weight=1 max_fails=2 fail_timeout=10s;
server www.zhengqingya.com:18848 weight=1 max_fails=2 fail_timeout=10s;
server www.zhengqingya.com:28848 weight=1 max_fails=2 fail_timeout=10s;
}

server {
listen 8880;
server_name localhost; # 服务器地址或绑定域名

# =========================================================
# ================== ↓↓↓↓↓↓ start ↓↓↓↓↓↓ ==================
# =========================================================

location / {
proxy_pass http://nacos-cluster;
}

# =========================================================
# ================== ↑↑↑↑↑↑ end ↑↑↑↑↑↑ ==================
# =========================================================

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

四、java客户端连接

tips: 已开启连接密码安全认证,在客户端连接时需配置账号密码

spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8880
username: nacos
password: nacos
config:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
username: ${spring.cloud.nacos.discovery.username}
password: ${spring.cloud.nacos.discovery.password}

今日分享语句:你要做的就是别人换不掉的,那你做不到怪谁,就是你自己没用!