Elasticsearch安装及使用

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

访问

{
  "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"
  }
}

参考博客:https://www.cnblogs.com/aaanthony/p/7380662.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章