Kubernetes Part6 ---- ELK Stack收集Kubernetes應用日誌
需求背景
•業務發展越來越龐大,伺服器越來越多
•各種訪問日誌、應用日誌、錯誤日誌量越來越多
•開發人員排查問題,需要到伺服器上查日誌,效率低、許可權不好控制
•運維需實時關注業務訪問情況
K8S環境中需要檢視的應用日誌
應用程式日誌記錄體現方式分為兩類:
•標準輸出:輸出到控制檯,使用kubectl logs可以看到
•日誌檔案:寫到容器的檔案系統的檔案
日誌獲取方式
針對標準輸出:以DaemonSet方式在每個Node上部署一個日誌收集程式,採集/var/lib/docker/containers/目錄下所有容器日誌
針對容器中日誌檔案:在Pod中增加一個容器執行日誌採集器,使用emtyDir共享日誌目錄讓日誌採集器讀取到日誌檔案
ELK 日誌系統介紹
ELK 是三個開源軟體的縮寫,提供一套完整的企業級日誌平臺解決方案。
分別是:
•Elasticsearch:搜尋、分析和儲存資料
•Logstash :採集日誌、格式化、過濾,最後將資料推送到Elasticsearch儲存
•Kibana:資料視覺化
•Beats :集合了多種單一用途資料採集器,用於實現從邊緣機器向Logstash 和Elasticsearch 傳送資料。裡面應用最多的是Filebeat,是一個輕量級日誌採集器。
ELK 日誌系統安裝配置
準備YAML檔案
[root@k8s-master03 elk]# ls -l total 20 -rw-r--r-- 1 root root 2108 Jan 1 18:09 app-log-logfile.yaml -rw-r--r-- 1 root root 528 Jan 1 18:09 app-log-stdout.yaml -rw-r--r-- 1 root root 1450 Jan 1 18:09 elasticsearch.yaml -rw-r--r-- 1 root root 3166 Jan 1 18:09 filebeat-kubernetes.yaml -rw-r--r-- 1 root root 924 Jan 1 18:09 kibana.yaml
搭建日誌系統:
•elasticsearch.yaml # ES資料庫
•kibana.yaml # 視覺化展示
日誌收集:
•filebeat-kubernetes.yaml # 採集所有容器標準輸出
•app-log-stdout.yaml # 標準輸出測試應用
•app-log-logfile.yaml # 日誌檔案測試應用
應用YAML
[root@k8s-master03 elk]# kubectl apply -f elasticsearch.yaml deployment.apps/elasticsearch created persistentvolumeclaim/es-pvc created service/elasticsearch created [root@k8s-master03 elk]# kubectl apply -f kibana.yaml deployment.apps/kibana created service/kibana created [root@k8s-master03 elk]# kubectl get pod -n ops NAME READY STATUS RESTARTS AGE elasticsearch-549b496f94-hnj4v 1/1 Running 0 79s grafana-757fcd5f7c-wdnt4 1/1 Running 0 90m kibana-55c8979979-kxqgh 1/1 Running 0 72s [root@k8s-master03 elk]# kubectl get svc -n ops NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE elasticsearch ClusterIP 10.106.236.50 <none> 9200/TCP 106s grafana NodePort 10.100.91.227 <none> 80:30030/TCP 90m kibana NodePort 10.109.229.66 <none> 5601:30601/TCP 99s kube-state-metrics ClusterIP 10.106.88.221 <none> 8080/TCP,8081/TCP 71m node-exporter ClusterIP None <none> 9100/TCP 78m prometheus NodePort 10.108.216.228 <none> 9090:30090/TCP 104m
NodePort訪問 http://172.16.0.21;30601 驗證可以登入
應用 filebeat 獲取所有容器標準輸出
[root@k8s-master03 elk]# kubectl apply -f filebeat-kubernetes.yaml configmap/filebeat-config created configmap/filebeat-inputs created daemonset.apps/filebeat created clusterrolebinding.rbac.authorization.k8s.io/filebeat created clusterrole.rbac.authorization.k8s.io/filebeat created serviceaccount/filebeat created [root@k8s-master03 elk]# kubectl get pod -n ops | grep file filebeat-7s9xq 1/1 Running 0 41s filebeat-qg7jw 1/1 Running 0 41s
檢視索引(日誌記錄集合):Management -> Stack Management -> 索引管理
將索引關聯到Kibana:索引模式-> 建立-> 匹配模式-> 選擇時間戳
一般一個索引對應一個應用日誌
導航至 discover
基於條件過濾 (只看ops namespace下的pod日誌)
kubernetes.namespace : "ops"
檢視索引(日誌記錄集合):Management -> Stack Management -> 索引管理