Use Docker Compose to quickly deploy an entire Nacos cluster

Use Docker Compose to quickly deploy an entire Nacos cluster

Nacos is an open source service discovery, configuration management and dynamic DNS service platform. It is highly available, scalable, and easy to use, so it is welcomed by many developers. However, for beginners, the installation and configuration of Nacos may be a little difficult. This article will introduce how to use Docker Compose to quickly deploy an entire Nacos cluster, making it easy for beginners to get started.

1. Install Docker and Docker Compose

Before getting started, Docker and Docker Compose need to be installed. If you have not installed it, you can follow the steps below to install it.

1. Install Docker

On Linux systems, Docker can be installed with the following command:

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

In the Windows system, you can download Docker from the official website and install it.

2. Install Docker Compose

On Linux systems, Docker Compose can be installed with the following command:

sudo curl -L "https://github.com/docker/compose/releases/download/1.28.6/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

In the Windows system, you can download Docker Compose from the official website and install it.

2. Create a Docker Compose file

Next, a Docker Compose file needs to be created to define the entire Nacos cluster. The specific content is as follows:

version: '3'

services:
  nacos1:
    image: nacos/nacos-server:latest
    container_name: nacos1
    ports:
      - "8848:8848"
      - "9555:9555"
    environment:
      - PREFER_HOST_MODE=hostname
      - NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848
    volumes:
      - ./nacos1/logs:/home/nacos/logs
      - ./nacos1/plugins:/home/nacos/plugins
  nacos2:
    image: nacos/nacos-server:latest
    container_name: nacos2
    ports:
      - "8849:8848"
      - "9556:9555"
    environment:
      - PREFER_HOST_MODE=hostname
      - NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848
    volumes:
      - ./nacos2/logs:/home/nacos/logs
      - ./nacos2/plugins:/home/nacos/plugins
  nacos3:
    image: nacos/nacos-server:latest
    container_name: nacos3
    ports:
      - "8850:8848"
      - "9557:9555"
    environment:
      - PREFER_HOST_MODE=hostname
      - NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848
    volumes:
      - ./nacos3/logs:/home/nacos/logs
      - ./nacos3/plugins:/home/nacos/plugins

This file defines three Nacos servers, corresponding to the three containers nacos1, nacos2 and nacos3 respectively. Each container maps two ports, 8848 and 9555, and mounts the logs and plugins directories respectively. Among them, PREFER_HOST_MODE=hostname means to use the container name as the host name, and NACOS_SERVERS=nacos1:8848 nacos2:8848 nacos3:8848 means the mutual access between the three servers.

3. Start the Nacos cluster

After creating the Docker Compose file, you can start the Nacos cluster. Execute the following command in the directory where the file is located:

docker-compose up -d

This command starts all containers defined in the Docker Compose file and runs them in the background. After waiting for a while, the Nacos cluster starts successfully.

4. Verify the Nacos cluster

In order to verify whether the Nacos cluster is successfully deployed, you can visit http://localhost:8848/nacos to enter the Nacos service management interface. All services and instances can be seen on the interface.

Additionally, the status between the three servers can be viewed using the following command:

docker exec -it nacos1 bash -c "curl -X GET 'http://nacos1:8848/nacos/v1/ns/operator/internal/ip'" 
docker exec -it nacos2 bash -c "curl -X GET 'http://nacos2:8848/nacos/v1/ns/operator/internal/ip'" 
docker exec -it nacos3 bash -c "curl -X GET 'http://nacos3:8848/nacos/v1/ns/operator/internal/ip'"

If the returned result is the IP address of the container, it means that the Nacos cluster deployment is successful.

Summarize

Using Docker Compose can quickly deploy the entire Nacos cluster, avoiding cumbersome installation and configuration processes. Through the introduction of this article, I believe readers have mastered how to use Docker Compose to quickly deploy Nacos clusters.

Guess you like

Origin blog.csdn.net/m0_37924754/article/details/131241702