一、系统和所需软件版本介绍

系统版本: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_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环境是否安装成功。



[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)



三、安装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.gz tar -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.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" }



四、安装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}
	  }
	}



  五、安装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端口。