Nacos high availability deployment

Environment preparation

Make sure you are using it in an environment:

  1. 64-bit OS Linux, Unix, or Mac. Linux is recommended.
  2. 64-bit JDK 1.8+; Download. Configure.
  3. Maven 3.2 x +; Download. Configure.
  4. Three or more Nacos nodes are required to form a cluster.

Download the source installation package

You can download the nacos-server-$version.zip package from the latest stable version.

$ unzip nacos-server-$versionZip or tar -xvf nacos-server-$version.tar.gz
$ cd nacos/bin
Copy the code

Configuring a Data Source

By default, Nacos comes with an embedded database called Derby. Each Nacos starts with a separate Derby database, and data is not synchronized in a clustered environment.

Therefore, you need to configure a custom data source, not the default data source that comes with Nacos.

Find the SQL script in nacos\conf: nacos-mysql.sql

 
CREATE DATABASE nacos_config;
USE nacos_config;
 
/* Database full name = nacos_config */
/* Table name = config_info */
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
CREATE TABLE `config_info` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT    'data_id',
  `group_id` varchar(255) DEFAULT NULL,
  `content` longtext NOT NULL COMMENT 'content',
  `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
  `gmt_create` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' COMMENT 'Creation time',
  `gmt_modified` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' COMMENT 'Modification time',
  `src_user` text COMMENT 'source user',
  `src_ip` varchar(20) DEFAULT NULL COMMENT 'source ip',
  `app_name` varchar(128) DEFAULT NULL,
  `tenant_id` varchar(128) DEFAULT ' ' COMMENT 'Tenant Field',
  `c_desc` varchar(256) DEFAULT NULL,
  `c_use` varchar(64) DEFAULT NULL,
  `effect` varchar(64) DEFAULT NULL,
  `type` varchar(64) DEFAULT NULL,
  `c_schema` text,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfo_datagrouptenant` (`data_id`,`group_id`,`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info';
 
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/* Database full name = nacos_config */
/* Table name = config_info_AGgr */
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
CREATE TABLE `config_info_aggr` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(255) NOT NULL COMMENT 'group_id',
  `datum_id` varchar(255) NOT NULL COMMENT 'datum_id',
  `content` longtext NOT NULL COMMENT 'content',
  `gmt_modified` datetime NOT NULL COMMENT 'Modification time',
  `app_name` varchar(128) DEFAULT NULL,
  `tenant_id` varchar(128) DEFAULT ' ' COMMENT 'Tenant Field'.PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfoaggr_datagrouptenantdatum` (`data_id`,`group_id`,`tenant_id`,`datum_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Add tenant field';
 
 
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/* Database full name = nacos_config */
/* Table name = config_info_beta */
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
CREATE TABLE `config_info_beta` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `content` longtext NOT NULL COMMENT 'content',
  `beta_ips` varchar(1024) DEFAULT NULL COMMENT 'betaIps',
  `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
  `gmt_create` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' COMMENT 'Creation time',
  `gmt_modified` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' COMMENT 'Modification time',
  `src_user` text COMMENT 'source user',
  `src_ip` varchar(20) DEFAULT NULL COMMENT 'source ip',
  `tenant_id` varchar(128) DEFAULT ' ' COMMENT 'Tenant Field'.PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfobeta_datagrouptenant` (`data_id`,`group_id`,`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_beta';
 
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/* Database full name = nacos_config */
/* Table name = config_info_tag */
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
CREATE TABLE `config_info_tag` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `tenant_id` varchar(128) DEFAULT ' ' COMMENT 'tenant_id',
  `tag_id` varchar(128) NOT NULL COMMENT 'tag_id',
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `content` longtext NOT NULL COMMENT 'content',
  `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
  `gmt_create` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' COMMENT 'Creation time',
  `gmt_modified` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' COMMENT 'Modification time',
  `src_user` text COMMENT 'source user',
  `src_ip` varchar(20) DEFAULT NULL COMMENT 'source ip'.PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfotag_datagrouptenanttag` (`data_id`,`group_id`,`tenant_id`,`tag_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_tag';
 
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/* Database full name = nacos_config */
/* Table name = config_TAGs_relation */
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
CREATE TABLE `config_tags_relation` (
  `id` bigint(20) NOT NULL COMMENT 'id',
  `tag_name` varchar(128) NOT NULL COMMENT 'tag_name',
  `tag_type` varchar(64) DEFAULT NULL COMMENT 'tag_type',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `tenant_id` varchar(128) DEFAULT ' ' COMMENT 'tenant_id',
  `nid` bigint(20) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`nid`),
  UNIQUE KEY `uk_configtagrelation_configidtag` (`id`,`tag_name`,`tag_type`),
  KEY `idx_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_tag_relation';
 
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/* Database full name = nacos_config */
/* Table name = group_capacity */
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
CREATE TABLE `group_capacity` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key ID',
  `group_id` varchar(128) NOT NULL DEFAULT ' ' COMMENT 'Group ID, null character for whole cluster ',
  `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Quota, 0 means default',
  `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Usage',
  `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum size of a single configuration, in bytes, with 0 indicating default',
  `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum number of aggregate sub-configurations, where 0 indicates default',
  `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Upper subconfiguration size of a single aggregate data, in bytes, with 0 indicating default.',
  `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum change history',
  `gmt_create` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' COMMENT 'Creation time',
  `gmt_modified` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' COMMENT 'Modification time'.PRIMARY KEY (`id`),
  UNIQUE KEY `uk_group_id` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Cluster and Group Capacity Information Table';
 
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/* Database full name = nacos_config */
/* Table name = his_config_info */
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
CREATE TABLE `his_config_info` (
  `id` bigint(64) unsigned NOT NULL,
  `nid` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `data_id` varchar(255) NOT NULL,
  `group_id` varchar(128) NOT NULL,
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `content` longtext NOT NULL,
  `md5` varchar(32) DEFAULT NULL,
  `gmt_create` datetime NOT NULL DEFAULT '2010-05-05 00:00:00',
  `gmt_modified` datetime NOT NULL DEFAULT '2010-05-05 00:00:00',
  `src_user` text,
  `src_ip` varchar(20) DEFAULT NULL,
  `op_type` char(10) DEFAULT NULL,
  `tenant_id` varchar(128) DEFAULT ' ' COMMENT 'Tenant Field'.PRIMARY KEY (`nid`),
  KEY `idx_gmt_create` (`gmt_create`),
  KEY `idx_gmt_modified` (`gmt_modified`),
  KEY `idx_did` (`data_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Multi-tenant Conversion';
 
 
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/* Database full name = nacos_config */
/* Table name = tenant_capacity */
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
CREATE TABLE `tenant_capacity` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key ID',
  `tenant_id` varchar(128) NOT NULL DEFAULT ' ' COMMENT 'Tenant ID',
  `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Quota, 0 means default',
  `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Usage',
  `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum size of a single configuration, in bytes, with 0 indicating default',
  `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum number of aggregate sub-configurations',
  `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Upper subconfiguration size of a single aggregate data, in bytes, with 0 indicating default.',
  `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Maximum change history',
  `gmt_create` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' COMMENT 'Creation time',
  `gmt_modified` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' COMMENT 'Modification time'.PRIMARY KEY (`id`),
  UNIQUE KEY `uk_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Tenant Capacity Information Table';
 
 
CREATE TABLE `tenant_info` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `kp` varchar(128) NOT NULL COMMENT 'kp',
  `tenant_id` varchar(128) default ' ' COMMENT 'tenant_id',
  `tenant_name` varchar(128) default ' ' COMMENT 'tenant_name',
  `tenant_desc` varchar(256) DEFAULT NULL COMMENT 'tenant_desc',
  `create_source` varchar(32) DEFAULT NULL COMMENT 'create_source',
  `gmt_create` bigint(20) NOT NULL COMMENT 'Creation time',
  `gmt_modified` bigint(20) NOT NULL COMMENT 'Modification time'.PRIMARY KEY (`id`),
  UNIQUE KEY `uk_tenant_info_kptenantid` (`kp`,`tenant_id`),
  KEY `idx_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='tenant_info';
 
CREATE TABLE users (
    username varchar(50) NOT NULL PRIMARY KEY,
    password varchar(500) NOT NULL,
    enabled boolean NOT NULL
);
 
CREATE TABLE roles (
    username varchar(50) NOT NULL,
    role varchar(50) NOT NULL
);
 
INSERT INTO users (username, password, enabled) VALUES ('nacos'.'$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu'.TRUE);
 
INSERT INTO roles (username, role) VALUES ('nacos'.'ROLE_ADMIN');
 

Copy the code

Find application.properties in the nacos\conf directory

####################### new configuration, Please fill in your mysql data source configuration # # # # # # # # # # # # # # # # # # # # # # # # # # # spring. The datasource. The platform = mysql db. The num = 1 db.url.0=jdbc:mysql://localhost:3306/nacos_config? characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true db.user=root db.password=123456Copy the code

The nacOS cluster configuration on a Linux server is cluster.conf

In the conf directory of nacos/, the configuration file cluster.conf exists. Set each line to IP :port. (Please configure 3 or more nodes)

[root@izbp1907haerdhq7kdb8mvz conf]# pwd /root/nacos/conf [root@izbp1907haerdhq7kdb8mvz conf]# cp cluster.conf.example cluster.conf [root@izbp1907haerdhq7kdb8mvz conf]# ll total 80 -rw-r--r-- 1 root root 1224 Dec 3 20:01 1.4.0-ipv6_support-update. SQL -rw-r--r-- 1 root root 7099 Jan 21 18:17 application.properties -rw-r--r-- 1 root root 6515 Dec 3 20:01 application.properties.example -rw-r--r-- 1 root root 670 Feb 19 15:37 cluster.conf -rw-r--r-- 1 root root 670 Nov 18 16:41 cluster.conf.example -rw-r--r-- 1 root root 28366 Dec 5 01:13 nacos-logback.xml -rw-r--r-- 1 root root 10660 Dec 3 20:01 nacos-mysql.sql -rw-r--r-- 1 root root 8795 Dec 3 20:01 schema.sql [root@izbp1907haerdhq7kdb8mvz conf]# vim cluster.conf#Caution You cannot set localhost or 127.0.0.1 to the actual IP address
#The default port number is 8848. If you want to use other ports, you can change the port number in the application.properties directory
192.168.16.101:8848
192.168.16.102:8848
192.168.16.103:8848

Copy the code

The configured Nacos is packaged and distributed to the other two machines

#Package the configured nacOS as nacos.tar
$ tar cf nacos.tar nacos/
#Distribute to the other two machines
$SCP. / nacos. Tar [email protected]: / root /
$SCP. / nacos. Tar [email protected]: / root /

#Decompress the received nacos.tar package on 172.22.19.45 172.22.19.45
$ tar xf nacos.tar
Copy the code

Run startup.sh in nacos/bin respectively to start nacOS

$ ./startup.sh
Copy the code

Modify the Nginx configuration file to start Nginx.

Nginx installation tutorial: www.runoob.com/linux/nginx…

Configuration changes:

  1. New upstream cluster
  2. Listen on port 8849. You can change it to another port
  3. Set location to proxy_pass http://cluster;
Upstream cluster{server 172.22.19.45:8848 server 172.22.19.46:8848 server 172.22.19.48:8848} server {listen 8849; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://cluster; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }}Copy the code

Nginx start

#Go to the sbin directory to specify the configuration file to start
$ ./nginx -c /usr/local/webserver/nginx/conf/nginx.conf

#/usr/local/webserver/nginx/sbin/nginx -s reload            Reload the configuration file
#/usr/local/webserver/nginx/sbin/nginx -s reopen            # restart Nginx
#/usr/local/webserver/nginx/sbin/nginx -s stop              # stop Nginx
Copy the code

Test access

  1. The test accessed nacOS through nginx – accessible successfully

    https://ip:8849/nacos/#/login

    The cloud server needs to open the external network port

  2. Create a new configuration test – NACOS normal

  3. Mysql > insert a new record

  4. Shut down one NACOS and check whether one nacOS cluster management node goes offline.

If the above tests pass, the high availability of Nacos is successfully set up.