
NATS is a lightweight, high-performance messaging system designed for distributed and cloud-native applications. It supports publish-subscribe, request-reply, and streaming workloads through JetStream. NATS is widely used for microservices communication, event-driven systems, IoT messaging, and real-time data pipelines.
This article explains how to deploy NATS using Docker Compose, enable JetStream persistence, configure authentication and authorization, and verify client connectivity using the NATS CLI.
Before you begin, you need to:
In this section, you create the required directory structure for NATS and define environment variables in a .env file that Docker Compose loads automatically.
Create the project directory.
Navigate into the project directory.
Create and edit the .env file.
Add the following variables:
Replace:
NATS_CLIENT_PORT with the client connection port.NATS_CLUSTER_PORT with the cluster communication port.Save and close the file.
In this section, you install the NATS CLI, generate secure credentials, and configure NATS with JetStream, authentication, and authorization.
Download the NATS CLI binary.
Move the binary into your system PATH.
Verify the installation.
Generate a password hash for the system or application user.
Enter the password twice and copy the generated hash.
Your output should be similar to the one below:
Repeat this step for each user you want to define.
Create the NATS configuration file.
Add the following configuration:
In the above configuration:
server_name identifies the NATS server in logs, monitoring output, and system events.system_account enables internal system features required for monitoring and JetStream management.accounts defines authentication boundaries for system and application users.authorization enforces subject-level access control, with an admin permission set that grants full publish and subscribe access.Replace the following values before starting the service:
NATS_SERVER_NAME with a descriptive server name.SYSTEM_PASSWORD_HASH with the bcrypt hash generated for the system user.USERNAME with your application user name.USER_PASSWORD_HASH with the bcrypt hash generated for the application user.Save and close the file.
In this section, you create the Docker Compose manifest that deploys NATS with JetStream persistence and monitoring enabled.
Create the Docker Compose manifest.
Add the following content:
Save and close the file.
In this manifest:
4222: Client connections.8222: Monitoring endpoint (localhost only).6222: Cluster communication../data: Persists JetStream messages../config/nats.conf: Injects the server configuration.Start the NATS service.
Verify that the NATS service is running.
Ensure that the output shows the NATS container is healthy and running.
View logs if needed.
For more information on managing a Docker Compose stack, see the How To Use Docker Compose article.
In this section, you verify that the NATS server is reachable and accepting client connections.
Test connectivity using the NATS CLI.
Replace SYS_USER_PASSWORD and SERVER_IP with the system user password and server IP.
Output:
Successful output confirms that the NATS server is reachable and authenticated correctly.
You successfully deployed NATS using Docker Compose with JetStream persistence enabled. You configured secure authentication and authorization, enabled persistent message storage, exposed the required messaging and verified client connectivity using the NATS CLI.
0 Comments
Be the first to comment and share your perspective with the community.