question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't connect to nats streaming server in Docker cluster

See original GitHub issue

It seems I get a connection time out every time I try to connect to a cluster on the docker container. I have no problem connecting with the Nats Client. The streaming client works until I turn on clustering.

Here’s how I fire up the cluster.

docker network create --driver bridge nats-cluster
docker run --network=nats-cluster -it --name a -p 0.0.0.0:4222:4222 -p 0.0.0.0:4248:4248 -v C:/data:/data nats-streaming -cid mycluster -st FILE -dir /data/a -cluster_log_path /data/mycluster --clustered --cluster_node_id a --cluster_peers b --cluster nats://0.0.0.0:4248 -routes nats-route://b:5248 -SDV

Here’s how I try to connect

var factory = new StanConnectionFactory();
var opts = StanOptions.GetDefaultOptions();
opts.NatsURL = "nats://localhost:4222";
var connection = factory.CreateConnection("mycluster", "mytestapp", opts);

Here’s the exception

STAN.Client.StanConnectRequestTimeoutException: ‘Connection Request Timed out.’

The non streaming client works fine. No errors doing this:

var fac = new ConnectionFactory();
var opts = ConnectionFactory.GetDefaultOptions();
opts.Url = "nats://localhost:4222";
fac.CreateConnection(opts);

navigating to localhost:8222 still brings up the monitor, I just can’t make a connection using the nats streaming client.

Am I doing something wrong?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
bennyr103commented, Mar 5, 2019

Oops, turns out the cluster log path can’t be the same for all three containers when they’re running locally! I guess that’s why those commands were working fine from a windows command prompt but not from the docker containers.

The streaming client connects fine now that the cluster is properly configured.

If anyone sees this and just wants a quick simple local setup using docker containers here are the commands

docker network create --driver bridge nats-cluster

docker run --network nats-cluster --name nats-streaming-a -p 0.0.0.0:4222:4222 --rm -v C:/ContainerData:/ContainerData nats-streaming -p 4222 -cid nats-cluster --clustered --cluster_node_id nats-streaming-a --cluster_peers nats-streaming-b,nats-streaming-c -st FILE -dir /ContainerData/nats-streaming-a -cluster_log_path /ContainerData/nbs-cluster1 --cluster nats://0.0.0.0:4248 -routes nats://nats-streaming-b:5248,nats://nats-streaming-c:6248

docker run --network nats-cluster --name nats-streaming-b -p 0.0.0.0:5222:5222 --rm -v C:/ContainerData:/ContainerData nats-streaming -p 5222 -cid nats-cluster --clustered --cluster_node_id nats-streaming-b --cluster_peers nats-streaming-a,nats-streaming-c -st FILE -dir /ContainerData/nats-streaming-b -cluster_log_path /ContainerData/nbs-cluster2 --cluster nats://0.0.0.0:5248 -routes nats://nats-streaming-a:4248,nats://nats-streaming-c:6248

docker run --network nats-cluster --name nats-streaming-c -p 0.0.0.0:6222:6222 --rm -v C:/ContainerData:/ContainerData nats-streaming -p 6222 -cid nats-cluster --clustered --cluster_node_id nats-streaming-c --cluster_peers nats-streaming-b,nats-streaming-a -st FILE -dir /ContainerData/nats-streaming-c -cluster_log_path /ContainerData/nbs-cluster3 --cluster nats://0.0.0.0:6248 -routes nats://nats-streaming-b:5248,nats://nats-streaming-a:4248

Streaming client works like this:

var factory = new StanConnectionFactory();
var sopts = StanOptions.GetDefaultOptions();
sopts.NatsURL = "nats://localhost:4222,nats://localhost:5222,nats://localhost:6222";
var connection = factory.CreateConnection("nats-cluster", "mytestapp", sopts);

Thanks for the responses!

0reactions
bennyr103commented, Mar 4, 2019

With the --cluster_raft_logging I’m seeing a lot of these

[1] 2019/03/04 23:34:31.689149 [TRC] STREAM: raft: Vote granted from nats-streaming-a in term 55. Tally: 1
[1] 2019/03/04 23:34:33.690077 [ERR] STREAM: raft: Failed to make RequestVote RPC to {Voter nats-streaming-c nbs-cluster.nats-streaming-c.nbs-cluster}: nats: timeout
[1] 2019/03/04 23:34:33.690353 [ERR] STREAM: raft: Failed to make RequestVote RPC to {Voter nats-streaming-b nbs-cluster.nats-streaming-b.nbs-cluster}: nats: timeout
[1] 2019/03/04 23:34:35.515287 [WRN] STREAM: raft: Election timeout reached, restarting election
[1] 2019/03/04 23:34:35.515354 [INF] STREAM: raft: Node at nbs-cluster.nats-streaming-a.nbs-cluster [Candidate] entering Candidate state in term 56
[1] 2019/03/04 23:34:35.515747 [TRC] STREAM: raft: Votes needed: 2

This probably isn’t a client issue anymore, is it…

Do my Docker run commands look correct?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Server Cannot find NATS from inside Docker Container
The server tries to send messages through NATs. To connect to NATs, it uses the following code: var nc, err = nats.Connect(nats.
Read more >
NATS and Docker - NATS Docs
The NATS server is provided as a Docker image on Docker Hub that you can run using the Docker daemon. The NATS server...
Read more >
NATS and Docker - NATS Docs
The NATS server is provided as a Docker image on Docker Hub that you can run using the Docker daemon. The NATS server...
Read more >
arm32v6/nats-streaming - Docker Image
nats -streaming is a high performance streaming server for the NATS Messaging ... You can however use v0.15.0+ and connect it to existing...
Read more >
High availability with nats-streaming-server (clustering)
I wanted to set up a high available nats-streaming-server cluster, but couldn't find a “quick” guide on how to do it.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found