ELK(ElasticSearch, Logstash, Kibana)
为什么要用
ELK
,它解决了什么问题?
![](https://i-blog.csdnimg.cn/blog_migrate/c9cfc632614e6ae9eddf11f77599310b.png)
![](https://i-blog.csdnimg.cn/blog_migrate/3ade76356f24d62ad36afe188013d56d.png)
ELK
的架构原理
![](https://i-blog.csdnimg.cn/blog_migrate/7639671c360cfa34f4eaab16163a4854.png)
安装
ELK
1
安装
ElasticSearch
安装步骤
注意:
1.安装 elasticsearch 时 linux 内核必须是 3.5+
2.jdk 必须是 jdk1.8.0_131 以上版本
3.如果是在虚拟机上安装,虚拟机分配至少 1.5G 以上的内存
-----------------------------Linux 内核升级步骤-----------------------------------
内核版本提升后,对每个线程的内存分配提升了,elasticsearch 需要至少 4096 的线程才能正常启动。
所以需要为虚拟机分配至少 1.5G 以上的内存。
升级内核
rpm –import http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-7
rpm -Uvh http://www.elrepo.org/elrepo-release-6-8.el6.elrepo.noarch.rpm
在这一步如果出错,说明可能是yum插件需要更新,可以用
yum update nss
yum --enablerepo=elrepo-kernel install kernel-lt -y
vim /etc/grub.conf
修改文件中内容,保证使用新内核启动。
default=0
重启系统
-----------------------------安装 elasticsearch--------------------------------------
安装 elasticsearch6.2.3
1.下载压缩
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.3.tar.gz
tar -zxvf elasticsearch-6.2.3.tar.gz
2.修改 elasticsearch 需要的系统配置。
vi /etc/security/limits.conf
增加下述内容。
* soft nofile 65536
* hard nofile 65536
3.vi /etc/security/limits.d/90-nproc.conf es 启动时的线程池最低容量
修改下述内容
* soft nproc 4096
root soft nproc unlimited
4.vi /etc/sysctl.conf
新增下述内容
vm.max_map_count=655360
使用命令,让 sysctl 配置生效
sysctl -p
修改 elasticsearch 的配置文件,设置可访问的客户端。0.0.0.0 代表任意客户端访问。
vi config/elasticsearch.yml
修改下述内容
etwork.host: 0.0.0.0
http.port: 9200
---------------------------------创建用户-------------------------------------
从 5.0 开始,ElasticSearch 安全级别提高了,不允许采用 root 帐号启动,所以我们要添加一个用户。
1.创建 elk 用户组
groupadd elk
2.创建用户 oldlu
useradd oldlu
passwd oldlu
3.将 oldlu 用户添加到 elk 组
usermod -G elk oldlu
4.设置 sudo 权限
visudo
找到 root ALL=(ALL) ALL 一行,添加 oldlu 用户,如下。
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
找到后添加如下内容
oldlu ALL=(ALL) ALL
5.为用户分配权限
chown -R oldlu:elk /usr/local/elasticsearch
--------------------------------切换用户 oldlu-------------------------------------
1.ElasticSearch 启动与停止
1.启动
./elasticsearch -d 后置启动(前置启动去了-d)
2.验证
curl http://192.168.70.140:9200 后置启动的访问方法
http://192.168.70.140:9200/ 前置启动需要用浏览器访问
![](https://i-blog.csdnimg.cn/blog_migrate/b81e7f074f7f8492ec348a7de698bf17.png)
![](https://i-blog.csdnimg.cn/blog_migrate/2963cde92cdf01872783406c6979e234.png)
2
安装
Head
插件
安装步骤
--------------------------------安装 Head 插件--------------------------------------
1.Head 插件简介
ElasticSearch-head 是一个 H5 编写的 ElasticSearch 集群操作和管理工具,可以对集群进行傻瓜式操作。
它提供了:
a.索引和节点级别操作
b.搜索接口能够查询集群中原始 json 或表格格式的检索数据
c.能够快速访问并显示集群的状态
---------------------------------切换用户 root-------------------------------------
2.安装 NodeJS
#要求在 root 下执行
curl -sL https://rpm.nodesource.com/setup_8.x | bash -
yum install -y nodejs
3.安装 npm
npm install -g cnpm --registry=https://registry.npm.taobao.org
4.使用 npm 安装 grunt
npm install -g grunt
npm install -g grunt-cli --registry=https://registry.npm.taobao.org --no-proxy
--------------------------------切换用户 oldlu------------------------------------
5.查看以上版本
node -v
npm -v
grunt -version
6.下载 head 插件源码
cd /home/oldlu
mkdir es
wget https://github.com/mobz/elasticsearch-head/archive/master.zip
unzip master.zip
7.国内镜像安装
cd elasticsearch-head-master
sudo npm install -g cnpm --registry=https://registry.npm.taobao.org
sudo cnpm install
------------------------------------配置-------------------------------------------
1.配置 ElasticSearch,使得 HTTP 对外提供服务
vi config/elasticsearch.yml
添加如下内容
# 增加新的参数,这样 head 插件可以访问 es。设置参数的时候:后面要有空格
http.cors.enabled: true
http.cors.allow-origin: "*"
2.修改 Head 插件配置文件
vi Gruntfile.js
找到 connect:server,添加 hostname 一项,如下
connect: {
server: {
options: {
hostname: '0.0.0.0',
port: 9100,
base: '.',
keepalive: true
}
}
}
六、启动
1.重启 elasticsearch
./elasticsearch -d (在你新建的用户下面运行的,不是root用户,如果你在root用户运行了,那么杀死进程之后,去看看logs下面的日志所属用户是否还是你新建的普通用户,如果不是,再将
chown -R oldlu:elk /usr/local/elasticsearch
运行一遍,这个命令是在root用户下运行的,如果不改日志的所属用户,启动会报错)
2.启动 head
elasticsearch-head-master 目录下:
grunt server
或
npm run start
3.访问 9100 端口
七、简单应用
1.创建索引
curl -XPUT http://192.168.70.140:9200/applog
2.查看 head 变化
http://192.168.70.140:9100/
![](https://i-blog.csdnimg.cn/blog_migrate/3b0a9a4c50bc382537665114ba415d0c.png)
安装
Logstash
安装步骤
1.下载压缩
wget https://artifacts.elastic.co/downloads/logstash/logstash-6.2.3.tar.gz
tar zxvf logstash-6.2.3.tar.gz
mv logstash-6.2.3/ logstash
2.测试
./bin/logstash -e 'input { stdin { } } output { stdout {} }'
3.修改配置
在 logstash 的主目录下
vim config/log_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
tcp {
mode => "server"
host => "192.168.70.140"
port => 9250
}
}
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 => "192.168.70.140:9200" #ElasticSearch host, can be array.
index => "applog" #The index to write data to.
}
}
2.启动
./bin/logstash -f config/log_to_es.conf
或 后台运行守护进程
./bin/logstash -f config/log_to_es.conf &
3.测试
curl 'http://192.168.70.140:9200/_search?pretty'
![](https://i-blog.csdnimg.cn/blog_migrate/6f8732656262a491dbb15e7cac50e86b.png)
4
安装
Kibana
安装步骤
1.下载压缩
wget https://artifacts.elastic.co/downloads/kibana/kibana-6.2.3-linux-x86_64.tar.gz
tar zxvf kibana-6.2.3-linux-x86_64.tar.gz
mv kibana-6.2.3-linux-x86_64.tar.gz/ kibana
2.修改配置
vim config/kibana.yml
把以下注释放开,使配置起作用。
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.url: http://192.168.70.140:9200
kibana.index: ".kibana"
3.启动
./bin/kibana
4.测试
http://192.168.70.140:5601/app/kibana