The discovery module in Elasticsearch is responsible for finding nodes that belong to a cluster and electing a master node. The Google Compute Engine (GCE) discovery plugin uses GCE APIs to perform automatic unicast discovery. In this post, we'll work through setting up a three node Elasticsearch cluster in Google Cloud using the GCE discovery plugin, and create a custom image that can be shared among different projects.
To simplify our tasks ahead, we will add the default project and region and omit them from rest of the commands. Also, all commands here that start with gcloud are executed from the local machine and not on the GCE instances.
We will begin with creating a firewall rule so that the nodes in our Elasticsearch cluster can communicate with each other. Elasticsearch uses the TCP transport module for internal communications; the default port range for the transport module is between 9300 - 9400. In addition, we will open up port 9200 to expose Elasticsearch APIs over HTTP.
The tags argument includes the Elasticsearch firewall rule along with http and https, which are optional. Once this process is complete, you should see the name of the instance, the zone it is created in, its internal and external IP, and the status of the instance.
The instance is running ubuntu-16.04.
The instance is using --scopes=compute-ro. This enables the GCE discovery module to call required APIs.
If this is the first time you are using gcloud ssh to access a GCE instance, it will create a ssh key for you.
Once Elasticsearch is ready, install the GCE discovery plugin. By default, the elasticsearch-plugin command can be found at /usr/share/elasticsearch/bin for both deb and rpm distributions.
In the near future, we are planning to make the GCE discovery setup even easier by auto-discovering some of these configurations.
We are ready to start Elasticsearch and verify everything is working.
Note, the above curl command can only run within the Google Cloud Platform. By default, nothing is opened to internet. Log off from the GCE instance as our work here is done.
We will use the instance that we built to make a custom image. We can share the image with other projects to create additional GCE instances with Elasticsearch and GCE discovery plugin installed. Although you can use the gcloud compute images export command to create a custom image from a virtual disk, we choose to use the manual method so we can clean up the files on the disk before packaging it.
We will stop the instance sherry-instance-gce-discovery as well. This is an optional step but ensures the integrity of the disk content in the snapshot.
Use the snapshot to build a new GCE disk that we will use to create an image later.
Make a temporary disk that is at least 50% larger than the original image disk size. We will store the raw image of the disk from the instance sherry-instance-gce-discovery and the compressed version that we will be creating on the temporary disk. Since our original disk is 10 GB, our temporary one will be 15 GB.
We will create a temporary GCE instance with the storage-rw scope and attach both the disk from the snapshot we made earlier and the temporary disk.
Delete the Elasticsearch data directory. Be default, it is located at /mnt/image-disk/var/lib/elasticsearch/nodes. Each new instance will create its own Elasticsearch data directory when the Elasticsearch service starts the first time.
Remove SSH key. The authorized_keys file is located in the /mnt/image-disk/home/[USER]/.ssh directory. This step is not required but highly recommended.
Create a disk.raw from the image disk on the temporary disk. Please note, it must be called disk.raw.
Create a tar file of the disk.raw file.
Log off the temporary instance after the file transfer is done.
We are ready to create three new instances from the images we just built.
Once completed, logon to each instance and start Elasticsearch. Please see Elasticsearch installation instructions to configure Elasticsearch to start automatically.
From the instance sherry-instance-1, we can run curl sherry-instance-1:9200/_cat/nodes. We should see all three nodes in our cluster listed in the output.
In a future blog, we will install and configure X-Pack to ensure our cluster is secure and production ready and the Google Cloud Storage repository plugin to provide the snapshot and restore capability. If you prefer the convenience of a managed cluster, Elastic Cloud, Elastic's official hosted solution is available on GCP. It comes with all of the X-Pack features and automatic snapshots.