準備: Elasticsearch和logstash都依賴java環境
圖例: (因為不會作圖,從網上找了一張我喜歡的,但是你要先忽略Beats,就當它不存在吧)

名稱/版本(版本務必一緻) | 位址 |
---|---|
Elasticsearch6.3 | 連結 |
Logstash6.3 | 連結 |
Kibana6.3 | 連結 |
下載下傳後解壓以上檔案到/home/elk 目錄下
tar -zxvf elasticsearch-6.3.0.tar.gz
tar -zxvf logstash-6.3.0.tar.gz
tar -zxvf kibana-6.3.0-linux-x86_64.tar.gz
配置 / 啟動
- Elasticsearch
1.配置檔案路徑 : /home/elk/elasticsearch-6.3.0/config/elasticsearch.yml
2.簡單配置如下:
# Set the bind address to a specific IP (IPv4 or IPv6):
network.host: 0.0.0.0 ##伺服器ip
# Set a custom port for HTTP:
http.port: 8012 ##服務端口
3.啟動 : /home/elk/elasticsearch-6.3.0/bin/elasticsearch -d
##本伺服器加了硬碟(/data) 在/data/es 路徑下有nohup.out檔案, 此路徑下執行 nohup /home/elk/elasticsearch-6.3.0/bin/elasticsearch > nohup.out 2>&1 & ##
4.驗證啟動成功:
請求http://47.99.76.xxx:8012/ 會響應一個json
{
name: "31EHmzp",
cluster_name: "elasticsearch",
cluster_uuid: "51wlJRHeRhiy8BDjlrx6Zg",
version: {
number: "6.3.0",
build_flavor: "default",
build_type: "tar",
build_hash: "424e937",
build_date: "2018-06-11T23:38:03.357887Z",
build_snapshot: false,
lucene_version: "7.3.1",
minimum_wire_compatibility_version: "5.6.0",
minimum_index_compatibility_version: "5.0.0"
},
tagline: "You Know, for Search"
}
**
啟動常見錯誤:
1. …for elasticsearch process is too low, increase to at least [65536]
原因: 方案中說是Linux對檔案建立數量限制為:65535. 這與ES對檔案建立數量相悖,是以需要配置.
配置檔案: /etc/security/limits.conf 配置後需要系統重新開機(經驗證,不需要重新開機)
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
2. max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
問題翻譯過來就是:elasticsearch使用者擁有的記憶體權限太小,至少需要262144;
解決:
執行指令:
sysctl -w vm.max_map_count=262144
檢視結果:
sysctl -a|grep vm.max_map_count
顯示:
vm.max_map_count = 262144
上述方法修改之後,如果重新開機虛拟機将失效,是以:
解決辦法:
在 /etc/sysctl.conf檔案最後添加一行
vm.max_map_count=262144
3. Native controller process has stopped - no new native processes can be started
看上去就像是記憶體不夠, 現象就是每隔一段時間就挂了 . 是以: 沒8g記憶體, 就不要吧ELK搞在一台伺服器上
不過,我配置最大記憶體最小記憶體都是256m也可以啟動

發現:
騰訊雲會出現第二個問題,阿裡雲會出現第一個問題
- Logstash
**
1.配置檔案路徑 /home/elk/logstash-6.3.0/config/logback-es.conf
2.簡單配置:
input { ##input 輸入源配置
tcp { ##使用tcp輸入源 官網有詳細文檔
port => 8013 ##伺服器監聽端口8013接受日志 預設ip localhost
codec => json_lines ##使用json解析日志 需要安裝json解析插件
}
}
filter { ##資料處理
}
output { ##output 資料輸出配置
elasticsearch { ##使用elasticsearch接收
hosts => "localhost:8012" ##叢集位址 多個用,隔開
}
stdout { codec => rubydebug} ##輸出到指令視窗
}
3.啟動前,還需安裝logstash的json插件:
/home/elk/logstash-6.3.0/bin/logstash-plugin install logstash-codec-json_lines
4.啟動 : nohup /home/elk/logstash-6.3.0/bin/logstash -f /home/logstash-6.3.0/config/logback-es.conf & ##背景線程形式
問題: 這樣啟動,會在bin目錄下生成nohup.out檔案,時間久了這個檔案會非常大
(注:目前elk伺服器挂了1t的硬碟,路徑為/data ,是以進入 /data/logstash下,執行
## nohup /home/elk/logstash-6.3.0/bin/logstash -f /home/elk/logstash-6.3.0/config/logback-es.conf > nohup.out 2>&1 & ##
将日志檔案輸出到 /data/logstash下的nohup.out檔案中.)
(注2: 如果伺服器顯示磁盤滿了,但是檔案顯然沒用那麼多,此時需要關閉logstash或是es,再啟動就ok.原因待查,可能與logstash運作時使用inode有關)

- Kibana
1.配置檔案路徑 /home/elk/kibana-6.3.0-linux-x86_64/config/kibana.yml
2.配置:
server.port: 8011 ##服務端口
server.host: "0.0.0.0" ##伺服器ip 本機
elasticsearch.url: "http://localhost:8012" ##elasticsearch服務位址
3.啟動 : nohup ./kibana-6.3.0-linux-x86_64/bin/kibana & #背景線程啟動
4.驗證啟動成功 :請求位址 http://47.99.76.xxx:8011
以上,ELK的服務都已經就緒,下面開始測試
- 在項目中依賴logstash
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>4.11</version>
</dependency>
2.建立logback.xml檔案
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>47.99.76.xxx:8013</destination> <!--指定logstash ip:監聽端口 tcpAppender 可自己實作如kafka傳輸等-->
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"/>
</appender>
<include resource="org/springframework/boot/logging/logback/base.xml"/> <!--引用springboot預設配置-->
<root level="INFO">
<appender-ref ref="LOGSTASH"/> <!--使用上述訂閱logstash資料tcp傳輸 -->
<appender-ref ref="CONSOLE"/> <!--使用springboot預設配置 調試視窗輸出-->
</root>
<root level="DEBUG">
<appender-ref ref="LOGSTASH"/> <!--使用上述訂閱logstash資料tcp傳輸 -->
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
3.模拟日志
@Slf4j
@SpringBootApplication
public class ElkdemoApplication {
public static void main(String[] args) {
SpringApplication.run(ElkdemoApplication.class, args);
for (int i = 100000000; ; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
if (i % 2 == 0) log.debug("elk log debug i={}", i);
if (i % 3 == 0) log.error("elk log error i={}", i);
log.info("elk log info i={}", i);
new LogErrorTest().syout(i);
}
}
}
@Slf4j
public class LogErrorTest {
public void syout(int i){
Runnable r = () -> log.info("LogErrorTest log in a new Thread i={}",i*10);
r.run();
log.info("LogErrorTest log i={}",i*10);
}
}
與logstash安裝目錄同級會建立nohup.out檔案,啟動測試項目後就會有日志輸出,樣例如下:
{
"host" => "125.121.37.56",
"@version" => 1,
"logger_name" => "mf.elkdemo.ElkdemoApplication",
"level" => "INFO",
"message" => "elk log info i=100000472",
"thread_name" => "main",
"level_value" => 20000,
"port" => 51357,
"@timestamp" => 2019-04-25T02:59:25.866Z
}
..........
此時Kibana也可以檢視到日志
先選擇要檢視的index(Elasticsearch中index相當于關系型資料庫的table)
點選上圖2後,通過設定正則來配置需要篩選的index:
日志的篩選:
箭頭1設定内容過濾
箭頭2可實作centos中grep的功能