基于kafka,zookeeper实现的日志收集平台搭建

1.项目简介

时间:2022年7月    项目人员:5人
项目环境:centos7, kafka2.12 ,nginx, filebeat, zk, python3.6, mysql
项目描述:收集前端nginx集群访问日志,统一存入kafka平台,对nginx日志做清洗,获取流量信息存入数据库。
2.项目步骤:
      1、搭建前端nginx集群
      2、搭建好kafka集群
      3、搭建filebeat,调试生产者和消费者
      4、编写消费者,清洗nginx日志,收集带宽信息存入数据库
      5、基于流量进行告警监控 

3.项目部署

1、准备好3台虚拟机搭建nginx和kafka集群
2、配置好静态ip地址
    备注:服务: NetworkManger 或者 network   只运行一个
          启动/停止/重启: systemctl start/stop/restart NetworkManager
         
          systemctl管理的服务 配置文件在 /usr/lib/systemd/system 下, 以.service结尾的配置文件

          /etc/systemd/system/multi-user.target.wants  开机自启的服务配置目录


   配置好dns
   [root@nginx-kafka03 ~]# cat /etc/resolv.conf
    # Generated by NetworkManager
    nameserver 114.114.114.114

   dns解析:
      1、浏览器的缓存
      2、本地hosts文件  --linux(/etc/hosts)
      3、找本地域名服务器  -- linux(/etc/resolv.conf)
      
   
3、修改主机名
   vim  /etc/hosthname
   hostname -F /etc/hostname

4、每一台机器上都写好域名解析
   vim  /etc/hosts
   192.168.0.94  nginx-kafka01
   192.168.0.95  nginx-kafka02
   192.168.0.96  nginx-kafka03

5、安装基本软件
   yum install wget lsof vim -y

6、安装时间同步服务
    yum -y install chrony
    systemctl enable chronyd   #设置开机自启  disable 关闭开机自启
    systemctl start chronyd

    设置时区:
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

7、关闭防火墙
    关闭防火墙:
    [root@nginx-kafka01 ~]# systemctl stop firewalld
    [root@nginx-kafka01 ~]# systemctl disable firewalld

    关闭selinux:
    vim /etc/selinux/config
    SELINUX=disabled

        selinux关闭 需要重启机器

        [root@nginx-kafka03 ~]# getenforce
        Disabled

        selinux是linux系统内核里一个跟安全相关的子系统
        规则非常繁琐 一般日常工作里都是关闭的


#############nginx搭建
安装好epel源:
yum install epel-release -y
yum install  nginx -y

启动:systemctl start nginx
设置开机自启
systemctl enable nginx


#编辑配置文件
[root@nginx-kafka01 ~]# cd /etc/nginx/
[root@nginx-kafka01 nginx]# ls
conf.d        fastcgi.conf.default    koi-utf     mime.types.default  scgi_params          uwsgi_params.default
default.d     fastcgi_params          koi-win     nginx.conf          scgi_params.default  win-utf
fastcgi.conf  fastcgi_params.default  mime.types  nginx.conf.default  uwsgi_params

主配置文件: nginx.conf
...              #全局块

events {         #events块
   ...
}

http      #http块
{
    ...   #http全局块
    server        #server块
    { 
        ...       #server全局块
        location [PATTERN]   #location块
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局块
}

1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。

2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。

3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。

4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。

5、location块:配置请求的路由,以及各种页面的处理情况


#配置文件修改
vim   nginx.conf

将 
   listen       80 default_server;
修改成:
   listen       80;

vim  /etc/nginx/conf.d/sc.conf

server {
    listen 80 default_server;
    server_name  www.sc.com;

    root         /usr/share/nginx/html;

    access_log  /var/log/nginx/sc/access.log main;

    location  / {

    }
}


#语法检测
[root@nginx-kafka01 html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] open() "/var/log/nginx/sc/access.log" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed
[root@nginx-kafka01 html]# mkdir /var/log/nginx/sc
[root@nginx-kafka01 html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

#重新加载nginx
nginx -s  reload

nginx做web网站的话 只能够支持静态网页展示  -- html


###################kafka
1、安装:
安装java:yum install java wget  -y
安装kafka: wget   https://mirrors.bfsu.edu.cn/apache/kafka/2.8.1/kafka_2.12-2.8.1.tgz 
解包:
tar  xf  kafka_2.12-2.8.1.tgz
使用自带的zookeeper集群配置
安装zookeeper:
wget   https://mirrors.bfsu.edu.cn/apache/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3-bin.tar.gz

2、配置kafka
修改config /server.properties:
broker.id=0
listeners=PLAINTEXT://nginx-kafka01:9092
zookeeper.connect=192.168.0.94:2181,192.168.0.95:2181,192.168.0.96:2181

3、配置zk
进入/opt/apache-zookeeper-3.6.3-bin/confs
cp zoo_sample.cfg zoo.cfg
修改zoo.cfg, 添加如下三行:
server.1=192.168.0.94:3888:4888
server.2=192.168.0.95:3888:4888
server.3=192.168.0.96:3888:4888

3888和4888都是端口  一个用于数据传输,一个用于检验存活性和选举

创建/tmp/zookeeper目录 ,在目录中添加myid文件,文件内容就是本机指定的zookeeper id内容
如:在192.168.0.94机器上
echo 1 > /tmp/zookeeper/myid

启动zookeeper:
bin/zkServer.sh start


开启zk和kafka的时候,一定是先启动zk,再启动kafka
关闭服务的时候,kafka先关闭,再关闭zk

#查看
[root@nginx-kafka03 apache-zookeeper-3.6.3-bin]# bin/zkServer.sh status
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.6.3-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader

启动kafka:
bin/kafka-server-start.sh -daemon config/server.properties

zookeeper使用:
运行
bin/zkCli.sh

[zk: localhost:2181(CONNECTED) 1] ls /
[admin, brokers, cluster, config, consumers, controller, controller_epoch, feature, isr_change_notification, latest_producer_id_block, log_dir_event_notification, sc, zookeeper]
[zk: localhost:2181(CONNECTED) 2] ls /brokers/ids
[1, 2, 3]
[zk: localhost:2181(CONNECTED) 3] create /sc/yy
Created /sc/yy
[zk: localhost:2181(CONNECTED) 4] ls /sc
[page, xx, yy]
[zk: localhost:2181(CONNECTED) 5] set /sc/yy 90
[zk: localhost:2181(CONNECTED) 6] get /sc/yy
90

#测试
创建topic
bin/kafka-topics.sh --create --zookeeper 192.168.0.95:2181 --replication-factor 1 --partitions 1 --topic sc
查看topic
 bin/kafka-topics.sh --list --zookeeper 192.168.0.95:2181
创建生产者
[root@localhost kafka_2.12-2.8.0]# bin/kafka-console-producer.sh --broker-list 192.168.0.94:9092 --topic sc
>hello
>sanchuang tongle
>nihao
>world !!!!!!1
>
创建消费者
[root@localhost kafka_2.12-2.8.0]# bin/kafka-console-consumer.sh --bootstrap-server 192.168.0.96:9092 --topic sc --from-beginning


kafka的日志可以按照两个维度来设置清除
1、按时间  7天
2、按大小  

任意一个按时间或者按大小的条件满足,都可以触发日志清理


kafka日志保存时按段保存的,segment
假设有如下segment
00.log   11.log  22.log

00.log保存的是第一条到11条的日志
11.log保存的是第12条到第22条的日志
22.log保存的是第22条之后的日志


tmux同步的效果:
按 ctrl+b  再按:
接着输入指令:set-window-option synchronize-panes on

连接zk:
bin/zkCli.sh

[zk: localhost:2181(CONNECTED) 0] ls /
[admin, brokers, cluster, config, consumers, controller, controller_epoch, feature, isr_change_notification, latest_producer_id_block, log_dir_event_notification, zookeeper]
[zk: localhost:2181(CONNECTED) 1] ls /brokers
[ids, seqid, topics]
[zk: localhost:2181(CONNECTED) 2] ls /brokers/ids
[0, 1, 2]
[zk: localhost:2181(CONNECTED) 3] get /brokers/ids
null
[zk: localhost:2181(CONNECTED) 4] get /brokers/ids/0
{"listener_security_protocol_map":{"PLAINTEXT":"PLAINTEXT"},"endpoints":["PLAINTEXT://nginx-kafka02:9092"],"jmx_port":9999,"features":{},"host":"nginx-kafka02","timestamp":"1642300427923","port":9092,"version":5}
[zk: localhost:2181(CONNECTED) 5] ls /brokers/ids/0
[]
[zk: localhost:2181(CONNECTED) 6] get /brokers/ids/0
{"listener_security_protocol_map":{"PLAINTEXT":"PLAINTEXT"},"endpoints":["PLAINTEXT://nginx-kafka02:9092"],"jmx_port":9999,"features":{},"host":"nginx-kafka02","timestamp":"1642300427923","port":9092,"version":5}


zookeeper  分布式,开源的配置管理服务  etcd

########################filebeat部署
#安装
1、rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
2、编辑 vim /etc/yum.repos.d/fb.repo
[elastic-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
3、yum安装
yum  install  filebeat -y

rpm -qa  |grep filebeat  #可以查看filebeat有没有安装  rpm -qa 是查看机器上安装的所有软件包
rpm -ql  filebeat  查看filebeat安装到哪里去了,牵扯的文件有哪些

4、设置开机自启
systemctl enable filebeat

#ymal格式
{
    "filebeat.inputs": [
     { "type":"log",
       "enabled":true,
       "paths":["/var/log/nginx/sc_access"]
     },
    ],

}
#配置
修改配置文件/etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
  # Change to true to enable this input configuration.
  enabled: true
  # Paths that should be crawled and fetched. Glob based paths.
  paths:
    - /var/log/nginx/sc_access.log 
#==========------------------------------kafka-----------------------------------
output.kafka:
  hosts: ["192.168.229.139:9092","192.168.229.140:9092"]
  topic: nginxlog
  keep_alive: 10s


#创建主题nginxlog
bin/kafka-topics.sh --create --zookeeper 192.168.77.132:2181 --replication-factor 3 --partitions 1 --topic nginxlog


#启动服务:
systemctl start  filebeat

[root@nginx-kafka01 opt]# ps -ef |grep filebeatroot        5537       1  0 15:32 ?        00:00:08 /usr/share/filebeat/bin/filebeat --environment systemd -c /etc/filebeat/filebeat.yml --path.home /usr/share/filebeat --path.config /etc/filebeat --path.data /var/lib/filebeat --path.logs /var/log/filebeat


filebeat数据文件
[root@nginx-kafka01 filebeat]# pwd
/var/lib/filebeat/registry/filebeat
[root@nginx-kafka01 filebeat]# less log.json
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值