简介
ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana。它们之间互相配合使用,完美衔接,高效的满足了很多场合的应用,实现了一种目前主流的日志系统。ELK的工作原理和更多种结合框架方案可以参考https://www.cnblogs.com/aresxin/p/8035137.html。本篇文章仅记录ELK三者的搭建记录。
环境及软件
- Centos7 (操作系统)
- JDK-1.8.0 (JRE也可)
- LogStash-2.4.0 (用于日志收集)
- ElasticSearch-2.2.0 (用于存储数据和定义检索规则)
- Kibana-4.4.0 (用于界面展示)
- Redis-4.0.2(非必须,仅测了该种情况)
安装及配置
Java
Elasticsearch需要用到java环境,所以先要安装jdk1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 解压
tar -zxvf jdk-8u144-linux-x64.tar.gz -C /home/app/Java
编辑环境变量配置
vi /etc/profile
----------
如下内容是加入到文件末尾的Java environment
export JAVA_HOME=/home/app/java/jdk1.8.0_144
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib:$JAVA_HOME/bin
----------
使环境变量配置生效
source /etc/profile
测试一下
java -version
Logstash
Logstash,用于日志收集,这里安装并测试了nginx日志到redis。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 下载压缩包
wget https://download.elastic.co/logstash/logstash/logstash-2.4.0.tar.gz
解压
tar zxvf logstash-2.4.0.tar.gz -C /home/app/
测试一下
/home/app/logstash-2.4.0/bin/logstash -e 'input { stdin { } } output { stdout {} }'
配置输出到redis
view /home/app/logstash-2.4.0/bin/logstash_nginx.conf
----------
input {
file {
path => ["/var/log/nginx/access.log"]
type => "nginx_log"
}
}
output {
redis{
host => "192.168.50.120"
key => 'logstash-redis'
data_type => 'list'
}
stdout {
codec => rubydebug
}
}
----------
启动
/home/app/logstash-2.4.0/bin/logstash -f ./logstash_nginx.conf --configtest
ElasticSearch
Elasticsearch,用于存储数据和定义检索规则。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 下载压缩包
wget https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.2.0/elasticsearch-2.2.0.tar.gz
解压
tar -zxvf elasticsearch-2.2.0.tar.gz -C /home/app
修改配置文件
vim /home/app/elasticsearch-2.2.0/config/elasticsearch.yml
----------
network.host: 127.0.0.1
http.port: 9200
----------
创建专用用户
groupadd elk
useradd es -g elk
chown -R es.elk /home/app/elasticsearch-2.2.0
su - es
安装kopf和head插件
/home/app/elasticsearch-2.2.0/bin/plugin install lmenezes/elasticsearch-kopf
/home/app/elasticsearch-2.2.0/bin/plugin install mobz/elasticsearch-head
启动
nohup /home/app/elasticsearch-2.2.0/bin/elasticsearch > /home/app/elasticsearch-2.2.0/nohub &
测试一下
curl http://127.0.0.1:9200
查看集群(用浏览器访问)
http://127.0.0.1/_plugin/head/
删除索引
curl -XDELETE http://localhost:9200/shakespeare
创建索引
curl -XPUT http://localhost:9200/shakespeare -d '
{
"mappings" : {
"_default_" : {
"properties" : {
"speaker" : {"type": "string", "index" : "not_analyzed" },
"play_name" : {"type": "string", "index" : "not_analyzed" },
"line_id" : { "type" : "integer" },
"speech_number" : { "type" : "integer" }
}
}
}
}
kibana
Kibana用于连接Elasticsearch,定义自己的图表输出样式和展示。1
2
3
4
5
6
7
8
9
10
11
12
13
14 下载压缩包
wget https://download.elastic.co/kibana/kibana/kibana-4.4.0-linux-x64.tar.gz
修改配置文件(对接elasticsearch)
vim /home/app/kibana-4.4.0-linux-x64/config/kibana.yml
elasticsearch.url: "http://127.0.0.1:9200"
server.port: 5601
server.host: "0.0.0.0"
启动
nohup /home/app/kibana-4.4.0-linux-x64/bin/kibana > /home/app/kibana-4.4.0-linux-x64/nohub.out &
测试一下
curl http://127.0.0.1:5601
Redis(非必需,仅记录)
这里仅用于配合LogStash测试。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 安装相关编译工具
yum install gcc tcl
下载压缩包
wget http://download.redis.io/releases/redis-4.0.2.tar.gz
解压
tar zxvf redis-4.0.2.tar.gz -C /home/app
编译
cd /home/app/redis-4.0.2/
make MALLOC=libc
make install
测试一下
make test
src目录下执行
./redis-server
加入开机启动项
vim /home/app/redis-4.0.2/redis.conf
----------
bind 127.0.0.1改为0.0.0.0
daemonize no改为yes
requirepass foobared改为自己的密码
----------
配置开机启动脚本
vim /etc/init.d/redis
----------
chkconfig: 2345 10 90
description: Start and Stop redis
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379 #实际环境而定
EXEC=/home/app/redis-4.0.2/src/redis-server #实际环境而定
REDIS_CLI=/home/app/redis-4.0.2/src/redis-cli #实际环境而定
PIDFILE=/var/run/redis.pid
CONF="/home/app/redis-4.0.2/redis.conf" #实际环境而定
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed."
else
echo "Starting Redis server..."
EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE exists, process is not running."
else
PID=$(cat $PIDFILE)
echo "Stopping..."
REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x $PIDFILE ]
do
echo "Waiting for Redis to shutdown..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
{0} stop
{0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac
----------
赋执行权限
chmod +x /etc/init.d/redis
尝试启动
service redis start
开启自启动
chkconfig redis on
整个搭配配置及效果记录(待更新。。。)
这段内容待更新。。。。。。先上两张Kibana的效果图吧:
本文链接: https://www.xiajunyi.com/pages/p57.html
版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。转载请注明出处!