NATS / NATS Streaming

構築方法

k8sを利用した構築方法

以下の通りに構築されます。

  • A statefulset with a single NATS server (no auth nor TLS)

  • A single NATS Streaming node using file store with persistence

  • A nats headless service to which you can connect

  • A NATS Streaming Server that uses the cluster name stan

**上記で設定されているNATS Streamingの"stan"やNATSの"nats"はClientからアクセスする場合に必要な情報となるので、忘れないでおくこと。

// NATSのInstall
kubectl apply -f https://raw.githubusercontent.com/nats-io/k8s/master/nats-server/single-server-nats.yml

//NATS StreamのInstall
kubectl apply -f https://raw.githubusercontent.com/nats-io/k8s/master/nats-streaming-server/single-server-stan.yml

参考URL Basic NATS and NATS Streaming Setup - NATS Docs

動作確認

動作確認向けに、nats-boxのpodを利用します。。

kubectl run -i --rm --tty nats-box --image=synadia/nats-box --restart=Never

# Send message to NATS
nats-box:~# nats-sub -s nats hello &
Listening on [hello]

nats-box:~# nats-pub -s nats hello world
# Send/Receive message to STAN
nats-box:~# stan-pub -s nats -c stan hello world
Published [hello] : 'world'

nats-box:~# stan-sub -s nats -c stan hello
Connected to nats clusterID: [stan] clientID: [stan-sub]
Listening on [hello], clientID=[stan-sub], qgroup=[] durable=[]
[#1] Received: sequence:1 subject:"hello" data:"world" timestamp:1579544643374163630

上記の"stan"やnatsはNATSサーバ構築時に設定されるものです。

nats-subコマンドは廃止予定らしいので、代わりに、natsコマンドを利用します。 接続するnatsサーバは--serverオプションで利用します。

nats-box:~# nats sub  --server=nats://10.165.8.64:31109  com.sample.nats.*  

DockerのNats-boxのコンテナを利用する場合は以下の通りです。

$ sudo docker run --rm -it synadia/nats-box:latest