<p>I wanted to test a very basic application for NATS-streaming on Kubernetes. To do so, <a href="https://docs.nats.io/nats-on-kubernetes/minimal-setup" rel="nofollow noreferrer">I followed the commands from the official NATS-docs</a>.</p>
<p>It basically comes down to running</p>
<pre class="lang-sh prettyprint-override"><code>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
</code></pre>
<p>in a terminal with access to the cluster (it's a <a href="https://kind.sigs.k8s.io/docs/user/quick-start/" rel="nofollow noreferrer">kind</a>-cluster in my case).</p>
<p>I used <a href="https://github.com/nats-io/stan.go" rel="nofollow noreferrer"><code>stan.go</code></a> as the NATS-streaming-client. Here is the code I tried to connect to the NATS-streaming-server:</p>
<pre class="lang-golang prettyprint-override"><code>package main

import stan &quot;github.com/nats-io/stan.go&quot;

func main() {
    sc, err := stan.Connect(&quot;stan&quot;, &quot;test-client&quot;)

    if err != nil {
        panic(err)
    }
    if err := sc.Publish(&quot;test-subject&quot;, []byte(&quot;This is a test-message!&quot;)); err != nil {
        panic(err)
    }
}
</code></pre>
<p>and this is the error I'm getting:</p>
<pre><code>panic: nats: no servers available for connection

goroutine 1 [running]:
main.main()
    /Users/thilt/tmp/main.go:9 +0x15d
exit status 2
</code></pre>
<p>so I think another name was used for the cluster or something. If I use the provided example with <code>nats-box</code> from the docs.nats-link above, it also doesn't work! Where did I go wrong here?</p>
<p>I will happily provide more information, if needed.</p>

Tim Hilt
<p>There is a <a href="https://github.com/nats-io/stan.go/blob/master/examples/stan-pub/main.go#L82-L93" rel="nofollow noreferrer">great example in stan.go docs</a>:</p>
<pre><code>// Connect to NATS
nc, err := nats.Connect(URL, opts...)
if err != nil {
    log.Fatal(err)
}
defer nc.Close()

sc, err := stan.Connect(clusterID, clientID, stan.NatsConn(nc))
if err != nil {
    log.Fatalf(&quot;Can't connect: %v.\nMake sure a NATS Streaming Server is running at: %s&quot;, err, URL)
}
defer sc.Close()
</code></pre>
<p>Your error happens because by default stan connects to localhost address (<a href="https://github.com/nats-io/stan.go/blob/910e9bca44c8caf96e3e479874a589bf6c36e817/stan.go#L33" rel="nofollow noreferrer">source code</a>):</p>
<pre><code>// DefaultNatsURL is the default URL the client connects to
DefaultNatsURL = &quot;nats://127.0.0.1:4222&quot;
</code></pre>
<p>Notice that povided above example overwrite this default connection.</p>
<p>Stan source code is short and easy to analyze. I really recommend you to try to analyze it and figure out what it does.</p>
<hr />
<p>Now let's put it all together; here is a working example:</p>
<pre><code>package main

import (
    nats &quot;github.com/nats-io/nats.go&quot;
    stan &quot;github.com/nats-io/stan.go&quot;
)

func main() {
    // Create a NATS connection 
    nc, err := nats.Connect(&quot;nats://nats:4222&quot;)
    if err != nil {
        panic(err)
    }

    // Then pass it to the stan.Connect() call.
    sc, err := stan.Connect(&quot;stan&quot;, &quot;me&quot;, stan.NatsConn(nc))
    if err != nil {
        panic(err)
    }
    if err := sc.Publish(&quot;test-subject&quot;, []byte(&quot;This is a test-message!&quot;)); err != nil {
        panic(err)
    }
}
</code></pre>
