Nacos 高可用集群部署


簡介

Nacos支持三種部署模式:

  • 單機模式 - 用於測試和單機試用。

  • 集群模式 - 用於生產環境,確保高可用。

  • 多集群模式 - 用於多數據中心場景。

這里我們主要講解 集群模式部署。至於測試使用的單機模式不是參考單機模式下運行Nacos

環境准備

架構說明

為了保證 Nacos 的高可用,使用 HAProxy 負載均衡3 節點集群的 nacos;nacos 數據庫使用雙主架構的MySQL,MySQL 通過 HAProxy 代理訪問,nacos 使用 HAProxy 代理后的地址訪問數據庫。

版本

系統:CentOS 7

Nacos:v1.2.1

MySQL:v5.7

HAProxy:v2.0.4

服務器配置信息

主機名 IP 配置
nacos01 192.168.17.37 4C8G100G
nacos02 192.168.17.38 4C8G100G
nacos03 192.168.17.39 4C8G100G

MySQL 主主

MySQL部署於192.168.17.37、和192.168.17.38上,互為主從,通過內網HAProxy代理(192.168.14.2)。

注意:我這里實驗環境所以,兩台 MySQL 和 nacos 機器部署在一起,生產環境MySQL請單獨部署

1. 預備環境准備

請確保是在環境中安裝使用:

  1. 64 bit OS Linux/Unix/Mac,推薦使用Linux系統。
  2. 64 bit JDK 1.8+;下載配置
  3. Maven 3.2.x+;下載配置
  4. 3個或3個以上Nacos節點才能構成集群。

2. JDK 1.8+ 環境配置

# cd /usr/
# tar -xf jdk-8u241-linux-x64.tar.gz
# mv jdk1.8.0_241 java

備注:至於為什么要將解壓后的jdk-8u241-linux-x64.tar.gz文件jdk1.8.0_241重命名為 java,且放置於/usr目錄下,請參考nacos目錄的bin/startup.sh腳本,有驚喜發現哦!

配置環境變量 /etc/profile

export JAVA_HOME=/usr/java
export PATH=$JAVA_HOME/bin:$PATH

使能環境變量

# source /etc/profile

驗證

# java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)

3. Maven 3.2.x+ 環境配置

# cd /usr/local
# tar -xf apache-maven-3.6.3-bin.tar.gz

配置環境變量 /etc/profile

export MAVEN_HOME=/usr/local/apache-maven-3.6.3
export PATH=$MAVEN_HOME/bin:$PATH

使能環境變量

# source /etc/profile

驗證

# mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/apache-maven-3.6.3
Java version: 1.8.0_241, vendor: Oracle Corporation, runtime: /usr/local/jdk1.8.0_241/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1062.18.1.el7.x86_64", arch: "amd64", family: "unix"

4. MySQL 57 yum 倉庫安裝

安裝MySQL57

# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
# yum -y install mysql57-community-release-el7-10.noarch.rpm
# yum -y install mysql-community-server
# systemctl start  mysqld.service
# grep "password" /var/log/mysqld.log
# mysql -uroot -p
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=1;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';

注意:如果后面的項目中使用root用戶帳號密碼連接數據庫,需要開放訪問權限

例如:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'new password';
mysql> flush privileges;

刪除mysql源

# yum -y remove mysql57-community-release-el7-10.noarch

5. MySQL 57 主主配置

數據庫采用雙主配置,即互為主從

主庫:192.168.17.37、從庫:192.168.17.38

主庫:192.168.17.38、從庫:192.168.17.37

備注:以下以主庫(192.168.17.37)-> 從庫(192.168.17.38) 為例配置;

依此類推可配置主庫(192.168.17.38)-> 從庫(192.168.17.37),請自行配置。

5.1 主庫

停止數據庫,修改配置文件 /etc/my.cnf

# systemctl stop mysqld
[mysqld]
datadir=/www/mysql
socket=/www/mysql/mysql.sock

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

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

bind-address=0.0.0.0

server_id=37
log-bin=mysql-bin
log_bin_index=/var/log/mysql/mysql-bin.log.index
relay_log=/var/log/mysql/mysql-relay-bin
relay_log_index=/var/log/mysql/mysql-relay-bin.index

binlog_format=row
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog_cache_size=1m
expire_logs_days=7
log-bin=mysql-bin
max_binlog_size=1024M


auto_increment_offset=1
auto_increment_increment=2

replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
replicate-ignore-db=test

innodb_buffer_pool_size=4G
max_connections=3000

explicit_defaults_for_timestamp=true

[client]
socket=/www/mysql/mysql.sock

備注:從庫配置過程中注意auto_increment_offset設置為2、兩台數據庫server_id的值不能一樣。

# systemctl start mysqld

登入數據庫,添加主從復制用戶及授權

# mysql -u root -p
mysql> CREATE USER 'repl'@'192.168.17.%' IDENTIFIED BY 'repl';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.17.%';

查看主庫狀態

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+---------------------------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                            | Executed_Gtid_Set |
+------------------+----------+--------------+---------------------------------------------+-------------------+
| mysql-bin.000005 |     1082 |              | mysql,information_schema,performance_schema |                   |
+------------------+----------+--------------+---------------------------------------------+-------------------+
1 row in set (0.00 sec)

5.2 從庫

停止數據庫,修改配置文件 /etc/my.cnf

# systemctl stop mysqld
[mysqld]
datadir=/www/mysql
socket=/www/mysql/mysql.sock

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

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

bind-address=0.0.0.0

server_id=38
log-bin=mysql-bin
log_bin_index=/var/log/mysql/mysql-bin.log.index
relay_log=/var/log/mysql/mysql-relay-bin
relay_log_index=/var/log/mysql/mysql-relay-bin.index

binlog_format=row
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
binlog_cache_size=1m
expire_logs_days=7
log-bin=mysql-bin
max_binlog_size=1024M

auto_increment_offset=2
auto_increment_increment=2

replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
replicate-ignore-db=test

innodb_buffer_pool_size=4G
max_connections=3000

explicit_defaults_for_timestamp=true

[client]
socket=/www/mysql/mysql.sock
# systemctl start mysqld

從庫連接主庫(Slave_IO_Running Slave_SQL_Running的值均為yes即為主從連接成功)

mysql> CHANGE MASTER TO MASTER_HOST='192.168.17.37', MASTER_USER='repl',MASTER_PASSWORD='repl',MASTER_LOG_FILE='mysql-bin.000005',MASTER_LOG_POS=1082;

啟動從庫

mysql> start slave;

查看從庫是否鏈接成功

mysql> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.17.37
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000043
          Read_Master_Log_Pos: 1186
               Relay_Log_File: mysql-relay-bin.000012
                Relay_Log_Pos: 1399
        Relay_Master_Log_File: mysql-bin.000043
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: information_schema,performance_schema,test
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1186
              Relay_Log_Space: 1772
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 37
                  Master_UUID: 5aa209a9-7d6d-11ea-9094-fa163e582a04
             Master_Info_File: /www/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

注意:雙主配置完畢后,請先自行測試是否配置成功。MySQL數據庫可采用Prometheus監控主從狀態,這里不再贅述。

6 配置代理轉發

為了保證 MySQL 、Nacos的高可用,使用內網HAProxy(192.168.14.2)做負載均衡。

6.1 MySQL 代理

為了保證MySQL的高可用,后端兩台MySQL機器已做主主,HAProxy 采用主備模式,HAProxy 每 2 秒檢測一次服務是否可用,如果連續檢測 2 次成功,則表示服務可用,如果連續 3 次檢測失敗,則表示服務不可用,自動切換至從庫。

frontend mysql_23306
  bind *:23306
  mode tcp
  timeout client 3600000
  default_backend mysql_23306_servers

backend mysql_23306_servers
  mode tcp
  timeout server 3600000
  server mysql01 192.168.17.37:3306 check inter 2000 rise 2 fall 3
  server mysql02 192.168.17.38:3306 check inter 2000 rise 2 fall 3 backup

注意:此處配置MySQL代理要在Nacos服務起來之前配置,Nacos 配置中使用的是代理后的數據庫地址。

6.2 Nacos 負載均衡

Nacos 為 3 節點,為了保證 3 節點的高可用及負載均衡,采用 3 台 nacos 輪詢模式。

acl is_nacos hdr_beg(host) -i nacos.com

use_backend www_nacos if is_nacos

backend www_nacos
  mode http
  server web01 192.168.17.37:8848 check inter 2000 rise 2 fall 3
  server web02 192.168.17.38:8848 check inter 2000 rise 2 fall 3
  server web03 192.168.17.39:8848 check inter 2000 rise 2 fall 3

7. 獲取 Nacos 安裝包並安裝部署

Nacos包下載地址:https://github.com/alibaba/nacos/releases

7.1 獲取 Nacos 安裝包

可以通過兩種方式來獲取 Nacos:

  • 從 Github 上下載源碼方式
unzip nacos-source.zip
cd nacos/
mvn -Prelease-nacos clean install -U
cd nacos/distribution/target/nacos-server-1.2.1/nacos/bin
  • 下載編譯后壓縮包方式
  unzip nacos-server-1.2.1.tar.gz 或者 tar -xvf nacos-server-1.2.1.tar.gz
  cd nacos/bin

我們使用第二種方式安裝,把下載好的nacos-server-1.2.1.tar.gz放置於/usr/local目錄下,並解壓縮。

7.3 配置集群配置文件

在nacos的解壓目錄nacos/的conf目錄下,有配置文件cluster.conf,請每行配置成ip:port。(請配置3個或3個以上節點)

# cp conf/cluster.conf.example conf/cluster.conf

將各個節點的IP配置於conf/cluster.conf配置文件中

192.168.17.37:8848
192.168.17.38:8848
192.168.17.39:8848

7.4 配置 MySQL 數據庫

注意:生產使用建議至少主備模式,或者采用高可用數據庫。

7.4.1 初始化 MySQL 數據庫

sql語句源文件

mysql> CREATE DATABASE `nacos_config`;
mysql> USE `nacos_config`;
mysql> source nacos-mysql.sql;

7.4.2 application.properties 配置

application.properties配置文件

#*************** Config Module Related Configurations ***************#
# 配置數據庫連接地址
### If user MySQL as datasource:
spring.datasource.platform=mysql

### Count of DB:
db.num=1

### Connect URL of DB:
db.url.0=jdbc:mysql://192.168.14.2:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=test


#*************** CMDB Module Related Configurations ***************#
### The interval to dump external CMDB in seconds:
nacos.cmdb.dumpTaskInterval=3600

### The interval of polling data change event in seconds:
nacos.cmdb.eventTaskInterval=10

### The interval of loading labels in seconds:
nacos.cmdb.labelTaskInterval=300

### If turn on data loading task:
nacos.cmdb.loadDataAtStart=false

# 激活權限認證
nacos.core.auth.enabled=true

#*************** Metrics Related Configurations ***************#
### Metrics for prometheus
# 激活Prometheus監控采集Exporter
management.endpoints.web.exposure.include=*

### Metrics for elastic search
#management.metrics.export.elastic.enabled=true
#management.metrics.export.elastic.host=http://localhost:9200

注意:三節點conf/cluster.confconf/application.properties配置一樣即可

7.4.3 啟動服務

# sh nacos/bin/startup.sh

查看端口是否啟動

# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      958/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1192/master         
tcp6       0      0 :::22                   :::*                    LISTEN      958/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1192/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      12155/mysqld        
tcp6       0      0 :::8848                 :::*                    LISTEN      12550/java    

如果8848端口以啟動,則可通過Web界面訪問8848端口,配置域名解析,解析至LB上,即可通過域名訪問例如:http://nacos.com

8 Nacos 服務起停配置

Nacos 的起停服務均在/usr/local/nacos/bin/目錄下,

/usr/local/nacos/bin/
├── logs
│   └── access_log.2020-04-15.log
├── shutdown.cmd
├── shutdown.sh								// 服務停止腳本
├── startup.cmd
├── startup.sh								// 服務啟動腳本
└── work
    └── Tomcat
        └── localhost
            └── nacos

5 directories, 5 files

Nacos服務器動:sh /usr/local/nacos/bin/startup.sh

Nacos服務停止:sh /usr/local/nacos/bin/shutdown.sh

腳本啟方式存在問題,如果服務器關機重啟后,需要手動啟動服務。因此,我們采用 systemd 守護進程的方式啟動 Nacos 服務,配置如下:

[Unit]
Description=nacos-server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service] 
Type=forking
ExecStart=/usr/local/nacos/bin/startup.sh
ExecStop=/usr/local/nacos/bin/shutdown.sh
Restart=always PrivateTmp=true

[Install]
WantedBy=multi-user.target
# 啟動服務
systemctl start nacos.service
# 查看服務狀態
systemctl status nacos.service
# 開啟自啟動
systemctl enable nacos.service

9 Prometheus 監控 Nacos 集群

9.1 配置 Prometheus

在 nacos 的配置文件application.properties中,有一項激活Prometheus的選項management.endpoints.web.exposure.include=*,開啟后通過ip:8848/nacos/actuator/prometheus可以獲取到Exporter監控采集信息。在Prometheus中添加采集配置。

  • scrape_interval:采集間隔時間60s
  • scrape_timeout:采集超時時間60s
  • metrics_path:prometheus 采集路徑
  - job_name: "Configure_Center_Nacos"
    scrape_interval: 60s
    scrape_timeout: 60s
    metrics_path: /nacos/actuator/prometheus
    scheme: http
    static_configs:
      - targets:['192.168.17.37:8848','192.168.17.38:8848','192.168.17.39:8848']

若采集成功,則如下圖所示

9.2 配置 Grafana 中添加 Dashboard

Dashboard地址: https://github.com/nacos-group/nacos-template/blob/master/nacos-grafana.json

可參考官網配置:Nacos 監控手冊

注意:在Grafana 中導入nacos-grafana.json配置時,默認使用的 datasource: proemtheus,如果Grafana 配置的 Data source中Prometheus名稱存在與 nacos-grafana.json 中的datasource大小寫不一致,請將 nacos-grafana.json 中的datasource全部替換。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM