Using a Load Balancer for External Access to NATS

    Also, it would be recommended to set no_advertise to in order to avoid gossiping internal addresses from pods in Kubernetes to NATS clients.

    Setting up a NATS Server with external access on Azure

    With the following, you can create a 3-node NATS Server cluster:

    1. kubectl apply -f https://raw.githubusercontent.com/nats-io/k8s/b55687a97a5fd55485e1af302fbdbe43d2d3b968/nats-server/leafnodes/nats-cluster.yaml

    Now let’s expose the NATS Server by creating an L4 load balancer on Azure:

    1. kubectl apply -f https://raw.githubusercontent.com/nats-io/k8s/b55687a97a5fd55485e1af302fbdbe43d2d3b968/nats-server/leafnodes/lb.yaml

    Confirm the public IP that was allocated to the nats-lb service that was created, in this case it is 52.155.49.45:

    1. leaf {
    2. {
    3. url: "nats://foo:bar@52.155.49.45:7422"
    4. }
    5. ]
    6. }

    You can also add a NATS Streaming cluster into the cluster connecting to the port 4222:

    Now if you create two NATS Servers that connect to the same leafnode port, they will be able to receive messages to each other:

    1. nats-server -c leafnodes/leaf.conf -p 4223 &
    2. $ nats-sub -s localhost:4222 foo &
    3. $ nats-pub -s localhost:4223 foo hello
    4. [#1] Received on [foo] : 'hello'