thingsHub Public Documentation

HA NATS Configuration for Deployment

NATS Repo: https://nats-io.github.io/k8s/helm/charts/

Chart Version: 2.14.x (tested with 2.14.5)

Upgrading from chart 0.x: the 1.x/2.x charts use a different values layout. For example, nats.jetstream.memStorage became config.jetstream.memoryStore, nats.jetstream.fileStorage became config.jetstream.fileStore, and auth.basic.accounts became config.merge.accounts. The chart also deploys NATS as a new StatefulSet, so plan the upgrade as a fresh installation.

Values File:

YAML
config:
  cluster:
    enabled: true
    noAdvertise: true
    replicas: 3            # at least 3 for High Availability

  jetstream:
    enabled: true
    fileStore:
      enabled: true
      pvc:
        enabled: true
        size: 10Gi
        storageClassName: <your-storage-class>
      maxSize: 10Gi        # max_file_store; keep <= pvc.size
    memoryStore:
      enabled: false       # see "Resource requests and limits" before enabling

  monitor:
    enabled: true          # monitoring endpoint on port 8222

  merge:
    accounts:
      A:
        users:
          - user: a
            password: <change-me>
      B:
        jetstream: enabled # required for the account used by the notifier
        users:
          - user: b
            password: <change-me>

natsBox:
  enabled: true
  container:
    resources:
      requests:
        cpu: 10m
        memory: 32Mi
      limits:
        cpu: 200m
        memory: 128Mi

podDisruptionBudget:
  enabled: true
  minAvailable: 1

# Resource requests and limits: see the section below.
container:
  env:
    GOMEMLIMIT: 900MiB
  resources:
    requests:
      cpu: 100m
      memory: 512Mi
    limits:
      cpu: "1"
      memory: 1Gi

reloader:
  merge:
    resources:
      requests:
        cpu: 5m
        memory: 16Mi
      limits:
        cpu: 50m
        memory: 64Mi

promExporter:
  enabled: true            # optional Prometheus exporter
  merge:
    resources:
      requests:
        cpu: 10m
        memory: 32Mi
      limits:
        cpu: 100m
        memory: 64Mi

Please make sure to set up a separate NATS user account for each tenant in the cluster. Using the same NATS account across multiple tenants can lead to message crossover and may cause further inconsistencies. Replace the example passwords with your own and keep them out of version control where possible.

Resource requests and limits

Every container in the NATS deployment should have explicit CPU and memory requests and limits. This keeps NATS from using unbounded node resources and protects it from eviction when the node is under pressure.

Container

Values key

Requests (CPU / memory)

Limits (CPU / memory)

NATS server (nats)

container.resources

100m / 512Mi

1 / 1Gi

Config reloader (reloader)

reloader.merge.resources

5m / 16Mi

50m / 64Mi

Prometheus exporter (prom-exporter)

promExporter.merge.resources

10m / 32Mi

100m / 64Mi

NATS box (nats-box)

natsBox.container.resources

10m / 32Mi

200m / 128Mi

These are recommended starting values for a typical thingsHub installation. In a 3-replica cluster serving several tenants, each NATS server used well under 100Mi of memory and a few millicores of CPU, so these values leave plenty of headroom. Check your own usage with kubectl top pod --containers and adjust.

GOMEMLIMIT

NATS is written in Go. Set GOMEMLIMIT to about 90% of the memory limit of the nats container, so the Go garbage collector frees memory before Kubernetes OOM-kills the container. With a 1Gi limit, use 900MiB. Always change the memory limit and GOMEMLIMIT together.

YAML
container:
  env:
    GOMEMLIMIT: 900MiB

Sizing JetStream

  • File store (config.jetstream.fileStore): stream data is written to the persistent volume. Set maxSize to the PVC size or less. File data does not count against the memory limit, but the operating system's page cache for it shows up in the pod's memory usage, so do not go below a 1Gi memory limit while JetStream is enabled.

  • Memory store (config.jetstream.memoryStore): disabled by default. If you enable it, the memory store must fit inside the container's memory budget:

memoryStore.maxSize + ~300Mi (NATS baseline) < GOMEMLIMIT

With a 1Gi limit (GOMEMLIMIT: 900MiB), that means a maxSize of at most 512Mi. For a larger memory store, raise container.resources.limits.memory and GOMEMLIMIT first.

YAML
config:
  jetstream:
    memoryStore:
      enabled: true
      maxSize: 512Mi       # needs limits.memory >= 1Gi and GOMEMLIMIT 900MiB

Applying changes

Changing resources or GOMEMLIMIT updates the StatefulSet. Kubernetes then restarts the NATS pods one at a time, and the PodDisruptionBudget keeps at least one replica available. Clients reconnect automatically, but expect short reconnects, so apply the change outside peak hours. Afterwards, check the values with:

kubectl describe pod <nats-pod> -n <namespace>
kubectl top pod -n <namespace> --containers

See also Allocating Resources To Services.

Client Service

We additionally use the ClusterIP service so that the load-balancing aspect is working well. We did not find proper documentation to show that the service that the chart already uses works well in that aspect.

apiVersion: v1
kind: Service
metadata:
  name: nats-svc
  namespace: thub
spec:
  type: ClusterIP
  selector:
    app.kubernetes.io/name: nats
    app.kubernetes.io/component: nats   # keeps nats-box out of this Service
  ports:
    - protocol: TCP
      port: 4222
      targetPort: 4222
      name: nats
    - protocol: TCP
      port: 8222
      targetPort: 8222
      name: monitoring

This allows us to use the DNS from Kubernetes to access the cluster with the service URL. We are additionally exposing port 8222 to obtain the monitoring information.