天天看點

Elastic Stack搭建部署及使用示例(史上最全)zookeeper安裝部署kafkabeatselasticsearchlogstashkibana

文章目錄

  • zookeeper安裝部署
    • 配置:zoo.cfg
    • 建立資料和日志目錄
    • 建立myid檔案
    • zk使用
    • zookeeper可視化:
  • kafka
    • kafka僞分布式安裝部署
  • beats
    • filebeat
    • metricbeat 配置
  • elasticsearch
  • logstash
  • kibana

本部落格是示範基于filebeat->kafka->logstash->elasticsearch->kibana 資料采集、及資料可視化分析系統搭建。

包括配置方法、使用執行個體、運維腳本

zookeeper安裝部署

采用僞叢集部署

zookeeper版本為:apache-zookeeper-3.6.1-bin.tar.gz

配置:zoo.cfg

zoo1.cfg

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/hadoop/zk/data/1
dataLogDir=/home/hadoop/zk/logs/1
clientPort=8181
#僞叢集部署
server.1=127.0.0.1:2881:3881
server.2=127.0.0.1:2882:3882
server.3=127.0.0.1:2883:3883
#為zookeeper添加SASL支援
authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider
requireClientAuthScheme=sasl
jaasLoginRenew=3600000
           

zoo2.cfg

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/hadoop/zk/data/2
dataLogDir=/home/hadoop/zk/logs/2
clientPort=8182
#僞叢集部署
server.1=127.0.0.1:2881:3881
server.2=127.0.0.1:2882:3882
server.3=127.0.0.1:2883:3883
#為zookeeper添加SASL支援
authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider
requireClientAuthScheme=sasl
jaasLoginRenew=3600000
           

zoo3.cfg

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/hadoop/zk/data/3
dataLogDir=/home/hadoop/zk/logs/3
clientPort=8183
#僞叢集部署
server.1=127.0.0.1:2881:3881
server.2=127.0.0.1:2882:3882
server.3=127.0.0.1:2883:3883
#為zookeeper添加SASL支援
authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider
requireClientAuthScheme=sasl
jaasLoginRenew=3600000
           

建立資料和日志目錄

cd /home/hadoop/zk
mkdir -p /home/hadoop/zk/data/3
mkdir -p /home/hadoop/zk/data/2
mkdir -p /home/hadoop/zk/data/1

mkdir -p /home/hadoop/zk/logs/1
mkdir -p /home/hadoop/zk/logs/2
mkdir -p /home/hadoop/zk/logs/3
           

建立myid檔案

在dataDir指定目錄下建立一個myid檔案。
然後分别在myid檔案中按照zoo.cfg檔案的server.(數字)中數字的數值,
在不同伺服器上的myid檔案中填寫相應的值。例如server.1 在目前機器的myid中就隻寫上1
#echo 1 > /home/hadoop/zk/data/myid
echo 1 > /home/hadoop/zk/data/1/myid
echo 2 > /home/hadoop/zk/data/2/myid
echo 3 > /home/hadoop/zk/data/3/myid
           

zk使用

啟動
zkServer.sh start ../conf/zoo1.cfg
停止
zkServer.sh stop ../conf/zoo1.cfg
檢視狀态
zkServer.sh status ../conf/zoo1.cfg
啟動用戶端
zkCli.sh -server 127.0.0.1:8181
zkCli.sh -server 127.0.0.1:8182
zkCli.sh -server 127.0.0.1:8183
或者
zkCli.sh -server 127.0.0.1:8181,127.0.0.1:8182,127.0.0.1:8183

用戶端連接配接zookeeper叢集,可以連接配接多台也可以連接配接1台伺服器,bin/zkCli.sh -server (ip1或域名1):2181,(ip2域名2):2181,(ip3域名3):2181,
多個ip或域名間用逗号分隔(這裡可以使用ip也可以使用域名。)
           
  • 這裡有本人自己寫的zookeeper 叢集管理腳本

zookeeper可視化:

  1. zookeeper-visualizer (很好使)
  2. idea zookeeper插件(不好使,容易卡死)

參考:

zookeeper 安裝及配置

zookeeper僞叢集安裝部署

kafka

kafka僞分布式安裝部署

kafka版本: kafka_2.11-2.4.1.tgz

server1.properties

#每台機器必須唯一
broker.id=0
#在配置叢集的時候必須設定,不然以後的操作會報找不到leader的錯誤
listeners=INSIDE://0.0.0.0:9091,OUTSIDE://0.0.0.0:8091
# INSIDE指的是broker之間通訊,例如:REPLICATION 與LEADER的通訊,OUTSIDE指的是外部用戶端與kafka叢集之間的通訊,例如:CLIENT與kafka叢集
advertised.listeners=INSIDE://127.0.0.1:9091,OUTSIDE://hostname:8091
listener.security.protocol.map=INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
#broker間通訊的監聽器名稱。如果未設定,則偵聽器名稱由security.inter.broker.protocol定義。 同時設定此項和security.inter.broker.protocol屬性是錯誤的,隻設定一個。
inter.broker.listener.name=INSIDE
#log.retention.minutes=5
log.retention.hours=168
zookeeper.connect=127.0.0.1:8181,127.0.0.1:8182,127.0.0.1:8183/kafka
compression.type=producer
delete.topic.enable=true
log.dirs=/home/hadoop/kafka/data/1
log.dir=/home/hadoop/kafka/data/1
num.partitions=1
min.insync.replicas=1
default.replication.factor=3
auto.create.topics.enable=false
log.retention.check.interval.ms=300000
log.segment.bytes=1073741824
           

server2.properties

#每台機器必須唯一
broker.id=1
#在配置叢集的時候必須設定,不然以後的操作會報找不到leader的錯誤
listeners=INSIDE://0.0.0.0:9092,OUTSIDE://0.0.0.0:8092
# INSIDE指的是broker之間通訊,例如:REPLICATION 與LEADER的通訊,OUTSIDE指的是外部用戶端與kafka叢集之間的通訊,例如:CLIENT與kafka叢集
advertised.listeners=INSIDE://127.0.0.1:9092,OUTSIDE://hostname:8092
listener.security.protocol.map=INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
#broker間通訊的監聽器名稱。如果未設定,則偵聽器名稱由security.inter.broker.protocol定義。 同時設定此項和security.inter.broker.protocol屬性是錯誤的,隻設定一個。
inter.broker.listener.name=INSIDE
#log.retention.minutes=5
log.retention.hours=168
zookeeper.connect=127.0.0.1:8181,127.0.0.1:8182,127.0.0.1:8183/kafka
compression.type=producer
delete.topic.enable=true
log.dirs=/home/hadoop/kafka/data/2
log.dir=/home/hadoop/kafka/data/2
num.partitions=1
min.insync.replicas=1
auto.create.topics.enable=false
log.retention.check.interval.ms=300000
log.segment.bytes=1073741824
           

server3.properties

#每台機器必須唯一
broker.id=2
#在配置叢集的時候必須設定,不然以後的操作會報找不到leader的錯誤
listeners=INSIDE://0.0.0.0:9093,OUTSIDE://0.0.0.0:8093
# INSIDE指的是broker之間通訊,例如:REPLICATION 與LEADER的通訊,OUTSIDE指的是外部用戶端與kafka叢集之間的通訊,例如:CLIENT與kafka叢集
advertised.listeners=INSIDE://127.0.0.1:9093,OUTSIDE://hostname:8093
listener.security.protocol.map=INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
#broker間通訊的監聽器名稱。如果未設定,則偵聽器名稱由security.inter.broker.protocol定義。 同時設定此項和security.inter.broker.protocol屬性是錯誤的,隻設定一個。
inter.broker.listener.name=INSIDE
#log.retention.minutes=5
log.retention.hours=168
zookeeper.connect=127.0.0.1:8181,127.0.0.1:8182,127.0.0.1:8183/kafka
compression.type=producer
delete.topic.enable=true
log.dirs=/home/hadoop/kafka/data/3
log.dir=/home/hadoop/kafka/data/3
num.partitions=1
min.insync.replicas=1
auto.create.topics.enable=false
log.retention.check.interval.ms=300000
log.segment.bytes=1073741824
           

啟動:

kafka-server-start.sh -daemon $KAFKA_HOME/config/server1.properties

kafka-server-start.sh -daemon $KAFKA_HOME/config/server2.properties

kafka-server-start.sh -daemon $KAFKA_HOME/config/server3.properties

  • 這裡有本人自己寫的kafka 叢集管理腳本
  • 如果需要配置安全kafka叢集請看這裡

beats

filebeat

filebeat版本:filebeat-6.8.4-linux-x86_64.tar.gz

filebeat配置:

本例是從本地目錄采集檔案到kafka

filebeat.yml 需要配置的地方如下:

#=========================== Filebeat prospectors =============================

filebeat.prospectors:

# Each - is a prospector. Most options can be set at the prospector level, so
# you can use different prospectors for various configurations.
# Below are the prospector specific configurations.

- type: log

  # Change to true to enable this prospector configuration.
  enabled: true

  # Paths that should be crawled and fetched. Glob based paths.
  paths:
    - /home/hadoop/fklek/filebeat/data_log/log.*
#-------------------------Kafka output ------------------------------
output.kafka:
  hosts: ["127.0.0.1:8091","127.0.0.1:8092","127.0.0.1:8093"]
  #topic: '%{[fields.my_log_topic]}'
  topic: test.3
  partition.round_robin:
    reachable_only: false
  #version: 0.10.0.0
  required_acks: -1
  compression: gzip
  max_message_bytes: 1000000
           

filebeat 配置驗證

./filebeat test config -c filebeat.yml

filebeat 啟動

./filebeat -e -c filebeat.yml

metricbeat 配置

metribeat版本:metricbeat-6.8.4-linux-x86_64.tar.gz

metricbeat 配置kibana儀表盤監控系統資源情況等(不需要的話,此項可以不配置)

metricbeat.yml 需要配置的地方

#============================== Kibana =====================================

# Starting with Beats version 6.0.0, the dashboards are loaded via the Kibana API.
# This requires a Kibana endpoint configuration.
setup.kibana:

  # Kibana Host
  # Scheme and port can be left out and will be set to the default (http and 5601)
  # In case you specify and additional path, the scheme is required: http://localhost:5601/path
  # IPv6 addresses should always be defined as: https://[2001:db8::1]:5601
  host: "127.0.0.1:8560"

  # Kibana Space ID
  # ID of the Kibana Space into which the dashboards should be loaded. By default,
  # the Default Space will be used.
  #space.id:

#================================ Outputs =====================================

# Configure what output to use when sending the data collected by the beat.

#-------------------------- Elasticsearch output ------------------------------
output.elasticsearch:
  # Array of hosts to connect to.
  hosts: ["localhost:8920"]

  # Protocol - either `http` (default) or `https`.
  #protocol: "https"

  # Authentication credentials - either API key or username/password.
  #api_key: "id:api_key"
  #username: "elastic"
  #password: "changeme"
           

檢視現有的Modules

./metricbeat modules list

執行指令啟動Metricbeat

nohup ./metricbeat -e > metricbeat.log & #背景啟動

參考:

metricbeat,filebeat儀表盤的安裝以及在Kibana上的展示

ELK日志處理之metricbeat伺服器性能監控

elasticsearch

elasticsearch版本:

elasticsearch-6.8.4.tar.gz(本例使用此版本,因為需要在有type的類型下做測試)

elasticsearch-7.x以上無type

elasticsearch.yml 需要配置的地方:

# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
cluster.name: my-application
node.name: node-1
path.data: /home/hadoop/fklek/es/data  
path.logs: /home/hadoop/fklek/es/logs
#修改綁定的ip:預設隻允許本機通路,修改為0.0.0.0後則可以遠端通路
network.host: 0.0.0.0
http.port: 8920 

#表示這個節點是否可以充當主節點,以及點是否充當資料節點。
node.master: true
node.data: true
#指定IK為分詞工具,新版本不需要設定,設定會報錯
#index.analysis.analyzer.ik.type : "ik"

#指定叢集中的節點中有幾個有master資格的節點。對于大叢集可以寫3個以上。
#covery.zen.minimum_master_nodes: 2   

#預設是3s,這是設定叢集中自動發現其它節點時ping連接配接逾時時間,為避免因為網絡差而導緻啟動報錯,我設成了40s。
#discovery.zen.ping.timeout: 40s

#設定是否打開多點傳播發現節點。
#discovery.zen.ping.multicast.enabled: false

#設定綁定的ip位址,這是我的master的IP。
#network.bind_host: 127.0.0.1

#設定其他節點和該節點互動的IP位址
#network.publish_host: 127.0.0.1

#同時設定bind_host和publish_host上面兩個參數
#network.host: 127.0.0.1

#指明叢集中其他可能為master的節點ip,以防es啟動後發現不了叢集中的其他節點。第一對引号裡是node1,預設端口是9300.第二個是node2,端口号是9301。第三個引号裡是node3,端口号為9302
#discovery.zen.ping.unicast.hosts: ["127.0.0.1:9300", "127.0.0.1:9301","127.0.0.1:9302"]
#discovery.zen.ping.unicast.hosts:["節點1的 ip","節點2 的ip","節點3的ip"]

#叢集結點之間通信端口,預設為9300
#transport.tcp.port: 9300

           

es啟動

./bin/elasticsearch -d -p pid

啟動叢集

./elasticsearch --cluster.name myes --node.name node1

logstash

logstash版本:logstash-6.8.4.tar.gz

參考:

logstash.yml 需要的配置

path.data: /home/hadoop/fklek/logstash/data    #資料存儲路徑 
#path.config: /etc/logstash/conf.d/*.conf   #配置檔案目錄
path.logs: /home/hadoop/fklek/logstash/logs   #日志輸出路徑
           

kafka_es.conf 配置

input {
    kafka {
        bootstrap_servers => "127.0.0.1:8091,127.0.0.1:8092,127.0.0.1:8093"
        topics => ["test.3"]
        auto_offset_reset => "earliest"
        group_id => "logstash_group_1"
        codec => "plain"
        consumer_threads => 1
        decorate_events => true
    }
}

filter {
}

output {
    elasticsearch {
        hosts => ["127.0.0.1:8920"]
        index => "kafka-es-%{+YYYY.MM.dd}"
        workers => 1
    }
    stdout {codec => rubydebug}
}
           

logstash啟動

nohup ./bin/logstash -l logs/log -f kafka_es.conf > logs/log.nohup 2>&1 &

kibana

kibana版本:kibana-6.8.4-linux-x86_64.tar.gz

kibana.yml 需要的配置

server.port: 8560
server.host: "0.0.0.0"
pid.file: /home/hadoop/fklek/kibana/kibana.pid
elasticsearch.hosts: ["http://localhost:8920"]
kibana.index: ".kibana"
           

啟動kibana

nohup ./bin/kibana -l logs/log > logs/log.nohup 2>&1 &

通路:http://127.0.0.1:8560