| Engineering | Development | Search | How to deploy ElasticSearch on Rails using Digital Ocean
How to deploy ElasticSearch on Rails using Digital Ocean


This guide is a continuation of How to deploy a Rails app using Digital Ocean and will walk through how to deploy Elastic Search on the same Digital Ocean Droplet as your Rails application.

Install Elastic Search 6.4.2

Update your package index.

$ sudo apt-get update

Download Elastic Search. We've chosen to use 6.4.2 however you can also use other versions.

$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.2.deb

Install Elastic Search 6.4.2

$ sudo dpkg -i elasticsearch-6.4.2.deb

Install Java 8 (OpenJDK 8)

For Elastic Search < 7.0.0, you will need to manually install Java 8 before Elastic Search. Read Guide Here.

Update your package index

$ sudo apt-get update

Install Java Runtime Environment (JRE).

$ sudo apt-get install default-jre

You can optionally install Java Development Kit (JDK) which is needed if you will be compiling Java programs. The JDK contains JRE so it will have a larger file size.

$ sudo apt-get install default-jdk

Configure Elastic Search to start automatically when the system boots up:

$ sudo systemctl daemon-reload
$ sudo systemctl enable elasticsearch.service

Start / Stop / Restart Elastic Search

$ sudo service elasticsearch start
$ sudo service elasticsearch stop
$ sudo service elasticsearch restart

Check that Elastic Search is running. The status you're looking for is Active: active (running) since ...

$ sudo service elasticsearch status

Confirm using a curl command

$  curl -XGET 'http://localhost:9200/_nodes?pretty'
$  curl -XGET ''

If you're using the Chewy gem in your rails application, update your config/chewy.yml to point at localhost:9200.

  host: 'localhost:9200'

After you've updated this file, redeploy.

$ cap production deploy

Create and Populate ES Indices

SSH into the server

$ ssh deploy@ip_address

Navigate to the rails application folder

$ cd ~/my_app_name/current

Run rake task to build your indices. Depending on your needs, you can also run other rake tasks.

$ RAILS_ENV=production bundle exec rake chewy:reset

If the indices are successfully built and populated your logs will look like this

Resetting index_nameIndex
  Imported index_nameIndex::index_name in 1s, stats: 
  Imported Chewy::Stash::Specification::Specification in 1s, stats: index 1
Total: 1s

Check what indices were created

$ curl -X GET \
  http://localhost:9200/_cat/indices \

Check the doc count in a specific index

$ curl -X POST \
  http://localhost:9200/[index_name]/_count \

Additional Security

Setup Ubuntu Firewall (UFW)

For an added layer of security, setup UFW to limit which ports and IPs can access the server.

Check initial firewall status

$ sudo ufw status

Enable firewall

$ sudo ufw enable  

Add application IP

$ sudo ufw allow from ip_address to any port 9200

Recheck Status

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
9200                       ALLOW       my_app_ip

To remove whitelisted IPs first list all rules by rule number and then send delete command for the rule number you'd like to remove. In the below example it is 2.

$ ufw firewall numbered
$ sudo ufw delete 2

To see your servers active connections run

$ netstat -a

Setup Digital Ocean Firewall

For an added layer of security, setup a Digital Ocean Firewall to limit which ports and IPs can access the server.

Open Digital Ocean Control Panel > Networking > Firewalls. Create Firewall with the following settings:


Name: firewall-app_name-prd

Inbound Rules

Custom TCP 9200 ip_address

Outbound Rules

# leave this with the default settings

Apply to Droplets

# select your droplet name

Checking Logs

Navigate to logs folder

sudo su root
cd /var/log/elasticsearch

Check the various log files using the the following commands

tail -f elasticsearch.log
tail -f elasticsearch_access.log
zcat elasticsearch-2019-08-12-1.log.gz

Additional Notes

Elastic Search on Ubuntu 16.04

To get Elasticsearch to run on Ubuntu 16.04 you have to set START_DAEMON to true on /etc/default/elasticsearch. It comes commented out by default, and uncommenting it makes Elasticsearch start again just fine. https://stackoverflow.com/questions/38674711/can-not-start-elasticsearch-as-a-service-in-ubuntu-16-04

If in the future you need to remove Elastic Search, run:

$ sudo apt-get --purge autoremove elasticsearch
$ sudo rm -rf /var/lib/elasticsearch/
$ sudo rm -rf /etc/elasticsearch

Resources Used