1、系統和所需軟件版本介紹php
系統版本:centos 6.5 64位java
軟件版本:jdk-8u60-linux-x64.tar.gz、elasticsearch-2.4.2.tar.gz、logstash-2.4.1.tar.gz、kibana-4.6.3-linux-x86_64.tar.gznode
2、安裝java環境linux
1)解壓jdk軟件壓縮包。nginx
tar -zxvf jdk-8u60-linux-x64.tar.gz
2)在/etc/profile文件的最後邊,添加下邊這幾行,設置一下環境變量。web
export JAVA_HOME=/data/elk/jdk1.8.0_60 export JAVA_BIN=/data/elk/jdk1.8.0_60/bin export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME JAVA_BIN PATH CLASSPATH
3)而後加載一下環境變量,查看下java環境是否安裝成功。shell
[root@elk]# source /etc/profile [root@elk]# java -version java version "1.8.0_60" Java(TM) SE Runtime Environment (build 1.8.0_60-b27) Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
3、安裝elasticsearch.數據庫
1)從官網如今相應版本的elasticsearch軟件包,這裏下載的是elasticsearch-2.4.2.tar.gz,下載完成後解壓。bootstrap
wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.4.2/elasticsearch-2.4.2.tar.gz tar -zxvf elasticsearch-2.4.2.tar.gz
2)2.4版本啓動elasticsearch的時候須要切換到普通用戶,這裏就使用已經建立好的elasticsearch啓動了,須要把elasticsearch目錄的屬主和屬組修改爲elasticsearch.centos
chown -R elasticsearch.elasticsearch elasticsearch
3)須要修改一下elasticsearch/config/elasticsearch.yml這個文件的兩行配置信息,默認是用#註釋的,在elasticsearch.yml的第55和58行。
grep -v "#" elasticsearch/config/elasticsearch.yml| grep -v "^$" network.host: 0.0.0.0 http.port: 9200
4)安裝elasticsearch經常使用的兩個插件,分別是bigdesk和head。
bigdesk: 它是elasticsearch的一個集羣監控工具,能夠經過它來查看es集羣的各類狀態,如:cpu、內存使用狀況,索引數據、搜索狀況,http鏈接數等.
head: elasticsearch-head是一個界面化的集羣操做和管理工具,能夠對集羣進行傻瓜式操做。你能夠經過插件把它集成到es(首選方式),也能夠安裝成一個獨立webapp。安裝插件的時候須要進入到elasticsearch的bin目錄,要用到一個plugin的命令。
cd /date/elk/elasticsearch/bin ls elasticsearch elasticsearch.in.bat elasticsearch-service-mgr.exe elasticsearch-service-x86.exe plugin.bat elasticsearch.bat elasticsearch.in.sh elasticsearch-service-x64.exe plugin service.bat ./plugin install mobz/elasticsearch-head #安裝elasticsearch-head插件 ./bin/plugin install lukas-vlcek/bigdesk/2.4.0 #安裝bigdesk插件
5)啓動elasticsearch.
[hello@iZbp1brwi55d51j9rfs9uyZ ~]$ ps -ef | grep elasticsearch hello 29513 29473 0 11:05 pts/0 00:00:00 grep elasticsearch hello 30161 1 6 Jan09 ? 01:20:03 /data/elk/jdk1.8.0_60/bin/java -Xms256m -Xmx1g -Djava.awt.headless=true -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -XX:+DisableExplicitGC -Dfile.encoding=UTF-8 -Djna.nosys=true -Des.path.home=/data/elk/elasticsearch -cp /data/elk/elasticsearch/lib/elasticsearch-2.4.2.jar:/data/elk/elasticsearch/lib/* org.elasticsearch.bootstrap.Elasticsearch start
6)測試是否能夠正常訪問,若是用瀏覽器訪問,界面出現相似下邊的顯示,說明elasticsearch啓動成功。
curl 127.0.0.1:9200 { "name" : "Bast", "cluster_name" : "elasticsearch", "cluster_uuid" : "V-dptv6pQo-wBiAUwH80Sw", "version" : { "number" : "2.4.2", "build_hash" : "161c65a337d4b422ac0c805f284565cf2014bb84", "build_timestamp" : "2016-11-17T11:51:03Z", "build_snapshot" : false, "lucene_version" : "5.5.2" }, "tagline" : "You Know, for Search" }
4、安裝logstash.
1).從官網下載logstash-2.4.1.tar.gz,而且解壓。
wget https://download.elastic.co/logstash/logstash/logstash-2.4.1.tar.gz
tar -zxvf logstash-2.4.1.tar.gz
2).配置logstash文件,nginx類型的日誌使用udp514端口,php類型的日誌使用udp515端口,向本地的elasticsearch數據庫傳輸。
cd /data/elk/logstash touch conf cd conf cat test.conf input { udp { port => 514 type => nginx } udp { port => 515 type => php } } output { if [type] == "nginx" { elasticsearch { hosts => "localhost:9200" index => "%{+YYYY.MM.dd}_nginx_log" } } if [type] == "php" { elasticsearch { hosts => "localhost:9200" index => "%{+YYYY.MM.dd}_php_error_log" } } stdout {codec => rubydebug} } }
5、安裝kibana,實現界面展現。
1).從官網下載kibana-4.6.3-linux-x86_64.tar.gz,而且解壓。
wget https://download.elastic.co/kibana/kibana/kibana-4.6.3-linux-x86_64.tar.gz
tar -zxvf kibana-4.6.3-linux-x86_64.tar.gz
2).修改kibana的配置文件kibana.yml。
[root@iZbp1brwi55d51j9rfs9uyZ config]# grep -v "#" kibana.yml|grep -v "^$"
server.port: 8000
server.host: "0.0.0.0"
elasticsearch.url: "http://127.0.0.1:9200"
3).由於kibana進程時常平白無故掛掉,因此這裏我就寫一個啓動,關閉的shell腳本。
touch /etc/init.d/kibana chmod +x /etc/init.d/kibana [root@iZbp1brwi55d51j9rfs9uyZ config]# cat /etc/init.d/kibana #!/bin/bash ### BEGIN INIT INFO # Provides: kibana # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Runs kibana daemon # Description: Runs the kibana daemon as a non-root user ### END INIT INFO # Process name NAME=kibana DESC="Kibana4" PROG="/etc/init.d/kibana" # Source function library. . /etc/rc.d/init.d/functions # Configure location of Kibana bin KIBANA_BIN=/data/home/user00/playcrab/elk/kibana/bin # PID Info PID_FOLDER=/var/run/kibana/ PID_FILE=/var/run/kibana/$NAME.pid LOCK_FILE=/var/lock/subsys/$NAME PATH=/bin:/usr/bin:/sbin:/usr/sbin:$KIBANA_BIN DAEMON=$KIBANA_BIN/$NAME # Configure User to run daemon process DAEMON_USER=root # Configure logging location KIBANA_LOG=/var/log/kibana.log # Begin Script RETVAL=0 if [ `id -u` -ne 0 ]; then echo "You need root privileges to run this script" exit 1 fi start() { echo -n "Starting $DESC : " pid=`pidofproc -p $PID_FILE kibana` if [ -n "$pid" ] ; then echo "Already running." exit 0 else # Start Daemon if [ ! -d "$PID_FOLDER" ] ; then mkdir $PID_FOLDER fi daemon --user=$DAEMON_USER --pidfile=$PID_FILE $DAEMON 1>"$KIBANA_LOG" 2>&1 & sleep 2 pidofproc node > $PID_FILE RETVAL=$? [[ $? -eq 0 ]] && success || failure echo [ $RETVAL = 0 ] && touch $LOCK_FILE return $RETVAL fi } reload() { echo "Reload command is not implemented for this service." return $RETVAL } stop() { echo -n "Stopping $DESC : " killproc -p $PID_FILE $DAEMON RETVAL=$? echo [ $RETVAL = 0 ] && rm -f $PID_FILE $LOCK_FILE } case "$1" in start) start ;; stop) stop ;; status) status -p $PID_FILE $DAEMON RETVAL=$? ;; restart) stop start ;; reload) reload ;; *) # Invalid Arguments, print the following message. echo "Usage: $0 {start|stop|status|restart}" >&2 exit 2 ;; esac
到這裏已經安裝好了elk server端的環境了,可是還須要在日誌服務器上配置rsyslog,由於我們是使用rsyslog來發送日誌服務器的log,而後elk接收到以後,通過logstash過濾以後,存儲到elasticsearch,最終經過kibana展現出來。
{ rsyslog } →→ { logstash →→ elasticsearch →→ kibana }
日誌服務器的配置:
[root@iZ23r24v08eZ ~]# cd /etc/rsyslog.d/ [root@iZ23r24v08eZ rsyslog.d]# cat test.com.conf $ModLoad imfile #im表明輸入模塊(input Modules) $InputFileName /data/usr/logs/nginx/test.com.error.log #讀取日誌文件 $InputFileTag test_nginx_error: #定義的NAME必須惟一,同一臺主機上不一樣的應用應當使用不一樣的NAME,不然會致使新定義的TAG不生效; $InputFileStateFile test_nginx_error #定義記錄偏移量數據文件名,定義的StateFile必須惟一,它被rsyslog用於記錄文件上傳進度,不然會致使混亂; $InputRunFileMonitor # $InputFileName /data/usr/logs/nginx/test.com.access.log $InputFileTag test_nginx_access: $InputFileStateFile test_nginx_access $InputRunFileMonitor $InputFilePollInterval 10 #等待十秒鐘發送一次 if $programname == 'test_nginx_error' then @10.23.0.24:514 把日誌傳輸到制定的elk server上,@表示使用udp傳輸,@@表示使用tcp傳輸 if $programname == 'test_nginx_error' then ~ if $programname == 'test_nginx_access' then @10.23.0.24:514 if $programname == 'test_nginx_access' then ~
[root@iZ23r24v08eZ rsyslog.d]# cat php.error.log.conf $ModLoad imfile $InputFileName /data/usr/logs/php-fpm/php-fpm.log $InputFileTag php-fpm_log: $InputFileStateFile state-php-fpm_log $InputRunFileMonitor $InputFilePollInterval 10 if $programname == 'php-fpm_log' then @10.23.0.24:515 if $programname == 'php-fpm_log' then ~
配置完後,須要從新啓動rsyslog.
/etc/init.d/rsyslog restart
除了重啓rsyslog服務以外,還須要看下elk server的防火牆,若是是用的雲主機還有安全組,須要把udp的514和515端口放開,容許日誌服務器能夠訪問elk server的udp514和515端口。