標籤:java 環境 server
一、系統和所需軟體版本介紹
系統版本:centos 6.5 64位
軟體版本: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.gz
二、安裝java環境
1)解壓jdk軟體壓縮包。
tar -zxvf jdk-8u60-linux-x64.tar.gz
2)在/etc/profile檔案的最後邊,添加下邊這幾行,設定一下環境變數。
export JAVA_HOME=/data/elk/jdk1.8.0_60export JAVA_BIN=/data/elk/jdk1.8.0_60/binexport PATH=$PATH:$JAVA_HOME/binexport CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jarexport JAVA_HOME JAVA_BIN PATH CLASSPATH
3)然後載入一下環境變數,查看下java環境是否安裝成功。
[[email protected]]# source /etc/profile[[email protected]]# java -versionjava 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)
三、安裝elasticsearch.
1)從官網現在相應版本的elasticsearch軟體包,這裡下載的是elasticsearch-2.4.2.tar.gz,下載完成後解壓。
wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.4.2/elasticsearch-2.4.2.tar.gztar -zxvf elasticsearch-2.4.2.tar.gz
2)2.4版本啟動elasticsearch的時候需要切換到普通使用者,這裡就使用已經建立好的elasticsearch啟動了,需要把elasticsearch目錄的屬主和屬組修改成elasticsearch.
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.0http.port: 9200
4)安裝elasticsearch常用的兩個外掛程式,分別是bigdesk和head。
bigdesk: 它是elasticsearch的一個叢集監控工具,可以通過它來查看es叢集的各種狀態,如:cpu、記憶體使用量情況,索引資料、搜尋情況,http串連數等.
head: elasticsearch-head是一個介面化的叢集操作和管理工具,可以對叢集進行傻瓜式操作。你可以通過外掛程式把它整合到es(首選方式),也可以安裝成一個獨立webapp。安裝外掛程式的時候需要進入到elasticsearch的bin目錄,要用到一個plugin的命令。
cd /date/elk/elasticsearch/binlselasticsearch 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.
[[email protected] ~]$ ps -ef | grep elasticsearch playcrab 29513 29473 0 11:05 pts/0 00:00:00 grep elasticsearchplaycrab 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"}
四、安裝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/logstashtouch confcd confcat test.confinput { 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} }}
五、安裝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。
[[email protected] 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/kibanachmod +x /etc/init.d/kibana[[email protected] 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 nameNAME=kibanaDESC="Kibana4"PROG="/etc/init.d/kibana" # Source function library.. /etc/rc.d/init.d/functions # Configure location of Kibana binKIBANA_BIN=/data/home/user00/playcrab/elk/kibana/bin # PID InfoPID_FOLDER=/var/run/kibana/PID_FILE=/var/run/kibana/$NAME.pidLOCK_FILE=/var/lock/subsys/$NAMEPATH=/bin:/usr/bin:/sbin:/usr/sbin:$KIBANA_BINDAEMON=$KIBANA_BIN/$NAME # Configure User to run daemon processDAEMON_USER=root# Configure logging locationKIBANA_LOG=/var/log/kibana.log # Begin ScriptRETVAL=0 if [ `id -u` -ne 0 ]; then echo "You need root privileges to run this script" exit 1fi start() { echo -n "Starting $DESC : " pid=`pidofproc -p $PID_FILE kibana` if [ -n "$pid" ] ; then echo "Already running." exit 0 else # Start Daemonif [ ! -d "$PID_FOLDER" ] ; then mkdir $PID_FOLDER fidaemon --user=$DAEMON_USER --pidfile=$PID_FILE $DAEMON 1>"$KIBANA_LOG" 2>&1 & sleep 2 pidofproc node > $PID_FILE RETVAL=$? [[ $? -eq 0 ]] && success || failureecho [ $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}" >&2exit 2 ;;esac
到這裡已經安裝好了elk server端的環境了,但是還需要在Log Service器上配置rsyslog,因為咱們是使用rsyslog來發送Log Service器的log,然後elk接收到之後,經過logstash過濾之後,儲存到elasticsearch,最終通過kibana展示出來。
{ rsyslog } →→ { logstash →→ elasticsearch →→ kibana }
Log Service器的配置:
[[email protected] ~]# cd /etc/rsyslog.d/[[email protected] 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不生效;$InputFileStateFiletest_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:514if $programname == ‘test_nginx_access‘ then ~
[[email protected] 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 10if $programname == ‘php-fpm_log‘ then @10.23.0.24:515if $programname == ‘php-fpm_log‘ then ~
配置完後,需要重新啟動rsyslog.
/etc/init.d/rsyslog restart
除了重啟rsyslog服務之外,還需要看下elk server的防火牆,如果是用的雲主機還有安全性群組,需要把udp的514和515連接埠放開,允許Log Service器可以訪問elk server的udp514和515連接埠。
本文出自 “歲月在流逝,光輝依然在” 部落格,謝絕轉載!
搭建elk server通過rsyslog展示nginx和php日誌