Use Docker to build Nacos persistence and cluster deployment

Table of contents

1. Prepare 

1.1 mysql installation

1.2 Create the database required by nacos

1.3 Download nacos image

1.4 Create a custom network (can be omitted)

1.5 Get nginx image

2. Nacos cluster deployment

2.1 Connect mysql to mynet network (can be omitted)

2.2 nacos cluster

2.3 Use nginx to access the cluster


1. Prepare 

1.1 mysql installation

  • download mirror
docker pull mysql/mysql-server:5.7
  • Relevant directories in the host, used to mount relevant data of the container
mkdir -p /data/mysql/{conf,data}
  • Write the my.cnf configuration file in the /data/mysql/conf directory (or download and upload directly)

my.cnf.txt - Lanzuo Cloud / You need to delete the following txt extension

[client]
default-character-set=utf8
 
[mysql]
default-character-set=utf8
 
[mysqld]
##官方的配置
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid


##下面为添加的自定义配置
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
# default: sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# modeified: 
sql_mode= STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
max_allowed_packet=10M
default-time_zone='+8:00'
default_authentication_plugin=mysql_native_password
  • Create and start the mysql container
    docker run -p 3306:3306 \
          --name mysql \
          -v /data/mysql/conf/my.cnf:/etc/my.cnf \
          -v /data/mysql/data:/var/lib/mysql \
          --privileged=true \
          --restart=always \
          -e MYSQL_ROOT_PASSWORD=123456 \
          -d mysql/mysql-server:5.7
    

    Parameter Description:

    -p 3306:3306: host port: container port

    --name mysql: container name

    -v: mount a directory of the host, the key to persistent storage, mount the host directory to the corresponding directory of the container, namely: configuration file, log file, data file

    -in /data/mysql/conf:/etc/mysql/conf.d

    -v /data/mysql/logs:/logs

    -v /data/mysql/data:/var/lib/mysq

    --privileged=true: With this parameter, the root inside the container has real root privileges, otherwise, the root inside the container is just an ordinary external user privilege

    --restart=always: container automatic startup parameters, its value can be [no,on-failure,always]

    no is the default value, which means that when the container exits, docker will not automatically restart the container

    on-failure means that if the exit status of the container is not 0, docker will automatically restart the container, and you can also specify the number of restarts. If the container fails to start after the specified number of times, it will give up

    always means that as long as the container exits, docker will automatically restart the container

    -e MYSQL_ROOT_PASSWORD=123456: set root password

    -d mysql/mysql-server:5.7: background startup mode and mirror image used

  • Modify mysql to allow Navicat to connect remotely
    # 进入容器
    docker exec -it mysql /bin/bash
    
    # 登录
    mysql -u root -p;
    
    # 授权
    grant all privileges on *.* to root@'%' identified by '123456';
    
    # 刷新权限
    flush privileges; 
    

    1.2 Create the database required by nacos

  • Add port 3306 to the firewall and use the mysql client to connect to the database
  • create database
    CREATE DATABASE IF NOT EXISTS nacos_config
    DEFAULT CHARACTER SET utf8
    DEFAULT COLLATE utf8_general_ci;
    

    data sheet

    /******************************************/
    /*   数据库全名 = nacos_config   */
    /*   表名称 = 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 '创建时间',
      `gmt_modified` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' COMMENT '修改时间',
      `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 '租户字段',
      `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';
    
    /******************************************/
    /*   数据库全名 = nacos_config   */
    /*   表名称 = 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 '内容',
      `gmt_modified` datetime NOT NULL COMMENT '修改时间',
      `app_name` varchar(128) DEFAULT NULL,
      `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
      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='增加租户字段';
    
    
    /******************************************/
    /*   数据库全名 = nacos_config   */
    /*   表名称 = 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 '创建时间',
      `gmt_modified` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' COMMENT '修改时间',
      `src_user` text COMMENT 'source user',
      `src_ip` varchar(20) DEFAULT NULL COMMENT 'source ip',
      `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
      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';
    
    /******************************************/
    /*   数据库全名 = nacos_config   */
    /*   表名称 = 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 '创建时间',
      `gmt_modified` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' COMMENT '修改时间',
      `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';
    
    /******************************************/
    /*   数据库全名 = nacos_config   */
    /*   表名称 = 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';
    
    /******************************************/
    /*   数据库全名 = nacos_config   */
    /*   表名称 = group_capacity   */
    /******************************************/
    CREATE TABLE `group_capacity` (
      `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
      `group_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Group ID,空字符表示整个集群',
      `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '配额,0表示使用默认值',
      `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用量',
      `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个配置大小上限,单位为字节,0表示使用默认值',
      `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '聚合子配置最大个数,,0表示使用默认值',
      `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值',
      `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最大变更历史数量',
      `gmt_create` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' COMMENT '创建时间',
      `gmt_modified` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' COMMENT '修改时间',
      PRIMARY KEY (`id`),
      UNIQUE KEY `uk_group_id` (`group_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='集群、各Group容量信息表';
    
    /******************************************/
    /*   数据库全名 = nacos_config   */
    /*   表名称 = 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 '租户字段',
      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='多租户改造';
    
    
    /******************************************/
    /*   数据库全名 = nacos_config   */
    /*   表名称 = tenant_capacity   */
    /******************************************/
    CREATE TABLE `tenant_capacity` (
      `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
      `tenant_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Tenant ID',
      `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '配额,0表示使用默认值',
      `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用量',
      `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个配置大小上限,单位为字节,0表示使用默认值',
      `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '聚合子配置最大个数',
      `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值',
      `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最大变更历史数量',
      `gmt_create` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' COMMENT '创建时间',
      `gmt_modified` datetime NOT NULL DEFAULT '2010-05-05 00:00:00' COMMENT '修改时间',
      PRIMARY KEY (`id`),
      UNIQUE KEY `uk_tenant_id` (`tenant_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='租户容量信息表';
    
    
    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 '创建时间',
      `gmt_modified` bigint(20) NOT NULL COMMENT '修改时间',
      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');
    

    After importing the data, we delete the database container and re-create mysql that cannot be accessed from the external network

    If there is no custom network, set up a custom network first

    Use –subnet to create a network (used to specify the ip segment), –gateway (used to specify the gateway), and my_net to create the name as follows

     sudo docker network create --driver bridge --subnet 192.168.0.1/16 --gateway 192.168.0.1 mynet
    
       docker run \
             --name mysql \
             --net mynet \
             --ip 192.168.0.11 \
             -v /data/mysql/conf/my.cnf:/etc/my.cnf \
             -v /data/mysql/data:/var/lib/mysql \
             --privileged=true \
             --restart=always \
             -e MYSQL_ROOT_PASSWORD=123456 \
             -d mysql/mysql-server:5.7   

    1.3 Download nacos image

    docker pull nacos/nacos-server:1.1.4

    1.4 Create a custom network (can be omitted)

    docker network create mynet --subnet=192.168.0.0/16

    1.5 Get nginx image

    docker pull nginx

    2. Nacos cluster deployment

    Because the machine configuration is low, this example uses two nacos servers to demonstrate the cluster. In a real environment, in order to facilitate the election of the master node, usually there are an odd number of cluster machines.

    2.1 Connect mysql to mynet network (can be omitted)

    docker network connect mynet mysql --ip 192.168.0.11

    At this point, you can view the specific configuration of the mynet network:

    docker network inspect mynet

    Note: If the mysql container has not been created, you can also specify the network configuration when creating the mysql container (the above has connected mysql to mynet, so this step is not necessary):

    docker run \
             --name mysql \
             --net mynet \
             --ip 192.168.0.11 \
             -v /data/mysql/conf/my.cnf:/etc/my.cnf \
             -v /data/mysql/data:/var/lib/mysql \
             --privileged=true \
             --restart=always \
             -e MYSQL_ROOT_PASSWORD=123456 \
             -d mysql/mysql-server:5.7 
    

    2.2 nacos cluster

  • Start the first node nacos node
    docker run -d \
               --net mynet \
               --ip 192.168.0.21 \
               -e PREFER_HOST_MODE=ip \
               -e MODE=cluster \
               -e NACOS_SERVERS="192.168.0.22:8848" \
               -e SPRING_DATASOURCE_PLATFORM=mysql \
               -e MYSQL_MASTER_SERVICE_HOST=192.168.0.11 \
               -e MYSQL_MASTER_SERVICE_PORT=3306 \
               -e MYSQL_MASTER_SERVICE_USER=root \
               -e MYSQL_MASTER_SERVICE_PASSWORD=123456 \
               -e MYSQL_MASTER_SERVICE_DB_NAME=nacos_config \
               -e MYSQL_DATABASE_NUM=1 \
               -e NACOS_SERVER_PORT=8848 \
               --name nacos01 \
               --restart=always \
               nacos/nacos-server:1.1.4
    

  • Start the second nacos node
    docker run -d \
               --net mynet \
               --ip 192.168.0.22 \
               -e PREFER_HOST_MODE=ip \
               -e MODE=cluster \
               -e NACOS_SERVERS="192.168.0.21:8848" \
               -e SPRING_DATASOURCE_PLATFORM=mysql \
               -e MYSQL_MASTER_SERVICE_HOST=192.168.0.11 \
               -e MYSQL_MASTER_SERVICE_PORT=3306 \
               -e MYSQL_MASTER_SERVICE_USER=root \
               -e MYSQL_MASTER_SERVICE_PASSWORD=123456 \
               -e MYSQL_MASTER_SERVICE_DB_NAME=nacos_config \
               -e MYSQL_DATABASE_NUM=1 \
               -e NACOS_SERVER_PORT=8848 \
               --name nacos02 \
               --restart=always \
               nacos/nacos-server:1.1.4
    

    2.3 Use nginx to access the cluster

  • Create a host mount directory
    [root@localhost data]# mkdir -p /data/nginx/{conf,log,html}

  • Upload the nginx.conf configuration file (upload to the /data/nginx/conf directory)
  • The content of the configuration file is as follows

    worker_processes 1;
    
    events {
        worker_connections 1024;
    }
    
    http {
        include mime.types;
        default_type  application/octet-stream;
        sendfile on;
    
        keepalive_timeout 65;
        gzip on;
    
        #服务器的集群
        #upstream  tomcats {  #服务器集群名字
                #TODO:172.17.0.3是docker容器的IP 
            #server    172.17.0.3:8080  weight=1;#服务器配置   weight是权重的意思,权重越大,分配的概率越大。
            #server    172.17.0.4:8080  weight=2;
        #}
        
        #新增1:nacos集群
        upstream  nacoses {
            server    192.168.0.21:8848  weight=1;
            server    192.168.0.22:8848  weight=1;
        }  
    
        #当前的Nginx的配置
        server {
            listen       80;#监听80端口,可以改成其他端口
            server_name  localhost;#当前服务的域名,没有域名可随便填写
       
                    #新增2:用于重定向至nacos集群
            location /nacos {
                    proxy_pass http://nacoses;
            }
    
            location / {
                    root         /usr/share/nginx/html/dist;#将要访问的网站的目录
                try_files $uri $uri/  /index.html;#该句代码是为解决history路由不能跳转的问题,在vue-router官网有介绍
            }
    
            #location  ^~/api/ {
                    #^~/api/表示匹配前缀是api的请求,proxy_pass的结尾有/, 则会把/api/*后面的路径直接拼接到后面,即移除api
                #proxy_pass http://tomcats/;
            #}
        }
    
    }
    

  • Start the nginx container:
    docker run \
           --name mynginx \
           --net mynet \
           -d -p 80:80 \
           -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
           -v /data/nginx/log:/var/log/nginx \
           -v /data/nginx/html:/usr/share/nginx/html \
           nginx:latest
    

  • Connect mynginx to the bridge network at the same time for communication with the host
    [root@localhost ~]# docker network connect bridge mynginx
    

  • Access the console to confirm the deployment:
  • http://192.168.229.130/nacos

    The following interface appears to indicate that the cluster is normal:

Guess you like

Origin blog.csdn.net/qq_62898618/article/details/128430108