Deployment Overview of NATS on Server¶
Prerequisites and Basic Requirements¶
The deployment of NATS requires a Linux environment, specifically Ubuntu, with the following prerequisites:
- Operating System: Ubuntu
- Privileges: Root access is required to manage Docker, systemd services, and file permissions.
- Docker Engine: The Docker service must be installed and running on the host.
- Ports:
4222: Client listener port for NATS.8222: Monitoring HTTP port for NATS status and metrics.80and443: Required for the Nginx proxy and Let's Encrypt certificate management.
File and Directory Structure¶
The application utilizes specific directories for configuration, data storage, and proxy management. The following paths are established on the server:
- NATS Configuration: Located at the path defined by the
nats_conf_filevariable (typically within/etc/nats/or a custom directory). - NATS Data Directory: Located at the path defined by the
nats_data_dirvariable, used for general data storage. - JetStream Storage: If JetStream is enabled, data is stored in the directory defined by
nats_js_store_dir. - Nginx Configuration:
- Main directory:
/root/nginx - User configuration files:
/data/nginx/user_conf.d - Environment variables:
/data/nginx/nginx-certbot.env - SSL Certificates: Stored in the external Docker volume
nginx_secretsmounted at/etc/letsencrypt.
Application Installation Process¶
NATS is deployed as a Docker container managed by a systemd service. The installation process involves the following components:
- Docker Image: The system pulls the NATS image specified by the
nats_imagevariable. - Container Name: The container is identified by the
nats_container_namevariable. - Configuration File: A configuration file is generated containing:
- Client listener on port
4222. - HTTP monitoring on port
8222. - Authorization settings, which can be configured for either token-based authentication or username/password authentication.
- JetStream settings, including memory and file store limits, if enabled.
The systemd unit file is created at /etc/systemd/system/nats.service to manage the lifecycle of the NATS container.
Docker Containers and Their Deployment¶
The NATS service runs within a Docker container with the following specifications:
- Container Execution: The container is started with the
--rmflag, ensuring it is removed upon stop. - Port Mapping:
- Host port
{{ nats_client_port }}maps to container port4222. - Host port
{{ nats_http_port }}maps to container port8222. - Volume Mounts:
- The NATS configuration file is mounted read-only at
/etc/nats/nats-server.conf. - The data directory is mounted at
/data. - Startup Command: The container executes the NATS server with the configuration file:
-c /etc/nats/nats-server.conf.
The systemd service handles the removal of any existing container with the same name before starting a new instance.
Proxy Servers¶
A reverse proxy is deployed using Docker Compose to handle SSL termination and routing for the NATS monitoring interface.
- Proxy Image:
jonasal/nginx-certbot:latest - Deployment Method: Managed via
docker composein the/root/nginxdirectory. - Configuration:
- The proxy uses the
hostnetwork mode. - It mounts the
nginx_secretsvolume for Let's Encrypt certificates. - User-specific configuration files are mounted from
/data/nginx/user_conf.d. - Routing:
- The proxy is configured to forward requests to the NATS monitoring interface at
http://127.0.0.1:8222. - The configuration file for the host is located at
/data/nginx/user_conf.d/{{ prefix }}{{ server_id }}.hostkey.in.conf. - Certificate Management:
- Certbot is integrated to automatically obtain and renew SSL certificates.
- The email address for certificate notifications is set to
[email protected].
Permission Settings¶
File and directory permissions are set to ensure secure operation:
- Nginx Directory:
/root/nginxis owned byrootwith permissions0755. - Docker Compose File:
/root/nginx/compose.ymlis owned byrootwith permissions0644. - NATS Configuration File: Set to permissions
0644. - NATS Directories: Configuration and data directories are created with permissions
0755. - Systemd Unit File:
/etc/systemd/system/nats.serviceis set to permissions0644.
Starting, Stopping, and Updating¶
The NATS service is managed using systemctl commands, while the proxy is managed via Docker Compose.
- Start NATS Service:
- Stop NATS Service:
- Restart NATS Service:
- Enable NATS on Boot:
- Reload Systemd Daemon:
For the Nginx proxy, the following commands are used in the /root/nginx directory:
- Start/Update Proxy:
- Stop Proxy:
The systemd service automatically pulls the latest NATS image if an update is available and restarts the container if the configuration or unit file has changed.