顯示具有 kibana 標籤的文章。 顯示所有文章
顯示具有 kibana 標籤的文章。 顯示所有文章

2014年9月24日 星期三

強化ELK平台ElasticSearch的安全性

在前一篇安裝說明時,是將ElasticSearch的9200 port開放出來,但這樣一來,所有人都可以操作ElasticSearch。

本篇關閉了9200~9300的對外port,限定只能從localhost存取ElasticSearch,然後利用apache的proxy功能,讓Kinaba透過80port 存取Elastic Search。

  1. 修改 /etc/elasticsearch/elasticsearch.yml 設定檔,新增以下兩行,限定只能在本機連結ElasticSearch
    script.disable_dynamic: true
    network.host: localhost

  2. 修改 Apache的設定, 在 /etc/httpd/conf.d 中新增檔案 elk.conf (名稱可以自訂)
    # Courtesy of https://github.com/sgzijl
    # config.js includes elasticsearch: "https://"+window.location.hostname+":80",

    <VirtualHost *:80>
    ServerName LogServer

    DocumentRoot /var/www/html
    <Directory /var/www/html>
    Allow from all
    Options -Multiviews
    </Directory>

    # Set global proxy timeouts
    <Proxy http://127.0.0.1:9200>
    ProxySet connectiontimeout=5 timeout=90
    </Proxy>

    # Proxy for _aliases and .*/_search
    <LocationMatch "^/(_nodes|_aliases|.*/_aliases|_search|.*/_search|_mapping|.*/_mapping)$">
    ProxyPassMatch http://127.0.0.1:9200/$1
    ProxyPassReverse http://127.0.0.1:9200/$1
    </LocationMatch>

    # Proxy for kibana-int/{dashboard,temp} stuff (if you don't want auth on /, then you will want these to be protected)
    <LocationMatch "^/(kibana-int/dashboard/|kibana-int/temp)(.*)$">
    ProxyPassMatch http://127.0.0.1:9200/$1$2
    ProxyPassReverse http://127.0.0.1:9200/$1$2
    </LocationMatch>

    # Optional disable auth for a src IP (eg: your monitoring host or subnet)
    <Location />
    Allow from 10.10.10.*
    Deny from all
    Satisfy any

    AuthType Basic
    AuthName "Log Server Authentication"
    AuthUserFile /var/www/html/.htpasswd
    require valid-user
    </Location>

    </VirtualHost>


  3. 在/var/www/html 目錄下增加 .htpasswd 作為使用者認證
    # htpasswd -c .htpasswd <username>
    New password:
    Re-type new password:
    Adding password for user <username>
    #


  4. 修正防火牆 iptables ,關閉原本開放的 9200-9300 port
    #!/bin/bash
    #
    # iptables 範例設定腳本
    #
    # 清除 iptables 內一切現存的規則
    #
    iptables -F
    #
    # 容讓 SSH 連線到 tcp 連接埠 22
    # 當透過 SSH 遠端連線到伺服器,你必須這樣做才能群免被封鎖於系統外
    #
    iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    #
    # 開啟HTTP 80 port
    #
    iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    #
    # 設定ElasticSearch使用的port 9200-9300
    #
    #iptables -A INPUT -p tcp --dport 9200:9300 -j ACCEPT
    #
    # 設定 INPUT、FORWARD、及 OUTPUT 鏈的預設政策
    #
    iptables -P INPUT DROP
    iptables -P FORWARD DROP
    iptables -P OUTPUT ACCEPT
    #
    # 設定 localhost 的存取權
    #
    iptables -A INPUT -i lo -j ACCEPT
    #
    # 接納屬於現存及相關連線的封包
    #
    iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    #
    # 儲存設定
    #
    /sbin/service iptables save
    #
    # 列出規則
    #
    iptables -L -v


  5. 修改Kibana的 config.js檔案 (/var/www.html),將原本 9200 port改為 80 port
     elasticsearch: "http://"+window.location.hostname+":80"

2014年9月22日 星期一

使用ELK(Elastic Search、Logstash、Kibana)建立Log查詢、蒐集與分析系統


公司想要開始要建立統一的Log查詢、蒐集與分析的環境,因此嘗試使用ELK來建立。

環境說明

先嘗試建立Standalone的環境,也就是ELK都先放在同一台。
  • 作業系統: CentOS 6.5 final
  • Elastic Search 1.3.2
  • Logstash 1.4.2
  • Kibana

參考資料


軟體安裝

  1. 作業系統安裝

    • 請記得安裝已下套件
      • Apache httpd (yum install httpd)
      • Java 7 SDK
  2. 安裝Elastic Search

    • 下載公鑰
      rpm --import http://packages.elasticsearch.org/GPG-KEY-elasticsearch
    • 在 /etc/yum.repos.d/ 目錄下新增檔案,例如 elasticsearch.repo,內容如下
      [elasticsearch-1.3]
      name=Elasticsearch repository for 1.3.x packages
      baseurl=http://packages.elasticsearch.org/elasticsearch/1.3/centos
      gpgcheck=1
      gpgkey=http://packages.elasticsearch.org/GPG-KEY-elasticsearch
      enabled=1
    • 安裝Elastersearch
      yum install elasticsearch

      image
  3. 設定Elasticsearch



    • 編輯 /etc/elasticsearch/elasticsearch.yml ,加入

      cluster.name: "LogCluster"
      node.name: "LogMaster"
      node.master: true
      node.data: true
      
      path.conf: /etc/elasticsearch
      path.data: /datapool/data1
      path.work: /datapool/work
      path.logs: /datapool/log
    • 將服務加入系統
      chkconfig --add elasticsearch

    • 啟動服務
      /etc/init.d/elasticsearch start

    • 測試啟動是否成功
      curl localhost:9200/_nodes/process?pretty
      
      {
        "cluster_name" : "LogCluster",
        "nodes" : {
          "sqhS68RMS3q1CtthFOdSxw" : {
            "name" : "LogMaster",
            "transport_address" : "inet[/10.10.10.151:9300]",
            "host" : "logserver",
            "ip" : "127.0.0.1",
            "version" : "1.3.2",
            "build" : "dee175d",
            "http_address" : "inet[/10.10.10.151:9200]",
            "attributes" : {
              "master" : "true"
            },
            "process" : {
              "refresh_interval_in_millis" : 1000,
              "id" : 30839,
              "max_file_descriptors" : 65535,
              "mlockall" : false
            }
          }
        }
      }
      

  4. 安裝Logstash



    • 參考資料:http://logstash.net/docs/1.4.2/repositories
    • 安裝金鑰
      rpm --import http://packages.elasticsearch.org/GPG-KEY-elasticsearch
    • 在 /etc/yum.repos.d/ 目錄下新增檔案,例如 logstash.repo,內容如下
      [logstash-1.4]
      name=logstash repository for 1.4.x packages
      baseurl=http://packages.elasticsearch.org/logstash/1.4/centos
      gpgcheck=1
      gpgkey=http://packages.elasticsearch.org/GPG-KEY-elasticsearch
      enabled=1
    • 安裝Logstash
      yum install logstash
      image

  5. 安裝Kibana



    • Kibana是一個純粹由javascript/CSS/html組成的套件,安裝好logstash之後可以在 /opt/logstash/vendor/kibana 目錄下找到。
    • 複製到httpd的 /var/www/html/ 目錄之下
      cp -R /opt/logstash/vendor/kibana/* /var/www/html/
    • 啟動httpd服務
      chkconfig httpd on
      
      /dev/init.d/httpd start
  6. 設定防火牆



    • 預設的防火牆是沒有開啟http及elastersearch的port,請加入以下兩個規則
      #
      # 開啟HTTP 80 port
      #
      iptables -A INPUT -p tcp --dport 80 -j ACCEPT
      #
      # 設定ElasticSearch使用的port 9200-9300
      #
      iptables -A INPUT -p tcp --dport 9200:9300 -j ACCEPT
      

  7. 開始使用



    • 用瀏覽器進入 http://<Server Address>/

image




待續…



  1. Logstash接收log設定值

  2. Kibana設定與使用

  3. Elastersearch進階設定

  4. and more…