nats-io / stan.go

NATS Streaming System
https://nats.io
Apache License 2.0
706 stars 117 forks source link

Cannot connect to Stan server (k8s) from local using port forward (help needed) #349

Closed torniker closed 3 years ago

torniker commented 3 years ago

I cannot connect to Stan server, which is running in remote Kubernetes cluster, from my local machine.

nats   ClusterIP   None         <none>        4222/TCP,6222/TCP,8222/TCP,7777/TCP,7422/TCP,7522/TCP  
stan   ClusterIP   None         <none>        7777/TCP                                                

I run 2 port forward commands:

kubectl port-forward -n nats svc/nats 4222:4222
kubectl port-forward -n nats svc/stan 7777:7777

here is my code:

nc, err := nats.Connect("nats://localhost:4222", nats.Name(clusterID))
if err != nil {
    panic(fmt.Sprintf("could not connect to nats, error: %s\n", err))
}
sc, err := stan.Connect(clusterID, clientID, stan.NatsConn(nc), stan.NatsURL(nc.ConnectedUrl()))
if err != nil {
    panic(fmt.Sprintf("could not connect to stan with clusterID \"%s\" and clientID: \"%s\" here is error: %s\n", clusterID, clientID, err))
}

and I get following error:

panic: could not connect to stan with clusterID "stan" and clientID: "foo" here is error: stan: connect request timeout (possibly wrong cluster ID?)

The cluster ID is correct. When I run the same code (of course nats URL is different) in cluster as a pod everything works fine. But for development purposes I need to connect to stan server from local.

kozlovic commented 3 years ago

So you are running a separate NATS cluster and Streaming cluster? Are you streaming servers connecting to the NATS cluster?

Note: if you are going to not configure the NATS connection, you don't need to have the 2 steps of creating the NATS connection then the STAN one. Remove nats.Connect() and simply do:

sc, err := stan.Connect(clusterID, clientID, stan.NatsURL("nats://localhost:4222"))
if err != nil {
    panic(fmt.Sprintf("could not connect to stan with clusterID \"%s\" and clientID: \"%s\" here is error: %s\n", clusterID, clientID, err))
}

The reason I asked if STAN is connecting to NATS is because if your STAN deployment does not, then the port you need to use is likely 7777, the one you use for STAN. If STAN is connecting to the NATS Server/cluster, then the port to use is indeed the one from NATS, which based on your config seem to be 4222.

torniker commented 3 years ago

Update:

I had installed nats and stan using basic streaming setup

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

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

I've uninstalled and installed it using helm charts and now it works...

kozlovic commented 3 years ago

But you see that those instructions were about "single server", both NATS and STAN. So with that setup you would get 2 independent NATS setups, and your STAN client should have connected to 7777, not 4222.

Anyway, since you get things working now, I am closing this issue.