how-to-install-elasticsearch-on-centos6.6-x86_64
写作缘由:测试 grafana过程中需要使用 elasticsearch才能实现dashboard的保存,了解到 elasticsearch对大数据的展示很高效,本文主要记录elasticsearch安装部署过程。
prerequisite
需要先安装java
install elasticsearch
mac-os-x:/Users/Users_datadir_docker/soft/elasticsearch=docker_container:/docker_vol01/soft/elasticsearch
cd /docker_vol01/soft/elasticsearch
curl -L -O https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.4.zip
unzip elasticsearch-1.4.4.zip
mv elasticsearch-1.4.4 /docker_vol01/app1
cd ~/app/
ln -s /docker_vol01/app1/elasticsearch-1.4.4 .
vi ~/.bash_profile
#For java
JAVA_HOME=~/app/java_home
JRE_HOME=$JAVA_HOME/jre
export JAVA_HOME JRE_HOME
PATH=$JAVA_HOME/bin:$PATH
#For elasticsearch
ES_INSTALL=~/app/elasticsearch-1.4.4
export ES_INSTALL
PATH=$ES_INSTALL/bin:$PATH
注意
docker container环境,启动 container时 ~/.bash_profile 不生效,需要在 bin/elasticsearch 设置 JAVA_HOME
test
启动 elasticsearch
bin/elasticsearch
bin/elasticsearch --cluster.name my_cluster_name --node.name my_node_name
http://monitor1:9200
#curl -XGET http://localhost:9200
curl -XGET http://monitor1:9200
configure elasticsearch
Now we need to setup ElasticSearch cluster name. ElasticSearch uses “elasticsearch” as default cluster name, We recommend to change it as per your need.
To change cluster named edit config/elasticsearch.yml file and update following values. Node names are dynamically generated, but to keep a fixed user-friendly name change it also.
cp config/elasticsearch.yml config/elasticsearch.yml.org
vi config/elasticsearch.yml
1) custom cluster.name and node.name
cluster.name: ES_Cluster1
node.name: "monitor1"
2)Elasticsearch & CORS
As of Elasticsearch v1.4 CORS is disabled by default. This needs to be enabled in the Elasticsearch config file, otherwise grafana will not be able to access Elasticsearch.
vi config/elasticsearch.yml
#add
http.cors.enabled: true
http.cors.allow-origin: *
问题1:
[root@monitor1 elasticsearch-1.4.4]# bin/elasticsearch & [1] 1495
[root@monitor1 elasticsearch-1.4.4]# [root@monitor1
elasticsearch-1.4.4]# {1.4.4}: Setup Failed …
- SettingsException[Failed to load settings from [file:/root/app/elasticsearch-1.4.4/config/elasticsearch.yml]]
ScannerException[while scanning an alias; expected alphabetic or
numeric character, but found but found ; in ‘reader’, line 391,
column 26:
*
^] org.elasticsearch.common.settings.SettingsException: Failed to load settings from
[file:/root/app/elasticsearch-1.4.4/config/elasticsearch.yml] at
org.elasticsearch.common.settings.ImmutableSettings Builder.loadFromStream(ImmutableSettings.java:947)atorg.elasticsearch.common.settings.ImmutableSettings Builder.loadFromUrl(ImmutableSettings.java:931)
at
org.elasticsearch.node.internal.InternalSettingsPreparer.prepareSettings(InternalSettingsPreparer.java:77)
at
org.elasticsearch.bootstrap.Bootstrap.initialSettings(Bootstrap.java:106)
at org.elasticsearch.bootstrap.Bootstrap.main(Bootstrap.java:177) at
org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:32)
Caused by: while scanning an alias in ‘reader’, line 391, column 25:
http.cors.allow-origin: *
^ expected alphabetic or numeric character, but found but foundin ‘reader’, line 391, column 26:
*
^at
org.elasticsearch.common.jackson.dataformat.yaml.snakeyaml.scanner.ScannerImpl.scanAnchor(ScannerImpl.java:1439)
at
org.elasticsearch.common.jackson.dataformat.yaml.snakeyaml.scanner.ScannerImpl.fetchAlias(ScannerImpl.java:918)
at
org.elasticsearch.common.jackson.dataformat.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.java:366)
at
org.elasticsearch.common.jackson.dataformat.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:226)
at
org.elasticsearch.common.jackson.dataformat.yaml.snakeyaml.parser.ParserImpl ParseBlockMappingValue.produce(ParserImpl.java:586)atorg.elasticsearch.common.jackson.dataformat.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:158)atorg.elasticsearch.common.jackson.dataformat.yaml.snakeyaml.parser.ParserImpl.getEvent(ParserImpl.java:168)atorg.elasticsearch.common.jackson.dataformat.yaml.YAMLParser.nextToken(YAMLParser.java:331)atorg.elasticsearch.common.xcontent.json.JsonXContentParser.nextToken(JsonXContentParser.java:51)atorg.elasticsearch.common.settings.loader.XContentSettingsLoader.serializeObject(XContentSettingsLoader.java:78)atorg.elasticsearch.common.settings.loader.XContentSettingsLoader.load(XContentSettingsLoader.java:67)atorg.elasticsearch.common.settings.loader.XContentSettingsLoader.load(XContentSettingsLoader.java:45)atorg.elasticsearch.common.settings.loader.YamlSettingsLoader.load(YamlSettingsLoader.java:46)atorg.elasticsearch.common.settings.ImmutableSettings Builder.loadFromStream(ImmutableSettings.java:944)
… 5 more[1]+ Exit 3 bin/elasticsearch [root@monitor1
elasticsearch-1.4.4]#
处理方法1:
注释问题配置(原因待查)
#http.cors.allow-origin: *
结果:正常启动elasticsearch
Install ElasticSearch-Head Plugin
elasticsearch-head is a web front end for browsing and interacting with an Elastic Search cluster. Use the following command to install it.
$ bin/plugin --install mobz/elasticsearch-head
[root@monitor1 elasticsearch-1.4.4]# bin/plugin –install
mobz/elasticsearch-head
-> Installing mobz/elasticsearch-head… Trying https://github.com/mobz/elasticsearch-head/archive/master.zip…
Downloading
……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………..DONE
Installed mobz/elasticsearch-head into
/root/app/elasticsearch-1.4.4/plugins/head Identified as a _site
plugin, moving to _site structure … [root@monitor1
elasticsearch-1.4.4]# ls plugins/ head [root@monitor1
elasticsearch-1.4.4]#
Starting ElasticSearch Cluster
As the ElasticSearch setup is completed. Let the start ElasticSearch cluster using following command.
后台运行
$ ./bin/elasticsearch &
Verify Setup
You have all done, just need to verify setup. ElasticSearch works on port default port 9200, open your browser to point your server on port 9200, You will find some thing like below output
http://monitor1:9200/_plugin/head/
http://monitor1:9200/
elasticsearch使用基本示例:Basic Examples of ElasticSearch Uses
Following examples will help to add, get and search data in ElasticSearch.
1)Creating Bucket
curl -XPUT http://localhost:9200/mybucket
curl -XPUT http://monitor1:9200/mybucket
Output:
{“acknowledged”:true}
2)Adding Data to ElasticSearch
Use following commands to add some data in ElasticSearch.
Command 1:
curl -XPUT 'http://localhost:9200/mybucket/user/johny' -d '{ "name" : "Rahul Kumar" }'
curl -XPUT 'http://monitor1:9200/mybucket/user/johny' -d '{ "name" : "Rahul Kumar" }'
Output:
{“_index”:”mybucket”,”_type”:”user”,”_id”:”johny”,”_version”:1,”created”:true}
Command 2:
curl -XPUT 'http://monitor1:9200/mybucket/post/1' -d '
{
"user": "Rahul",
"postDate": "01-15-2015",
"body": "This is Demo Post 1 in ElasticSearch" ,
"title": "Demo Post 1"
}'
Output:
{“_index”:”mybucket”,”_type”:”post”,”_id”:”1”,”_version”:1,”created”:true}
Command 3:
curl -XPUT 'http://monitor1:9200/mybucket/post/2' -d '
{
"user": "TecAdmin",
"postDate": "01-15-2015",
"body": "This is Demo Post 2 in ElasticSearch" ,
"title": "Demo Post 2"
}'
Output:
{“_index”:”mybucket”,”_type”:”post”,”_id”:”2”,”_version”:1,”created”:true}
3)Fetching Data from ElasticSearch
Use following command to GET data from ElasticSearch and read the output.
curl -XGET 'http://localhost:9200/mybucket/user/johny?pretty=true'
curl -XGET 'http://localhost:9200/mybucket/post/1?pretty=true'
curl -XGET 'http://localhost:9200/mybucket/post/2?pretty=true'
curl -XGET 'http://monitor1:9200/mybucket/user/johny?pretty=true'
curl -XGET 'http://monitor1:9200/mybucket/post/1?pretty=true'
curl -XGET 'http://monitor1:9200/mybucket/post/2?pretty=true'
4)Searching in ElasticSearch
Use following command to search data from elastic search. Below command will search all data assiciated with user johny.
curl 'http://localhost:9200/mybucket/post/_search?q=user:TecAdmin&pretty=true'
curl 'http://monitor1:9200/mybucket/post/_search?q=user:TecAdmin&pretty=true'
Output:
{ “took” : 29, “timed_out” : false, “_shards” : {
“total” : 5,
“successful” : 5,
“failed” : 0 }, “hits” : {
“total” : 1,
“max_score” : 0.30685282,
“hits” : [ {
“_index” : “mybucket”,
“_type” : “post”,
“_id” : “2”,
“_score” : 0.30685282,
“_source”: {
“user”: “TecAdmin”,
“postDate”: “01-15-2015”,
“body”: “This is Demo Post 2 in ElasticSearch” ,
“title”: “Demo Post 2” }
} ] } }