Linux 安装 ElasticSearch-7.15.2
延申阅读: SpringBoot 整合 ElasticSearch-7.15.2
相关文件
Linux: CentOS 7 64位
Jdk: 1.8.0_231
ElasticSearch: elasticsearch-7.15.2
Kibana: Kibana-7.15.2
Logstash: Logstash-7.15.2
Ik-Analysis: elasticsearch-analysis-ik-7.15.2
关于Jdk
从ES7.x开始,JDK开始使用java 11, 如果本地JDK使用的不是JDK11,会报警告,但是影响不大
可以通过修改ES的/elasticsearch-7.15.2/bin/elasticsearch-env
配置文件,解决这个问题,但不建议修改。
关于 Kibana
Kibana是ElasticSearch的可视化平台,依赖ElasticSearch,需要优先安装ElasticSearch。
关于 ELK
ELK是一套实时数据收集,存储,索引,检索,统计分析及可视化的解决方,包括Elasticsearch、Logstash、Kibana三大开源框架。其中,
Elasticsearch是一个基于Lucene、分布式、通过Restful方式进行交互的近实时搜索平台框架。
Logstash是ELK的中央数据流引擎,用于从不同目标(文件/数据存储/MQ)收集的不同格式数据,经过过滤后支持输出到不同目的地(文件/MQ/redis/elasticsearch/kafka等)。
Kibana可以将elasticsearch的数据通过友好的页面展示出来,提供实时分析的功能。
下载、上传、解压相关文件
官网下载地址(2021/11/18):
ElasticSearch
Kibana
Logstash
elasticsearch-analysis-ik(GitHub)
百度网盘下载:
ElasticSearch:hvv9
Kibana:fpxu
Logstash:mzxh
elasticsearch-analysis-ik:z5j2
或者在Linux使用命令下载:
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.15.2-x86_64.rpm
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.15.2-x86_64.rpm
上传到Linux,并解压
rz -E
tar -zxf elasticsearch-7.15.2-linux-x86_64.tar.gz
tar -zxf kibana-7.15.2-linux-x86_64.tar.gz
tar -zxf logstash-7.15.2-linux-x86_64.tar.gz
cp -rf elasticsearch-7.15.2 /usr/local/
cp -rf kibana-7.15.2-linux-x86_64 /usr/local/kibana-7.15.2
cp -rf logstash-7.15.2 /usr/local/
一、搭建 ElasticSearch 服务器
- 修改elasticsearch的配置文件
-
修改elasticsearch.yml
mkdir /usr/local/elasticsearch-7.15.2/data vim /usr/local/elasticsearch-7.15.2/config/elasticsearch.yml
打开下面被注释内容并修改 或
直接添加下面的内容到最后面cluster.name: my-application #集群名称 node.name: node-1 #节点名称 path.data: /usr/local/elasticsearch-7.15.2/data #数据的存储目录 path.logs: /usr/local/elasticsearch-7.15.2/logs #日志的存储目录 network.host: 0.0.0.0 #设置绑定的ip,设置为0.0.0.0,可以让任何计算机节点访问 http.port: 9200 #端口 cluster.initial_master_nodes: ["node-1"] #设置在集群中的所有节点名称,这个节点名称就是之前所修改的,当然你也可以采用默认的也行,目前是单机,放入一个节点即可
-
修改 limits.conf
vim /etc/security/limits.conf
添加以下内容:
es soft nofile 65536 es hard nofile 65536 es soft nproc 4096 es hard nproc 4096
-
修改 20-nproc.conf 里面的 * 为 es
vim /etc/security/limits.d/20-nproc.conf
es soft nproc 4096 root soft nproc unlimited
-
向 sysctl.conf 添加
vm.max_map_count = 655360
vim /etc/sysctl.conf
执行sysctl
sysctl -p
-
- 添加用户
不能使用root用户去启动elasticsearch,因为elasticsearch内置的安全性
添加es用户,密码为es,并修改elasticsearch-7.15.2目录的权限:adduser es -p es chown -R es /usr/local/elasticsearch-7.15.2/ chown -R es /usr/local/kibana-7.15.2/
二、启动 ElasticSearch
-
登录su用户,启动es:
前台启动su es /usr/local/elasticsearch-7.15.2/bin/elasticsearch
后台启动:
su es /usr/local/elasticsearch-7.15.2/bin/elasticsearch -d
如果启动报内存太小
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000, 986513408, 0) failed; error='Cannot allocate memory' (errno=12)
修改elasticsearch-7.15.2/config/jvm.options,根据情况填写大小:
-Xms128m -Xmx128m
-
测试
浏览器测试 访问 http://ip:9200/
或者 使用命令测试curl 127.0.0.1:9200
-
关闭elasticsearch
前台启动的,只需要使用 CTRL+C 即可退出;
如果是后台启动则需要关闭进程:
查看es进程,并关闭:ps -ef | grep elastic
关掉上面的进程即可(下面其他的不是真正的elasticsearch进程)kill -9 3952
三、安装 Kibana 并启动
-
修改/config/kibana.yml
vim /usr/local/kibana-7.15.2/config/kibana.yml
打开并修改注释或添加以下内容:
server.host: "0.0.0.0" elasticsearch.hosts: ["http://localhost:9200"]
-
在root用户下暴露端口
/sbin/iptables -I INPUT -p tcp --dport 5601 -j ACCEPT
-
登录su用户,启动es
启动 Kibana 需要先启动 ElasticSearch
前台启动
su es /usr/local/kibana-7.15.2/bin/kibana
后台启动
su es /usr/local/kibana-7.15.2/bin/kibana & #挂起服务器会被关闭 nohup /usr/local/kibana-7.15.2/bin/kibana & #这个不会
-
访问
http://ip:5601/app/home/
-
关闭
前台关闭直接CTRL+C即可
后台关闭需要关闭进程:ps -ef|grep kibana kill -9 4569
简单使用
进入Console:
-
查看服务器状态(健康度),green 表示一切OK
GET /_cat/health?v
点击绿色三角形运行,如下:
-
查看索引
GET /_cat/indices?v
-
增加索引
PUT /ea?pretty
-
删除索引
DELETE /ea?pretty
四、安装 Logstash 并启动
-
创建一个名为“logstash-simple.conf”的文件,并将其保存在与Logstash相同的目录中。
cd /usr/local/logstash-7.15.2 vim logstash-simple.conf
input { stdin { } } output { elasticsearch { hosts => ["localhost:9200"] } stdout { codec => rubydebug } }
-
启动
启动 Logstash 需要先启动 ElasticSearch
使用配置文件启动:
cd /usr/local/logstash-7.15.2 bin/logstash -f logstash-simple.conf
直接启动:
bin/logstash -e 'input { stdin { } } output { stdout {} }' bin/logstash -e 'input { stdin {} } output { elasticsearch {hosts => ["localhost:9200"]} stdout{} }' bin/logstash -e 'input { stdin {} } output { elasticsearch {hosts => ["localhost:9200"]} stdout{ codec => rubydebug } }'
启动后如果没有报错需要等待logstash 完成,此时间可能比较长
-
这里启动成功的实例
-
同样的使用CTRL+C退出/或kill进程。
常用配置案例
-
logstash-filter.conf
配置过滤器。过滤器是一种在线处理机制,它提供了对数据进行切片和剪裁的灵活性,以满足您的需要。让我们来看看一些实际应用中的过滤器。下面的配置文件设置grok和date过滤器。input { stdin { } } filter { grok { match => { "message" => "%{COMBINEDAPACHELOG}" } } date { match => [ "timestamp" , "dd/MMM/yyyy:HH:mm:ss Z" ] } } output { elasticsearch { hosts => ["localhost:9200"] } stdout { codec => rubydebug } }
-
flow-es.conf
input { file { type => "flow" path => "/var/nginx_logs/*.log" discover_interval => 5 start_position => "beginning" } } output { if [type] == "flow" { elasticsearch { index => "flow-%{+YYYY.MM.dd}" hosts => ["172.16.0.14:9200", "172.16.0.15:9200", "172.16.0.16:9200"] } } }
-
flow-kafka.conf
input { file { path => "/export/data/logs/*.log" discover_interval => 5 start_position => "beginning" } } output { kafka { topic_id => "accesslog" codec => plain { format => "%{message}" charset => "UTF-8" } bootstrap_servers => "192.168.175.128:9092,192.168.175.133:9092,192.168.175.130:9092" } }
-
kafka-es.conf
input { kafka { type => "level-one" auto_offset_reset => "smallest" codec => plain { charset => "GB2312" } group_id => "es" topic_id => "test" zk_connect => "172.16.0.11:2181,172.16.0.12:2181,172.16.0.13:2181" } } filter { mutate { split => { "message" => " " } add_field => { "event_type" => "%{message[3]}" "current_map" => "%{message[4]}" "current_X" => "%{message[5]}" "current_y" => "%{message[6]}" "user" => "%{message[7]}" "item" => "%{message[8]}" "item_id" => "%{message[9]}" "current_time" => "%{message[12]}" } remove_field => [ "message" ] } } output { elasticsearch { index => "level-one-%{+YYYY.MM.dd}" codec => plain { charset => "GB2312" } hosts => ["172.16.0.14:9200", "172.16.0.15:9200", "172.16.0.16:9200"] } }
-
logstash.conf
input { file { type => "syslog" path => "/var/log/messages" discover_interval => 10 start_position => "beginning" } file { type => "gamelog" path => "/log/*/*.log" discover_interval => 10 start_position => "beginning" } } output { if [type] == "syslog" { elasticsearch { index => "syslog-%{+YYYY.MM.dd}" hosts => ["172.16.0.14:9200", "172.16.0.15:9200", "172.16.0.16:9200"] } } if [type] == "gamelog" { elasticsearch { index => "gamelog-%{+YYYY.MM.dd}" hosts => ["172.16.0.14:9200", "172.16.0.15:9200", "172.16.0.16:9200"] } } }
五、安装 IK 中文分词插件
-
上传到
/usr/local/elasticsearch-7.15.2/plugins/
,
解压到/usr/local/elasticsearch-7.15.2/plugins/elasticsearch-analysis-ik-7.15.2
cd /usr/local/elasticsearch-7.15.2/plugins/ unzip -d elasticsearch-analysis-ik-7.15.2 elasticsearch-analysis-ik-7.15.2.zip rm -rf elasticsearch-analysis-ik-7.15.2.zip
-
同步权限
chown -R es elasticsearch-analysis-ik-7.15.2/
-
重启ElasticSearch即可
出现下面的日志,表示成功
-
使用 Kibana 测试
GET _analyze { "analyzer":"ik_max_word", "text":"你的城市没有一扇门" } GET _analyze { "analyzer":"ik_smart", "text":"你的城市没有一扇门" }
参考官网: Logstash configuration examples | Logstash Reference [7.15] | Elastic
若有不正之处,请谅解和批评指正,谢谢~
转载请标明:
https://blog.youkuaiyun.com/vihem/article/details/121404092