Skip to content

Planned NATS restarts and rollouts

Use this for any planned restart of a NATS cluster’s servers: a NATS or chart upgrade, a StatefulSet spec change, moving pods to new nodes. It restarts one server at a time and moves its JetStream leaders off first, so replicated (R3) streams stay available throughout and no server rebuilds its streams on start.

It is based on the orbit-prd NATS 2.15.3 rollout, see siphon#383.

[[TOC]]

For most changes a normal rolling update (an ArgoCD sync with updateStrategy: RollingUpdate) is enough.

Use this procedure when the cluster is busy in production, the services that depend on it need close to zero downtime, and you want to control the order and pace of the restarts and check each step before going on to the next.

  • scripts/nats/leader-handoff.sh runs from your laptop with kubectl and python3 (it parses /jsz with jsz_report.py next to it):

    • leaders <pod> lists the replicated streams and consumers that <pod> leads.
    • move <pod> [pause_s] steps them down one at a time, alternating the new leader between the other servers. DRY_RUN=1 prints the list and changes nothing.
    • check reports, for every replicated stream, whether all its replicas are current, judged from the stream leader’s view.

    Set KUBE_CONTEXT to the cluster’s context and NATS_NAMESPACE if it isn’t nats.

  • nats-box (kubectl -n nats exec -it deploy/nats-box -- sh) for the NATS CLI. It has no $SYS access, so nats server report jetstream, nats server list and nats server raft step-down fail with ensure the account used has system privileges. Use the server’s /jsz monitoring endpoint or the exporter metrics below instead.

  1. Announce it in the teams’ channels (see Escalation Path). Name the R1 assets that will go offline (step 3) and their owners.

  2. Find the meta leader. It is restarted last.

    Terminal window
    kubectl -n nats exec nats-0 -c nats -- wget -qO- localhost:8222/jsz |
    python3 -c 'import json,sys; print(json.load(sys.stdin)["meta_cluster"]["leader"])'

    Or in Prometheus: the NATS exporter puts the meta leader in a meta_leader label on every series, e.g. count by (meta_leader) (nats_account_storage_used). Stream and consumer series also carry stream_leader and consumer_leader.

  3. List the R1 streams and KV buckets. Each one is offline while its server restarts, and anything using it stops.

    Terminal window
    kubectl -n nats exec deploy/nats-box -- nats stream report -a

    Raise those that matter to R3 now, if the cluster has room. For a stream:

    Terminal window
    nats stream edit <stream> --replicas=3

    For a KV bucket, use nats kv edit, not nats stream edit on the KV_<bucket> stream, and pass the bucket’s current TTL and history (from nats kv info <bucket>) so they are kept:

    Terminal window
    nats kv info consumer_locks
    nats kv edit consumer_locks --replicas=3 --ttl=1m --history=1

    Then change the owning application’s config too, or a recreated asset comes back as R1. Siphon logs existing NATS stream has a different replica count from the config when the two disagree.

  4. Check that every replicated stream is current before anything is restarted:

    Terminal window
    KUBE_CONTEXT=<context> scripts/nats/leader-handoff.sh check
  5. Check for long-running work that a stream leader election could interrupt. For Siphon, see whether a consistency pass is running (siphon_consistency_* metrics or the consistency source logs).

Only needed when the change is shipped through ArgoCD and the sync must not restart the running pods itself.

updateStrategy and minReadySeconds are among the few StatefulSet fields that can be changed in place, and neither changes the pod template revision.

  1. Switch the StatefulSet to OnDelete. rollingUpdate has to be cleared at the same time:

    Terminal window
    kubectl -n nats patch statefulset nats --type merge \
    -p '{"spec":{"updateStrategy":{"type":"OnDelete","rollingUpdate":null}}}'
  2. Merge the change and sync the application in ArgoCD (check whether auto-sync is on first: see Deploying changes via ArgoCD). The StatefulSet’s updateRevision changes, and no pod restarts.

    Terminal window
    kubectl -n nats get statefulset nats \
    -o jsonpath='{.status.currentRevision} {.status.updateRevision} {.status.updatedReplicas}/{.status.replicas}{"\n"}'

Go through the servers followers first, meta leader last. Restarting the meta leader hands meta leadership to another server; on orbit-prd that took 24 ms.

For each pod:

  1. Move its leaders off. Start with a dry run, then move:

    Terminal window
    export KUBE_CONTEXT=<context>
    DRY_RUN=1 scripts/nats/leader-handoff.sh move nats-1
    scripts/nats/leader-handoff.sh move nats-1 5

    Each step-down makes the new leader catch up and do disk work, so the pause matters on a busy cluster. 5 s worked on orbit-prd for about 130 consumers; start higher if disk utilization climbs, and stop and re-run with a different pause at any time. A leader that moved on its own is skipped.

    Re-run leaders nats-1 afterwards. It should list nothing. If a few remain, re-run move, or step them down by hand:

    Terminal window
    nats consumer cluster step-down <stream> <consumer> --preferred nats-0
    nats stream cluster step-down <stream> --preferred nats-0
  2. Wait for disk utilization to return to baseline on the servers that took over (node disk I/O, or the NATS servers dashboard).

  3. Follow the pod’s logs in a second terminal, before deleting it. kubectl delete waits until the pod is gone, so following them afterwards misses the shutdown:

    Terminal window
    kubectl -n nats logs nats-1 -c nats -f
  4. Delete the pod with a grace period that covers lame duck and the JetStream shutdown:

    Terminal window
    kubectl -n nats delete pod nats-1 --grace-period=900

    A clean stop logs Entering lame duck mode, then JetStream Shutdown a few seconds later (2.5–3.4 s on orbit-prd), then Server Exiting (about 25 s). The pod being killed before Server Exiting, or the new pod logging prior state file and rebuilding streams, means the shutdown was not clean.

  5. Wait for the new pod to be Ready. With a clean shutdown it took about 5 s on orbit-prd, with no stream rebuild. Check the image or revision is the new one:

    Terminal window
    kubectl -n nats get pod nats-1 -o jsonpath='{.spec.containers[?(@.name=="nats")].image}{"\n"}'
  6. Check every replicated stream is current again before the next pod:

    Terminal window
    scripts/nats/leader-handoff.sh check

    Run this rather than reading one server’s /jsz: a follower reports the other followers as current: false, so only the stream leader’s view is reliable.

  1. When updatedReplicas equals replicas, switch the StatefulSet back so the next sync rolls normally:

    Terminal window
    kubectl -n nats patch statefulset nats --type merge \
    -p '{"spec":{"updateStrategy":{"type":"RollingUpdate","rollingUpdate":{"partition":0}}}}'

    Under OnDelete, currentRevision stays at the old revision until this is done.

  2. Confirm the ArgoCD application is Synced and Healthy.

  3. Leadership is left spread across the servers; no rebalancing is needed.

  4. Post the results (shutdown timings, any rebuilds) on the change issue.