0
votes

I try to start elasticsearch and configure its schema on Vagrantfile. But, when I put schema json file using curl on Vagrantfile, “connection refused” error happens, though elasticsearch has started successfully.

curl -X PUT http://192.168.33.20:9200/test --data-binary @/synced_folder/schema.json

[error message]

0curl: (7) Failed connect to 192.168.33.20:9200; Connection refused

※After vagrant up has finished, this command is successful. concretely...

sudo ssh 192.168.33.20

and

curl -X PUT http://192.168.33.20:9200/test --data-binary @/synced_folder/schema.json

It’s no error and schema is configured successfully.

{"acknowledged":true}

But, I want to configure schema on "vagrant up" process. Why does "connection refused" error happen on Vagrantfile?

[Vagrantfile]

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
 config.vm.box = "bento/centos-7.1"
 config.vm.box_url = "https://atlas.hashicorp.com/bento/boxes/centos-7.1"
 config.vm.network :private_network, ip: "192.168.33.20"
 config.vm.synced_folder "./", "/synced_folder"
 config.vm.provision "shell", path: "./script.sh"
end

[script.sh]

#!/bin/sh

#install java
yum -y install java

#install and start elasticsearch
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
cp /synced_folder/elasticsearch.repo /etc/yum.repos.d
yum -y install elasticsearch
sed -i '/# network.host: 192.168.0.1/a\network.host: 192.168.33.20' /etc/elasticsearch/elasticsearch.yml
cd /usr/share/elasticsearch
#Japanese morphological analysis plugin
bin/plugin install analysis-kuromoji
service elasticsearch start

#configure index 
curl -X PUT http://192.168.33.20:9200/test --data-binary @/synced_folder/schema.json

[/synced_folder/schema.json]

{"mappings":{"comment_data":{"properties":{"comment":{"type":"string","store":"yes","index":"analyzed"},"date":{"type":"date","store":"yes"},"vps":{"type":"float","store":"yes"}}}}}
1
disable firewall if its running (sudo systemctl disable firewalld) and see if it helpsFrederic Henri
Thanks! I firstly think "firewalld" is the cause and tried it, but it wasn't the cause.masataka
Could you please check the telnet 192.168.33.20 9200 response? It sounds like the port is not accessible.Arun Prakash
Thanks! I tried telnet 192.168.33.20 9200. The response console is Trying 192.168.33.20... Connected to 192.168.33.20. Escape character is '^]'. .It looks successful.masataka

1 Answers

4
votes

I could solve this problem. I found elasticsearch actually start a few seconds later after service started. So I add a waiting process to a shell script. Thanks!

while true;
do
    echo "waiting Elasticsearch..."
    curl -X -s GET http://192.168.33.20:9200
    if [ $? -eq "0" ]; then
        echo "Elasticsearch started!"
        break
    fi
    sleep 1s
done