You install NATS on a single node because the README makes it look like a one-binary affair. The application starts publishing, subscribers receive, life is fine. Six months later you turn on JetStream because somebody needs durable messaging, you push the streams to that single node, and the next outage takes the streams with it — no replication, no quorum, no recovery. Then you discover that the cluster has no TLS, that every account uses the same default credentials, and that anyone on the private network can publish to any subject. NATS is a message system that fits on one binary; production NATS is a cluster with consensus, mTLS, and account isolation.
What’s happening
A production NATS 2.x deployment with JetStream is three nodes (Raft-based JetStream cluster), TLS on both client-facing and route-facing listeners, mutual TLS on the routes so a stray node can’t join the cluster, an operator/account/user JWT chain so each tenant has its own subject namespace and limits, and JetStream streams created with replicas: 3 so the data survives a node loss. File-storage mode (store_dir) on a real disk — the default memory mode doesn’t survive a restart and is appropriate only for caching. The system account ($SYS) is for monitoring tools, not the application; the application gets its own account with its own subject prefix. JetStream consumers should be durable by default, with ack_policy: explicit so unacked messages aren’t lost when a worker crashes mid-batch. Properly sized, a three-node JetStream cluster acks publishes within single-digit milliseconds and tolerates one node failure with no data loss.
How to verify
Cluster topology from any node:
nats --server tls://nats01.example.com:4222 --user admin --password '...' \
server list
# Server Cluster Host Version JS Cores Load Conns
# nats01 nats-prod 10.40.0.11 2.10.x yes 4 0.5 12
# nats02 nats-prod 10.40.0.12 2.10.x yes 4 0.4 11
# nats03 nats-prod 10.40.0.13 2.10.x yes 4 0.5 13
JetStream meta-leader and stream health:
nats stream report
# +------------+---------+--------+-----------+---------+-----+
# | Stream | Storage | Replicas | Consumers | Messages| Bytes |
# +------------+---------+--------+-----------+---------+-----+
# | ORDERS | File | 3 | 4 | 124,003 | 87 MiB|
Replicas: 3 is what makes a stream survive a node outage; anything lower is a misconfigured stream and should be fixed before traffic notices.
Consumer pending and ack state:
nats consumer info ORDERS workers
# State:
# Last Delivered: 18324
# Acknowledged: 18320
# Pending: 4
# Redelivered: 1
Persistent growth in Pending is a slow consumer; Redelivered consistently above zero is a consumer that errors and lets messages be retried.
The fix
Save the script as /root/setup-nats-jetstream-node.sh and run it on each of the three Ubuntu 22.04 nodes after editing the per-node variables. It performs the full installer: base hardening (UFW, fail2ban, unattended-upgrades, SSH key-only), sysctl tuning (net.core.somaxconn, fs.file-max, net.ipv4.tcp_max_syn_backlog), the official NATS server 2.10 release pulled from GitHub, NSC operator/account/user JWT bootstrap on the first node, mTLS on the route listener with a shared CA, a hardened nats-server.conf with JetStream file storage on /var/lib/nats and a per-tenant app account, a systemd unit with LimitNOFILE=200000, a listener-wait loop, and on the first node a skeleton ORDERS stream + workers consumer so you can see the topology end-to-end before traffic.
#!/usr/bin/env bash
set -euo pipefail
### Ubuntu 22.04 NATS 2.10 + JetStream cluster installer
### Run on each of 3 nodes after editing the variables below
### No local backups; assumes VM-level snapshots
### `nats stream backup` to object storage is the additional layer
if [[ "$(id -u)" -ne 0 ]]; then
echo "Run as root."
exit 1
fi
# === EDIT PER NODE ===
NODE_NAME="nats01" # nats01 / nats02 / nats03
NODE_IP="10.40.0.11" # this node's private IP
PEER_NAMES=("nats01" "nats02" "nats03")
PEER_IPS=("10.40.0.11" "10.40.0.12" "10.40.0.13")
CLUSTER_NAME="nats-prod"
NATS_VERSION="2.10.20"
IS_BOOTSTRAP="yes" # "yes" only on the first node (NSC keys)
# =====================
echo
echo "========================================"
echo "NATS ${NODE_NAME} (${NODE_IP})"
echo "========================================"
echo
export DEBIAN_FRONTEND=noninteractive
echo "==> Updating base OS"
apt-get update
apt-get -y upgrade
apt-get -y install curl wget gnupg lsb-release ca-certificates \
chrony fail2ban unattended-upgrades lsof tcpdump dnsutils \
iproute2 ufw jq unzip
systemctl enable --now chrony
echo "==> UFW (private network only)"
ufw default deny incoming
ufw default allow outgoing
ufw allow OpenSSH
for ip in "${PEER_IPS[@]}"; do
ufw allow from "${ip}" to any port 4222 proto tcp
ufw allow from "${ip}" to any port 6222 proto tcp
ufw allow from "${ip}" to any port 8222 proto tcp
done
ufw --force enable
echo "==> Kernel/sysctl tuning"
cat > /etc/sysctl.d/99-nats.conf <<'SYSCTL'
net.core.somaxconn = 4096
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
net.ipv4.ip_local_port_range = 10240 65535
fs.file-max = 2097152
vm.swappiness = 10
SYSCTL
sysctl --system
cat > /etc/security/limits.d/99-nats.conf <<'LIMITS'
nats soft nofile 200000
nats hard nofile 200000
LIMITS
echo "==> Hardening SSH"
sed -i 's|^[#[:space:]]*PermitRootLogin.*|PermitRootLogin prohibit-password|' /etc/ssh/sshd_config
sed -i 's|^[#[:space:]]*PasswordAuthentication.*|PasswordAuthentication no|' /etc/ssh/sshd_config
sshd -t && systemctl restart ssh
echo "==> fail2ban + unattended-upgrades"
cat > /etc/fail2ban/jail.d/sshd.local <<'F2B'
[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 4
findtime = 10m
bantime = 1h
F2B
systemctl enable --now fail2ban
dpkg-reconfigure -plow unattended-upgrades
echo "==> nats user + dirs"
id nats >/dev/null 2>&1 || useradd --system --home /var/lib/nats --shell /usr/sbin/nologin nats
mkdir -p /etc/nats /var/lib/nats/jetstream /var/log/nats /etc/nats/tls
chown -R nats:nats /var/lib/nats /var/log/nats /etc/nats
echo "==> Installing NATS server ${NATS_VERSION}"
cd /tmp
wget -q "https://github.com/nats-io/nats-server/releases/download/v${NATS_VERSION}/nats-server-v${NATS_VERSION}-linux-amd64.zip"
unzip -o "nats-server-v${NATS_VERSION}-linux-amd64.zip"
install -m 0755 "nats-server-v${NATS_VERSION}-linux-amd64/nats-server" /usr/local/bin/nats-server
echo "==> Installing nats + nsc CLIs"
wget -q https://github.com/nats-io/natscli/releases/download/v0.1.5/nats-0.1.5-linux-amd64.zip
unzip -o nats-0.1.5-linux-amd64.zip
install -m 0755 nats-0.1.5-linux-amd64/nats /usr/local/bin/nats
wget -q https://github.com/nats-io/nsc/releases/latest/download/nsc-linux-amd64.zip
unzip -o nsc-linux-amd64.zip
install -m 0755 nsc /usr/local/bin/nsc
cd /
echo "==> CA + node cert for mTLS routes"
if [[ ! -f /etc/nats/tls/ca.crt ]]; then
cd /etc/nats/tls
openssl req -x509 -newkey rsa:4096 -nodes -days 3650 \
-subj "/CN=NATS Cluster CA" \
-keyout ca.key -out ca.crt
openssl req -newkey rsa:4096 -nodes \
-subj "/CN=${NODE_NAME}.${CLUSTER_NAME}" \
-keyout node.key -out node.csr
cat > node.ext <<EOF
subjectAltName = DNS:${NODE_NAME}.${CLUSTER_NAME},IP:${NODE_IP},IP:127.0.0.1
extendedKeyUsage = serverAuth,clientAuth
EOF
openssl x509 -req -in node.csr -CA ca.crt -CAkey ca.key -CAcreateserial \
-out node.crt -days 1825 -sha256 -extfile node.ext
chown nats:nats /etc/nats/tls/*
chmod 600 /etc/nats/tls/*.key
cd /
fi
if [[ "${IS_BOOTSTRAP}" == "yes" ]]; then
echo "==> NSC bootstrap (operator/account/user)"
export NSC_HOME=/etc/nats/nsc
mkdir -p "${NSC_HOME}"
nsc init --name nats-ops --dir "${NSC_HOME}" || true
nsc add account -n APP || true
nsc edit account -n APP \
--js-mem-storage 256M \
--js-disk-storage 50G \
--js-streams 32 \
--js-consumer 256 || true
nsc add user -a APP -n app-publisher || true
nsc add user -a APP -n app-worker || true
nsc add user -a APP -n monitor --allow-sub '_INBOX.>' || true
nsc generate config --mem-resolver --config-file /etc/nats/resolver.conf
chown -R nats:nats /etc/nats/nsc /etc/nats/resolver.conf
fi
echo "==> Writing nats-server.conf"
ROUTES_LIST=""
for ip in "${PEER_IPS[@]}"; do
ROUTES_LIST+=" tls://${ip}:6222\n"
done
cat > /etc/nats/nats-server.conf <<EOF
server_name: ${NODE_NAME}
listen: ${NODE_IP}:4222
http: 127.0.0.1:8222
max_payload: 8MB
max_pending: 256MB
write_deadline: "5s"
tls {
cert_file: "/etc/nats/tls/node.crt"
key_file: "/etc/nats/tls/node.key"
ca_file: "/etc/nats/tls/ca.crt"
verify: false
timeout: 5
}
cluster {
name: ${CLUSTER_NAME}
listen: ${NODE_IP}:6222
tls {
cert_file: "/etc/nats/tls/node.crt"
key_file: "/etc/nats/tls/node.key"
ca_file: "/etc/nats/tls/ca.crt"
verify: true
timeout: 5
}
routes = [
$(echo -e "${ROUTES_LIST}")
]
authorization {
timeout: 5
}
}
jetstream {
store_dir: "/var/lib/nats/jetstream"
max_memory_store: 4GB
max_file_store: 200GB
domain: ${CLUSTER_NAME}
}
include /etc/nats/resolver.conf
EOF
chown -R nats:nats /etc/nats/nats-server.conf
echo "==> systemd unit"
cat > /etc/systemd/system/nats.service <<'NATS'
[Unit]
Description=NATS server
After=network-online.target
[Service]
Type=simple
User=nats
Group=nats
ExecStart=/usr/local/bin/nats-server -c /etc/nats/nats-server.conf
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=5
LimitNOFILE=200000
PrivateTmp=true
NoNewPrivileges=true
[Install]
WantedBy=multi-user.target
NATS
systemctl daemon-reload
systemctl enable --now nats
echo "==> Waiting for NATS listener"
for i in {1..30}; do
if ss -lntp | grep -q ":4222\b"; then break; fi
sleep 1
done
if ! ss -lntp | grep -q ":4222\b"; then
echo "NATS failed to listen on 4222"
journalctl -u nats -n 100 --no-pager
exit 1
fi
if [[ "${IS_BOOTSTRAP}" == "yes" ]]; then
echo "==> Sample ORDERS stream + workers consumer"
nats --server "tls://${NODE_IP}:4222" --tlsca /etc/nats/tls/ca.crt \
stream add ORDERS \
--subjects "orders.>" \
--storage file \
--replicas 3 \
--retention limits \
--max-age 168h \
--max-bytes 50GB \
--max-msgs-per-subject 1000000 \
--discard old \
--dupe-window 2m \
--defaults || true
nats --server "tls://${NODE_IP}:4222" --tlsca /etc/nats/tls/ca.crt \
consumer add ORDERS workers \
--pull \
--filter "orders.>" \
--ack explicit \
--max-deliver 5 \
--replay instant \
--deliver all \
--defaults || true
fi
echo
echo "========================================"
echo "DONE"
echo "========================================"
echo
echo "Listeners:"
ss -lntp | grep -E ':(4222|6222|8222)\b' || true
echo
echo "Server list (run from bootstrap node):"
echo "nats --server tls://${NODE_IP}:4222 --tlsca /etc/nats/tls/ca.crt server list"
echo
echo "Stream report:"
echo "nats --server tls://${NODE_IP}:4222 --tlsca /etc/nats/tls/ca.crt stream report"
echo
echo "Repeat on the other 2 nodes with their NODE_NAME / NODE_IP."
echo "Copy /etc/nats/tls/ca.crt + node.{crt,key} to each peer before running."
Make it executable and run on each node — the bootstrap one first, then the other two:
chmod 700 /root/setup-nats-jetstream-node.sh
/root/setup-nats-jetstream-node.sh
After all three nodes report up, run a failover drill: systemctl stop nats on the JetStream meta-leader and watch the cluster re-elect. The streams should keep accepting publishes within a couple of seconds and consumers should keep draining. Schedule nats stream backup ORDERS /var/backups/nats/ORDERS to object storage nightly — JetStream replicas protect against a node dying, not against a nats stream rm ORDERS that replicates.
For NATS HA you don’t have to operate yourself, our Cloud PaaS team runs it under Managed Cloud Ops — start a scoping call.