From Zero to K8S to Leafnodes using Helm

First, we need a number of Kubernetes clusters to be setup already. In this case we’ll create a few in Digital Ocean using the doctl tool but you could use any K8S solution available:

  1. brew install doctl
  2. doctl kubernetes cluster create nats-k8s-sfo2 --count 3 --region sfo2
  3. doctl kubernetes cluster create nats-k8s-ams3 --count 3 --region ams3

Next, get your NGS credentials with leafnodes enabled. For this follow these instructions and choose the Developer plan which is free and will allow you to create leafnode connections for a couple of clusters. Once you got the credentials, upload them as a secret to your K8S clusters:

  1. for ctx in do-ams3-nats-k8s-ams3 do-sfo2-nats-k8s-sfo2; do
  2. kubectl --context $ctx create secret generic ngs-creds --from-file $HOME/.nkeys/creds/synadia/NGS/NGS.creds
  3. done

Install Helm3 and add the NATS helm chart repo:

  1. brew install helm
  2. helm repo add nats https://nats-io.github.io/k8s/helm/charts/
  3. helm repo update

Create the config that adds the leafnode connection to NGS:

  1. # nats.yaml
  2. leafnodes:
  3. enabled: true
  4. remotes:
  5. - url: tls://connect.ngs.global:7422
  6. credentials:
  7. secret:
  8. name: ngs-creds
  9. key: NGS.creds
  10. natsbox:
  11. enabled: true

Deploy it to your K8S regions:

  1. for ctx in do-ams3-nats-k8s-ams3 do-sfo2-nats-k8s-sfo2; do
  2. helm --kube-context $ctx install nats nats/nats -f nats.yaml
  3. done

To test the multi-region connectivity by using the nats-box container that got deployed in each cluster:

  1. kubectl --context do-ams3-nats-k8s-ams3 exec -it nats-box -- nats-sub -s nats hello
  2. Listening on [hello]
  3. while true; do
  4. kubectl --context do-sfo2-nats-k8s-sfo2 exec -it nats-box -- nats-pub -s nats hello 'Hello World!'
  5. done

Results from the subscribe session:

  1. [#1] Received on [hello]: 'Hello World!'
  2. [#2] Received on [hello]: 'Hello World!'
  3. [#3] Received on [hello]: 'Hello World!'
  4. [#4] Received on [hello]: 'Hello World!'
  5. [#5] Received on [hello]: 'Hello World!'
  6. [#6] Received on [hello]: 'Hello World!'
  7. [#7] Received on [hello]: 'Hello World!'
  8. [#8] Received on [hello]: 'Hello World!'
  9. [#9] Received on [hello]: 'Hello World!'