1. 程式人生 > >Logstash、Kafka和ElastaticSearch安裝以及簡單使用(Linux)

Logstash、Kafka和ElastaticSearch安裝以及簡單使用(Linux)

前段時間公司需要做一個日誌分析的需求,於是開始了對於日誌分析這方面的研究。現在系統中整合的是Fluem + elastaticSearch + 還有influxdb,如果基於這些做日誌分析的話,就可能需要使用Flume + kafka + elastaticSearch但是我對比了一下Flume和logstash 他們共同的特點是支援的採集方式很全,支援分散式。區別在於Flume作為生產方傳輸資料的功能比較強大,而logstash在做資料處理時支援grok表示式,在收集資料集進行格式化比較有優勢,綜合分析需求最後還是選擇使用ELK這一套技術架構。

廢話不多說了,開始幹正事。

版本:  elasticsearch-5.2.2

logstash-5.2.2

kafka_2.10-0.10.2.1(logstash + kafka必須使用0.9以上版本)

注意:版本問題一定要搞清楚,千萬別亂了,本人在版本上出過兩個問題,第一次是JavaAPI消費不了kafka裡面的資料。另一個是logstash的資料傳不進kafka。

一、首選安裝ElastaticSearch

1、首先在opt資料夾建立elasticsearch資料夾

mkdir elasticsearch
2、把elasticsearch-5.2.2.tar.gz拷貝到該目錄下解壓
tar -zxvf elasticsearch-5.2.2.tar.gz
3、修改/opt/elasticsearch/elasticsearch-5.2.2/config/
elasticsearch.yml配置檔案
(該配置檔案不用修改其實也能直接執行,但是內外網可能無法訪問,其他配置在文件上都有註釋可以根據自己的需求配置)
network.host: 0.0.0.0
這樣就可以內網訪問了。

4、啟動。ES啟動為了安全是不支援root啟動的,需要使用其他使用者啟動

[[email protected] elasticsearch-5.2.2]# su me
[[email protected] elasticsearch-5.2.2]$ cd /opt/elasticsearch/elasticsearch-5.2.2/bin/
[[email protected]
bin]$ ls -l total 360 -rwxrwxrwx. 1 root root 7852 May 26 05:28 elasticsearch -rwxrwxrwx. 1 root root 3341 May 26 05:28 elasticsearch.bat -rwxrwxrwx. 1 root root 828 May 26 05:28 elasticsearch.in.bat -rwxrwxrwx. 1 root root 404 May 26 05:28 elasticsearch.in.sh -rwxrwxrwx. 1 root root 2540 May 26 05:28 elasticsearch-plugin -rwxrwxrwx. 1 root root 733 May 26 05:28 elasticsearch-plugin.bat -rwxrwxrwx. 1 root root 11239 May 26 05:28 elasticsearch-service.bat -rwxrwxrwx. 1 root root 104448 May 26 05:28 elasticsearch-service-mgr.exe -rwxrwxrwx. 1 root root 103936 May 26 05:28 elasticsearch-service-x64.exe -rwxrwxrwx. 1 root root 80896 May 26 05:28 elasticsearch-service-x86.exe -rwxrwxrwx. 1 root root 223 May 26 05:28 elasticsearch-systemd-pre-exec -rwxrwxrwx. 1 root root 2514 May 26 05:28 elasticsearch-translog -rwxrwxrwx. 1 root root 1435 May 26 05:28 elasticsearch-translog.bat -rwxrwxrwx. 1 root root 16442 May 26 05:28 hs_err_pid6021.log [[email protected] bin]$ ./elasticsearch
OK,這樣就可以啟動成功。但是注意如果用虛擬機器的話,可能出現分配的記憶體太少,ES的預設啟動記憶體就是2G這個可以在:/opt/elasticsearch/elasticsearch-5.2.2/config/jvm.options修改引數
# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms512m
-Xmx512m
二、安裝Logstash並採集Tomcat access日誌
1、在opt目錄下建立資料夾
mkdir Logstash
2、拷貝logstash-5.2.2.tar.gz 到Logstash目錄 然後解壓
tar -zxvf logstash-5.2.2.tar.gz
3、在bin目錄或者是config目錄建立tomcat_access.config檔案(這個目錄是在啟動的時候指定的我為了方便直接寫在了bin目錄下內容如下)
input {
    file {
        path => "/opt/tomcat/apache-tomcat-8.5.15/logs/localhost_access_log.*.txt"#監控       本地的Tomcat access日誌
        start_position => "beginning"
    }
}

filter {
  grok {
      patterns_dir => "/opt/logstash/logstash-5.2.2/patterns" #指定正則檔案的存放位置
      match => {
      "message" => "%{ACCESSLOG}" #呼叫access日誌正則
      }
  }
}
output{
    stdout{ #標準輸出控制檯
        codec => rubydebug
    }
    elasticsearch {
      hosts => ["10.255.0.167"] #輸出到ES
        index => "tomcat-acccess" #索引名
    }
}
4、 啟動(直接進入logstash bin目錄 -f 是指的當前資料夾下剛才建立的tomcat_access.config )

./logstash -f tomcat_access.config
啟動成功後,隨便點選幾下Tomcat上執行的專案,ES馬上就有一個tomcat-acccess索引.這樣就OK啦!

三、安裝kafka(Java API消費kafka以後會有單獨一節介紹,這裡只是介紹如何安裝)
1.kafka的啟動依賴於zookeeper所以首先要安裝zookeeper。在opt目錄下建立兩個資料夾
mkdir zookeeper
mkdir kafka
2.在zookeeper資料夾裡解壓zookeeper
tar -zxvf zookeeper-3.4.9.tar.gz
3.在安裝目錄下找到conf資料夾的zoo_sample.cfg重新命名為zoo.cfg(最好是copy以防萬一)
4.zookeeper的服務直接在bin目錄下執行
1. 啟動ZK服務:       ./zkServer.sh start
2. 停止ZK服務:       ./zkServer.sh stop
3. 重啟ZK服務:       ./zkServer.sh restart
4. 檢視ZK服務狀態:   ./zkServer.sh status

5.啟動後執行jps(QuorumPeerMain程序就是zookeeper獨立的程序s說明啟動成功)

6.在/opt/kafka目錄下解壓kafka_2.11-0.10.2.1.tgz

tar -zxvf kafka_2.11-0.10.2.1.tgz
7.修改kafka安裝目錄config server.properties修改配置資訊(下面貼一個完整可執行的測試配置)
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# see kafka.server.KafkaConfig for additional details and defaults

############################# Server Basics #############################

# The id of the broker. This must be set to a unique integer for each broker.
broker.id=0

# Switch to enable topic deletion or not, default value is false
#delete.topic.enable=true

############################# Socket Server Settings #############################

# The address the socket server listens on. It will get the value returned from 
# java.net.InetAddress.getCanonicalHostName() if not configured.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
#listeners=PLAINTEXT://:9092

# Hostname and port the broker will advertise to producers and consumers. If not set, 
# it uses the value for "listeners" if configured.  Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
advertised.listeners=PLAINTEXT://10.255.0.167:9092

# Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details
#listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL

# The number of threads handling network requests
num.network.threads=3

# The number of threads doing disk I/O
num.io.threads=8

# The send buffer (SO_SNDBUF) used by the socket server
socket.send.buffer.bytes=102400

# The receive buffer (SO_RCVBUF) used by the socket server
socket.receive.buffer.bytes=102400

# The maximum size of a request that the socket server will accept (protection against OOM)
socket.request.max.bytes=104857600


############################# Log Basics #############################

# A comma seperated list of directories under which to store log files
log.dirs=/tmp/kafka-logs

# The default number of log partitions per topic. More partitions allow greater
# parallelism for consumption, but this will also result in more files across
# the brokers.
num.partitions=1

# The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
# This value is recommended to be increased for installations with data dirs located in RAID array.
num.recovery.threads.per.data.dir=1

############################# Log Flush Policy #############################

# Messages are immediately written to the filesystem but by default we only fsync() to sync
# the OS cache lazily. The following configurations control the flush of data to disk.
# There are a few important trade-offs here:
#    1. Durability: Unflushed data may be lost if you are not using replication.
#    2. Latency: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush.
#    3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to exceessive seeks.
# The settings below allow one to configure the flush policy to flush data after a period of time or
# every N messages (or both). This can be done globally and overridden on a per-topic basis.

# The number of messages to accept before forcing a flush of data to disk
#log.flush.interval.messages=10000

# The maximum amount of time a message can sit in a log before we force a flush
#log.flush.interval.ms=1000

############################# Log Retention Policy #############################

# The following configurations control the disposal of log segments. The policy can
# be set to delete segments after a period of time, or after a given size has accumulated.
# A segment will be deleted whenever *either* of these criteria are met. Deletion always happens
# from the end of the log.

# The minimum age of a log file to be eligible for deletion due to age
log.retention.hours=168

# A size-based retention policy for logs. Segments are pruned from the log as long as the remaining
# segments don't drop below log.retention.bytes. Functions independently of log.retention.hours.
#log.retention.bytes=1073741824

# The maximum size of a log segment file. When this size is reached a new log segment will be created.
log.segment.bytes=1073741824

# The interval at which log segments are checked to see if they can be deleted according
# to the retention policies
log.retention.check.interval.ms=300000

############################# Zookeeper #############################

# Zookeeper connection string (see zookeeper docs for details).
# This is a comma separated host:port pairs, each corresponding to a zk
# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
# You can also append an optional chroot string to the urls to specify the
# root directory for all kafka znodes.
zookeeper.connect=localhost:2181

# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=6000
8.啟動kafka在bin目錄下執行(後面就是指定的server.properties檔案路徑)
./kafka-server-start.sh ../config/server.properties
9.建立 topic 名為 test
./bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
10.進去安裝根目錄,建立一個新的終端執行一個生產者
bin/kafka-console-producer.sh --broker-list 10.255.0.167:9092 --topic test #該成自己的對應的IP
11.進去安裝根目錄,再建立一個終端執行一個消費者
bin/kafka-console-consumer.sh --zookeeper 10.255.0.167:2181 --topic test #du對應自己的IP
這樣kafka就安裝成功了。
這裡再附幾個kafka常用的幾個命令:
#通過group_id 檢視當前詳細的消費情況
bin/kafka-consumer-groups.sh --group logstashtest--describe --zookeeper 127.0.0.1:2181
#檢視topic屬性
bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic topicname
#檢視kafka log檔案詳細資訊
bin/kafka-run-class.sh kafka.tools.DumpLogSegments --files /tmp/kafka-logs/test3-0/00000000000000000000.log  --print-data-log   #--files 對應的data資料路徑

附:這樣子基本上環境就是搭建完成了,但是整個ELK的技術棧還有很多東西比如,Logstash的beat採集外掛、kafka-manager管理工具、還有用來展示的Kibana、ES的head外掛,有時間我就補充上。







相關推薦

LogstashKafkaElastaticSearch安裝以及簡單使用Linux

前段時間公司需要做一個日誌分析的需求,於是開始了對於日誌分析這方面的研究。現在系統中整合的是Fluem + elastaticSearch + 還有influxdb,如果基於這些做日誌分析的話,就可能需要使用Flume + kafka + elastaticSearch但是我

Elasticsearch 2.3.5 部署安裝以及外掛Linux

1    環境依賴 1.       linux 2.       jdk1.8.0 以上 2    相關安裝包以及版本 1.       elasticsearch-2.3.5.zip 2.       elasticsearch-head-master.zip 3.  

SODBASE CEP學習進階篇續:日誌採集-LogstashKafkaCEP整合

相比Flume,筆者更推薦使用Logstash做日誌採集,見SODBASE CEP學習進階篇(二)續:日誌採集-Logstash、Kafka和CEP整合。如果之前專案中已經選型使用Flume,則本文供參考。 1. 啟動CEP模型 啟動CEP Server ./catalina

【Tomcat】tomcat簡單介紹,安裝以及啟動

最為一個資深的吊死程式猿,在工作中,經常使用tomcat,但是從來沒有做過深入的總結,真是罪過。從這篇博文開始,我們對tomcat進行一個深入的學習,先是基本使用學習,之後我們再看一看tomcat的原始碼部分。不多說了,我們直接進入正題: tomcat介紹

頁面重繪回流以及優化

圖片大小 處理流 create 意圖 borde 基本上 nal arch 似的 源文章地址:http://www.css88.com/archives/4996 在討論頁面重繪、回流之前。需要對頁面的呈現流程有些了解,頁面是怎麽把html結合css等顯示到瀏覽器上的,下面

kafka集群安裝及管理

ini nohup class 技術分享 -o -h timeout lba 能夠 一、環境配置1.系統環境[root@date ~]# cat /etc/centos-release CentOS Linux release 7.4.1708 (Core)2..安裝JAV

kafka集群安裝及管理

進程 node 規則 nfa 情況 tor back per art 一、broker的遷移1.查看zookeeper和kafka啟動情況[root@slave1 ~]# pssh -h hostlist -i 'jps' [1] 22:08:11 [SUC

輸入過濾輸出——PowerShell三分鐘

PowerShell今天的三分鐘給大家歸納一下PowerShell日常對數據的輸入過濾和輸出的處理PowerShell輸入數據的方式有很多種,包括直接輸入字符、導入數據、捕獲界面輸入等對於較少的信息,可以直接手工在PowerShell界面中輸入:這種方式很常見,對於需求信息較少的查詢和操作非常方便除此之外,還

PHPMySQLJavaScript學習手冊筆記

first 超級 轉換 his java post ace put lin 第三章 賦值運算符: $j*=3; 等價於 $j=$j*3; 字符串的單引號雙引號雙引號中的變量可以解析,單引號就是絕對的字符串。 heredoc長字符串 <?php $au

PHPMySQLJavaScript學習手冊筆記

本地 後端 驗證 css 考題 php 語言 html oot 第一章思考題1.創建一個完全動態網頁至少需要哪四大要素?服務器 動態語言php js 數據庫2.html代表超文本xxx3.因為sql分支用sql語言4.php用在服務器端 處理後端任務 js用在客戶端 本地驗

PHPMySQLJavaScript學習手冊筆記

元素 book 個數 爆炸 foreach 學習 mysql 手冊 一個 第六章 數組 $myIndexArray=array(‘apple‘,‘banana‘,‘cat‘,‘dog‘);` $myArray=array(‘book‘=>200,

PHPMySQLJavaScript學習手冊筆記

php efault ava asc 學習 mys comm 否則 hello 第四章 條件語句 <?php if ($cond<100) { echo "cond <100"; } else { echo "cond >100"; }

PHPMySQLJavaScript學習手冊筆記

index 查詢 user 邏輯 creat date text select HERE 第八章此系列的文章都是圍繞《PHP、MySQL與JavaScript學習手冊》開展的,記錄下我自學的歷程。 mysql入門 1.進入mysql管理 mysql -u username

教你webpackreactnode.js環境配置下篇

上篇我介紹了前端下webpack和react、redux等環境的配置,這篇將繼續重點介紹後臺node.js的配置。 我把所有程式碼都放到了我的github上:webpack-react-express環境配置 server 後臺這邊的配置就簡單了很多,我這裡拿node.js的exp

安裝JDKTomcat及常用命令Linux

檢視ip: -------------------------- ifconfig -a 配置網路選項 ------------------------ setup service network restart (配置完IP等必須重啟網路服務才生效) 檢視使用者登入資

MongoDB的安裝以及連線windows

一.Windows環境下安裝MongoDB資料庫 1.從官網上下載MongoDB安裝包 https://www.mongodb.com/download-center#community 2.點選下載好的安裝包 點選“Custom“選擇自定義安裝 選擇安裝路徑 ne

Akka入門安裝以及示例Java

一、安裝開發環境1.確保安裝的java環境是1.6或以上;2.設定JAVA_HOME環境變數(java SDK所安裝的目錄)    # export JAVA_HOME=..root of Java distribution..    # export PATH=$PATH

安裝centos minimal 版本後安裝setup包linux

過程 min linu ins 配置 命令 .cn width idt 網絡配置好後,輸入命令 yum install setuptool,安裝過程有兩個確認,輸入Y即可 安裝centos minimal 版本後安裝setup包(linux)

was安裝相關步驟Linux

目錄 appserver eight 動圖 文件夾 manage 引導 通過 onf 本次試驗目的主要對websphere 二次內部解剖對中間件性能優化墊鋪。 1、準備相關文件 其中 iso文件為WAS主要鏡像文件(WAS文件所在地) Instalmgr為IBM安

vsftpd檔案伺服器的安裝與配置Linux

vsftpd是一款免費的、開源的ftp伺服器軟體。   安裝環境(centos 6.8 64位)   安裝版本 vsftpd-2.2.2-24.el6.x86_64   安裝步驟:     一:執行 yum -y install vsftpd(1、可以先執行 rp