传送门:
ELKstack 之 Elasticsearch【一】
ELKstack 之 Elastic集群主从【二】
ELKstack 之 Logstach 【三】
ELKstack 之 Kibana【四】
ELKstack 之 head 插件
ELKstack 之 监控Elastic集群状态【五】
ELKstack 之 Filebeat收集json格式日志【六】
Elkstack 之 消息解耦 redis【七】
ELKstack 之 权限验证(X-pack或nginx)【八】
ELKstack 之 License过期重签 【九】
Elkstack 之 Metricbeat 监控 【十】
ELK+Filebeat 收集多项目日志实战【十一】
一、简介
ELK由Elasticsearch、Logstash和Kibana三部分组件组成;
Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。
Logstash是一个完全开源的工具,它可以对你的日志进行收集、分析,并将其存储供以后使用
kibana 是一个开源和免费的工具,它可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助您汇总、分析和搜索重要数据日志。
redis 缓存或中间件 本次没有添加,如果您的业务需要,自己加入即可,主要功能 解耦 异步提高效率。
也可以对 Kubernetes 集群进行日志的收集,具体的应用,请查阅K8S专栏。
二、Elkstack 常见架构
1 Elasticsearch + Logstash + Kibana
- 这是一种最
简单
的架构。这种架构,通过logstash收集日志,Elasticsearch分析日志,然后在Kibana(web界面)中展示。这种架构虽然是官网介绍里的方式,但是往往在生产中很少使用。
2 Elasticsearch + Logstash + filebeat + Kibana
- 本次部署采用这种架构,架构图见文章头部
- 与上一种架构相比,这种架构增加了一个
filebeat
模块。filebeat是一个轻量的日志收集代理,用来部署在客户端,优势是消耗非常少的资源(较logstash), 所以生产中,往往会采取这种架构方式,但是这种架构有一个缺点,当logstash出现故障, 会造成日志的丢失。
3 Elasticsearch + Logstash + filebeat + redis
- Kibana+Elasticsearch + Logstash + filebeat + redis(也可以是其他中间件,比如kafka)
- 这种架构是上面那个架构的完善版,通过
增加中间件
,来避免数据的丢失。当Logstash出现故障,日志还是存在中间件中,当Logstash再次启动,则会读取中间件中积压的日志。目前我司使用的就是这种架构,我个人也比较推荐这种方式。
三、核心组件
- Logstash: logstash server端用来搜集日志;
-
Elasticsearch: 存储各类日志;
-
Kibana: web化接口用作查寻和可视化日志;
-
Filebeat是一个日志文件托运工具,在你的服务器上安装客户端后,filebeat会监控日志目录或者指定的日志文件,追踪读取这些文件(追踪文件的变化,不停的读),并且转发这些信息到elasticsearch或者logstarsh中存放。
四、ELKstack 自动化部署
4.1 环境规划
hostname | IP地址 | 身份 | 配置 |
---|---|---|---|
elk-server | 192.168.56.13 | ELK服务端,接收日志,提供日志搜索服务 | 2C4G |
nginx-server | 192.168.56.11 | Nginx服务端访问日志通过filebeat上报到Logstash | 2C2G |
[root@dwt-node1 home]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@dwt-node1 home]# uname -a
Linux dwt-node1 3.10.0-693.2.2.el7.x86_64 #1 SMP Tue Sep 12 22:26:13 UTC 2017 x86_64 x86_64 x86_64 GNU/Linu
2C4G
4.1 规范脚本存储目录
[root@node02 ~]# mkdir /server/scripts -p
[root@node02 ~]# cd /server/scripts/
[root@node02 scripts]# vim elkstack_install.sh
[root@node02 scripts]# chmod +x elkstack_install.sh
echo "192.168.56.13 elk-server" >>/etc/hosts
注意:服务器配置 最低2c4G
,如果配置不行导致elasticsearch启动不了,不要提问;vmware 慎用 我的没起来
4.2 Master 脚本自动化部署
1.将127.0.0.1 修改为master主机地址,除了定义的变量,只要修改配置文件中的地址
2.如果下载速度慢,请到QQ群共享下载软件包 ,上传到/usr/local/software
目录下,并注销掉脚本wget_fun
即可。
cat /server/scripts/elkstack_install.sh
#如果您想自定义,可以将 url
替换成您的软件版本。
#!/bin/bash
#author:Qiuyuetao
#blog:www.dgstack.cn
#mail:598759292@qq.com
#data:2019/1/1
#AutoInstall ELK scripts
#Software:elasticsearch-6.2.3|logstash-6.2.3|filebeat-6.2.3|kibana-6.2.3
clear
echo "#############################################################################"
echo "# Auto Install ELK. ##"
echo "# Press Ctrl+C to cancel ##"
echo "# Any key to continue ##"
echo "# Softwae:elasticsearch-6.2.3|logstash-6.2.3|filebeat-6.2.3|kibana-6.2.3 ##"
echo "#############################################################################"
read -n 1
software_dir="/usr/local/software"
elasticsearch_url="https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.3.tar.gz"
kibana_url="https://artifacts.elastic.co/downloads/kibana/kibana-6.2.3-linux-x86_64.tar.gz"
logstash_url="https://artifacts.elastic.co/downloads/logstash/logstash-6.2.3.tar.gz"
filebeat_url="https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.2.3-linux-x86_64.tar.gz"
sys_version=`cat /etc/redhat-release |awk '{print $4}'|cut -d. -f1`
IP=`ip addr|grep "inet "|grep -v 127.0.0.1|awk '{print $2}'|cut -d/ -f1`
jvm_conf="/usr/local/elasticsearch/config/jvm.options"
sys_mem=`free -m|grep Mem:|awk '{print $2}'|awk '{sum+=$1} END {print sum/1024}'|cut -d. -f1`
#wget software
wget_fun() {
if [ ! -d ${software_dir} ];then
mkdir -p ${software_dir} && cd ${software_dir}
else
cd ${software_dir}
fi
for software in $elasticsearch_url $kibana_url $logstash_url $filebeat_url
do
wget -c $software
done
clear
}
#initial system:install java wget;set hostname;disable firewalld
init_sys() {
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
[ "${sys_version}" != "7" ] && echo "Error:This Scripts Support Centos7.xx" && exit 1
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
setenforce 0
yum install -y java-1.8.0-openjdk wget
hostnamectl set-hostname elk-server
systemctl stop firewalld
cat >>/etc/security/limits.conf<<EOF
* soft nofile 65536
* hard nofile 65536
* soft nGproc 65536
* hard nproc 65536
EOF
}
#install elasticsearch
install_elasticsearch() {
cd $software_dir
tar zxf elasticsearch-6.2.3.tar.gz
mv elasticsearch-6.2.3 /usr/local/elasticsearch
mkdir -p /usr/local/elasticsearch/data /usr/local/elasticsearch/logs
useradd elasticsearch
chown -R elasticsearch:elasticsearch /usr/local/elasticsearch
echo "vm.max_map_count = 655360" >>/etc/sysctl.conf && sysctl -p
#不开启默认1g
#if [ ${sys_mem} -eq 0 ];then
# sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx512m"#g" ${jvm_conf}
# sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms512m"#g" ${jvm_conf}
#else
# sed -i "s#`grep "^-Xmx" ${jvm_conf}`#"-Xmx${sys_mem}g"#g" ${jvm_conf}
# sed -i "s#`grep "^-Xms" ${jvm_conf}`#"-Xms${sys_mem}g"#g" ${jvm_conf}
#fi
cat >>/usr/local/elasticsearch/config/elasticsearch.yml<<EOF
cluster.name: my-application
node.name: elk-server
path.data: /usr/local/elasticsearch/data
path.logs: /usr/local/elasticsearch/logs
network.host: 192.168.56.13
http.port: 9200
discovery.zen.ping.unicast.hosts: ["elk-server"]
EOF
su - elasticsearch -c "/usr/local/elasticsearch/bin/elasticsearch -d"
}
#install logstash
install_logstash() {
cd $software_dir
tar -zxf logstash-6.2.3.tar.gz
mv logstash-6.2.3 /usr/local/logstash
cat>/usr/local/logstash/config/01-syslog.conf<<EOF
input {
beats {
port => "5044"
codec => "json" #如果不是请取消
}
}
output {
elasticsearch {
hosts => "192.168.56.13:9200"
index => "nginx-%{+YYYY.MM.dd}" #如果不需要自定义,请取消
}
stdout { codec => rubydebug }
}
EOF
nohup /usr/local/logstash/bin/logstash -f /usr/local/logstash/config/01-syslog.conf & >/dev/null
}
#install filebeat
install_filebeat() {
cd $software_dir
tar -zxf filebeat-6.2.3-linux-x86_64.tar.gz
mv filebeat-6.2.3-linux-x86_64 /usr/local/filebeat
cat >/usr/local/filebeat/filebeat.yml<<EOF
filebeat.prospectors:
- input_type: log
paths:
- /var/log/nginx/*.log
output.logstash:
hosts: ["192.168.56.13:5044"]
#filebeat.prospectors: #下面是java版本的简单过滤规则
#- input_type: log
# multiline:
# pattern: '^\['
# negate: true
# match: after
# paths:
# - /root/.gosuv/log/fuqinfinance-client-api/output.log
#
#output.logstash:
# hosts: ["172.17.92.43:5044"]
# enabled: true
# compression_level: 3
EOF
cd /usr/local/filebeat/
nohup /usr/local/filebeat/filebeat & >/dev/null
}
#install kibana
install_kibana() {
cd $software_dir
tar -zxf kibana-6.2.3-linux-x86_64.tar.gz
mv kibana-6.2.3-linux-x86_64 /usr/local/kibana
cat >> /usr/local/kibana/config/kibana.yml <<EOF
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.url: "http://192.168.56.13:9200"
EOF
nohup /usr/local/kibana/bin/kibana & >/dev/null
}
check() {
port=$1
program=$2
check_port=`netstat -lntup|grep ${port}|wc -l`
check_program=`ps -ef|grep ${program}|grep -v grep|wc -l`
if [ $check_port -gt 0 ] && [ $check_program -gt 0 ];then
action "${program} run is ok!" /bin/true
else
action "${program} run is error!" /bin/false
fi
}
main() {
init_sys
wget_fun
install_elasticsearch
install_filebeat
install_logstash
install_kibana
echo -e "33[32m Checking Elasticsearch...33[0m"
sleep 20
check :9200 "elasticsearch"
echo -e "33[32m Checking Logstash...33[0m"
sleep 20
check ":9600" "logstash"
echo -e "33[32m Checking Kibana...33[0m"
sleep 20
check ":5601" "kibana"
action "ELK install is success!" /bin/true
echo "url:http://$IP:5601"
}
main
4.3 执行脚本
sh /server/scripts/elkstack_install.sh
#运行后 根据提示操作即可,按[1]立即安装。
#如果脚本提示 失败,但是端口启动了,建议您,重启下服务器,在逐一的启动即可。
4.4 Web访问
查看ElasticSearch 状态 http://192.168.56.13:9200/
elasticsearch日志
tail -f /usr/local/elasticsearch/logs/elasticsearch.log
查看kibana状态 http://192.168.56.13:5601
logstach日志
tail -f /usr/local/logstash/logs/logstash-plain.log
至此,ELK服务启动成功
,接下来我们将业务日志上报上来:nginx-server;
五、nginx-server 节点配置
1,安装nginx,产生日志
yum install nginx
systemctl start nginx.service
#由于换了个机器,log日志变更为 /var/log/nginx
2,配置filebeat
收集日志即可。
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.2.3-linux-x86_64.tar.gz
tar -zxf filebeat-6.2.3-linux-x86_64.tar.gz
mv filebeat-6.2.3-linux-x86_64 /usr/local/filebeat
cat >/usr/local/filebeat/filebeat.yml<<EOF
filebeat.prospectors:
- input_type: log
paths:
- /var/log/nginx/*.log
output.logstash:
hosts: ["192.168.56.13:5044"]
EOF
cd /usr/local/filebeat/
nohup /usr/local/filebeat/filebeat & >/dev/null
## 第一行定义抓取日志类型 log
## 第二行 定义要抓取的日志文件 可以用正则
## 第三行 输出到 logstach,并配置地址及端口
## 最后 启动filebeat
六、Kibana web页面配置
6.1 创建Index Patterns
点击左上角的Discover,如下图红框,可以看到访问日志已经被ELK搜集了:
6.2 如下图,输入logstash-*
,点击”Next step”:
6.3 如下图,选择Time Filter
,再点击“Create index pattern”:
6.4 页面提示创建Index Patterns
成功:
6.5 点击左上角的”Discover
”按钮,即可看到最新的日志信息,如下图:
七、elasticsearch6.x 插件之head
插件是为了完成不同的功能,官方提供了一些插件但大部分是收费的,另外也有一些开发爱好者提供的插件,可以实现对elasticsearch集群的状态监控与管理配置等功能。
在elasticsearch 5.x版本以后不再支持直接安装head插件,而是需要通过启动一个服务方式,git地址:https://github.com/mobz/elasticsearch-head
7.1 Google Chrome Head插件
Google Chrome 也可以搜所head 插件
按包装即可,前提需要翻墙
7.2 服务器安装head插件
[root@linux-host1 ~]# yum install -y npm
# NPM的全称是Node Package Manager,是随同NodeJS一起安装的包管理和分发工具,它很方便让JavaScript开发者下载、安装、上传以及管理已经安装的包。
[root@linux-host1 ~]# cd /usr/local/src/
[root@linux-host1 src]# git clone git://github.com/mobz/elasticsearch-head.git
[root@linux-host1 src]# cd elasticsearch-head/
[root@linux-host1 elasticsearch-head]# npm install grunt -save
[root@linux-host2 elasticsearch-head]# ll node_modules/grunt #确认生成文件
[root@linux-host1 elasticsearch-head]# npm install #执行安装
[root@linux-host1 elasticsearch-head]# cd /usr/local/src/elasticsearch-head && npm run start & #后台启动head插件服务
7.2 修改elasticsearch服务配置文件:
开启跨域访问支持,然后重启elasticsearch服务:
vi /usr/local/elasticsearch/config/elasticsearch.yml
http.cors.enabled: true #最下方添加,开启http访问,并授权所有可以访问。
http.cors.allow-origin: "*"
ps -ef|grep elasticsearch|awk '{print $2}'|xargs kill #关闭
su - elasticsearch -c "/usr/local/elasticsearch/bin/elasticsearch -d #开启
然后重新连接:
7.3 测试提交数据:
1) 查看数据:
2) 验证索引是否存在
7.4 Master与Slave的区别:
Master的职责:
统计各node节点状态信息、集群状态信息统计、索引的创建和删除、索引分配的管理、关闭node节点等
Slave的职责:
同步数据、等待机会成为Master
八、elasticsearch插件之kopf:
Git地址为:https://github.com/lmenezes/elasticsearch-kopf
但是目前还不支持6.x版本的elasticsearch,但是可以安装在elasticsearc 1.x或2.x的版本安装。
九、监控elasticsearch集群状态
9.1 通过shell获取集群状态
curl –sXGET http://192.168.56.11:9200/_cluster/health?pretty=true
获取到的是一个json格式的返回值,那就可以通过python对其中的信息进行分析,例如对status进行分析,如果等于green(绿色)就是运行在正常,等于yellow(黄色)表示副本分片丢失,red(红色)表示主分片丢失
9.2 python脚本:
[root@linux-host1 ~]# cat els-cluster-monitor.py
#!/usr/bin/env python
#coding:utf-8
#Author qiuyuetao
#import smtplib
#From email.mime.text import MIMEText
#from email.utils import formataddr
import subprocess
body = ""
false="false"
obj = subprocess.Popen(("curl -sXGET http://192.168.56.11:9200/_cluster/health?pretty=true"),shell=True, stdout=subprocess.PIPE)
data = obj.stdout.read()
data1 = eval(data)
status = data1.get("status")
if status == "green":
print "50"
else:
print "100"
#脚本执行结果:
[root@linux-host1 ~]# python els-cluster-monitor.py
50
通过数字的判断就会知道集群的状态,还可以接入zabbix进行监控。
完!!!
更多精彩:Elkstack学习专栏
十、报错总结
4.1【ElasticSearch故障处理】OpenJDK 64-Bit Server VM warning
VM虚拟机安装ElasticSearch集群,有三台Linux,完成安装后,执行启动命令:bin/elasticsearch -d 。 运行结果报错:
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
原因就是资源不足
建议还是不要使用虚拟机脚本自动部署,elasticsearch会占用大量内存,swap、CPu资源,最终导致ES无法启动,系统oom自动kill掉进程。
- QQ精品交流群
-
- 微信公众号
-
2019年1月12日 上午9:41 沙发
博主🐂
2019年1月12日 上午9:42 板凳
软件包在群里 怎么找?
2019年1月14日 上午9:34 1层
@哥哥的人 QQ群有共享包,搜索elkstack即可
2019年2月20日 下午7:24 地板
找了好久,这个真的帮我大忙了
2019年2月21日 上午9:36 1层
@朋友圈 感谢您的评价,有哪些不足,还请您指出
2019年2月23日 上午8:38 4楼
哈哈,比官网的都强

2019年2月25日 下午1:49 1层
@绝代佳人 说笑了,我都是和官网学习,感谢您的支持。
2019年4月16日 下午5:47 5楼
很好的文章
2019年4月24日 下午4:06 1层
@上海-lsof 谢谢,我会努力
2019年5月16日 下午1:10 6楼
先mark 慢慢学习
感谢分享
2019年5月16日 下午1:40 1层
@hostdare 欢迎加群,随时骚扰。
2019年6月28日 下午4:14 7楼
你这个脚本中可以加入screen
2019年11月25日 下午5:47 8楼
5分钟就搞定了Elk,一键部署帮了我大忙了,感谢群主分享,多多分享高质量博文
2019年12月10日 上午8:01 9楼
群里面没找到部署脚本啊
2019年12月12日 上午10:05 1层
@wlibai 群里分享内容太老删除了,在博客复制即可。
2020年6月28日 上午10:04 2层
@admin 怎么复制不了呢
2020年9月24日 上午11:30 3层
@星星 注册个账号,登陆即可。
2021年6月5日 下午5:26 10楼
麻雀虽小五脏俱全