ELK的快速搭建

环境的准备

192.168.177.5:ES、ES-head

192.168.177.4 :Kibana、logstash、JDK1.8、Nginx

安装

docker安装es

拉取es镜像

docker pull elasticsearch:7.13.4

运行容器

docker run --name elasticsearch -d -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e "discovery.type=single-node" -p 9200:9200 -p 9300:9300 elasticsearch:7.13.4

docker安装es-head插件

docker pull mobz/elasticsearch-head:5

docker run -p 9100:9100 mobz/elasticsearch-head:5

 

 网页测试es和es-head运行

在谷歌输入IP+9200测试es运行 IP+9200测试插件

es和es-head启动无误

如果遇到es-head连接es失败

docker exec -it 容器ID /bin/bash

 

在此目录下编辑elasticsearch.yml文件

在elasticsearch.yml文件中加入
http.cors.enabled: true
http.cors.allow-origin: "*"

再重新启动es的容器

安装logstash

 在/etc/yum.repos.d
创建logstash.repo文件

加入以下代码
[logstash-7.x]
name=Elastic repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

yum install logstash -y

创建一个软连接,每次执行命令的时候不用在写安装路劲(默认安装在/usr/share下)
ln -s /usr/share/logstash/bin/logstash /bin

需要在/etc/logstash/conf.d/  创建elk文件elk.conf

[root@stt2 conf.d]# pwd
/etc/logstash/conf.d
[root@stt2 conf.d]# ls
access.log  elk.conf
[root@stt2 conf.d]# vim elk.conf 



input {
        file {
            path => "/var/log/messages"
            type => "system"
            start_position => "beginning"
            }


        file {
           path => "/var/log/secure"
           type => "secure"
           start_position => "beginning"
    }

        file {
           path => "/usr/local/webserver/nginx/logs/access.log_json"
           type => "nginx"
           start_position => "beginning"
    }

    }
output {
     if [type] == "system"{

          elasticsearch {
            hosts => ["192.168.177.5:9200"]
            index => "system-%{+YYYY.MM.dd}"
                }
        }


    if [type] == "secure" {

        elasticsearch {
            hosts => ["192.168.177.5:9200"]
            index => "nagios-secure-%{+YYYY.MM.dd}"
                }
        }


   if [type] == "nginx" {

        elasticsearch {
            hosts => ["192.168.177.5:9200"]
            index => "nagios-nginx-%{+YYYY.MM.dd}"
        }
    }

}

在elk.conf文件中添加以上配置文件

es的地址需要根据自己的实际地址填写已经日志的地址也需要更加自己实际的地址填写

配置Nginx日志输出为json格式

编辑Nginx.conf文件

      log_format json '{"@timestamp":"$time_iso8601",'   #配置NGINX的日志格式 json
                       '"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",'
                        '"xff":"$http_x_forwarded_for",'
                        '"referer":"$http_referer",'
                        '"agent":"$http_user_agent",'
                        '"status":"$status"}';
        access_log  /usr/local/webserver/nginx/logs/access.log_json json;

加入以上代码块

 /usr/local/webserver/nginx/logs/access.log_json 日志文件提前在目录中创建

配置成功后重启Nginx

[root@stt2 sbin]# ps -ef |grep nginx
root      23175      1  0 13:27 ?        00:00:00 nginx: master process ./nginx
root      23176  23175  0 13:27 ?        00:00:00 nginx: worker process
root      23186  17631  0 13:27 pts/1    00:00:00 grep --color=auto nginx
[root@stt2 sbin]# killall nginx 
[root@stt2 sbin]# ./nginx 
[root@stt2 sbin]# ps -ef |grep nginx
root      23264      1  0 13:27 ?        00:00:00 nginx: master process ./nginx
root      23265  23264  0 13:27 ?        00:00:00 nginx: worker process
root      23295  17631  0 13:27 pts/1    00:00:00 grep --color=auto nginx
[root@stt2 sbin]# 

在配置完成Nginx之后回到logstash配置文件目录

默认在/etc/logstash/conf.d

最后后台启动logstash

nohup logstash -f /etc/logstash/conf.d/elk.conf >access.log 2>&1 &

监控一下启动的日志tail -f access.log

 无报错即启动成功

安装kibana

wget https://artifacts.elastic.co/downloads/kibana/kibana-7.13.4-linux-x86_64.tar.gz
需要和es版本一致,本次操作下载的是7.13.4的es 若版本不一致会出现兼容性问题

解压kibana的tar包
# tar -xzf kibana-7.13.4-linux-x86_64.tar.gz

进入解压好的kibana
# mv kibana-7.13.4-linux-x86_64 /usr/local

创建kibana的软连接
# ln -s /usr/local/kibana-7.13.4-linux-x86_64/ /usr/local/kibana 

编辑kibana的配置文件
# vim /usr/local/kibana/config/kibana.yml

修改配置文件如下,开启以下的配置
server.port: 5601

server.host: "0.0.0.0"

elasticsearch.url: "http://装es的ip:9200"

kibana.index: ".kibana"

i18n.locale: "en" //并将en修改为zh-CN 设置为中文启动kibana

启动kibana

进入kibana安装的目录找到bin目录里面有kibana可执行文件
[root@stt2 bin]# ll
-rw-r--r-- 1 root root 1360024 12月  7 17:16 access.log
-rwxr-xr-x 1 root root     850 7月  15 02:07 kibana
-rwxr-xr-x 1 root root     783 7月  15 02:07 kibana-encryption-keys
-rwxr-xr-x 1 root root     776 7月  15 02:07 kibana-keystore
-rwxr-xr-x 1 root root     813 7月  15 02:07 kibana-plugin
[root@stt2 bin]# pwd
/usr/local/kibana-7.13.4-linux-x86_64/bin

[root@stt2 bin]# nohup ./kibana --allow-root > access.log 2>&1 &
[2] 26543
[root@stt2 bin]# 

采用后台启动kibana
本次操作采用的是root用户 所以得设置--allow-root参数
tail -f access.log 
日志无报错kibana启动成功

kibana的使用

在浏览器访问IP+5601

点击添加数据

找到logstash,完成默认步骤,会让你添加索引模式

查看到Nginx日志

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值