官網地址:https://www.elastic.co/cn/html
官網權威指南:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.htmljava
安裝指南:https://www.elastic.co/guide/en/elasticsearch/reference/5.x/rpm.htmlnode
ELK是Elasticsearch、Logstash、Kibana的簡稱,這三者是核心套件,但並不是所有。nginx
Elasticsearch是實時全文搜索和分析引擎,提供蒐集、分析、存儲數據三大功能;是一套開放REST和JAVA API等結構提供高效搜索功能,可擴展的分佈式系統。它構建於Apache Lucene搜索引擎庫之上。web
Logstash是一個用來蒐集、分析、過濾日誌的工具。它支持幾乎任何類型的日誌,包括系統日誌、錯誤日誌和自定義應用程序日誌。它能夠從許多來源接收日誌,這些來源包括 syslog、消息傳遞(例如 RabbitMQ)和JMX,它可以以多種方式輸出數據,包括電子郵件、websockets和Elasticsearch。docker
Kibana是一個基於Web的圖形界面,用於搜索、分析和可視化存儲在 Elasticsearch指標中的日誌數據。它利用Elasticsearch的REST接口來檢索數據,不只容許用戶建立他們本身的數據的定製儀表板視圖,還容許他們以特殊的方式查詢和過濾數據json
操做系統 | 主機名 | IP地址 | 相關軟件 |
ubuntu-16.04.5-server-amd64 | jqb-node128 | 192.168.91.128 | elasticsearch,kibana |
ubuntu-16.04.5-server-amd64 | jqb-node128 | 192.168.91.128 | logstash,filebeat,nginx |
說明:ubuntu
文本主要是採集Nginx的access日誌。採集的方式有2中,分別是logstash和filebeat。vim
logstash依賴於java環境,比較重。而filebeat是一個輕量級的logstash,它不須要java環境。並且安裝包比較小!api
因此本文會分別介紹2中採集工具的使用方法!
本文所使用的鏡像是ubuntu,注意:使用docker push鏡像時,必定要加版本號!使用16.04
爲何呢?由於默認的是ubuntu是18.04,安裝logstash的deb包時,會報錯一個ruby錯誤!沒法解決!
使用16.04就不會出現這個問題了!
打開官網 https://www.elastic.co/cn/,目前最新版是6.4.3。那麼就直接懟最新版,不要慫!
各個組件版本,統一爲6.4.3
本文所使用的幾個組件,都是基於docker安裝的。這樣方便部署!
2臺服務器,修改ubuntu的更新源爲阿里雲。默認的更新源太慢了!
vim /etc/apt/sources.list
內容以下:
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted deb http://mirrors.aliyun.com/ubuntu/ xenial universe deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu xenial-security main restricted deb http://mirrors.aliyun.com/ubuntu xenial-security universe deb http://mirrors.aliyun.com/ubuntu xenial-security multiverse
更新資源
apt-get update
2臺服務器都安裝docker
apt-get install -y docker.io
默認的更新源太慢了,因此這裏直接使用阿里雲的更新源
在2臺服務器上面修改
vim /etc/docker/daemon.json
內容以下:
{ "registry-mirrors": [ "https://kv3qfp85.mirror.aliyuncs.com" ] }
2臺服務器都 重啓docker服務
systemctl restart docker
登陸到128服務器,建立目錄,並下載軟件包。待會dockerfile會用到!
mkdir /opt/elasticsearch cd /opt/elasticsearch wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.3.deb mkdir /opt/kibana cd /opt/kibana wget https://artifacts.elastic.co/downloads/kibana/kibana-6.4.3-amd64.deb
若是下載速度比較慢,能夠開迅雷下載,再上傳到服務器!
複製sources.list,待會dockerfile會用到!
cp /etc/apt/sources.list /opt/elasticsearch/ cp /etc/apt/sources.list /opt/kibana/
登陸到129服務器,建立目錄,並下載軟件包。待會dockerfile會用到!
mkdir /opt/logstash cd /opt/logstash wget https://artifacts.elastic.co/downloads/logstash/logstash-6.4.3.deb mkdir /opt/filebeat cd /opt/filebeat wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.4.3-amd64.deb
複製sources.list,待會dockerfile會用到!
cp /etc/apt/sources.list /opt/logstash/ cp /etc/apt/sources.list /opt/filebeat/
登陸到128服務器,修改系統參數
vim /etc/sysctl.conf
修改參數vm.max_map_count,此參數必須修改,不然啓動失敗!
vm.max_map_count = 2621440
使用如下命令刷新
sysctl -p
請確保服務器有2G的可用內存!不然會致使elasticsearch啓動失敗!
建立dockerfile
vim /opt/elasticsearch/dockerfile
內容以下:
FROM ubuntu:16.04 # 修改更新源爲阿里雲 ADD sources.list /etc/apt/sources.list ADD elasticsearch-6.4.3.deb ./ # 安裝jdk和elasticsearch RUN apt-get update && apt-get install -y openjdk-8-jdk --allow-unauthenticated && apt-get clean all && dpkg -i elasticsearch-6.4.3.deb && rm -rf elasticsearch-6.4.3.deb EXPOSE 9200 # 添加啓動腳本 ADD run.sh . RUN chmod 755 run.sh ENTRYPOINT [ "/run.sh"]
準備run.sh
vim /opt/elasticsearch/run.sh
內容以下
#!/bin/bash set -e # 添加時區 TZ=Asia/Shanghai ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone # 設置權限 chown -R elasticsearch:elasticsearch /etc/elasticsearch # 判斷目錄是否存在,不然建立 if [ ! -d /var/lib/elasticsearch/data ];then mkdir -p /var/lib/elasticsearch/data chown -R elasticsearch:elasticsearch /var/lib/elasticsearch/data fi if [ ! -d /var/log/elasticsearch/logs ];then mkdir -p /var/log/elasticsearch/logs chown -R elasticsearch:elasticsearch /var/log/elasticsearch/logs fi # 修改數據和日誌目錄 sed -i '33s@/var/lib/elasticsearch@/var/lib/elasticsearch/data@g' /etc/elasticsearch/elasticsearch.yml sed -i '37s@/var/log/elasticsearch@/var/log/elasticsearch/logs@g' /etc/elasticsearch/elasticsearch.yml # 修改綁定ip和端口 sed -i '55s@#network.host: 192.168.0.1@network.host: 0.0.0.0@g' /etc/elasticsearch/elasticsearch.yml sed -i '59s@#http.port: 9200@http.port: 9200@g' /etc/elasticsearch/elasticsearch.yml # 修改啓動文件,去掉-d參數,避免後臺運行 sed -i 72's@-d -p $PID_FILE@-p $PID_FILE@g' /etc/init.d/elasticsearch # 啓動elasticsearch,要hold住,不然容器啓動就退出了! /etc/init.d/elasticsearch start
此時, /opt/elasticsearch目錄結構以下:
./ ├── dockerfile ├── elasticsearch-6.4.3.deb ├── run.sh └── sources.list
生成鏡像
docker build -t elasticsearch-6.4.3 /opt/elasticsearch
啓動容器
docker run -d -it --restart=always -p 9200:9200 elasticsearch-6.4.3
等待幾秒鐘,查看端口是否起來了
root@jqb-node128:/opt/elasticsearch# netstat -anpt Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1303/sshd tcp6 0 0 :::9200 :::* LISTEN 13342/docker-proxy tcp6 0 0 :::22 :::* LISTEN 1303/sshd
訪問頁面
http://192.168.91.128:9200/
效果以下:
按照網絡大部分的文章來說,這個時候,應該要安裝elasticsearch-head插件。可是很差意思,目錄的elasticsearch-head插件不支持elasticsearch6.x版本。
最高支持到elasticsearch5.x版本!
登陸到129服務器,在安裝logstash以前,先來安裝Nginx
apt-get install -y nginx
安裝好以後,默認就啓動了。訪問首頁:
默認的access日誌爲
/var/log/nginx/access.log
建立dockerfile,內容以下:
FROM ubuntu:16.04 # 修改更新源爲阿里雲 ADD sources.list /etc/apt/sources.list ADD logstash-6.4.3.deb ./ # 安裝jdk和elasticsearch RUN apt-get update && apt-get install -y openjdk-8-jdk --allow-unauthenticated && apt-get clean all && dpkg -i logstash-6.4.3.deb && rm -rf logstash-6.4.3.deb EXPOSE 9600 # 添加啓動腳本 ADD run.sh . RUN chmod 755 run.sh ENTRYPOINT [ "/run.sh"]
準備run.sh,內容以下:
#!/bin/bash # 添加時區 TZ=Asia/Shanghai ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone # 修改配置文件 sed -i '41s@# pipeline.workers: 2@# pipeline.workers: 1@g' /etc/logstash/logstash.yml sed -i '45s@# pipeline.batch.size: 125@pipeline.batch.size: 125@g' /etc/logstash/logstash.yml sed -i '50s@# pipeline.batch.delay: 50@pipeline.batch.delay: 5@g' /etc/logstash/logstash.yml sed -i '64s@# path.config:@path.config: /etc/logstash/conf.d@g' /etc/logstash/logstash.yml sed -i '190s@# http.host: "127.0.0.1"@http.host: "0.0.0.0"@g' /etc/logstash/logstash.yml sed -i '195s@# http.port: 9600-9700@# http.port: 9600@g' /etc/logstash/logstash.yml cp -r /etc/logstash /usr/share/logstash/config # 啓動 /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/logstash.conf
準備logstash.conf,內容以下:
input { file { path => "/var/log/nginx/access.log" type => "system" start_position => "beginning" } } output { elasticsearch { hosts => ["192.168.91.128:9200"] index => "system-%{+YYYY.MM.dd}" } stdout { codec => rubydebug } }
參數解釋:
input : 表示輸入的內容 file: 表示類型爲文件 path: 文件路徑 type: 類型 start_position: 監聽文件的起始位置,默認是end。beginning表示開始 output : 表示輸出的內容 elasticsearch : 表示輸出到elasticsearch hosts: ip地址,這裏的ELASTICSEARCH,待會會被run.sh替換爲真正的地址,好比192.168.91.128:9200 index: 索引值,index經常使用的%{+YYYY.MM.dd}這種寫法必須讀取@timestamp數據。這樣寫容易作分片,方便es刪除歷史數據! stdout { codec => rubydebug } 開啓debug日誌輸出
注意:調試階段要先開啓日誌輸出,不然沒法知道有沒有發送數據!
若是還想收集nginx錯誤日誌,path還能夠這麼寫
/var/log/nginx/*.log
由於error日誌也在/var/log/nginx/目錄下。
若是想收集/var/log裏面的全部文件,能夠這麼寫
/var/log/*/*
此時, /opt/logstash目錄結構以下:
./ ├── dockerfile ├── logstash-6.4.3.deb ├── logstash.conf ├── run.sh └── sources.list
生成鏡像
docker build -t logstash-6.4.3 /opt/logstash
啓動容器
docker run -it --restart=always -p 9600:9600 -v "/opt/logstash:/etc/logstash/conf.d" -v /var/log:/var/log logstash-6.4.3
注意:這裏使用2個-v選項,用來掛載文件
logstash.conf 是配置文件,須要配置文件路徑以及elasticsearch地址,注意要帶端口號
/var/log 是日誌文件,由於要監控nginx的日誌,因此要把日誌目錄掛載到容器中
run.sh 啓動時,會加載配置文件 /etc/logstash/conf.d/logstash.conf
等待幾秒鐘,查看端口是否起來了
root@jqb-node129:/opt/logstash# netstat -anpt Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 32174/nginx -g daem tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 8798/sshd tcp 0 0 192.168.91.129:22 192.168.91.1:54112 ESTABLISHED 33699/sshd: root@no tcp 0 36 192.168.91.129:22 192.168.91.1:52999 ESTABLISHED 31702/0 tcp6 0 0 :::80 :::* LISTEN 32174/nginx -g daem tcp6 0 0 :::22 :::* LISTEN 8798/sshd tcp6 0 0 :::9600 :::* LISTEN 41726/docker-proxy
訪問頁面
http://192.168.91.129:9600/
效果以下:
查看elasticsearch的數據
http://192.168.91.128:9200/_search?pretty
效果以下:
沒有發現index值,數據是空的!
訪問一下nginx頁面,查看access日誌是否有了
root@b304d87c93c4:/etc/logstash/conf.d# tail -f /var/log/nginx/access.log 192.168.91.1 - - [15/Nov/2018:18:08:23 +0800] "GET / HTTP/1.1" 200 396 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36" 192.168.91.1 - - [15/Nov/2018:18:08:23 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "http://192.168.91.129/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36"
查看docker進程
root@jqb-node129:/opt/logstash# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b304d87c93c4 logstash-6.4.3 "/run.sh" 7 minutes ago Up 6 minutes 0.0.0.0:9600->9600/tcp inspiring_fermat
查看日誌輸出:
docker logs -t b304d87c93c4
輸出以下信息:
{ "type" => "system", "path" => "/var/log/nginx/access.log", "@timestamp" => 2018-11-15T10:08:24.189Z, "message" => "192.168.91.1 - - [15/Nov/2018:18:08:23 +0800] \"GET / HTTP/1.1\" 200 396 \"-\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36\"", "@version" => "1", "host" => "b304d87c93c4" } { "type" => "system", "path" => "/var/log/nginx/access.log", "@timestamp" => 2018-11-15T10:08:24.276Z, "message" => "192.168.91.1 - - [15/Nov/2018:18:08:23 +0800] \"GET /favicon.ico HTTP/1.1\" 404 209 \"http://192.168.91.129/\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36\"", "@version" => "1", "host" => "b304d87c93c4" }
再次查看elasticsearch頁面,發現數據已經有了!index值爲system-2018.11.15
既然有了數據,就可使用kin展現了!
查看索引
http://192.168.91.128:9200/_cat/indices?v
這個索引,就是接下來kabana要填的索引值。
更多api,請參考連接:
https://blog.csdn.net/hanyuyang19940104/article/details/81743459
登陸到128服務器,建立dockerfile
內容以下:
FROM ubuntu:16.04 ADD kibana-6.4.3-amd64.deb ./ # 安裝jdk和elasticsearch RUN dpkg -i kibana-6.4.3-amd64.deb && rm -rf kibana-6.4.3-amd64.deb EXPOSE 5601 # 添加啓動腳本 ADD run.sh . RUN chmod 755 run.sh ENTRYPOINT [ "/run.sh"]
注意:kibana不須要安裝jdk,直接安裝deb包便可!
準備run.sh,內容以下:
#!/bin/bash # 添加時區 TZ=Asia/Shanghai ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone elasticsearch="192.168.91.128" # 修改配置文件 # 修改監聽地址 sed -i '7s@#server.host: "localhost"@server.host: "0.0.0.0"@g' /etc/kibana/kibana.yml # 去除註釋 sed -i '28s@#elasticsearch.url: "http://localhost:9200"@elasticsearch.url: "http://localhost:9200"@g' /etc/kibana/kibana.yml # 修改ip sed -i "28s?localhost?$elasticsearch?g" /etc/kibana/kibana.yml # 啓動 /usr/share/kibana/bin/kibana "-c /etc/kibana/kibana.yml"
此時, /opt/kibana目錄結構以下:
./ ├── dockerfile ├── kibana-6.4.3-amd64.deb └── run.sh
生成鏡像
docker build -t kibana-6.4.3 /opt/kibana
啓動容器
docker run -d -it --restart=always -p 5601:5601 kibana-6.4.3
等待幾秒鐘,查看端口是否起來了
root@jqb-node128:/opt/elasticsearch# netstat -anpt Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1303/sshd tcp6 0 0 :::9200 :::* LISTEN 13342/docker-proxy tcp6 0 0 :::5601 :::* LISTEN 2488/docker-proxy tcp6 0 0 :::22 :::* LISTEN 1303/sshd
訪問頁面
http://192.168.91.128:5601
選擇不參加體驗計劃
點擊左邊的Discover,下面已經提示了index值
將index值複製過來,選擇下一步
時間選擇@timestamp,點擊建立
效果以下:
多刷新幾回Nginx頁面,刷10次。
效果以下:
若是數據較少,是不會出現這個頁面的!
下面演示以下使用filebeat
查看進程
root@jqb-node129:/opt/logstash# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b304d87c93c4 logstash-6.4.3 "/run.sh" 49 minutes ago Up 50 seconds 0.0.0.0:9600->9600/tcp inspiring_fermat
殺掉logsth的進程
docker rm b304d87c93c4 -f
因爲時間關係,這裏不演示使用docker了。直接服務器安裝filebeat
dpkg -i filebeat-6.4.3-amd64.deb
關於filebeat,主要是配置文件
/etc/filebeat/filebeat.yml
要修改的部分以下:
- type: log # Change to true to enable this input configuration. enabled: false # Paths that should be crawled and fetched. Glob based paths. paths: - /var/log/*.log #- c:\programdata\elasticsearch\logs\* ... output.elasticsearch: # Array of hosts to connect to. hosts: ["localhost:9200"]
修改後的的文件以下:
- 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/access.log #- c:\programdata\elasticsearch\logs\* ... output.elasticsearch: # Array of hosts to connect to. hosts: ["192.168.91.128:9200"]
注意:enabled必定要配置爲true,不然不會生效,不會發送日誌
啓動filebeat
systemctl start filebeat.service
查看elasticsearch數據,發現有filebeat的數據了