copy-pastable example that uses docker-machine to provision a docker host and run an elasticsearch cluster with a simple test app that prints the number of cluster nodes. Assumes a correct ~/.aws/credentials
file and a security group with open inbound ports 22, 2375 and (optionally) 9300 defined for the VPC you're deploying to.
docker-machine create -d amazonec2 --amazonec2-instance-type t2.medium --amazonec2-vpc-id vpc-**** --amazonec2-security-group estesting estest01
eval $(docker-machine env estest01)
docker network create estesting
# dedicated master
docker run -d --name es01 --network estesting elasticsearch -Dnode.data=false
# data nodes
docker run -d --name es02 --network estesting elasticsearch -Ddiscovery.zen.ping.unicast.hosts=es01 -Dnode.master=false
docker run -d --name es03 --network estesting elasticsearch -Ddiscovery.zen.ping.unicast.hosts=es01 -Dnode.master=false
docker exec es03 curl http://es02:9200/_cluster/health
docker run -it --rm -w /project --network estesting maven bash
pom="<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">
<modelVersion>4.0.0</modelVersion>
<groupId>com.foo</groupId>
<artifactId>es-test</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>es-test</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.4.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation=\"org.apache.maven.plugins.shade.resource.ManifestResourceTransformer\">
<mainClass>com.foo.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>"
printf %s "$pom" > pom.xml
mkdir -p src/main/java/com/foo
app="package com.foo;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import java.net.InetSocketAddress;
public class App
{
public static void main( String[] args )
{
TransportClient client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(System.getProperty(\"esHost\"), 9300)));
ClusterHealthResponse healths = client.admin().cluster().prepareHealth().get();
System.out.println(\"Number of nodes: \" + healths.getNumberOfNodes());
}
}"
printf %s "$app" > src/main/java/com/foo/App.java
mvn package
java -DesHost=es02 -jar target/es-test-1.0-SNAPSHOT.jar
# after exiting the container (ctrl+d)
docker-machine rm estest01
# alternatively you can also test connectivity to the cluster from outside
# the docker estesting network by creating a client only elasticsearch instance
# with host port 9300 forwarded to the same port in the container and running the
# the same java application from somewhere using the public IP of the AWS instance
# that docker machine created for you. Note that you should open port 9300 in such
# a way to limit where connections can come from.
docker run -d --name es04 --network estesting -p 9300:9300 elasticsearch -Ddiscovery.zen.ping.unicast.hosts=es01 -Dnode.master=false -Dnode.data=false