NATS is a high-performance open-source messaging system widely used in various fields such as microservices, IoT, and cloud-native applications. This blog provides a detailed guide on installing and running NATS using Docker-Compose and Helm. With this guide, you can easily set up NATS, from local development environments to Kubernetes clusters.
Prerequisites :
- Docker and Docker-Compose installed
- Access to a Kubernetes cluster
- Helm installed
Contents :
- Installing with Docker Compose:
- Set up NATS locally using Docker.
- Installing on Kubernetes:
- Deploy NATS using Helm.
* Installing Locally with Docker Compose
* In the example, the local path for storing data files is set to /data/nats. The configuration file nats.conf is created externally and passed to the container.
user:~$ sudo mkdir -p /data/nats
user:~$ cd /data/nats
user:/data/nats$ vi nats.conf
### NATS Clients Port ###############################################
port: 4222
# PID file shared with configuration reloader.
# pid_file: "/var/run/nats/nats.pid"
### Monitoring #####################################################
http: 8222
server_name: nats-0
server_tags: [
"mem:4Gi",
]
### NATS JetStream ##################################################
jetstream {
max_mem:2G
store_dir: "/data"
max_file:10Gi
unique_tag: "natsuniquetag"
}
### NATS Full Mesh Clustering Setup #################################
# cluster {
# name: natscluster
# port: 6222
# routes = [
# nats://nats-0.nats.nats.svc.cluster.local:6222
# nats://nats-1.nats.nats.svc.cluster.local:6222
# nats://nats-2.nats.nats.svc.cluster.local:6222
# ]
# cluster_advertise: $CLUSTER_ADVERTISE
# connect_retries: 120
# }
lame_duck_grace_period: 10s
lame_duck_duration: 30s
docker-compose.yml
services:
# define nats container
nats:
image: nats:alpine
restart: unless-stopped
container_name: nats
ports:
- "4222:4222" # client port
- "8222:8222" # monitoring port
volumes:
- /data/nats/nats.conf:/etc/nats/nats.conf
- /data/nats:/data
command: -c /etc/nats/nats.conf
Commands to Start and Stop the Container
# start container
user:~$ docker compose up nats -d
# stop container
user:~$ docker container stop
# remove container
user:~$ docker container rm
* Installing on Kubernetes with Helm
Using Helm simplifies the installation and management of NATS in a Kubernetes environment.
Add the NATS Helm repository and update:
user:~$ helm repo add nats https://nats-io.github.io/k8s/helm/charts/
user:~$ helm repo update
Check configuration options:
user:~$ helm show values nats/nats > nats-values.yaml
* This command saves the configurable options to nats-values.yaml.
* The official documentation for more details : https://artifacthub.io/packages/helm/nats/nats
* The official documentation for more details : https://artifacthub.io/packages/helm/nats/nats
Install NATS with Helm:
user:~$ helm install nats nats/nats --namespace nats --create-namespace \
--set config.jetstream.enabled=true \
--set config.jetstream.fileStore.pvc.size=5Gi \
--set config.jetstream.fileStore.pvc.storageClassName=local-path
* Expose NATS externally by creating a LoadBalancer service
# export port (optional)
user:~$ cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
name: nats
namespace: nats
spec:
type: LoadBalancer
ports:
- port: 4222
targetPort: 4222
protocol: TCP
selector:
app.kubernetes.io/instance: nats
app.kubernetes.io/name: nats
EOF
Verify the installation
# Check if it is installed and running properly.
user:~$ kubectl get all -n nats