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]]
When to use this
Section titled “When to use this”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.shruns from your laptop withkubectlandpython3(it parses/jszwithjsz_report.pynext 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=1prints the list and changes nothing.checkreports, for every replicated stream, whether all its replicas are current, judged from the stream leader’s view.
Set
KUBE_CONTEXTto the cluster’s context andNATS_NAMESPACEif it isn’tnats. -
nats-box(kubectl -n nats exec -it deploy/nats-box -- sh) for the NATS CLI. It has no$SYSaccess, sonats server report jetstream,nats server listandnats server raft step-downfail with ensure the account used has system privileges. Use the server’s/jszmonitoring endpoint or the exporter metrics below instead.
Before you start
Section titled “Before you start”-
Announce it in the teams’ channels (see Escalation Path). Name the R1 assets that will go offline (step 3) and their owners.
-
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_leaderlabel on every series, e.g.count by (meta_leader) (nats_account_storage_used). Stream and consumer series also carrystream_leaderandconsumer_leader. -
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 -aRaise those that matter to R3 now, if the cluster has room. For a stream:
Terminal window nats stream edit <stream> --replicas=3For a KV bucket, use
nats kv edit, notnats stream editon theKV_<bucket>stream, and pass the bucket’s current TTL and history (fromnats kv info <bucket>) so they are kept:Terminal window nats kv info consumer_locksnats kv edit consumer_locks --replicas=3 --ttl=1m --history=1Then 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 configwhen the two disagree. -
Check that every replicated stream is current before anything is restarted:
Terminal window KUBE_CONTEXT=<context> scripts/nats/leader-handoff.sh check -
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).
Apply the spec without restarting pods
Section titled “Apply the spec without restarting pods”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.
-
Switch the StatefulSet to
OnDelete.rollingUpdatehas to be cleared at the same time:Terminal window kubectl -n nats patch statefulset nats --type merge \-p '{"spec":{"updateStrategy":{"type":"OnDelete","rollingUpdate":null}}}' -
Merge the change and sync the application in ArgoCD (check whether auto-sync is on first: see Deploying changes via ArgoCD). The StatefulSet’s
updateRevisionchanges, and no pod restarts.Terminal window kubectl -n nats get statefulset nats \-o jsonpath='{.status.currentRevision} {.status.updateRevision} {.status.updatedReplicas}/{.status.replicas}{"\n"}'
Restart each server
Section titled “Restart each server”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:
-
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-1scripts/nats/leader-handoff.sh move nats-1 5Each 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-1afterwards. It should list nothing. If a few remain, re-runmove, or step them down by hand:Terminal window nats consumer cluster step-down <stream> <consumer> --preferred nats-0nats stream cluster step-down <stream> --preferred nats-0 -
Wait for disk utilization to return to baseline on the servers that took over (node disk I/O, or the NATS servers dashboard).
-
Follow the pod’s logs in a second terminal, before deleting it.
kubectl deletewaits until the pod is gone, so following them afterwards misses the shutdown:Terminal window kubectl -n nats logs nats-1 -c nats -f -
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=900A clean stop logs
Entering lame duck mode, thenJetStream Shutdowna few seconds later (2.5–3.4 s on orbit-prd), thenServer Exiting(about 25 s). The pod being killed beforeServer Exiting, or the new pod loggingprior state fileand rebuilding streams, means the shutdown was not clean. -
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"}' -
Check every replicated stream is current again before the next pod:
Terminal window scripts/nats/leader-handoff.sh checkRun this rather than reading one server’s
/jsz: a follower reports the other followers ascurrent: false, so only the stream leader’s view is reliable.
Afterwards
Section titled “Afterwards”-
When
updatedReplicasequalsreplicas, 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,currentRevisionstays at the old revision until this is done. -
Confirm the ArgoCD application is
SyncedandHealthy. -
Leadership is left spread across the servers; no rebalancing is needed.
-
Post the results (shutdown timings, any rebuilds) on the change issue.