## 搭建生产可用的Nacos集群 ``` 软件包其中有JDK安装包 链接:https://pan.baidu.com/s/1QiWfPZVCg-kEmysfNSzHCg 提取码:virc ``` ### 搭建方案 ![](https://img.kancloud.cn/d0/3d/d03d68020fc9d13aa7e80333cc687469_889x455.png) ### 简介 生产环境中部署`nacos`需要使用集群模式`cluster`保证高可用,本文主要详细介绍如何搭建搭建生产可用的Nacos集群 * nacos采用raft协议实现集群,有leader的概念 ![](https://img.kancloud.cn/2f/b5/2fb56e47353f346c52e62978ecd5abf4_1920x738.png) ### 主机规划 #### mysql | 节点 | 端口 | | :-- | :-- | | 59.110.164.254| 3306| #### nacos集群 | 节点 | 端口 | | :-- | :-- | | 192.168.235.130| 8848 | | 192.168.235.131| 8848 | | 192.168.235.132 | 8848 | nacos集群中,每台机器安装JDK ##### jdk安装步骤 * 1.将下载好的jdk跟maven上传到服务器上 * 2.解压jdk到/usr/local/ tar -zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/ * 3.配置环境变量 vi /etc/profile 在最下面,按i进入insert模式,添加一下内容 JAVA\_HOME=/usr/local/jdk1.8.0\_91 export JAVA\_HOME CLASSPATH=.:$JAVA\_HOME/lib export CLASSPATH PATH=$PATH:$JAVA\_HOME/bin:$CLASSPATH export PATH 按esc进入命令行模式,再按:wq保存退出 * 4.激活配置 source /etc/profile #### openresty集群 | 节点 | 端口 | | :-- | :-- | | 192.168.235.133| 80 | | 192.168.235.134| 80| VIP > 192.168.235.135 ### 准备阶段 mysql中新建数据库nacos,导入初始化脚本,本文采用单节点部署mysql方式,为了快速部署nacos集群,生产环境选用Galera mysql集群方式部署。 ### 初始化脚本 ![](https://img.kancloud.cn/06/79/06799a87078a9a05c456a344a704ccec_916x376.png) 找到 `conf/nacos-mysql.sql` ,将其导入到数据库中。 导入后查看数据库 ![](https://img.kancloud.cn/76/a0/76a0f02b6e697bd422a8a653e46ed5c8_1192x448.png) ### 192.168.235.130 * 创建目录 ``` mkdir /app ``` * 将文章顶部百度网盘中的文件上传到 192.168.235.130中的/app目录 ![](https://img.kancloud.cn/b2/cf/b2cf732cfc08d6979f60da7f54ae83df_1917x242.png) * 修改application.properties文件,最后部分增加如下配置 ``` spring.datasource.platform=mysql db.num=1 db.url.0=jdbc:mysql://59.110.164.254:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true db.user=root db.password=root ``` * 修改cluster.conf文件,nacos集群的三台主机信息 ``` # ip:port 192.168.235.130:8848 192.168.235.131:8848 192.168.235.132:8848 ``` * 启动 ``` cd /app/nacos-server/bin ./startup.sh ``` * 查看启动情况 ``` tail -f /app/nacos-server/logs/start.out ``` ### 192.168.235.131 * 创建目录 ``` mkdir /app ``` * 将文章顶部百度网盘中的文件上传到 192.168.235.131中的/app目录 ![](https://img.kancloud.cn/b2/cf/b2cf732cfc08d6979f60da7f54ae83df_1917x242.png) * 修改application.properties文件,最后部分增加如下配置 ``` spring.datasource.platform=mysql db.num=1 db.url.0=jdbc:mysql://59.110.164.254:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true db.user=root db.password=root ``` * 修改cluster.conf文件,nacos集群的三台主机信息 ``` # ip:port 192.168.235.130:8848 192.168.235.131:8848 192.168.235.132:8848 ``` * 启动 ``` cd /app/nacos-server/bin ./startup.sh ``` * 查看启动情况 ``` tail -f /app/nacos-server/logs/start.out ``` ### 192.168.235.132 * 创建目录 ``` mkdir /app ``` * 将文章顶部百度网盘中的文件上传到 192.168.235.132中的/app目录 ![](https://img.kancloud.cn/b2/cf/b2cf732cfc08d6979f60da7f54ae83df_1917x242.png) * 修改application.properties文件,最后部分增加如下配置 ``` spring.datasource.platform=mysql db.num=1 db.url.0=jdbc:mysql://59.110.164.254:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true db.user=root db.password=root ``` * 修改cluster.conf文件,nacos集群的三台主机信息 ``` # ip:port 192.168.235.130:8848 192.168.235.131:8848 192.168.235.132:8848 ``` * 启动 ``` cd /app/nacos-server/bin ./startup.sh ``` * 查看启动情况 ``` tail -f /app/nacos-server/logs/start.out ``` ### 192.168.235.133 * OpenResty安装 ``` ●Centos:yum install -y readline-devel pcre-devel openssl-devel gcc GeoIP-devel ●wget https://openresty.org/download/openresty-1.11.2.1.tar.gz ●tar -zxvf openresty-1.11.2.1.tar.gz ●cd openresty-1.11.2.1 ●./configure --with-luajit --with-pcre --with-http_gzip_static_module --with-http_realip_module --with-http_geoip_module --with-http_ssl_module --with-http_stub_status_module --prefix=/app/openresty ●make && make install ``` * 安装完成 * ![](https://img.kancloud.cn/cf/48/cf48a4ca7226216c7aad5bd6685182ff_1025x356.png) * OpenResty配置/app/openresty/nginx/conf/nginx.conf ``` #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include 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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream nacosCluster { server 192.168.235.130:8848; server 192.168.235.131:8848; server 192.168.235.132:8848; } server { listen 80; server_name nacostest.com; location / { proxy_pass http://nacosCluster; } } } ~ ``` * OpenResty启动 ``` cd /app/openresty/nginx/sbin ./nginx ``` * 验证 http://192.168.235.133/nacos 用户名/密码 nacos nacos ![](https://img.kancloud.cn/a8/13/a8136d9e4491577facd9a42ff8499928_1716x740.png) ### 192.168.235.134 * OpenResty安装 ``` ●Centos:yum install -y readline-devel pcre-devel openssl-devel gcc GeoIP-devel ●wget https://openresty.org/download/openresty-1.11.2.1.tar.gz ●tar -zxvf openresty-1.11.2.1.tar.gz ●cd openresty-1.11.2.1 ●./configure --with-luajit --with-pcre --with-http_gzip_static_module --with-http_realip_module --with-http_geoip_module --with-http_ssl_module --with-http_stub_status_module --prefix=/app/openresty ●make && make install ``` * 安装完成 * ![](https://img.kancloud.cn/cf/48/cf48a4ca7226216c7aad5bd6685182ff_1025x356.png) * OpenResty配置/app/openresty/nginx/conf/nginx.conf ``` #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include 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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream nacosCluster { server 192.168.235.130:8848; server 192.168.235.131:8848; server 192.168.235.132:8848; } server { listen 80; server_name nacostest.com; location / { proxy_pass http://nacosCluster; } } } ~ ``` * OpenResty启动 ``` cd /app/openresty/nginx/sbin ./nginx ``` * 验证 http://192.168.235.134/nacos 用户名/密码 nacos nacos ![](https://img.kancloud.cn/c4/83/c483d4d0aed9a10ed237520c7f7ded6f_1920x791.png) ### 192.168.235.133 * 安装keepalived ``` yum install -y net-tools psmisc net-snmp ipvsadm keepalived ``` ![](https://img.kancloud.cn/21/ef/21efa6337b6060e0c07194f038d07422_948x438.png) * 安装killall命令 ``` yum install psmisc -y ``` * 配置执行脚本 vi /etc/keepalived/check_nginx.sh ``` #!/bin/bash count=$(ps -ef |grep nginx |egrep -cv "grep|$$") if [ "$count" -eq 0 ];then killall keepalived fi ``` chmod +x /etc/keepalived/check_nginx.sh * 配置/etc/keepalived/keepalived.conf,vip 192.168.235.135 ``` ! Configuration File for keepalived global_defs { # 陆脫脮始镁碌脴路 notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } # 脫录镁路垄脣碌脴路 notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id NGINX_MASTER } vrrp_script check_nginx { script "/etc/keepalived/check_nginx.sh" } vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 # VRRP 路脫 ID实脌拢卢每赂枚媒禄碌脛 priority 100 # 脫脧录露拢卢卤赂路镁脦脝脡脰 90 advert_int 1 # 指露篓VRRP 脨脤掳眉盲时录洌兟 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.235.135/24 } track_script { check_nginx } } ``` * 启动keepalived ``` service keepalived start service keepalived status ``` ![](https://img.kancloud.cn/63/e5/63e5d61a487da067093baaa1a1c3b518_1447x407.png) * 查看虚拟VIP ![](https://img.kancloud.cn/d6/72/d672034b102d5d4a246ae6421dcd7d9b_1315x341.png) ![](https://img.kancloud.cn/19/d2/19d28d1ea66dc61ba2854249b25f9543_1920x757.png) ### 192.168.235.134 * 安装keepalived ``` yum install -y net-tools psmisc net-snmp ipvsadm keepalived ``` ![](https://img.kancloud.cn/21/ef/21efa6337b6060e0c07194f038d07422_948x438.png) * 安装killall命令 ``` yum install psmisc -y ``` * 配置执行脚本 vi /etc/keepalived/check_nginx.sh ``` #!/bin/bash count=$(ps -ef |grep nginx |egrep -cv "grep|$$") if [ "$count" -eq 0 ];then killall keepalived fi ``` chmod +x /etc/keepalived/check_nginx.sh * 配置/etc/keepalived/keepalived.conf,vip 192.168.235.135 ``` ! Configuration File for keepalived global_defs { # 陆脫脮始镁碌脴路 notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } # 脫录镁路垄脣碌脴路 notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id NGINX_MASTER } vrrp_script check_nginx { script "/etc/keepalived/check_nginx.sh" } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 # VRRP 路脫 ID实脌拢卢每赂枚媒禄碌脛 priority 90 # 脫脧录露拢卢卤赂路镁脦脝脡脰 90 advert_int 1 # 指露篓VRRP 脨脤掳眉盲时录洌兟 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.235.135/24 } track_script { check_nginx } } ``` * 启动keepalived ``` service keepalived start service keepalived status ``` ![](https://img.kancloud.cn/63/e5/63e5d61a487da067093baaa1a1c3b518_1447x407.png) * 查看虚拟VIP ![](https://img.kancloud.cn/d6/72/d672034b102d5d4a246ae6421dcd7d9b_1315x341.png) ![](https://img.kancloud.cn/19/d2/19d28d1ea66dc61ba2854249b25f9543_1920x757.png)