CentOS Stram 9 : NATS server

Installing via Docker
1.Install Docker

dnf install docker

2.Install nats

# docker pull nats:latest

3.To run NATS on Docker:

# docker run -p 4222:4222 -ti nats:latest
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
? docker.io/library/nats:latest
Trying to pull docker.io/library/nats:latest...
Getting image source signatures
Copying blob 28c3b732adf6 done
Copying blob 5e5797f39fa0 done
Copying config 842c053e15 done
Writing manifest to image destination
Storing signatures
[1] 2022/07/29 13:55:11.454076 [INF] Starting nats-server
[1] 2022/07/29 13:55:11.454119 [INF]   Version:  2.8.4
[1] 2022/07/29 13:55:11.454125 [INF]   Git:      [66524ed]
[1] 2022/07/29 13:55:11.454131 [INF]   Cluster:  my_cluster
[1] 2022/07/29 13:55:11.454137 [INF]   Name:     NDPEOZYCJEJ5ECF6RQY5ARFA34WMKRSBUOYMYJPAQKUKAGOJDFMMRT7T
[1] 2022/07/29 13:55:11.454141 [INF]   ID:       NDPEOZYCJEJ5ECF6RQY5ARFA34WMKRSBUOYMYJPAQKUKAGOJDFMMRT7T
[1] 2022/07/29 13:55:11.454149 [INF] Using configuration file: nats-server.conf
[1] 2022/07/29 13:55:11.454604 [INF] Starting http monitor on 0.0.0.0:8222
[1] 2022/07/29 13:55:11.454709 [INF] Listening for client connections on 0.0.0.0:4222
[1] 2022/07/29 13:55:11.454894 [INF] Server is ready
[1] 2022/07/29 13:55:11.454926 [INF] Cluster name is my_cluster
[1] 2022/07/29 13:55:11.454970 [INF] Listening for route connections on 0.0.0.0:6222

4.Usage
By default the NATS server exposes multiple ports:

4222 is for clients.
8222 is an HTTP management port for information reporting.
6222 is a routing port for clustering.
Use -p or -P to customize.
To run a server with the ports exposed on a docker network:
First create the 'docker network' nats
# docker network create nats
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
nats
# docker run --name nats --network nats --rm -p 4222:4222 -p 8222:8222 nats --http_port 8222
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
[1] 2022/07/29 14:24:32.155195 [INF] Starting nats-server
[1] 2022/07/29 14:24:32.155225 [INF]   Version:  2.8.4
[1] 2022/07/29 14:24:32.155228 [INF]   Git:      [66524ed]
[1] 2022/07/29 14:24:32.155230 [INF]   Name:     NDY4CMCUOGZFSEMJYYZIBBPFBRINCDK6XXGYBVFZ5RMC33HWIC5UQZUO
[1] 2022/07/29 14:24:32.155239 [INF]   ID:       NDY4CMCUOGZFSEMJYYZIBBPFBRINCDK6XXGYBVFZ5RMC33HWIC5UQZUO
[1] 2022/07/29 14:24:32.155684 [INF] Starting http monitor on 0.0.0.0:8222
[1] 2022/07/29 14:24:32.155753 [INF] Listening for client connections on 0.0.0.0:4222
[1] 2022/07/29 14:24:32.155943 [INF] Server is ready
# curl http://127.0.0.1:8222/routez
{
  "server_id": "NDY4CMCUOGZFSEMJYYZIBBPFBRINCDK6XXGYBVFZ5RMC33HWIC5UQZUO",
  "now": "2022-07-29T14:32:36.353519474Z",
  "num_routes": 0,
  "routes": []
}#

5.cluster
– nuts

# docker run --name nats --network nats --rm -p 4222:4222 -p 8222:8222 nats --http_port 8222 --cluster_name NATS --cluster nats://0.0.0.0:6222
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
[1] 2022/07/29 14:36:56.289258 [INF] Starting nats-server
[1] 2022/07/29 14:36:56.289291 [INF]   Version:  2.8.4
[1] 2022/07/29 14:36:56.289294 [INF]   Git:      [66524ed]
[1] 2022/07/29 14:36:56.289296 [INF]   Cluster:  NATS
[1] 2022/07/29 14:36:56.289299 [INF]   Name:     NCSG2BBBV52PUQKNFUJVN7U6DPZBKXBMFIXFGH7MYOIOSWTET3L4DAIH
[1] 2022/07/29 14:36:56.289302 [INF]   ID:       NCSG2BBBV52PUQKNFUJVN7U6DPZBKXBMFIXFGH7MYOIOSWTET3L4DAIH
[1] 2022/07/29 14:36:56.289806 [INF] Starting http monitor on 0.0.0.0:8222
[1] 2022/07/29 14:36:56.289891 [INF] Listening for client connections on 0.0.0.0:4222
[1] 2022/07/29 14:36:56.290062 [INF] Server is ready
[1] 2022/07/29 14:36:56.290087 [INF] Cluster name is NATS
[1] 2022/07/29 14:36:56.290114 [INF] Listening for route connections on 0.0.0.0:6222

– nuts1

# docker run --name nats-1 --network nats --rm nats --cluster_name NATS --cluster nats://0.0.0.0:6222 --routes=nats://ruser:T0pS3cr3t@nats:6222
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
[1] 2022/07/29 14:38:02.762872 [INF] Starting nats-server
[1] 2022/07/29 14:38:02.762930 [INF]   Version:  2.8.4
[1] 2022/07/29 14:38:02.762937 [INF]   Git:      [66524ed]
[1] 2022/07/29 14:38:02.762940 [INF]   Cluster:  NATS
[1] 2022/07/29 14:38:02.762943 [INF]   Name:     NBSC2XWENOM2ADIS5SDPM6XUB3OCCMTSJBVSBIULTFD5AADM7H6OQP4X
[1] 2022/07/29 14:38:02.762946 [INF]   ID:       NBSC2XWENOM2ADIS5SDPM6XUB3OCCMTSJBVSBIULTFD5AADM7H6OQP4X
[1] 2022/07/29 14:38:02.763500 [INF] Listening for client connections on 0.0.0.0:4222
[1] 2022/07/29 14:38:02.763700 [INF] Server is ready
[1] 2022/07/29 14:38:02.763726 [INF] Cluster name is NATS
[1] 2022/07/29 14:38:02.763783 [INF] Listening for route connections on 0.0.0.0:6222
[1] 2022/07/29 14:38:02.764744 [INF] 10.89.0.3:6222 - rid:4 - Route connection created

– nuts側にmessageが出る

[1] 2022/07/29 14:38:02.764761 [INF] 10.89.0.4:60114 - rid:4 - Route connection created

– nuts2

# docker run --name nats-2 --network nats --rm nats --cluster_name NATS --cluster nats://0.0.0.0:6222 --routes=nats://ruser:T0pS3cr3t@nats:6222
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
[1] 2022/07/29 14:38:52.063476 [INF] Starting nats-server
[1] 2022/07/29 14:38:52.063507 [INF]   Version:  2.8.4
[1] 2022/07/29 14:38:52.063511 [INF]   Git:      [66524ed]
[1] 2022/07/29 14:38:52.063514 [INF]   Cluster:  NATS
[1] 2022/07/29 14:38:52.063517 [INF]   Name:     NAYY23JRPVU4DLYGZF3Z25QISK3A7WWQAV5JO7TJ3OOWTBXOHQX4TEXH
[1] 2022/07/29 14:38:52.063519 [INF]   ID:       NAYY23JRPVU4DLYGZF3Z25QISK3A7WWQAV5JO7TJ3OOWTBXOHQX4TEXH
[1] 2022/07/29 14:38:52.064159 [INF] Listening for client connections on 0.0.0.0:4222
[1] 2022/07/29 14:38:52.064365 [INF] Server is ready
[1] 2022/07/29 14:38:52.064399 [INF] Cluster name is NATS
[1] 2022/07/29 14:38:52.064514 [INF] Listening for route connections on 0.0.0.0:6222
[1] 2022/07/29 14:38:52.065462 [INF] 10.89.0.3:6222 - rid:4 - Route connection created
[1] 2022/07/29 14:38:52.066121 [INF] 10.89.0.4:35700 - rid:5 - Route connection created

– nuts側にmessageが出る

[1] 2022/07/29 14:38:52.065382 [INF] 10.89.0.5:41262 - rid:5 - Route connection created

6.To verify the routes are connected, you can make a request to the monitoring endpoint on /routez as follows and confirm that there are now 2 routes:

#curl http://127.0.0.1:8222/routezz
{
  "server_id": "NCSG2BBBV52PUQKNFUJVN7U6DPZBKXBMFIXFGH7MYOIOSWTET3L4DAIH",
  "now": "2022-07-29T14:40:58.464745232Z",
  "num_routes": 2,
  "routes": [
    {
      "rid": 4,
      "remote_id": "NBSC2XWENOM2ADIS5SDPM6XUB3OCCMTSJBVSBIULTFD5AADM7H6OQP4X",
      "did_solicit": false,
      "is_configured": false,
      "ip": "10.89.0.4",
      "port": 60114,
      "start": "2022-07-29T14:38:02.764689961Z",
      "last_activity": "2022-07-29T14:40:58.046908427Z",
      "rtt": "175μs",
      "uptime": "2m55s",
      "idle": "0s",
      "pending_size": 0,
      "in_msgs": 12,
      "out_msgs": 9,
      "in_bytes": 8855,
      "out_bytes": 6626,
      "subscriptions": 35
    },
    {
      "rid": 5,
      "remote_id": "NAYY23JRPVU4DLYGZF3Z25QISK3A7WWQAV5JO7TJ3OOWTBXOHQX4TEXH",
      "did_solicit": false,
      "is_configured": false,
      "ip": "10.89.0.5",
      "port": 41262,
      "start": "2022-07-29T14:38:52.065350756Z",
      "last_activity": "2022-07-29T14:40:58.046908427Z",
      "rtt": "209μs",
      "uptime": "2m6s",
      "idle": "0s",
      "pending_size": 0,
      "in_msgs": 11,
      "out_msgs": 6,
      "in_bytes": 8835,
      "out_bytes": 5064,
      "subscriptions": 35
    }
  ]
}#

7.Testing the Clusters

#docker run --network nats --rm -it synadia/nats-boxx
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
? docker.io/synadia/nats-box:latest
Trying to pull docker.io/synadia/nats-box:latest...
Getting image source signatures
Copying blob a3ed95caeb02 done
Copying blob 8591fa42d73d done
Copying blob ba3557a56b15 done
Copying blob 417204a428f2 done
Copying blob 248f99e65485 done
Copying blob 6820ef7b0234 done
Copying blob 3b3563957223 done
Copying config ae9af06d3d done
Writing manifest to image destination
Storing signatures
             _             _
 _ __   __ _| |_ ___      | |__   _____  __
| '_ \ / _` | __/ __|_____| '_ \ / _ \ \/ /
| | | | (_| | |_\__ \_____| |_) | (_) >  <
|_| |_|\__,_|\__|___/     |_.__/ \___/_/\_\

nats-box v0.5.0
73a03d0f6599:~#
73a03d0f6599:~# nats sub -s nats://nats:4222 hello &
73a03d0f6599:~# nats pub -s "nats://nats-1:4222" hello first
14:46:39 Subscribing on hello
14:46:39 Published 5 bytes to "hello"
[#1] Received on "hello"
first

73a03d0f6599:~# nats pub -s "nats://nats-2:4222" hello second
14:47:04 Published 6 bytes to "hello"
[#2] Received on "hello"
second

73a03d0f6599:~#

8.Test the NATS server to verify it is running.

# telnet localhost 4222
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
INFO {"server_id":"NCSG2BBBV52PUQKNFUJVN7U6DPZBKXBMFIXFGH7MYOIOSWTET3L4DAIH","server_name":"NCSG2BBBV52PUQKNFUJVN7U6DPZBKXBMFIXFGH7MYOIOSWTET3L4DAIH","version":"2.8.4","proto":1,"git_commit":"66524ed","go":"go1.17.10","host":"0.0.0.0","port":4222,"headers":true,"max_payload":1048576,"client_id":7,"client_ip":"10.89.0.1","cluster":"NATS","connect_urls":["10.89.0.3:4222","10.89.0.5:4222","10.89.0.4:4222"]}