virtualMachinenats

Create a NATS cluster

This section describes the creation of a NATS cluster with servers located on different hosts. This way, messages published on one server will be routed and received by a subscriber on another server. The following example shows a cluster comprised of two instances, follow these instructions:

  • Launch as much NATS instances as nodes you want to have in the cluster. (In this example, two instances). Note the IP addresses of both instances.

Now, it is time to configure, on the first instance, the IP address it needs to listen on for connections from the second instance, and vice versa:

  • In the /opt/bitnami/nats/conf/nats-server.conf file, navigate to the “Clustering multiple servers together” section and modify those lines as shown below.

    You must set a password (PASSWORD_SERVER1) to authorize route connections. In the example below, remember to replace PASSWORD_SERVER2 with the password you will define in the /opt/bitnami/nats/conf/nats-server.conf file of the second instance, and IP_ADDRESS_SERVER2 with the IP address of that instance.

      # Clustering multiple servers together. Disabled by default.
      cluster {
    
      listen: 0.0.0.0:6222 # host/port for inbound route connections
    
      # Authorization for route connections
      authorization {
          user: nats
          pass: PASSWORD_SERVER1
          timeout: 2
      }
    
      # Routes are actively solicited and connected to from this server.
      # Other servers can connect to us if they supply the correct credentials
      # in their routes definitions from above
      routes = [nats://nats:PASSWORD_SERVER2@IP_ADDRESS_SERVER2:6222]
      }
    

Configure the second NATS instance

  • On the second instance, edit the /opt/bitnami/nats/conf/nats-server.conf file and set a password (PASSWORD_SERVER2) to authorize route connections.

    In this case replace the PASSWORD_SERVER1 with the password you have defined in the /opt/bitnami/nats/conf/nats-server.conf file of the first server, and IP_ADDRESS_SERVER1 with the IP address of that instance. That way, the communication between instances will be bi-directional:

      # Clustering multiple servers together. Disabled by default.
      cluster {
    
      listen: 0.0.0.0:6222 # host/port for inbound route connections
    
      # Authorization for route connections
      authorization {
          user: nats
          pass: PASSWORD_SERVER2
          timeout: 2
      }
    
      # Routes are actively solicited and connected to from this server.
      # Other servers can connect to us if they supply the correct credentials
      # in their routes definitions from above
      routes = [nat://nats:PASSWORD_SERVER1@IP_ADDRESS_SERVER1:6222]
    
  • To check if the cluster is working, use the client on the second instance to listen the message as shown below. Replace PASSWORD with the password displayed in the “Authorization for client connections” line of the second instance’s /opt/bitnami/nats/conf/nats-server.conf file.

      $ nats-echo -s nats://nats:PASSWORD@127.0.0.1:4222 SomeSubject
      Connected to NATS server: nats://127.0.0.1:4222
    
  • Use a client on the first instance to send a message. Replace PASSWORD with the default password displayed in the “Authorization for client connections” line of the first instance’s configuration file (/opt/bitnami/nats/conf/nats-server.conf).

    NOTE: Check the Connect to NATS page to learn how to use a Golang client to write a simple NATS client.

      $ nats-pub -s nats://nats:PASSWORD@127.0.0.1:4222 -reply Hi SomeSubject "Hi everyone"
      Connected to NATS server: nats://127.0.0.1:4222
      Published [SomeSubject] : 'Hi everyone'
    
  • On the second instance, you should see a confirmation that the subscriber is receiving the messages:

      ...
      Listening on [SomeSubject]
      Received message 'Hi everyone'
    

Congratulations, you have just scaled your NATS solution!

Last modification November 18, 2021