Cloud Native 13 min read

Nacos Cluster Deployment Guide and Raft Leader Election Mechanism

This article provides a step‑by‑step guide to deploying a Nacos cluster on Linux, covering environment preparation, installation, MySQL configuration, application.properties settings, and cluster.conf setup, startup commands, and explains the Raft‑based leader election and data synchronization mechanisms for reliable microservice registration.

Top Architect
Top Architect
Top Architect
Nacos Cluster Deployment Guide and Raft Leader Election Mechanism

Officially recommended to place all services under a VIP and bind it to a domain, then expose Nacos via http://nacos.com:port/openAPI with an internal SLB to improve readability and IP flexibility.

Nacos cluster design key points:

Microservices access services via domain names, not raw IPs, using DNS for resolution.

Nacos nodes expose ports 8848 (API & data sync) and 7848 (leader election) and require an external MySQL for persistent data.

Each server should use a virtual IP (VIP) bound by DNS to hide physical IPs and provide a unified entry point.

Linux deployment – Step 1: Environment preparation

At least three nodes are required (odd number) for a valid Raft cluster. Install JDK 1.8 and set JAVA_HOME . Deploy a MySQL 5.7/8.0 instance for Nacos data storage.

Step 2: Download and extract Nacos

Download Nacos 2.0.2 from https://github.com/alibaba/nacos/releases/ and upload to /usr/data on each node, then run tar -xvf nacos-server-2.0.2.tar.gz to unpack.

Step 3: Initialize the database

Create a database named nacos_config and execute the script /usr/data/nacos/conf/nacos-mysql.sql to create the required tables (config_*, users, roles, permissions).

Step 4: Configure the datasource

Edit /usr/data/nacos/conf/application.properties (around line 36) to enable MySQL:

spring.datasource.platform=mysql
 db.num=1
 db.url.0=jdbc:mysql://xxx:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
 db.user=root
 db.password=root

Step 5: Set up cluster.conf

Copy the example file and list all node IPs and ports:

cp cluster.conf.example cluster.conf
ip1:8848 ip2:8848 ip3:8848

Place the same cluster.conf on every Nacos server.

Step 6: Start the cluster

Run sh /usr/local/nacos/bin/startup.sh on each node (no -m flag needed for cluster mode). Monitor the startup with tail -f /usr/local/nacos/logs/start.out . Successful start logs show the IP list and a message like “Nacos started successfully in cluster mode”.

Raft leader election in Nacos

Nodes assume three roles: Leader, Candidate, and Follower. Elections occur when a node starts without a leader, when the cluster size changes, or when the current leader fails. A candidate must obtain a majority of votes to become leader; the process repeats until a leader is elected.

Data synchronization process

Follower forwards registration heartbeats to the Leader.

Leader processes the registration and writes the log.

Leader instructs Followers to replicate the log.

Followers acknowledge (ACK) the replication.

Leader returns success to the microservice after receiving majority ACKs.

Microservice integration example

# Application name
spring.application.name=sample-service
# Nacos server addresses
spring.cloud.nacos.discovery.server-addr=ip1:8848,ip2:8848,ip3:8848
# Credentials
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
# Service port
server.port=9000

After starting the microservice, accessing any node’s UI (e.g., http://ip1:8848/nacos/#/serviceManagement ) shows consistent service lists, confirming successful data synchronization across the cluster.

DockerMicroservicesService DiscoveryNacosClusterRaft
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.