概述
Elasticsearch + Logstash + Kibana(ELK)是一套開源的日誌管理方案。
環境準備
3 臺服務器:
- CPU 4 核,內存 4GB,IP 10.16.4.21,主要跑 Elasticsearch-master,Logstash,kibana 服務;
- CPU 2 核,內存 2GB,IP 10.16.4.56,主要跑 Elasticsearch-data 服務;
- CPU 2 核,內存 8GB,IP 10.16.1.22,redis 緩存服務;
Elasticsearch 版本:6.2.4
Logstash 版本:6.2.4
Kibana 版本:6.2.4
redis 版本:4.0.2
更改主機名
$ hostnamectl set-hostname elk-manage
$ hostnamectl set-hostname elk-data
$ hostnamectl set-hostname elk-redis
關閉防火牆
$ systemctl stop firewalld
$ systemctl disable firewalld
安裝 JDK
elasticsearch,logstash 的運行需要依賴 java 環境。
下載並解壓 jdk 二進制包。
$ cd /opt
$ wget http://mirrors.aorise.org:8000/java/jdk/1.8.0/x64/jdk-8u144-linux-x64.tar.gz
$ tar zxvf jdk-8u144-linux-x64.tar.gz -C /usr/local
$ mv /usr/local/jdk1.8.0-8u144 /usr/local/java
$ cd ~
配置 java 環境變量。
在 ~/.bashrc 文件末尾添加如下內容:
$ export JAVA_HOME=/usr/local/java
$ export JRE_HOME=$JAVA_HOME/jre
$ export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/bin/tools.jar:$JRE_HOME/lib
$ export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
使配置生效。
$ source ~/.bashrc
安裝 Elasticsearch
創建用戶
elasticsearch 不能以 root 用戶啓動,故需要創建非 root 用戶來啓動 elasticsearch。
$ adduser elasticsearch
下載 elasticsearch 安裝包
$ cd /opt
$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.4.tar.gz
sha512sum elasticsearch-6.2.4.tar.gz
$ tar zxvf elasticsearch-6.2.4.tar.gz -C /usr/local
$ chown -R elasticsearch:elasticsearch /usr/local/elasticsearch-6.2.4
$ cd ~
修改 elasticsearch 配置文件
配置文件 .../elasticsearch-6.2.4/config/elasticsearch.yml
node-maser 配置文件如下:
cluster.name: es-cluster
node.name: node-master
node.master: true
node.data: false
path.logs: /var/log/elasticsearch
network.host: 10.16.4.21
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10.16.4.21", "10.16.4.56"]
discovery.zen.minimum_master_nodes: 1
node-data 配置文件如下:
cluster.name: es-cluster
node.name: node-data
node.master: false
node.data: true
path.data: /data/es-data
path.logs: /var/log/elasticsearch
network.host: 10.16.4.56
http.port: 9200
discovery.zen.ping.unicast.hosts: ["10。16.4.21", "10.16.4.56"]
discovery.zen.minimum_master_nodes: 1
創建對應的路徑,並更改用戶屬性。
$ mkdir -p /var/log/elasticsearch
$ mkdir -p /data/es-data
$ chown -R elasticsearch:elasticsearch /var/log/elasticsearch
$ chown -R elasticsearch:elasticsearch /data/es-data
注:集羣名稱必須相同。
創建啓動腳本
#!/bin/sh
# description: elasticsearch
export JAVA_HOME=/usr/local/java
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/bin/tools.jar:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
case "$1" in
start)
su elasticsearch<<!
cd /usr/local/elasticsearch-6.2.4
./bin/elasticsearch -d
!
echo "elasticsearch startup"
;;
stop)
es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
kill -9 $es_pid
echo "elasticsearch stopped"
;;
restart)
es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
kill -9 $es_pid
echo "elasticsearch stopped"
su elasticsearch<<!
cd /usr/local/elasticsearch-6.2.4
./bin/elasticsearch -d
!
echo "elasticsearch startup"
;;
*)
echo "start|stop|restart"
;;
esac
exit $?
添加執行權限。
$ chmod +x elasticsearch
設置開機啓動。
$ echo "/usr/local/elasticsearch-6.2.4/elasticsearch start" >> /etc/rc.d/rc.local
檢驗 elasticsearch 安裝
在瀏覽器執行 http://10.16.4.21:9200/_cluster/health?pretty,看到如下內容:
{
"cluster_name" : "es-cluster",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 2,
"number_of_data_nodes" : 1,
"active_primary_shards" : 3,
"active_shards" : 3,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
即安裝成功!
遇到的問題
啓動報 “max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]” 錯誤。
在 /etc/security/limits.conf 文件末尾添加如下內容:
elasticsearch hard nofile 65536
elasticseatch soft nofile 65536
可通過如下命令檢驗配置是否生效。
$ su - elasticsearch
$ ulimit -Hn
啓動報 “max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]”。
切換至 root 用戶。
在 /etc/sysctl.conf 文件末尾添加如下內容:
vm.max_map_count=262144
使配置生效。
$ sysctl -p
安裝 Kibana
下載 kibana 安裝包
$ cd /opt
$ wget https://artifacts.elastic.co/downloads/kibana/kibana-6.2.4-linux-x86_64.tar.gz
sha512sum kibana-6.2.4-linux-x86_64.tar.gz
$ tar zxvf kibana-6.2.4-linux-x86_64.tar.gz -C $ /usr/local
$ cd ~
修改 kibana 配置文件
server.port: 5601
server.host: "10.16.4.21"
elasticsearch.url: "http://10.16.4.21:9200"
啓動 kibana
$ /usr/local/kibana-6.2.4/bin/kibana
設置開機啓動。
$ echo "/usr/local/kibana6.2.4/bin/kibana" >> /etc/rc.d/rc.local
或者設置 systemctl 啓動。
創建 kibana.service 文件:
$ vi /etc/systemd/system/kibana.service
添加如下內容:
[Unit]
Description=kibana
[Service]
Type=simple
ExecStart=/usr/local/kibana-6.2.4/bin/kibana
[Install]
WantedBy=multi-user.target
授權。
$ chmod +x /etc/systemd/system/kibana.service
啓動。
$ systemctl start kibana
$ systemctl enable kibana
安裝 Logstash
建議 Linux 類的服務器下載 rmp 包安裝。
下載 logstash 安裝包
$ cd /opt
$ wget https://artifacts.elastic.co/downloads/logstash/logstash-6.2.4.rpm
sha512sum logstash-6.2.4.rpm
$ rpm -ivh logstash-6.2.4.rpm
$ cd ~
配置 systemd 啓動
rpm
安裝時,創建啓動腳本的配置文件是
$ /etc/logstash/startup.options
/usr/share/logstash/bin/system-install /etc/logstash/startup.options systemd
創建配置文件
默認是沒有配置文件的。
Shipper 配置文件(logstash_shipper.conf)。
input{
file{
type => "redis"
path => "/var/log/redis/redis.log"
start_position => "beginning"
sincedb_path => "/dev/null"
}
}
output{
if [type] == "redis"{
redis{
host => "127.0.0.1"
data_type => "list"
key => "redis-elk"
port => 6379
db => 2
password => "123456"
}
}
}
Indexer 配置文件(logstash_indexer.conf)。
input{
redis{
host => "127.0.0.1"
data_type => "list"
key => "redis"
password => "123456"
db => 2
}
output{
if [type] == "redis" {
elasticsearch{
hosts => ["10.16.4.56"]
index => "redis-%{+YYYY-MM-dd}"
}
}
}
啓動 Logstash
在啓動前,可通過如下方式檢查配置文件。
$ /usr/share/logstash/bin/logstash -t -f $ /etc/logstash/conf.d/logstash_shipper/indexer.conf
啓動 logstash。
$ systemctl start logstash
設置開機啓動。
$ systemctl enable logstash
至此,安裝完成!
參考地址:
版本依賴:https://www.elastic.co/support/matrix#matrix_compatibility
http://www.ttlsa.com/bigdata/elk-platform-for-log-management/
ELK 下載地址:https://www.elastic.co/downloads/past-releases
ELK 安裝:https://www.elastic.co/guide/en/x-pack/current/installing-xpack.html#xpack-installing-offline
elasticsearch 集羣安裝:https://blog.csdn.net/qq_24879495/article/details/77983941
elasticsearch 啓動腳本:https://www.jianshu.com/p/06794b2a7588
創建 logstash 啓動腳本:https://www.cnblogs.com/keithtt/p/7189489.html
創建 logstash 啓動腳本:https://www.elastic.co/guide/en/logstash/current/running-logstash.html
參數詳解:https://www.cnblogs.com/yangk1996/p/11184086.html