Helm chartv2.15.3App 2.15.3

A Helm chart for the NATS.io High Speed Cloud Native Distributed Communications Technology.

About this chart

NATS Server


NATS is a simple, secure and performant communications system for digital systems, services and devices. NATS is part of the Cloud Native Computing Foundation (CNCF). NATS has over 30 client language implementations, and its server can run on-premise, in the cloud, at the edge, and even on a Raspberry Pi. NATS can secure and simplify design and operation of modern distributed systems.

helm repo add nats https://nats-io.github.io/k8s/helm/charts/
helm upgrade --install nats nats/nats

Upgrade Notes

  • Upgrading from 0.x: The values.yaml schema changed significantly from 0.x to 1.x. Read UPGRADING.md for instructions on upgrading a 0.x release to 1.x.

Values

There are a handful of explicitly defined options which are documented with comments in the values.yaml file.

Everything in the NATS Config or Kubernetes Resources can be overridden by merge and patch, which is supported for the following values:

keytypeenabled by default
configNATS Configyes
config.clusterNATS Clusterno
config.cluster.tlsNATS TLSno
config.jetstreamNATS JetStreamno
config.jetstream.fileStore.pvck8s PVCyes, when config.jetstream is enabled
config.nats.tlsNATS TLSno
config.leafnodesNATS LeafNodesno
config.leafnodes.tlsNATS TLSno
config.websocketNATS WebSocketno
config.websocket.tlsNATS TLSno
config.websocket.ingressk8s Ingressno
config.mqttNATS MQTTno
config.mqtt.tlsNATS TLSno
config.gatewayNATS Gatewayno
config.gateway.tlsNATS TLSno
config.resolverNATS Resolverno
config.resolver.pvck8s PVCyes, when config.resolver is enabled
containernats k8s Containeryes
reloaderconfig reloader k8s Containeryes
promExporterprometheus exporter k8s Containerno
promExporter.podMonitorprometheus PodMonitorno
servicek8s Serviceyes
statefulSetk8s StatefulSetyes
podTemplatek8s PodTemplateyes
headlessServicek8s Serviceyes
configMapk8s ConfigMapyes
natsBox.contexts.defaultNATS Contextyes
natsBox.contexts.[name]NATS Contextno
natsBox.containernats-box k8s Containeryes
natsBox.deploymentk8s Deploymentyes
natsBox.podTemplatek8s PodTemplateyes
natsBox.contextsSecretk8s Secretyes
natsBox.contentsSecretk8s Secretyes

Merge

Merging is performed using the Helm merge function. Example - add NATS accounts and container resources:

config:
  merge:
    accounts:
      A:
        users:
        - {user: a, password: a}
      B:
        users:
        - {user: b, password: b}
natsBox:
  contexts:
    a:
      merge: {user: a, password: a}
    b:
      merge: {user: b, password: b}
  defaultContextName: a

Patch

Patching is performed using JSON Patch. Example - add additional route to end of route list:

config:
  cluster:
    enabled: true
    patch:
    - op: add
      path: /routes/-
      value: nats://demo.nats.io:6222

Common Configurations

JetStream Cluster on 3 separate hosts

config:
  cluster:
    enabled: true
    replicas: 3
  jetstream:
    enabled: true
    fileStore:
      pvc:
        size: 10Gi

podTemplate:
  topologySpreadConstraints:
    kubernetes.io/hostname:
      maxSkew: 1
      whenUnsatisfiable: DoNotSchedule

NATS Container Resources

We recommend setting both requests and limits - for both CPU and memory - to the same value for the following reasons:

Deviate from this recommendation only if you fully understand the implications of your settings.

container:
  env:
    # Different from k8s units, suffix must be B, KiB, MiB, GiB, or TiB
    # Should be ~80% of memory limit
    GOMEMLIMIT: 6GiB
  merge:
    # Recommended minimum: at least 2 CPU cores and 8Gi memory for production JetStream clusters
    resources:
      requests:
        cpu: "2"
        memory: 8Gi
      limits:
        cpu: "2"
        memory: 8Gi

Graceful Shutdown and Health Probes

On pod shutdown, a preStop hook puts the NATS Server into lame duck mode. The server waits config.lameDuckGracePeriod before it begins evicting clients, then spreads client evictions over config.lameDuckDuration. podTemplate.terminationGracePeriodSeconds should allow time for both, plus roughly 20s of shutdown overhead.

The nats container's startupProbe, readinessProbe, and livenessProbe are applied whenever config.monitor is enabled (the default), and their defaults follow the recommended NATS health check configuration. scheme: HTTPS is added automatically when config.monitor.tls is enabled. Probe fields set in your values are deep-merged over the chart defaults. Example - extend the lame duck window and relax the liveness probe:

config:
  lameDuckGracePeriod: 10s
  lameDuckDuration: 2m
container:
  livenessProbe:
    periodSeconds: 60
podTemplate:
  # 10s grace period + 2m duration + 20s overhead
  terminationGracePeriodSeconds: 150

Specify Image Version

The container image can now be overridden by specifying either the image tag, an image digest, or a full image name. Examples below illustrate the options:

  • To set the tag:
    container:
      image:
        tag: x.y.z-alpine
    
  • To use an image digest, which overrides the tag:
    container:
      image:
        repository: nats
        digest: sha256:abcdef1234567890...
    
  • To override the registry, repository, tag, and digest all at once, specify a full image name:
    container:
      image:
        fullImageName: custom-reg.io/myimage@sha256:abcdef1234567890...
    

Operator Mode with NATS Resolver

Run nsc generate config --nats-resolver and replace the OPERATOR_JWT, SYS_ACCOUNT_ID, and SYS_ACCOUNT_JWT with your values. Make sure that you do not include the trailing , in the SYS_ACCOUNT_JWT.

config:
  resolver:
    enabled: true
    merge:
      type: full
      interval: 2m
      timeout: 1.9s
  merge:
    operator: OPERATOR_JWT
    system_account: SYS_ACCOUNT_ID
    resolver_preload:
      SYS_ACCOUNT_ID: SYS_ACCOUNT_JWT

Accessing NATS

The chart contains 2 services by default, service and headlessService.

service

The service is intended to be accessed by NATS Clients. It is a ClusterIP service by default, however it can easily be changed to a different service type.

The nats, websocket, leafnodes, and mqtt ports will be exposed through this service by default if they are enabled.

Example: change this service type to a LoadBalancer:

service:
  merge:
    spec:
      type: LoadBalancer

headlessService

The headlessService is used for NATS Servers in the Stateful Set to discover one another. It is primarily intended to be

Deploy nats with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has nats on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartnats is pinned at v2.15.3 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply