Upgrade to Pro — share decks privately, control downloads, hide ads and more …

使用Docker建置 EFK

Avatar for reyhsu reyhsu
August 11, 2017

使用Docker建置 EFK

使用docker建置站台並且使用EFK收集log

Avatar for reyhsu

reyhsu

August 11, 2017

Other Decks in Technology

Transcript

  1.  Elasticsearch  一套資料搜尋引擎  Fluentd  用來收集、整合log  Kibana

     一套視覺與圖形化的軟體,把分析的資料用圖 像的方式來表現
  2.  Elasticsearch 有分散式、即時性  Cluster:一群安裝Elasticsearch的站台  Node:各個站台  Shard:中譯叫做分片,是將一個完整的 Index

    分成多個部分,儲 存在相同或不同的 Node 上  Replica: Shard 的備份  Elasticsearch資料單位  Field:儲存資料的最小單位,類似於關聯式資料庫的 Column  Document: Field 集合成一個 Document,類似於資料庫的 Row  Type:一個 Document 必須隸屬於一個 Type,類似於資料庫的 Table
  3.  在Fluentd中,設定檔非常重要,它定義了 Fluentd應該執行的操作  source:定義資料來源  match:告訴Fluentd做什麼  filter:過濾,事件處理 

    system:系統設置  label :定義重複使用、內部路由 <source> type tail tag nginx.access path /var/log/nginx/access.log pos_file /var/lib/google-fluentd/pos/nginx-access.pos format /^(?<remote>[^ ]*) (?<host>[^ ]*) (?<user>[^ ]*) \[(?<time>[^\]]*)\] "(?<method>\S+)(?: +(?<path>[^ ]*) +\S*)?" (?<code>[^ ]*) (?<size>[^ ]*)(?: "(?<referer>[^\"]*)" "(?<agent>[^\"]*)" "(?<forwarder>[^\"]*)")?/ time_format %d/%b/%Y:%H:%M:%S %z read_from_head true </source> <match nginx.**> type forward <server> host 8d2b50a49fed </server> </match>
  4.  提供許多Input、Output格式的Plugin  例 Input • in_forward • in_tail •

    in_syslog Output • out_file • out_forward • out_stdout
  5.  修改完td-agent.conf後  log 出現 Permission denied  兩種解決方式 

    1. 將/etc/init.d/td-agent 底下的執行者改為root  2. 將td-agent使用者加入 adm group sudo usermod -aG adm td-agent tail -f /var/log/td-agent/td-agent.log 2015-08-19 14:17:14 +0900 [error]: Permission denied @ xxxxxxx - /var/log/nginx/error.log 2015-08-19 14:17:14 +0900 [error]: suppressed same stacktrace
  6.  建立索引 <match **> type elasticsearch logstash_format true host localhost

    port 9200 target_index_key sp index_name fluentd target_type_key fluentd type_name fluentd flush_interval 5s </match>
  7.  frontend  設定http請求  綁定port  backend  設定balance

    模式  HAProxy後面對應的Server  listen stats:8080  設定儀表板  設定對應的port  登入帳戶、密碼 frontend http-in bind *:80 default_backend website option forwardfor backend website mode http balance roundrobin option forwardfor server web1 127.0.0.1:8081 check server web2 127.0.0.1:8083 check listen stats :8080 mode http stats enable stats hide-version stats uri / stats auth admin:admin
  8.  與Apache一樣作為Web Server  具備Load Balance功能  設定 Load balance

    設定檔,需要使用 upstream 這個模組  編輯設定檔nginx.conf upstream backend { server 127.0.0.1:8081; server 127.0.0.1:8083; } server { listen 80; server_name www.example.com; location / { proxy_pass http://backend; # 指到 upstream } }
  9.  Container技術直接將一個應用程式所需的相 關程式碼、函式庫、環境配置檔都打包起來運 行  容器除了執行其中 應用外,基本不消 耗額外的系統資源  Dockerfile

     使用 Dockerfile 讓使用者 可以建立自定義的映像檔 docker run --privileged=true -d -v /var/log/nginx:/var/log/nginx -v /var/lib/google-fluentd/pos/:/var/lib/google-fluentd/pos/ --name EFK --link web1:web1 -p 24224:24224 -p 8082:80 -p 5601:5601 -p 9200:9200 --expose=443 --expose=5140 --expose=9300 bcd4bb2d0f9b /usr/sbin/init
  10.  建立一個dockerfile文件  docker build -t mysite .  包成自己的image

     docker run -d -p 80:80 mysite FROM ubuntu:12.04 RUN apt-get update && \ apt-get install -y apache2 && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* ENV APACHE_RUN_USER www-data \ APACHE_RUN_GROUP www-data \ APACHE_LOG_DIR /var/log/apache2 EXPOSE 80 CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]
  11. Container Container Nginx Proxy WEB01 Nginx (log)→tail→Fluentd WEB02 Nginx (log)→tail→Fluentd

    Container Elasticsearch Fluentd Kibana 8081 port 8083 port forward 80 port 80 port Listen:80