基于Docker搭建Nacos集群
准备机器3台
- 192.168.1.160
- 192.168.1.161
- 192.168.1.162
docker安装
安装
mysql
- 基于docker安装mysql
docker run \ -p 3306:3306 \ --name nacos-mysql \ -v $PWD/conf:/etc/mysql/conf.d \ -v $PWD/logs:/logs \ -v $PWD/data:/var/lib/mysql \ -v /etc/localtime:/etc/localtime:ro \ -e TZ=Asia/Shanghai \ -e MYSQL_ROOT_PASSWORD=root \ -d mysql:5.7
- 初始化sqlsql脚本入口: https://github.com/alibaba/nacos/blob/master/distribution/conf/nacos-mysql.sql
Nacos
-
创建Nacos的工作目录
// 每个节点都创建 mkdir -p /usr/local/docker/nacos-server mkdir -p /usr/local/docker/nacos-server/env mkdir -p /usr/local/docker/nacos-server/logs mkdir -p /usr/local/docker/nacos-server/init.d
-
调整custom.properties
vim /usr/local/docker/nacos-server/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-hostname.env
vim /usr/local/docker/nacos-server/env/nacos-hostname.env #nacos dev env # 首选主机模式 PREFER_HOST_MODE=hostname # 当前主机的IP NACOS_SERVER_IP=192.168.1.160 # 集群的各个节点 NACOS_SERVERS=192.168.1.160:8848 192.168.1.161:8848 192.168.1.162:8848 # 数据库的配置 MYSQL_SERVICE_HOST=192.168.1.100 MYSQL_SERVICE_DB_NAME=nacos MYSQL_SERVICE_PORT=3306 MYSQL_SERVICE_USER=root MYSQL_SERVICE_PASSWORD=root # 从节点 这里就使用单节点测试,因此就不配置从节点 #MYSQL_SLAVE_SERVICE_HOST=xxx #MYSQL_SLAVE_SERVICE_PORT=3306 # JVM参数 默认是2G 如果使用虚拟机,内存没有2G,就需要调整这里的参数,否则将无法启动 JVM_XMS=256m JVM_XMX=256m JVM_XMN=256m
-
将配置文件拷贝到其他两台机器
scp -r /usr/local/docker/nacos-server/env/nacos-hostname.env root@192.168.1.161:/usr/local/docker/nacos-server/env/nacos-hostname.env scp -r /usr/local/docker/nacos-server/env/nacos-hostname.env root@192.168.1.162:/usr/local/docker/nacos-server/env/nacos-hostname.env scp -r /usr/local/docker/nacos-server/init.d/custom.properties root@192.168.1.161:/usr/local/docker/nacos-server/init.d/custom.properties scp -r /usr/local/docker/nacos-server/init.d/custom.properties root@192.168.1.162:/usr/local/docker/nacos-server/init.d/custom.properties
-
docker启动(3个节点)
docker run \ -p 8848:8848 \ --restart=always \ --name nacos-node \ --env-file=/usr/local/docker/nacos-server/env/nacos-hostname.env \ -v /usr/local/docker/nacos-server/logs:/home/nacos/logs \ -v /usr/local/docker/nacos-server/init.d/custom.properties:/home/nacos/init.d/custom.properties \ -d nacos/nacos-server
-
异常查看
// 如果出现异常,可以在/usr/local/docker/nacos-server/logs目录下查看 tail -f /usr/local/docker/nacos-server/logs/nacos.log
测试
访问任意节点的8848端口; 如: 192.168.1.160:8848/nacos 默认用户名和密码为: nacos/nacos ;登录之后查看节点即可看到如下效果.
当停用任何一个节点,都不会影响正常的使用
Nginx代理Nacos集群
-
创建nacos.conf;添加以下配置
upstream nacos_server { server 192.168.1.160:8848; server 192.168.1.161:8848; server 192.168.1.162:8848; } server { listen 80; listen 443 ssl; #填写绑定证书的域名 server_name nacos.你的域名; #ssl on; ssl_certificate /你的证书路径.crt; ssl_certificate_key /你的密钥路径.key; ssl_session_timeout 5m; #按照这个协议配置 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个套件配置 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; charset utf-8; resolver 223.5.5.5 223.6.6.6 1.2.4.8 114.114.114.114 valid=3600s; location / { proxy_read_timeout 90; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $http_connection; 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 X-Forwarded-Proto $scheme; proxy_cache_bypass $http_upgrade; proxy_pass http://nacos_server; } }
-
重启服务
sbin/nginx -t sbin/nginx -s reload