使用EFK收集Kubernetes多集羣日誌

使用EFK收集Kubernetes多集羣日誌

Collecting logs for multi kubernetes clusters with EFKjavascript

在有多個集羣的狀況下,一個集羣就搭建一套日誌收集分析服務實在太浪費。所以能夠嘗試只跑一套Elasticsearch和Kibana實例,而後讓各個集羣中的日誌收集器向Elasticsearch發送日誌。html

如圖所示:java

graph TD

Elastic[Elastic server]

Elastic-->|analysis the data|Kibana

cluster1-->|send log|Elastic
cluster2-->|send log|Elastic
cluster3-->|send log|Elastic
cluster(...)-->|send log|Elastic

Elasticsearch和Kibana能夠放在Kubernetes集羣中也能夠獨立部署。這裏爲了方便就選擇獨立部署.node

部署ElasticSearch和Kibana

Elastic自建了docker鏡像倉庫,所以到https://www.docker.elastic.co/拉去最新的Elasticsearch和Kibana 的鏡像.git

而後編寫docker-compose.yaml文件.github

關於詳細配置,參考官方文檔:docker

version: '2'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.4.0
    restart: unless-stopped
    ports:
     - "9200:9200"
     - "9300:9300"
    ulimits:
      memlock:
        soft: -1
        hard: -1
      nofile:
        soft: -1
        hard: -1
    environment:
      bootstrap.memory_lock: "true"
      discovery.type: "single-node"
    volumes:
     - /opt/es_data:/usr/share/elasticsearch/data
  kibana:
    image: docker.elastic.co/kibana/kibana:6.4.0
    restart: unless-stopped
    ports:
     - "5601:5601"
    environment:
      server.host: "0.0.0.0"
      elasticsearch.url: "http://elasticsearch:9200"
    volumes:
    # 掛載kibana.yaml,能夠在其中編寫更詳細的配置.這裏圖方便,用環境變量傳進去.
    #  - ./kibana.yml:/usr/share/kibana/config/kibana.yml:ro
     - /opt/kibana_data:/usr/share/kibana/data

而後啓動docker-compose up -d,查看日誌 docker-compose logs -f能夠看到ES和Kibana輸出的所有都是INFO等級的日誌,沒有ERROR,而且Kibana已經鏈接上ES。json

curl -X GET 127.0.0.1:9200bootstrap

{
  "name" : "h9sEa61",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "8MjE8hwVSq2Vvbe2azZggQ",
  "version" : {
    "number" : "6.4.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "595516e",
    "build_date" : "2018-08-17T23:18:47.308994Z",
    "build_snapshot" : false,
    "lucene_version" : "7.4.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

curl 127.0.0.1:5601瀏覽器

<script>
var hashRoute = '/app/kibana';
var defaultRoute = '/app/kibana';

var hash = window.location.hash;
if (hash.length) {
  window.location = hashRoute + hash;
} else {
  window.location = defaultRoute;
}
</script>

能夠看到ES和Kibana成功返回數據.

部署Fluent

將Fluent以DaemonSet的方式部署在Kuberntes集羣中,讓Fluent收集每一個Node上的日誌。

在Gihub上Fluent的官方庫下載fluentd-daemonset-elasticsearch-rbac.yaml

只需對yaml文件修改一個地方,就是將envFLUENT_ELASTICSEARCH_HOST的value改爲ES的IP地址或網址便可。至於X-Pack的鑑權,ES默認是收費使用的,暫不用理會,或裝第三方的HTTP Basic Auth實現鑑權。

同時有的人會遇到一個錯誤,若是K8s集羣是用root權限運行的,則fluentd在收集日誌的時候會遇到權限問題。

由於fluentd鏡像在構建的時候用的是fluent用戶權限運行,因此會發生權限不足的狀況。

解決方法就是能夠去拉取fluent代碼自行構建並在Dockfile中指定用戶;或使用最簡單的方法,在env中指定fluent用戶的UID爲0,以下所示:

...
        env:
          - name: FLUENT_UID
            value: "0"
...

查看es日誌,發現相似以下的日誌

xxx update_mapping [fluentd]

說明fluent已經鏈接.

用瀏覽器打開Kibana,點擊Discover欄能夠看到相似logstash-xxxx.xx.xx的條目,說明Kibana成功獲取到了日誌。接下來就是根據本身的日誌格式編寫過濾語句了。

相關文章
相關標籤/搜索