Connecting Erlang nodes when an internal and external IP address are at play

后端 未结 2 2060

I have two virtual machines that use internal IP addresses to speak to one another while the outside world knows about these VMs only via external IP addresses.

I h

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-31 21:27

    Before answering the question, I want to point out, that Distributed Erlang is not secure and you should not use it outside of trusted local area network. (I had to leave that comment after I saw "external ip address", I assume it doesn't mean public). Below is a list of 4 important things, you should be aware of:

    1. Ip address is part of the node name.

    When you start a node, it is good to give it a name like this:

    erl -name myname@192.168.0.1
    

    When you try to connect to that node from other machine, you can use something like this in erlang shell:

    net_kernel:connect('myname@192.168.0.1').
    

    The important part is that node name: 'myname@192.168.0.1' is an atom. It is not "name and ip" - it is one thing. Even, if your second node is on the same machine, you can't use:

    net_kernel:connect('myname@127.0.0.1').
    

    because it is different node name.

    1. Connections are on top of TCP

    This means, that to connect two nodes, only one has to see the other.

    Example: you have:

    • one node on a machine with external ip address (the client) and
    • one node on a machine with internal ip address (the caching node)

    than:

    • you can't connect from client to caching node (because it can't see that ip address)
    • you can connect from caching node to client (because caching node sees the external ip of client) and the connection is bidirectional!

    In the second case, the connections is just as if they were on the same network. You only have to think about who should initialise the connection.

    1. When you connect new node to existing cluster, it tries to connect to all other nodes.

    If you don't want to that - use hidden nodes.

    1. Make sure, that you use the same Erlang cookie everywhere.

    But I think, you have that covered, if you were able connect other nodes.

    The easiest solution is to use external ip addresses everywhere, because Erlang distribution was designed to run on local network. Harder solution involves making sure, that you connect from nodes in local network to nodes with external ip.

提交回复
热议问题