Skip to content

Instantly share code, notes, and snippets.

@adutra
Last active January 22, 2016 17:17
Show Gist options
  • Save adutra/47da50d6f3ddabde6d3e to your computer and use it in GitHub Desktop.
Save adutra/47da50d6f3ddabde6d3e to your computer and use it in GitHub Desktop.
Connecting to a cluster with SSH port forwardings
# Before running this demo, make sure that "Remote Login" is enabled on your macbook!
sudo ifconfig lo0 alias 127.0.0.2 up
sudo ifconfig lo0 alias 127.0.0.3 up
# Simulate a "remote" cluster with 3 nodes
ccm create -v 3.0.0 -n 3 ssh_demo
ccm start
# Establish port forwardings to each node:
# node 1 -> localhost:19042
# node 2 -> localhost:29042
# node 3 -> localhost:39042
node1="127.0.0.1"
node2="127.0.0.2"
node3="127.0.0.3"
# Caution: these tunnels will be automatically closed if idle for more than 5 minutes!
ssh -f -L 19042:localhost:9042 $node1 sleep 300
ssh -f -L 29042:localhost:9042 $node2 sleep 300
ssh -f -L 39042:localhost:9042 $node3 sleep 300
# Check that the ports are open:
netstat -atp tcp | egrep "(19042|29042|39042)"
# Now, test the driver!
# Only when you are done testing, close all SSH tunnels:
killall ssh
import com.datastax.driver.core.policies.AddressTranslator;
import java.net.InetSocketAddress;
import java.util.Map;
public class SSHPortForwardingAddressTranslator implements AddressTranslator {
private final Map<InetSocketAddress, InetSocketAddress> portForwardings;
public SSHPortForwardingAddressTranslator(Map<InetSocketAddress, InetSocketAddress> portForwardings) {
this.portForwardings = portForwardings;
}
@Override
public InetSocketAddress translate(InetSocketAddress remote) {
InetSocketAddress local = portForwardings.get(remote);
if (local == null) {
System.err.println("Cannot find local port forwarding for node "
+ remote + ". The driver will not be able to connect to this node.");
return remote;
}
return local;
}
@Override
public void init(Cluster cluster) {
}
@Override
public void close() {
}
}
import com.google.common.collect.ImmutableMap;
import java.net.InetSocketAddress;
import java.util.Map;
public class SSHPortForwardingDemo {
// These should reflect the ports used when establishing port forwardings with SSH
static final Map<InetSocketAddress, InetSocketAddress> PORT_FORWARDINGS = ImmutableMap.of(
// distant // local
new InetSocketAddress("127.0.0.1", 9042), new InetSocketAddress("localhost", 19042),
new InetSocketAddress("127.0.0.2", 9042), new InetSocketAddress("localhost", 29042),
new InetSocketAddress("127.0.0.3", 9042), new InetSocketAddress("localhost", 39042)
);
public static void main(String[] args) {
Cluster cluster = null;
try {
cluster = Cluster.builder()
// cluster will talk to local addresses only
.addContactPointsWithPorts(PORT_FORWARDINGS.values())
// use a translator to map distant addresses to local ones
.withAddressTranslator(new SSHPortForwardingAddressTranslator(PORT_FORWARDINGS))
.build();
cluster.init();
// prints all hosts with their respective ports
System.out.println("Successfully connected!");
System.out.println("Nodes in this cluster (as they appear to the driver):");
System.out.println(cluster.getMetadata().getAllHosts());
} finally {
if (cluster != null)
cluster.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment