Project

General

Profile

Support #437

Updated by Daniel Curtis about 9 years ago

{{>toc}} 

 I've decided to centralize all the logs generated by a client's production systems to a syslog server and after assessing a bunch of products, I am now working with Fluentd. 

 The platform chosen to run Fluentd is FreeBSD inside a Jail (9.3-RELEASE at the time), a rock-solid and very well documented UNIX-like operating system. Besides, it also ships a production-ready ZFS implementation which always comes handy in the data center. FreeBSD-9.3 currently has the _sysutils/rubygem-fluentd_ is available in the ports tree. This is a guide on how to install Fluentd with ElasticSearch and Kibana.  

 h1. Setting up the Environment 

 * Start by making sure everything is up to date: 
 <pre> 
 pkg update && pkg upgrade 
 portsnap fetch extract 
 </pre> 

 * Set the default Ruby version to 2.2: 
 <pre> 
 echo 'DEFAULT_VERSIONS= ruby=2.2' >> /etc/make.conf 
 </pre> 

 * Set the default Java version to 8: 
 <pre> 
 echo 'JAVA_PREFERRED_PORTS=JAVA_PORT_NATIVE_OPENJDK_JDK_1_8' >> /etc/make.conf 
 </pre> 

 * Install portmaster: 
 <pre> 
 cd /usr/ports/ports-mgmt/portmaster 
 make install clean 
 pkg2ng 
 </pre> 

 h1. Install Fluentd 

 * Install rubygem-fluentd 
 <pre> 
 portmaster sysutils/rubygem-fluentd 
 </pre> 

 * Start and enable fluentd at boot: 
 <pre> 
 echo 'fluentd_enable="YES"' >> /etc/rc.conf 
 service fluentd start 
 </pre> 

 h2. Installing Fluentd Plugins 

 We need a couple of plugins: 
 # *out_elasticsearch*: this plugin lets Fluentd to stream data to Elasticsearch. 
 # *outrecordreformer*: this plugin lets us process data into a more useful format. 

 * The following commands install both plugins: 
 <pre> 
 fluent-gem install fluent-plugin-elasticsearch 
 fluent-gem install fluent-plugin-record-reformer 
 </pre> 

 h2. Add the Syslog configuration to Fluentd 

 Next, we configure Fluentd to listen to syslog messages and send them to Elasticsearch.  

 * Edit the fluentd config file: 
 <pre> 
 vi /usr/local/etc/fluentd/fluent.conf 
 </pre> 
 #* And add the following lines at the top of the file: 
 <pre> 
 ## Syslog input 
 <source> 
  type syslog 
  port 5140 
  tag    system 
 </source> 
 <match system.*.*> 
  type record_reformer 
  tag elasticsearch 
  facility ${tag_parts[1]} 
  severity ${tag_parts[2]} 
 </match> 
 <match elasticsearch> 
  type copy 
  <store> 
    type stdout 
  </store> 
  <store> 
  type elasticsearch 
  logstash_format true 
  flush_interval 5s #debug 
  </store> 
 </match> 
 </pre> 

 * Restart fluentd: 
 <pre> 
 service fluentd restart 
 </pre> 
 #* (Optional) Start Fluentd with verbose debugging, run the following command: 
 <pre> 
 fluentd -c /usr/local/fluentd/fluent.conf -vv & 
 </pre> 

 --- 

 h1. Install ElasticSearch 

 * Install ElasticSearch: 
 <pre> 
 portmaster textproc/elasticsearch 
 </pre> 

 * Start and enable ElasticSearch at boot 
 <pre> 
 echo 'elasticsearch_enable="YES"' >> /etc/rc.conf 
 service elasticsearch start 
 </pre> 

 h2. Securing Elasticsearch 

 * Up to version 1.2, Elasticsearch's dynamic scripting capability was enabled by default. Since this tutorial sets up the Kibana dashboard to be accessed from the public Internet, let's disable dynamic scripting by running the following line to append the parameter at the end of the ElasticSearch configuration file: 
 <pre> 
 echo 'script.disable_dynamic: true' >> /usr/local/etc/elasticsearch/elasticsearch.yml 
 </pre> 

 * Restart Elasticsearch: 
 <pre> 
 service elasticsearch restart 
 </pre> 

 --- 

 h1. Install Kibana 

 * Install kibana: 
 <pre> 
 portmaster textproc/kibana 
 </pre> 

 h3. Configuring Kibana 

 Since Kibana will use port 80 to talk to Elasticsearch as opposed to the default port 9200, Kibana's config.js must be updated. 

 * Open Kibana configuration file and look for the following line: 
 <pre> 
 vi /usr/local/www/kibana/config.js 
 </pre> 
 #* And change the @elasticsearch: "http://"+window.location.hostname+":9200",@ parameter to the following: 
 <pre> 
 elasticsearch: "http://"+window.location.hostname+":80", 
 </pre> 

 h2. Setting Up Kibana Dashboard Panels 

 Kibana's default panels are very generic, so it's recommended to customize them. Here, we show two methods. 

 h3. *Method 1*: Using a Template 

 * The Fluentd team offers an alternative Kibana configuration that works with this setup better than the default one. To use this alternative configuration, run the following command: 
 <pre> 
 sudo cp default.json /usr/local/kibana/app/dashboards/default.json 
 </pre> 

 Note: The original configuration file is from the author's GitHub gist. 

 If you refresh your Kibana dashboard home page at your server's URL, Kibana should now be configured to show histograms by syslog severity and facility, as well as recent log lines in a table. 

 h3. *Method 2*: Manually Configuring 

 Go to your server's IP address or domain to view the Kibana dashboard. 

 There are a couple of starter templates, but let's choose the blank one called *Blank Dashboard*. 

 Next, click on the *+ ADD A ROW* button on the right side of the dashboard. A configuration screen for a new row (a row consists of one or more panels) should show up. Enter a title, press the *Create Row* button, followed by *Save*. This creates a row. 

 When an empty row is created, Kibana shows the prompt Add panel to empty row on the left. Click this button. It takes you to the configuration screen to add a new panel. Choose histogram from the dropdown menu.  

 There are many parameters to configure for a new histogram, but you can just scroll down and press the Save button. This creates a new panel. 

 h1. Install Nginx 

 We will use Nginx as a proxy server to allow access to the dashboard from the Public Internet (with basic authentication). 

 * Install Nginx as follows: 
 <pre> 
 portmaster www/nginx security/py-htpasswd 
 </pre> 

 * Enable nginx to start at boot 
 <pre> 
 echo 'nginx_enable="YES"' >> /etc/rc.conf 
 </pre> 

 * Start nginx 
 <pre> 
 service nginx start 
 </pre> 

 * Edit /usr/local/etc/nginx/nginx.conf  
 <pre> 
 vi /usr/local/etc/nginx/nginx.conf 
 </pre> 
 #* And change the primary server block as follows: 
 <pre> 
 # 
 # Nginx proxy for Elasticsearch + Kibana 
 # 
 # In this setup, we are password protecting the saving of dashboards. You may 
 # wish to extend the password protection to all paths. 
 # 
 # Even though these paths are being called as the result of an ajax request, the 
 # browser will prompt for a username/password on the first request 
 # 
 # If you use this, you'll want to point config.js at http://FQDN:80/ instead of 
 # http://FQDN:9200 
 # 
 server { 
  listen                  *:80 ; 
  server_name             localhost; 
  access_log              /var/log/nginx-kibana.log; 

  location / { 
    root    /usr/local/www/kibana; 
    index    index.html    index.htm; 
  } 

  location ~ ^/_aliases$ { 
   proxy_pass http://127.0.0.1:9200; 
   proxy_read_timeout 90; 
  } 

  location ~ ^/.*/_aliases$ { 
   proxy_pass http://127.0.0.1:9200; 
   proxy_read_timeout 90; 
  } 

  location ~ ^/_nodes$ { 
   proxy_pass http://127.0.0.1:9200; 
   proxy_read_timeout 90; 
  } 

  location ~ ^/.*/_search$ { 
   proxy_pass http://127.0.0.1:9200; 
   proxy_read_timeout 90; 
  } 

  location ~ ^/.*/_mapping { 
   proxy_pass http://127.0.0.1:9200; 
   proxy_read_timeout 90; 
  } 

  # Password protected end points 
  location ~ ^/kibana-int/dashboard/.*$ { 
   proxy_pass http://127.0.0.1:9200; 
   proxy_read_timeout 90; 
   limit_except GET { 
    proxy_pass http://127.0.0.1:9200; 
    auth_basic "Restricted"; 
    auth_basic_user_file /usr/local/etc/nginx/log.example.com.htpasswd; 
   } 
  } 

  location ~ ^/kibana-int/temp.*$ { 
   proxy_pass http://127.0.0.1:9200; 
   proxy_read_timeout 90; 
   limit_except GET { 
    proxy_pass http://127.0.0.1:9200; 
    auth_basic "Restricted"; 
    auth_basic_user_file /usr/local/etc/nginx/log.example.com.htpasswd; 
   } 
  } 
 } 
 </pre> 

 * And generate a htpasswd file: 
 <pre> 
 python2.7 /usr/local/bin/htpasswd.py -c -b /usr/local/etc/nginx/log.example.com.htpasswd username SuperSecretPassword 
 </pre> 
 *NOTE*: Make sure to change the username and SuperSecretPassword to your needs 

 * Start and enable nginx at boot: 
 <pre> 
 echo 'nginx_enable="YES"' >> /etc/rc.conf 
 service nginx start 
 </pre> 

 Now, you should be able to see the generic Kibana dashboard at your server's IP address or domain, using your favorite browser. 

 --- 

 h1. Forwarding Syslog to Fluentd 

 h2. Forwarding Debian rsyslog Traffic to Fluentd 

 I use Debian an many production systems, and one one the packages is rsyslogd. It needs to be reconfigured to forward syslog events to the port Fluentd listens to (port 5140 in this example). 

 * Open the rsyslog configuration file and add the following line at the top 
 <pre> 
 sudo vi/etc/rsyslog.conf 
 </pre> 
 #* And add the following line 
 <pre> 
 *.* @127.0.0.1:5140 
 </pre> 

 * After saving and exiting the editor, restart rsyslogd as follows: 
 <pre> 
 sudo service rsyslog restart 
 </pre> 

 h2. Forwarding FreeBSD syslogd Traffic to Fluentd 

 I also have been switching many of my production systems to FreeBSD, and the default logging mechanism is syslogd. It needs to be reconfigured to forward syslog events to the port Fluentd listens to (port 5140 in this example). 

 * Open the rsyslog configuration file and add the following line at the top 
 <pre> 
 sudo vi/etc/syslog.conf 
 </pre> 
 #* And add the following line 
 <pre> 
 *.* @127.0.0.1:5140 
 </pre> 

 --- 

 h1. Resources 

 * http://docs.fluentd.org/articles/install-from-source 
 * https://www.digitalocean.com/community/tutorials/elasticsearch-fluentd-and-kibana-open-source-log-search-and-visualization 
 * https://raw.githubusercontent.com/elasticsearch/kibana/kibana3/sample/nginx.conf 

Back