Categories
Software development

Using NATS and STAN in Kubernetes

In this article we’re going to:

  1. Install NATS and STAN using Helm into Kubernetes
  2. Follow along in a NATS minimal setup tutorial
    1. Create an administrative pod for NATS and STAN
    2. Confirm NATS sub/pub is working
    3. Confirm STAN sub/pub is working

This installs NATS so that clients can use it without authenticating!

At the time of this writing I’m not aware of a good way to install STAN supporting authentication. For example, like referencing a Config Map to get an account and Secret to get a password. Therefore, so I opted out of requiring client authentication for NATS so that I can do minimal testing.

I will follow-up in Github and in NATS documentation and circle back here to add the missing authentication settings.

# install NATS
helm repo add bitnami https://charts.bitnami.com/bitnami
helm install nats bitnami/nats --set replicaCount=3 \
--set auth.enabled=false \
--set clusterAuth.enabled=true,clusterAuth.user=admin,clusterAuth.password=$password

# install STAN (notice how no creds are NOT needed!)
helm repo add nats https://nats-io.github.io/k8s/helm/charts/
helm install stan nats/stan \
--set store.type=file --set store.file.storageSize=1Gi \
--set stan.nats.url=nats://nats-client.default.svc.cluster.local:4222 \
--set store.cluster.enabled=true \
--set store.cluster.logPath=/data/stan/store/log

Now let’s follow along in the tutorial.

kubectl run -i --rm --tty nats-box --image=synadia/nats-box --restart=Never
nats-sub -s nats-client.default.svc.cluster.local -t test &
nats-pub -s nats-client.default.svc.cluster.local -t test "this is a test"

Which should yield:

2020/05/03 23:20:11 [#1] Received on [test]: 'this is a test'

stan-sub -c stan -s nats-client.default.svc.cluster.local test &
stan-pub -c stan -s nats-client.default.svc.cluster.local test "this is a test"

Which should yield:

[#1] Received: sequence:1 subject:"test" data:"this is a test" timestamp:1588550818187067125