1.环境准备
nacos 的官方地址
文档手册
https://nacos.io/zh-cn/index.html
下载页面
https://github.com/alibaba/nacos/releases
由于服务器资源有限,方便测试,我主要采用2台centos云服务器,并且已经具备docker swarm环境(搭建参见 docker swarm环境),来搭建4个节点的nacos集群服务:
两台服务器,每台分别部署两个nacos节点,IP及端口如下:
192.168.62.15:9851
192.168.62.15:9850
192.168.62.37:9848
192.168.62.37:9849
集群需要mysql数据库环境支持,脚本conf/nacos-mysql.sql。
同时4个节点通过nginx通过对外服务,nginx搭建请参考其他文件,nginx配置在后面会给出。
2.docker-cluster.yml文件
通过swarm集群,配置好docker-compose文件内容,自动部署到不同的docker节点
version: '3.7'
services:
nacos_cluster_1:
container_name: nacos_cluster_1
image: nacos/nacos-server:1.2.0
restart: always
ports:
- target: 9848
published: 9848
protocol: tcp
mode: host #采用host模式(默认为ingress,配置较灵活,根据自己的需求也可调整为ingress,本案例防止nacos 采用 swarm集群调度,所以改为host模式,两台服务器之间通过内网及nacos端口访问,通过nginx配置对外服务)
volumes:
- cluster1_logs:/home/nacos/logs #配置docker存储日志的卷
environment:
PREFER_HOST_MODE: ip
NACOS_SERVERS: 192.168.62.37:9848 192.168.62.37:9849 192.168.62.15:9850 192.168.62.15:9851
NACOS_SERVER_IP: 192.168.62.37
NACOS_SERVER_PORT: 9848
NACOS_AUTH_ENABLE: 'true' #1.2.0版本默认关闭登陆界面
MYSQL_SERVICE_HOST: 192.168.62.37
MYSQL_SERVICE_DB_NAME: nacos_devtest
MYSQL_SERVICE_PORT: 23306
MYSQL_SERVICE_USER: nacos
MYSQL_SERVICE_PASSWORD: 123456
deploy:
replicas: 1 #部署时,指定部署一个副本
placement:
constraints:
- node.labels.env==docker-server-1 #根据标签指定部署到的节点,标签需要自己事先配置好(本案例节点docker-server-1为192.168.62.37 docker-server-2为192.168.62.15)
restart_policy:
condition: on-failure
nacos_cluster_2:
container_name: nacos_cluster_2
image: nacos/nacos-server:1.2.0
restart: always
ports:
- target: 9849
published: 9849
protocol: tcp
mode: host
volumes:
- cluster2_logs:/home/nacos/logs
environment:
PREFER_HOST_MODE: ip
NACOS_SERVERS: 192.168.62.37:9848 192.168.62.37:9849 192.168.62.15:9850 192.168.62.15:9851
NACOS_SERVER_IP: 192.168.62.37
NACOS_SERVER_PORT: 9849
NACOS_AUTH_ENABLE: 'true'
MYSQL_SERVICE_HOST: 192.168.62.37
MYSQL_SERVICE_DB_NAME: nacos_devtest
MYSQL_SERVICE_PORT: 23306
MYSQL_SERVICE_USER: nacos
MYSQL_SERVICE_PASSWORD: 123456
deploy:
replicas: 1
placement:
constraints:
- node.labels.env==docker-server-1
restart_policy:
condition: on-failure
nacos_cluster_3:
container_name: nacos_cluster_3
image: nacos/nacos-server:1.2.0
restart: always
ports:
- target: 9850
published: 9850
protocol: tcp
mode: host
volumes:
- cluster3_logs:/home/nacos/logs
environment:
PREFER_HOST_MODE: ip
NACOS_SERVERS: 192.168.62.37:9848 192.168.62.37:9849 192.168.62.15:9850 192.168.62.15:9851
NACOS_SERVER_IP: 192.168.62.15
NACOS_SERVER_PORT: 9850
NACOS_AUTH_ENABLE: 'true'
MYSQL_SERVICE_HOST: 192.168.62.37
MYSQL_SERVICE_DB_NAME: nacos_devtest
MYSQL_SERVICE_PORT: 23306
MYSQL_SERVICE_USER: nacos
MYSQL_SERVICE_PASSWORD: 123456
deploy:
replicas: 1
placement:
constraints:
- node.labels.env==docker-server-2
restart_policy:
condition: on-failure
nacos_cluster_4:
container_name: nacos_cluster_4
image: nacos/nacos-server:1.2.0
restart: always
ports:
- target: 9851
published: 9851
protocol: tcp
mode: host
volumes:
- cluster4_logs:/home/nacos/logs
environment:
PREFER_HOST_MODE: ip
NACOS_SERVERS: 192.168.62.37:9848 192.168.62.37:9849 192.168.62.15:9850 192.168.62.15:9851
NACOS_SERVER_IP: 192.168.62.15
NACOS_SERVER_PORT: 9851
NACOS_AUTH_ENABLE: 'true'
MYSQL_SERVICE_HOST: 192.168.62.37
MYSQL_SERVICE_DB_NAME: nacos_devtest
MYSQL_SERVICE_PORT: 23306
MYSQL_SERVICE_USER: nacos
MYSQL_SERVICE_PASSWORD: 123456
deploy:
replicas: 1
placement:
constraints:
- node.labels.env==docker-server-2
restart_policy:
condition: on-failure
volumes:
cluster1_logs:
cluster2_logs:
cluster3_logs:
cluster4_logs:
3.启动
docker stack deploy -c docker-cluster.yml nacos_cluster
采用stack部署,会根据配置文件中部署条件,按照docker节点标签部署到不同的节点上,每个docker节点部署2个nacos节点
4.Nginx代理配置
nginx配置文件添加
upstream nacos {
server 192.168.62.37:9848 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.62.37:9849 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.62.15:9850 weight=1 max_fails=2 fail_timeout=10s;
server 192.168.62.15:9851 weight=1 max_fails=2 fail_timeout=10s;
}
location /nacos/ {
proxy_pass http://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;
}
5.web访问是否成功
在浏览器输入:http://nginxIP:端口/nacos/index.html访问,如下:表示部署成功