elk安装

1.创建linux用户与组

1
2
3
4
5
6
7
8
9
10
添加工作组
groupadd elastic
创建elastic用户
useradd -g elastic elastic
设置密码
passwd elastic
修改目录权限
chown -R elastic:elastic /usr/local/elk
切换用户
su elastic

2. 安装elasticsearch 7.6

下载解压:

1
2
3
cd /usr/local/elk
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.0-linux-x86_64.tar.gz
tar xvf elasticsearch-7.6.0-linux-x86_64.tar.gz

修改配置文件

1
2
3
4
5
6
7
8
9
10
vim /usr/local/elk/elasticsearch-7.6.0/bin/elasticsearch-env
# 把java查找改为elasiticSearch自带的jdk
#now set the path to java
if [ "$(uname -s)" = "Darwin" ]; then
# macOS has a different structure
JAVA="$ES_HOME/jdk.app/Contents/Home/bin/java"
else
JAVA="$ES_HOME/jdk/bin/java"
fi
JAVA_TYPE="bundled jdk"
1
2
3
4
5
cd /usr/local/elk/elasticsearch-7.6.0/config
vim jvm.options
# 修改内存
-Xms512m
-Xmx512m

启动:

1
2
cd /usr/local/elk/elasticsearch-7.6.0/bin
./elasticsearch -d

测试安装完成:

1
curl 127.0.0.1:9200

3.安装kibana 7.6

1
2
3
cd /usr/local/elk
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.6.0-linux-x86_64.tar.gz
tar xvf kibana-7.6.0-linux-x86_64.tar.gz

修改配置,能外网访问,配合为中文

1
2
3
4
5
6
cd /usr/local/elk/kibana-7.6.0-linux-x86_64/config
vim kibana.yml
修改1. 改为外网可访问形式(不建议,最好限制ip)
server.host: "0.0.0.0"
修改2. 最后一行增加
i18n.locale: "zh-CN"
1
2
3
4
5
# 启动
cd /usr/local/elk/kibana-7.6.0-linux-x86_64/bin
nohup ./kibana &
# 查找进程号
fuser -n tcp 5601

4.安装logstash

1
2
3
cd /usr/local/elk
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.6.0.tar.gz
tar xvf logstash-7.6.0.tar.gz
1
2
3
4
5
cd /usr/local/elk/logstash-7.6.0/config
vim jvm.options
修改:
-Xms256m
-Xmx256m

启动并验证

1
2
3
4
5
cd /usr/local/elk/logstash-7.6.0
bin/logstash -e 'input { stdin { } } output { stdout {} }'
# 在logstash启动后的命令行输入:
Hello Logstash
# 看到成功响应即为成功

安装插件:logstash-codec-json_lines

1
2
cd /usr/local/elk/logstash-7.6.0/bin
./logstash-plugin install logstash-codec-json_lines

5.整合

logstash 相关配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cp logstash-sample.conf logstash.conf
vim logstash.conf
## 输入
input {
tcp {
host => "127.0.0.1"
port => 9250
mode => "server"
tags => ["tags"]
codec => json_lines
}
}
output{
elasticsearch {
hosts => ["localhost:9200"]
index => "%{[appName]}-%{+YYYY.MM.dd}" #用一个项目名称来做索引
}
stdout { codec => rubydebug }
}

127.0.0.1如果服务器不在一台机器上时,可填写内网地址或公网地址

5044是logstash接收数据的端口

codec => json_lines是一个json解析器,接收json的数据。这个要装 logstash-codec-json_lines 插件

ouput elasticsearch指向我们安装的地址

stdout会打印收到的消息,调试用

1
2
3
# logstash 后台启动
cd /usr/local/elk/logstash-7.6.0/bin
nohup ./logstash -f ../config/logstash.conf &

测试logstash 至 elasticsearch

1
2
3
curl http://localhost:9600/
# 查看/usr/local/elk/elasticsearch-7.6.0/log/selasticsearch.log
adding template [logstash] for index patterns [logstash-*]

spring boot 项目:

pom.xml

1
2
3
4
5
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>7.0.1</version>
</dependency>

logback.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- logstash -->
<appender name="stash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<!-- 如果logstash和应用程序不在一台机器时,填写内网地址或公网地址 -->
<destination>localhost:9250</destination>
<!-- encoder必须配置,有多种可选 -->
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder" >
<!-- "appname":"trans-core" 的作用是指定创建索引的名字时用,并且在生成的文档中会多了这个字段 -->
<customFields>{"appname":"trans-core"}</customFields>
</encoder>
</appender>

<root level="info">
<appender-ref ref="stash"/>
</root>

elk安装
http://hanqichuan.com/2022/01/21/监控/elk安装/
作者
韩启川
发布于
2022年1月21日
许可协议