NATS is a connective technology that is built for the ever-increasingly hyper-connected world. It is the one and only technology that allows applications to securely interact across any combination of cloud vendors, on-premise, edge, web, mobile, and other devices. It is being used globally by various companies and can be used to accelerate or replace the traditional messaging system.
Messaging Patterns of NATS:
NATS basically supports 4 major patterns for communicating messages across entities and they include:
Features:
It will take a few minutes for your VM to be deployed. When the deployment is finished, move on to the next section.
Connect to virtual machine
Create an SSH connection with the VM.
bashCopy
ssh azureuser@10.111.12.123
Usage/Deployment Instructions
Step 1: Access NATS in Azure Marketplace and click on get it now button.
Click on Continue then on Create;
Step 2: Now to create a virtual machine, enter or select appropriate values for zone, machine type, resource group and so on as per your choice.
Click on Review + create;
Step 3: Use the browser to access the application at http://<instance ip address> replace <instance ip address> with the actual ip address of the running instance.
Note: You will get the Instance IP Address as shown in the screenshot below:
Step 4: Start the NATS Server;
Sudo su
/srv/nats/bin/gnatsd -c /srv/nats/gnatsd.config
Check the Status;
Systemctl status nats
You can also start NATS as a service;
systemctl start nats
Step 5 — Securing Connections to the NATS Service
Follow the below Steps to secure the Service
Create a directory to hold the certificate temporarily:
Copy this command, creates an RSA certificate with 2048 bits and 10 years of validity. Note that we have used an arbitrary domain name since we won’t enable TLS verification for the gnatsd server in this article.
You should now have the files gnatsd.key and gnatsd.crt in the ~/priv directory. Let’s move those files under our /srv/nats/ directory structure so everything is in one place. Execute the following command:
Now, make /srv/nats/priv accessible to only to the nats user and group:
Now we update /srv/nats/gnatsd.config to contain the certificate and key we just created. Open the configuration file again:
add the following section to tell gnatsd to use your certificate and key:
/srv/nats/gnatsd.config
. . .
tls {
cert_file: “/srv/nats/priv/gnatsd.crt”
key_file: “/srv/nats/priv/gnatsd.key”
timeout: 1
}
Save the file and exit the editor. Then restart the service so it can pick up the changes.
Let’s test that our certificates work. Run this command:
This time, the command outputs this message:
Output
INFO {“server_id”:”npkIPrCE5Kp8O3v1EfV8dz”,”version”:”0.9.4″,”go”:”go1.6.3″,”host”:”127.0.0.1″,”port”:4222,”auth_required”:false,”ssl_required”:true,”tls_required”:true,”tls_verify”:false,”max_payload”:1048576}
-ERR ‘Secure Connection – TLS Required’
The server returned the message -ERR ‘Secure Connection – TLS Required’ which confirms that the new configuration was picked up and a secure connection is required, which nc doesn’t know how to do.
In order to be able to communicate with our NATS service without installing a full blown NATS client, we will be using a tool called catnats. Let’s download it first:
Finally, move catnats.py to the /srv/nats/bin folder and rename it to catnats:
Let’s check that we can communicate with our NATS service using catnats, by sending the same PING message we have sent before:
Copy,You’ll see this output indicating our connection is secure:
Output
INFO {“server_id”:”npkIPrCE5Kp8O3v1EfV8dz”,”version”:”0.9.4″,”go”:”go1.6.3″,”host”:”127.0.0.1″,”port”:4222,”auth_required”:false,”ssl_required”:true,”tls_required”:true,”tls_verify”:false,”max_payload”:1048576}
PONG
Now that we have secured the communication,
sudo systemctl restart nats