/ 城寒 / ElasticSearch - Install & Config

ElasticSearch - Install & Config

2018-02-09 posted in [ElasticSearch]

ElasticSearch 安装和配置

ELK整体配置都比较简单,可以直接参考官方文档

安装ES

要求配置Java环境,1.8以上

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.1.tar.gz
tar -xzf elasticsearch-6.2.1.tar.gz
cd elasticsearch-6.2.1/ 
# 安装head插件
./bin/plugin install mobz/elasticsearch-head
# 编辑配置文件
vim config/elasticsearch.yml
# 不同版本的配置名称可能不太一样,但是项目相同
######### elasticsearch.yml ##########
# 文件位置
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
# 网络和名称相关
http.port: 8200 
http.cors.allow-origin: "/.*/"
http.cors.enabled: true
cluster.name: elasticsearch1
node.name: "node1"
# 如果不在同一个局域网中,需要添加如下配置
discovery.zen.ping.unicast.hosts: ["ip1", "ip2" ...]
######################################
vim service/elasticsearch.yml
# 修改使用的内存,ES比较占用内存
set.default.ES_HEAP_SIZE=32768

后台运行即可 ./bin/elasticsearch & 然后可以在 localhost:8200 可以看到json的集群信息 如果 head 插件安装成功,访问 localhost:8200/_plugin/head,可以看到更详细的信息

给正在运行中的集群添加插件

ES必须重启才能生效,所以需要逐个节点重启来保证服务不中断

添加SQL查询插件

方便使用SQL语句进行查询,还支持结果导出,非常不错 https://github.com/NLPchina/elasticsearch-sql

下载及安装

cd elasticsearch
./bin/plugin install https://github.com/NLPchina/elasticsearch-sql/releases/download/2.4.1.0/elasticsearch-sql-2.4.1.0.zip

此时插件就安装完成了,但是还不能使用,会出现 _sql 无法解析的错误,需要重启节点

节点滚动重启

参考官网

需要先关闭shard均衡,再重启,然后再开启均衡

curl -XPUT http://hostip:8200/_cluster/settings -d '{"transient":{"cluster.routing.allocation.enable":"none"}}' 
./elasticsearch -d -p pid -Dpath.conf=/path/config # 不要用root用户
# 通过head插件观察节点是否加入,或者在日志中观察是否启动完成,然后再执行下一句
curl -XPUT http://hostip:8200/_cluster/settings -d '{"transient":{"cluster.routing.allocation.enable":"all"}}'
 

安装和配置Logstash

安装非常简单,解压即可,主要是配置对应的处理规则

vim config/log4j_to_es.conf
########## log4j_to_es.conf ##########
# For detail structure of this file
# Set: https://www.elastic.co/guide/en/logstash/current/configuration-file-structure.html
input {
  # For detail config for log4j as input, 
  # See: https://www.elastic.co/guide/en/logstash/current/plugins-inputs-log4j.html
  log4j {
    mode => "server"
    host => "centos2"
    port => 4567
  }
}
filter {
  #Only matched data are send to output.
  # 如果需要对数据进行处理,主要是在这个位置
}
output {
  # For detail config for elasticsearch as output, 
  # See: https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html
  elasticsearch {
    action => "index"            #The operation on ES
    hosts  => "centos2:9200"     #ElasticSearch host, can be array.
    index  => "ec"               #The index to write data to, can be any string.
  }
}
######################################

# 启动
./bin/logstash agent -f config/log4j_to_es.conf


安装和配置Kibana

kibana的安装也很简单,解压后修改配置即可

server.port: 5601
server.host: “centos2”
elasticsearch.url: http://centos2:9200
kibana.index: “.kibana”