Elasticsearch簡介
- Elasticsearch是一個開源的高擴展的分佈式全文檢索引擎,它可以近乎實時的存儲、檢索數據;本身擴展性很好,可以擴展到上百臺服務器,處理PB級別的數據。
- Elasticsearch也使用Java開發並使用Lucene作爲其核心來實現所有索引和搜索的功能,但是它的目的是通過簡單的RESTful API來隱藏Lucene的複雜性,從而讓全文搜索變得簡單。
Elasticsearch特點和優勢
- 分佈式實時文件存儲,可將每一個字段存入索引,使其可以被檢索到。
- 實時分析的分佈式搜索引擎。
- 分佈式:索引分拆成多個分片,每個分片可有零個或多個副本。集羣中的每個數據節點都可承載一個或多個分片,並且協調和處理各種操作;
- 負載再平衡和路由在大多數情況下自動完成。
- 可以擴展到上百臺服務器,處理PB級別的結構化或非結構化數據。也可以運行在單臺PC上(已測試)
- 支持插件機制,分詞插件、同步插件、Hadoop插件、可視化插件等。
Elasticsearch安裝
- 下載elasticsearch-6.2.1.tar.gz,上傳服務器,解壓
cd /usr/local/elk
上傳elasticsearch-6.2.1.tar.gz
tar -zxvf elasticsearch-6.2.1.tar.gz
- 修改配置文件
cd /usr/local/elk/elasticsearch-6.2.1/config
vi elasticsearch.yml
network.host: 172.30.1.45
- 啓動
cd /usr/local/elk/elasticsearch-6.2.1/bin
./elasticsearch
查看進程: ps -ef | grep elasticsearch
後臺運行: nohup ./elasticsearch &
啓動常見問題:
- 問題一:內存不足
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error='Cannot allocate memory' (errno=12)
原因:內存不足: 減少啓動程序所需內存,或加大內存,如關閉一些程序。
由於elasticsearch5.0默認分配jvm空間大小爲2g,修改jvm空間分配:
vi /usr/local/elk/elasticsearch-6.2.1/config/jvm.options
-Xms2g
-Xmx2g
修改爲
-Xms512m
-Xmx512m
- 問題二:格式問題(空格)
Exception in thread "main" SettingsException[Failed to load settings from [elasticsearch.yml]]; nested: ElasticsearchParseException[malformed, expected settings to start with 'object', instead was [VALUE_STRING]];
原因:
配置文件elasticsearch.yml中:network.host:172.30.1.45
冒號後面應該加空格:network.host: 172.30.1.45
- 問題三:操作用戶問題
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
這個錯誤,是因爲使用root用戶啓動elasticsearch,elasticsearch是不允許使用root用戶啓動的,所以我們需要添加用戶。
解決方案:新建用戶elk-weifan
groupadd elktest
useradd -g elktest elk-weifan
passwd elk-weifan
密碼: elk-weifan
再次輸入密碼: elk-weifan
chown -R elk-weifan:elktest /usr/local/elk/elasticsearch-6.2.1
chmod 755 /usr/local/elk/elasticsearch-6.2.1
su - elk-weifan
切換用戶之後:
cd /usr/local/elk/elasticsearch-6.2.1/bin
./elasticsearch
啓動成功
- 問題四: 系統版本過低(此警告可以忽略)
java.lang.UnsupportedOperationException: seccomp unavailable: CONFIG_SECCOMP not compiled into kernel, CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER are needed
原因:這個錯誤是因爲centos6的內核不支持SecComp
解決:
查看centos版本
lsb_release -a
可以不必理會
補充問題:
ERROR: [1] bootstrap checks failed
[1]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
cd /usr/local/elk/elasticsearch-6.2.1/config
vi elasticsearch.yml
# 這個可以在配置文件中找到
bootstrap.memory_lock: false
# 這個找不到,複製粘貼就好,默認開啓,由於內核問題,找不到,設置爲false即可解決
bootstrap.system_call_filter: false
- 問題五:創建文件數設置(需重新打開窗口,否則會出現修改後未生效!!!)
ERROR: bootstrap checks failed
max file descriptors [65535] for elasticsearch process likely too low, increase to at least [65536]
原因:無法創建本地文件問題,用戶最大可創建文件數太小
解決:
切換到root用戶,編輯limits.conf配置文件, 添加類似如下內容:
vi /etc/security/limits.conf
添加如下內容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 65535
- 問題六:創建線程數設置
ERROR: bootstrap checks failed
max number of threads [1024] for user [elk-weifan] likely too low, increase to at least [4096]
原因:無法創建本地線程問題,用戶最大可創建線程數太小
解決:切換到root用戶,進入limits.d目錄下,修改90-nproc.conf 配置文件。
vi /etc/security/limits.d/90-nproc.conf
找到如下內容:
* soft nproc 1024
修改爲
* soft nproc 4096
- 問題七:最大虛擬內存太小
max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
原因:最大虛擬內存太小
解決:切換到root用戶下,修改配置文件sysctl.conf
vi /etc/sysctl.conf
添加下面配置:
vm.max_map_count=655360
並執行命令,使修改生效:
sysctl -p
訪問
- 瀏覽器訪問http://172.30.1.45:9200/,返回:
{
"name" : "_bPCCkF",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "3CBmAzhBSViAPTDmhxEd6g",
"version" : {
"number" : "6.2.1",
"build_hash" : "253032b",
"build_date" : "2016-10-26T04:37:51.531Z",
"build_snapshot" : false,
"lucene_version" : "6.2.1"
},
"tagline" : "You Know, for Search"
}
- 完整測試:
命令測試elasticsearch:
# 創建索引create_myindex
curl -XPUT '172.30.1.45:9200/create_myindex?pretty'
# 放入數據Fan Wei,,type:create_mytype,ID:1
curl -XPOST 'localhost:9200/create_myindex/create_mytype/1?pretty' -d' {"name": "Fan Wei" }'
# 查看索引信息
curl -XGET '172.30.1.45:9200/create_myindex/create_mytype/1?pretty'
# 獲取索引內容
curl '172.30.1.45:9200/create_myindex/_search?q=*&pretty'
# 刪除索引
curl -XDELETE '172.30.1.45:9200/create_myindex?pretty'
# 列出所有索引
curl 'http://172.30.1.45:9200/_cat/indices?v'
- 模板相關命令:
# 查看所有模板
curl -XGET 'http://172.30.1.45:9200/_template'
# 查看單個模板metricbeat-6.2.1
curl -XGET 'http://172.30.1.45:9200/_template/metricbeat-6.2.1'
# 刪除單個模板metricbeat-6.2.1
curl -XDELETE 'http://172.30.1.45:9200/_template/metricbeat-6.2.1'
常用命令
# 查看版本信息:
curl 172.30.1.45:9200
# 查看集羣是否健康:
curl 'http://172.30.1.45:9200/_cat/health?v'
# 查看節點列表:
curl 'http://172.30.1.45:9200/_cat/nodes?v'
# 列出所有索引及存儲大小:
curl 'http://172.30.1.45:9200/_cat/indices?v'
# 刪除索引:
curl -XDELETE 'http://172.30.1.45:9200/test_system_log?pretty'
# 查看索引mapping
curl -XGET "http://172.30.1.45:9200/ecapi-2018.04.08/_mapping?pretty"
# 查看索引內容(其中 q=* 表示匹配索引中所有的數據。)
curl '172.30.1.45:9200/ecapi-2018.04.08/_search?q=*&pretty'
# 索引數據遷移:
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}