文章目录
背景
从本文开始,我将连续用几篇文章记录一下去年学习ElasticSearch7.6。ElasticSearch和其套件Logstash、Kibana均安装在CentOS7下,数据也存储在CentOS7,对其的访问则都在Windows中。
介绍
ElasticSearch、Logstash和Kibana合称ELK,分别负责数据存储与检索、数据导入和检索可视化。ELK套件可通过docker或源码安装。
安装
安装操作都在CentOS7下进行
docker安装
首先需要安装docker,参见文章CentOS安装docker,然后进行以下操作:
下载镜像
[root@localhost szc]# docker pull elasticsearch
Using default tag: latest
Trying to pull repository docker.io/library/elasticsearch ...
latest: Pulling from docker.io/library/elasticsearch
05d1a5232b46: Pull complete
5cee356eda6b: Pull complete
89d3385f0fd3: Pull complete
65dd87f6620b: Pull complete
78a183a01190: Pull complete
1a4499c85f97: Pull complete
2c9d39b4bfc1: Pull complete
1b1cec2222c9: Pull complete
59ff4ce9df68: Pull complete
1976bc3ee432: Pull complete
5af49e8af381: Pull complete
42c8b75ff7af: Pull complete
7e6902915254: Pull complete
99853874fa54: Pull complete
596fbad6fcff: Pull complete
Digest: sha256:a8081d995ef3443dc6d077093172a5931e02cdb8ffddbf05c67e01d348a9770e
Status: Downloaded newer image for docker.io/elasticsearch:latest
查看镜像id
[root@localhost szc]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest e791337790a6 2 months ago 127 MB
docker.io/elasticsearch latest 5acf0e8da90b 21 months ago 486 MB
运行es
同时需要指定jvm堆大小,并进行端口号映射
[root@localhost szc]# docker run -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -d -p 9200:9200 -p 9300:9300 5acf0e8da90b
d9b827f45a54746b794eb5f260152e252a3307e7fcba2d2e4298dbd9ce83567e
开放端口号并访问
开放9200和9300端口号,前者对外提供web服务,后者负责ES集群中结点的通信,在windows浏览器上访问9200即可
[root@localhost szc]# firewall-cmd --add-port=9200/tcp --permanent
success
[root@localhost szc]# firewall-cmd --add-port=9300/tcp --permanent
success
[root@localhost szc]# firewall-cmd --reload
success
然后再Windows下通过URL:CentOS的IP:9200,就可以访问到ES了

源码解压安装
首先下载ES7.6的Linux版本源码、Kibana7.6的Linux源码 和Logstash7的Linux源码,然后进行以下操作
ElasticSearch
解压源码
[root@localhost ElasticSearch]# tar -zxvf elasticsearch-7.6.0-linux-x86_64.tar.gz
新建es用户
这一步是因为源码解压后的es不能用root启动,而docker下不存在此问题
[root@localhost ElasticSearch]# useradd es
[root@localhost ElasticSearch]# passwd es
修改配置文件
我们需要修改一些配置文件:
1、elasticsearch-7.6.0源码目录/config/elasticsearch.yml
# ------------------------------------ Node ------------------------------------
node.name: node-1
# ---------------------------------- Network -----------------------------------
network.host: 192.168.57.141 # 自己CentOS的IP
http.port: 9200 # ES对外端口
# --------------------------------- Discovery ----------------------------------
cluster.initial_master_nodes: ["node-1"]
2、elasticsearch-7.6.0源码目录/bin/elasticsearch-env,在文件java部分的开头设置JAVA_HOME为es自带的jdk
.....
#set the path to java
JAVA_HOME="$ES_HOME/jdk"
.....
3、/etc/security/limits.conf,添加如下内容
* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096
4、/etc/sysctl.conf,添加如下内容
vm.max_map_count=262144
启动ES
切换至用户es,启动es
[root@localhost ElasticSearch]# su es
[es@localhost ElasticSearch]$ elasticsearch-7.6.0/bin/elasticsearch
这时开放9200端口就同样能在Windows下访问ES了。
Logstash
同样,解压源码压缩包,直接运行Logstash源码目录/bin/logstash即可通过配置文件导入数据
[es@localhost logstash-7.3.2]$ bin/logstash -f jobs/insert-movielen.conf
使用的配置文件jobs/insert-movielen.conf为
input {
file {
path => "/home/szc/ElasticSearch/data/movies.csv"
start_position => "beginning"
sincedb_path => "/dev/null"
}
}
filter {
csv {
separator => ","
columns => ["id","content","genre"]
}
mutate {
split => { "genre" => "|" }
remove_field => ["path", "host","@timestamp","message"]
}
mutate {
split => ["content", "("]
add_field => { "title" => "%{[content][0]}"}
add_field => { "year" => "%{[content][1]}"}
}
mutate {
convert => {
"year" => "integer"
}
strip => ["title"]
remove_field => ["path", "host","@timestamp","message","content"]
}
}
output {
elasticsearch {
hosts => "http://192.168.57.141:9200"
index => "movies"
document_id => "%{id}"
}
stdout {}
}
其中input字段里的path为自己的输入文件路径
filter中csv用来处理csv文件,指定每一行的分隔符和分隔后的数据列
mutate用来对列进行分隔、字段修改、字段添加与删除等
output字段用来把数据进行输出
Kibana
这是个可视化组件,后续我们基本都是通过它来操作ES的。
解压压缩包
[root@localhost ElasticSearch]# tar -zxvf kibana-7.6.0-linux-x86_64.tar.gz
修改配置文件
进入解压目录,修改配置文件conf/kibana.yml
server.port: 5601
server.host: "192.168.57.141" # IP换成自己CentOS的IP
elasticsearch.hosts: ["http://192.168.57.141:9200"] # IP换成自己CentOS的IP
启动Kibana
[root@localhost ElasticSearch]# su es
[es@localhost ElasticSearch]$ kibana-7.6.0-linux-x86_64/bin/kibana
结语
下一篇文章,我通过web测试工具Postman,来演示ES的基本用法
191

被折叠的 条评论
为什么被折叠?



