libp2p / go-libp2p-pubsub

The PubSub implementation for go-libp2p
https://github.com/libp2p/specs/tree/master/pubsub
Other
321 stars 185 forks source link

pubsub peer discovery when running docker containers #347

Open pocockn opened 4 years ago

pocockn commented 4 years ago

I followed the pubsub example that uses the mdns peer discovery. This all works perfectly when running my go code in different terminal windows.

I'm a bit confused as to if this approach would work when I go to deploy my code within multiple docker containers. For example, if I had one container publishing messages and then say 5 containers subscribed to those messages. I'm assuming mdns isn't appropriate for this, will I have to use some other form of peer discovery?

Any advice would be greatly appreciated or if there's an example that would be great!

yusefnapora commented 4 years ago

Hi, glad you found the example! It may be possible to get mDNS working with docker, but I agree it's not a great fit :)

For a real application you'd probably want to use a different peer discovery mechanism. It's possible to use the kad DHT as a peer discovery service, for example by Provide-ing a fixed value. Then you can call FindProviders to see who's advertising that value and connect to them.

Another option that recently became possible is to use pubsub itself to bootstrap peer discovery using the Peer Exchange on Prune mechanism that was added in v1.1. of the gossipsub protocol. The basic idea is that you'd configure a few bootstrapper peers that join one or more pre-determined topics, whose addresses are bundled into your app.

You can configure the bootstrappers so that they don't form a mesh with other peers (by setting the GossipSubD and related globals to zero). This reduces the bandwidth required to run a bootstrapper, and importantly it will cause them to immediately PRUNE any peer that tries to GRAFT into their mesh. (see https://docs.libp2p.io/concepts/publish-subscribe if the GRAFT / PRUNE terms are new to you).

New peers can connect to the bootstrappers and subscribe to the predetermined topic, which will cause them to try to GRAFT into the mesh of the bootstrapper. The bootstraper will send them a PRUNE message, which includes signed peer records for other known peers, and the gossipsub message router will automatically attempt to connect to the discovered peers if it needs more peers to fill its mesh.

For all that to work, you'll need to target go-libp2p v0.9.1 or later, since peer exchange depends on the signed peer records feature that was recently added.

Unfortunately we haven't got an example of this setup to point you at yet, but I'm planning to add this to a new example project that should be up in the next few weeks. Until then, if you decide to investigate bootstrapping via peer exchange & have questions, feel free to tag me in them!

pocockn commented 4 years ago

@yusefnapora Thank you for the detailed reply, I appreciate the effort! The kad DHT approach sounds interesting, I don't suppose there's an example for that is there? I'm trying to piece together how everything works with libp2p and struggling a bit!

Would it be similar to this approach?

https://github.com/libp2p/go-libp2p-examples/blob/master/chat-with-rendezvous/chat.go

Where it advertises a rendezvous?

I'm thinking would I need to bootstrap a node (my publisher) and then create some peers (subscribers) and the dht just sits in the publisher node