众所周知,ELK是日志收集套装,这里就不多做介绍了。

画了一个粗略的架构图,如下:

这里实际用了三个节点,系统版本为CentOS6.6,ES版本为2.3.5,logstash版本为2.4.0,kibana版本为4.5.4-1,nginx版本为1.8.1。

192.168.3.56    ES01+logstash01+kibana+redis+nginx
192.168.3.49 ES02+logstash02
192.168.3.57 ES03

1、为三个节点安装java环境

# yum install -y java java-1.8.0-openjdk-devel
# vim /etc/profile.d/java.sh
export JAVA_HOME=/usr
# source /etc/profile.d/java.sh

2、三节点同步时间

# ntpdate pool.ntp.org

3、安装elasticsearch集群,配置集群很简单,三节点保持集群名称相同即可,rpm包是提前在官网下载的

节点1,ES01:

# yum install -y elasticsearch-2.3.5.rpm

# vim /etc/elasticsearch/elasticsearch.yml
cluster.name: oupenges
node.name: es01
network.host: 192.168.3.56
discovery.zen.ping.unicast.hosts: ["192.168.3.56", "192.168.3.49", "192.168.3.57"]

节点2,ES02:

# yum install -y elasticsearch-2.3.5.rpm

# vim /etc/elasticsearch/elasticsearch.yml
cluster.name: oupenges
node.name: es02
network.host: 192.168.3.49
discovery.zen.ping.unicast.hosts: ["192.168.3.56", "192.168.3.49", "192.168.3.57"]

节点3,ES03:

# yum install -y elasticsearch-2.3.5.rpm

# vim /etc/elasticsearch/elasticsearch.yml
cluster.name: oupenges
node.name: es03
network.host: 192.168.3.57
discovery.zen.ping.unicast.hosts: ["192.168.3.56", "192.168.3.49", "192.168.3.57"]

启动服务:

# service elasticsearch start
# chkconfig elasticsearch on

通过cluster API查看集群状态:

# curl -XGET 'http://192.168.3.56:9200/_cluster/health?pretty=true'
{
"cluster_name" : "oupenges",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 3,
"active_primary_shards" : 56,
"active_shards" : 112,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}

4、为ES三个节点安装head插件

# /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head

用浏览器访问head:

这个是我装完所有组件之后的状态,后面装完之后就不再贴head图了。

星形代表master

圆形代表slave

5、在节点1上安装logstash01

# yum install logstash-2.4.0.noarch.rpm

命令行验证logstash:

标准输入 --> 标准输出

# /opt/logstash/bin/logstash -e "input {stdin{}} output{stdout{ codec=>"rubydebug"}}"
Settings: Default pipeline workers: 12
Pipeline main started
hello
{
"message" => "hello",
"@version" => "1",
"@timestamp" => "2017-06-20T03:09:21.113Z",
"host" => "uy-s-167"
}

标准输入 --> elasticsearch

# /opt/logstash/bin/logstash -e 'input {stdin{}} output{ elasticsearch { hosts => ["192.168.3.56:9200"] index => "test"}}'
Settings: Default pipeline workers: 12
Pipeline main started
hello
hi opera



从时间和内容可以看出,红色框的两条是我刚才添加的两条信息。

6、安装kibana

# yum install -y kibana-4.5.4-1.x86_64.rpm

# vim /opt/kibana/config/kibana.yml
elasticsearch.url: "http://192.168.3.56:9200" # service kibana start
# chkconfig kibana on

用浏览器访问 http://192.168.3.56:5601

7、安装redis

# yum install -y redis

# vim /etc/redis.conf
daemonize yes
bind 192.168.3.56
appendonly yes # service redis start
# chkconfig redis on

8、安装Nginx,使用nginx代理kibanna,并设置添加身份验证

# wget http://nginx.org/download/nginx-1.8.1.tar.gz
# tar xvf nginx-1.8.1.tar.gz # yum groupinstall -y "Development tools"
# cd nginx-1.8.1/
# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre # mkdir -pv /var/tmp/nginx/client/
# /usr/local/nginx/sbin/nginx # vim /usr/local/nginx/conf/nginx.conf 在http段添加一个server段
server {
listen 8080;
server_name 192.168.3.56; #当前主机名
auth_basic "Restricted Access";
auth_basic_user_file /usr/local/nginx/conf/htpasswd.users; #身份验证
location / {
proxy_pass http://192.168.3.56:5601; #代理到kibana
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
} # yum install -y httpd-tools
# htpasswd -bc /usr/local/nginx/conf/htpasswd.users admin admin
# cat /usr/local/nginx/conf/htpasswd.users
admin:TvypNSDg6V3Rc # /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload

9、将Nginx的日志格式转换为json格式

# vim /usr/local/nginx/conf/nginx.conf
log_format access1 '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"status":"$status"}';
access_log /var/log/nginx/access.log access1; # /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload

10、在需要收集日志也就是nginx server上安装filebeat

# yum install -y filebeat-1.2.3-x86_64.rpm

# mv /etc/filebeat/filebeat.yml /etc/filebeat/filebeat.yml.bak
# vim /etc/filebeat/filebeat.yml
filebeat:
prospectors:
-
paths:
- /var/log/messages
input_type: log
document_type: nginxs1-system-message
-
paths:
- /var/log/nginx/access.log
input_type: log
document_type: nginxs1-access-log
registry_file: /var/lib/filebeat/registry
output:
logstash:
hosts: ["192.168.3.56:5044"]
file:
path: "/tmp/"
filename: filebeat.txt
shipper:
logging:
to_files: true
files:
path: /tmp/mybeat # service filebeat start
# chkconfig filebeat on

11、配置logstash01接收filebeat发出的日志,并输出到redis

# vim /etc/logstash/conf.d/nginx.conf
input {
beats {
port => 5044
codec => "json"
}}
output {
if [type] == "nginxs1-system-message" {
redis {
data_type => "list"
key => "nginxs1-system-message"
host => "192.168.3.56"
port => "6379"
db => "0"
}}
if [type] == "nginxs1-access-log" {
redis {
data_type => "list"
key => "nginxs1-access-log"
host => "192.168.3.56"
port => "6379"
db => "0"
}}
file {
path => "/tmp/nginx-%{+YYYY-MM-dd}messages.gz"
}
} # /etc/init.d/logstash configtest
# service logstash restart

12、在节点2上安装logstash02

# yum install logstash-2.4.0.noarch.rpm

13、配置logstash02从redis读取日志,并输出到elasticsearch中

# vim /etc/logstash/conf.d/redis-to-es.conf
input {
redis {
host => "192.168.3.56"
port => "6379"
db => "0"
key => "nginxs1-system-message"
data_type => "list"
batch_count => 1
}
redis {
host => "192.168.3.56"
port => "6379"
db => "0"
key => "nginxs1-access-log"
data_type => "list"
codec => "json"
batch_count => 1
}
}
output {
if [type] == "nginxs1-system-message" {
elasticsearch {
hosts => ["192.168.3.56:9200"]
index => "nginxs1-system-message-%{+YYYY.MM.dd}"
manage_template => true
flush_size => 2000
idle_flush_time => 10 }}
if [type] == "nginxs1-access-log" {
elasticsearch {
hosts => ["192.168.3.56:9200"]
index => "logstash-nginxs1-access-log-%{+YYYY.MM.dd}"
manage_template => true
flush_size => 2000
idle_flush_time => 10 }}
}

14、登录配置kibana

配置完成后,就可以在Discover中看到nginx的日志了。

在Visualize里面可以画各种图,这里就不细说了。

展示一个我画的很简单的Dashboard:

搭建ELK收集Nginx日志的更多相关文章

  1. Docker 部署 ELK 收集 Nginx 日志

    一.简介 1.核心组成 ELK由Elasticsearch.Logstash和Kibana三部分组件组成: Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引 ...

  2. ELK 二进制安装并收集nginx日志

    对于日志来说,最常见的需求就是收集.存储.查询.展示,开源社区正好有相对应的开源项目:logstash(收集).elasticsearch(存储+搜索).kibana(展示),我们将这三个组合起来的技 ...

  3. 使用Docker快速部署ELK分析Nginx日志实践(二)

    Kibana汉化使用中文界面实践 一.背景 笔者在上一篇文章使用Docker快速部署ELK分析Nginx日志实践当中有提到如何快速搭建ELK分析Nginx日志,但是这只是第一步,后面还有很多仪表盘需要 ...

  4. ELK Stack (2) —— ELK + Redis收集Nginx日志

    ELK Stack (2) -- ELK + Redis收集Nginx日志 摘要 使用Elasticsearch.Logstash.Kibana与Redis(作为缓冲区)对Nginx日志进行收集 版本 ...

  5. ELK收集Nginx|Tomcat日志

    1.Nginx 日志收集,先安装Nginx cd /usr/local/logstash/config/etc/,创建如下配置文件,代码如下 Nginx.conf input { file { typ ...

  6. ELK日志系统之使用Rsyslog快速方便的收集Nginx日志

    常规的日志收集方案中Client端都需要额外安装一个Agent来收集日志,例如logstash.filebeat等,额外的程序也就意味着环境的复杂,资源的占用,有没有一种方式是不需要额外安装程序就能实 ...

  7. ELK filter过滤器来收集Nginx日志

    前面已经有ELK-Redis的安装,此处只讲在不改变日志格式的情况下收集Nginx日志. 1.Nginx端的日志格式设置如下: log_format access '$remote_addr - $r ...

  8. 使用Docker快速部署ELK分析Nginx日志实践

    原文:使用Docker快速部署ELK分析Nginx日志实践 一.背景 笔者所在项目组的项目由多个子项目所组成,每一个子项目都存在一定的日志,有时候想排查一些问题,需要到各个地方去查看,极为不方便,此前 ...

  9. 利用ELK分析Nginx日志生产实战(高清多图)

    本文以api.mingongge.com.cn域名为测试对象进行统计,日志为crm.mingongge.com.cn和risk.mingongge.com.cn请求之和(此二者域名不具生产换环境统计意 ...

随机推荐

  1. 如何用Python为你的邮箱加油?还有这种操作!

    我来介绍一下我是如何使用 Python 来节省成本的. 我最近在开一辆烧 93 号汽油的车子.根据汽车制造商的说法,它只需要加 91 号汽油就可以了.然而,在美国只能买到 87 号.89 号.93 号 ...

  2. golang--性能测试和分析

    前言 测试分为:压力测试.负载测试.性能测试,功能测试等等,其中在开发过程中开发人员经常要写一些test case unit 自己的模块进行功能测试测和性能.在分析出模块的性能瓶颈后开发人员就需要针对 ...

  3. rename命令详解

    基础命令学习目录首页 原文链接:http://man.linuxde.net/rename 将main1.c重命名为main.c rename main1.c main.c main1.c renam ...

  4. bg,fg,job命令详解

    基础命令学习目录首页 原文链接:http://www.cnblogs.com/chjbbs/p/6307333.html linux提供的fg和bg命令,可以让我们轻松调度正在运行的任务 假如你发现前 ...

  5. oracle删除死锁进程

    在命令行下运行: select SID,SERIAL# from v$session t1, v$locked_object t2 where t1.sid = t2.SESSION_ID; alte ...

  6. Scrum立会报告+燃尽图(十二月五日总第三十六次):Final阶段分配任务

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...

  7. 《Spring2之站立会议7》

    <Spring2之站立会议7> 昨天,查相关资料解决debug:: 今天,解决了debug: 遇到问题,一些问题是得到解决了,但是一些还未被解决.

  8. WebGL学习笔记三

    在上一章中主要说明了通过矩阵来实现平面图形的平移.旋转.缩放,到最后完全可以用4*4矩阵实现所有的动作,在本章就是第四章主要是对矩阵进行了封装,其WebGL的流程和上一章大部分大部分相同,定义可以在w ...

  9. 项目Beta冲刺(团队)第六天

    1.昨天的困难 可以获得教务处通知栏的15条文章数据了,但是在显示的时候出了问题. 私信聊天的交互还没研究清楚 2.今天解决的进度 成员 进度 陈家权 研究私信模块 赖晓连 研究问答模块 雷晶 研究服 ...

  10. unix网络编程——TCP套接字编程

    TCP客户端和服务端所需的基本套接字.服务器先启动,之后的某个时刻客户端启动并试图连接到服务器.之后客户端向服务器发送请求,服务器处理请求,并给客户端一个响应.该过程一直持续下去,直到客户端关闭,给服 ...