KubeSphere 開源 KubeEye:Kubernetes 集羣自動巡檢工具

爲何開源 KubeEye

Kubernetes 做爲容器編排的事實標準,雖然架構優雅功能也很是強大,可是 Kubernetes 在平常運行過程當中總會有一些疑難雜症和隱性的問題讓集羣管理員和 Yaml 工程師們很是頭疼,node

  • 基礎設施守護進程問題:ntp 服務中斷;
  • 硬件問題:如 CPU,內存或磁盤異常;
  • 內核問題:內核死鎖,文件系統損壞;
  • 容器運行時問題:運行時守護進程無響應;
  • ···

這樣的問題還有不少,而且這些隱性的異常問題對集羣的控制面來講是不可見的,所以 Kubernetes 將繼續將 Pod 調度到異常的節點,進而形成集羣和運行的應用帶來很是大的安全與穩定性的風險。nginx

什麼是 KubeEye

KubeEye 是一款開源的集羣自動巡檢工具,旨在發現 Kubernetes 上的各類問題,好比應用配置錯誤、集羣組件不健康和節點問題。KubeEye 使用 Go 語言基於 PolarisNode-Problem-Detector 開發,內置了一系列異常檢測規則。除了預約義的規則,它還支持自定義規則。git

KubeEye 能作什麼

  • 發現與檢測 Kubernetes 集羣控制平面的問題,包括 kube-apiserver/kube-controller-manager/etcd 等;
  • 幫助你檢測 Kubernetes 的各類節點問題,包括內存/CPU/磁盤壓力,意外的內核錯誤日誌等;
  • 根據行業最佳實踐驗證你的工做負載 yaml 規範,幫助你使你的集羣穩定。

架構圖

KubeEye 經過調用 Kubernetes API,經過常規匹配日誌中的關鍵錯誤信息和容器語法的規則匹配來獲取集羣診斷數據,詳見架構。github

內置檢查項

是/否 檢查項 描述
ETCDHealthStatus 若是 etcd 啓動並正常運行
ControllerManagerHealthStatus 若是 kubernetes kube-controller-manager 正常啓動並運行
SchedulerHealthStatus 若是 kubernetes kube-schedule 正常啓動並運行
NodeMemory 若是節點內存使用量超過閾值
DockerHealthStatus 若是 docker 正常運行
NodeDisk 若是節點磁盤使用量超過閾值
KubeletHealthStatus 若是 kubelet 激活狀態且正常運行
NodeCPU 若是節點 CPU 使用量超過閾值
NodeCorruptOverlay2 Overlay2 不可用
NodeKernelNULLPointer node 顯示 NotReady
NodeDeadlock 死鎖是指兩個或兩個以上的進程在爭奪資源時互相等待的現象。
NodeOOM 監控那些消耗過多內存的進程,尤爲是那些消耗大量內存很是快的進程,內核會殺掉它們,防止它們耗盡內存
NodeExt4Error Ext4 掛載失敗
NodeTaskHung 檢查D狀態下是否有超過 120s 的進程
NodeUnregisterNetDevice 檢查對應網絡
NodeCorruptDockerImage 檢查 docker 鏡像
NodeAUFSUmountHung 檢查存儲
NodeDockerHung Docker hang住, 檢查 docker 的日誌
PodSetLivenessProbe 若是爲pod中的每個容器設置了 livenessProbe
PodSetTagNotSpecified 鏡像地址沒有聲明標籤或標籤是最新
PodSetRunAsPrivileged 以特權模式運行 Pod 意味着 Pod 能夠訪問主機的資源和內核功能
PodSetImagePullBackOff Pod 沒法正確拉出鏡像,所以能夠在相應節點上手動拉出鏡像
PodSetImageRegistry 檢查鏡像形式是否在相應倉庫
PodSetCpuLimitsMissing 未聲明 CPU 資源限制
PodNoSuchFileOrDirectory 進入容器查看相應文件是否存在
PodIOError 這一般是因爲文件 IO 性能瓶頸
PodNoSuchDeviceOrAddress 檢查對應網絡
PodInvalidArgument 檢查對應存儲
PodDeviceOrResourceBusy 檢查對應的目錄和 PID
PodFileExists 檢查現有文件
PodTooManyOpenFiles 程序打開的文件/套接字鏈接數超過系統設置值
PodNoSpaceLeftOnDevice 檢查磁盤和索引節點的使用狀況
NodeApiServerExpiredPeriod 將檢查 ApiServer 證書的到期日期少於30天
PodSetCpuRequestsMissing 未聲明 CPU 資源請求值
PodSetHostIPCSet 設置主機 IP
PodSetHostNetworkSet 設置主機網絡
PodHostPIDSet 設置主機 PID
PodMemoryRequestsMiss 沒有聲明內存資源請求值
PodSetHostPort 設置主機端口
PodSetMemoryLimitsMissing 沒有聲明內存資源限制值
PodNotReadOnlyRootFiles 文件系統未設置爲只讀
PodSetPullPolicyNotAlways 鏡像拉策略並不是老是如此
PodSetRunAsRootAllowed 以 root 用戶執行
PodDangerousCapabilities 您在 ALL / SYS_ADMIN / NET_ADMIN 等功能中有危險的選擇
PodlivenessProbeMissing 未聲明 ReadinessProbe
privilegeEscalationAllowed 容許特權升級
NodeNotReadyAndUseOfClosedNetworkConnection http 2-max-streams-per-connection
NodeNotReady 沒法啓動 ContainerManager 沒法設置屬性 TasksAccounting 或未知屬性

注:未標註的項目正在開發中正則表達式

怎麼使用

  • 機器上安裝 KubeEyedocker

    • Releases 中下載預構建的可執行文件。
    • 或者你也能夠從源代碼構建
    git clone https://github.com/kubesphere/kubeeye.git
    cd kubeeye 
    make install
  • [可選] 安裝 Node-problem-Detector
    注意:這一行將在你的集羣上安裝 npd,只有當你想要詳細的報告時才須要。 ke install npdjson

  • KubeEye 執行自動巡檢:api

root@node1:# ke diag
NODENAME        SEVERITY     HEARTBEATTIME               REASON              MESSAGE
node18          Fatal        2020-11-19T10:32:03+08:00   NodeStatusUnknown   Kubelet stopped posting node status.
node19          Fatal        2020-11-19T10:31:37+08:00   NodeStatusUnknown   Kubelet stopped posting node status.
node2           Fatal        2020-11-19T10:31:14+08:00   NodeStatusUnknown   Kubelet stopped posting node status.
node3           Fatal        2020-11-27T17:36:53+08:00   KubeletNotReady     Container runtime not ready: RuntimeReady=false reason:DockerDaemonNotReady message:docker: failed to get docker version: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

NAME            SEVERITY     TIME                        MESSAGE
scheduler       Fatal        2020-11-27T17:09:59+08:00   Get http://127.0.0.1:10251/healthz: dial tcp 127.0.0.1:10251: connect: connection refused
etcd-0          Fatal        2020-11-27T17:56:37+08:00   Get https://192.168.13.8:2379/health: dial tcp 192.168.13.8:2379: connect: connection refused

NAMESPACE       SEVERITY     PODNAME                                          EVENTTIME                   REASON                MESSAGE
default         Warning      node3.164b53d23ea79fc7                           2020-11-27T17:37:34+08:00   ContainerGCFailed     rpc error: code = Unknown desc = Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
default         Warning      node3.164b553ca5740aae                           2020-11-27T18:03:31+08:00   FreeDiskSpaceFailed   failed to garbage collect required amount of images. Wanted to free 5399374233 bytes, but freed 416077545 bytes
default         Warning      nginx-b8ffcf679-q4n9v.16491643e6b68cd7           2020-11-27T17:09:24+08:00   Failed                Error: ImagePullBackOff
default         Warning      node3.164b5861e041a60e                           2020-11-27T19:01:09+08:00   SystemOOM             System OOM encountered, victim process: stress, pid: 16713
default         Warning      node3.164b58660f8d4590                           2020-11-27T19:01:27+08:00   OOMKilling            Out of memory: Kill process 16711 (stress) score 205 or sacrifice child Killed process 16711 (stress), UID 0, total-vm:826516kB, anon-rss:819296kB, file-rss:0kB, shmem-rss:0kB
insights-agent  Warning      workloads-1606467120.164b519ca8c67416            2020-11-27T16:57:05+08:00   DeadlineExceeded      Job was active longer than specified deadline
kube-system     Warning      calico-node-zvl9t.164b3dc50580845d               2020-11-27T17:09:35+08:00   DNSConfigForming      Nameserver limits were exceeded, some nameservers have been omitted, the applied nameserver line is: 100.64.11.3 114.114.114.114 119.29.29.29
kube-system     Warning      kube-proxy-4bnn7.164b3dc4f4c4125d                2020-11-27T17:09:09+08:00   DNSConfigForming      Nameserver limits were exceeded, some nameservers have been omitted, the applied nameserver line is: 100.64.11.3 114.114.114.114 119.29.29.29
kube-system     Warning      nodelocaldns-2zbhh.164b3dc4f42d358b              2020-11-27T17:09:14+08:00   DNSConfigForming      Nameserver limits were exceeded, some nameservers have been omitted, the applied nameserver line is: 100.64.11.3 114.114.114.114 119.29.29.29


NAMESPACE       SEVERITY     NAME                      KIND         TIME                        MESSAGE
kube-system     Warning      node-problem-detector     DaemonSet    2020-11-27T17:09:59+08:00   [livenessProbeMissing runAsPrivileged]
kube-system     Warning      calico-node               DaemonSet    2020-11-27T17:09:59+08:00   [runAsPrivileged cpuLimitsMissing]
kube-system     Warning      nodelocaldns              DaemonSet    2020-11-27T17:09:59+08:00   [cpuLimitsMissing runAsPrivileged]
default         Warning      nginx                     Deployment   2020-11-27T17:09:59+08:00   [cpuLimitsMissing livenessProbeMissing tagNotSpecified]
insights-agent  Warning      workloads                 CronJob      2020-11-27T17:09:59+08:00   [livenessProbeMissing]
insights-agent  Warning      cronjob-executor          Job          2020-11-27T17:09:59+08:00   [livenessProbeMissing]
kube-system     Warning      calico-kube-controllers   Deployment   2020-11-27T17:09:59+08:00   [cpuLimitsMissing livenessProbeMissing]
kube-system     Warning      coredns                   Deployment   2020-11-27T17:09:59+08:00   [cpuLimitsMissing]

可參考常見 FAQ內容來優化您的集羣。安全

添加自定義檢查規則

除了上述預置的巡檢項目與規則,KubeEye 還支持自定義檢查規則,來看個例子:微信

添加 npd 自定義檢查規則

  • 安裝 NPD 指令 ke install npd
  • 由 kubectl 編輯 configmap kube-system/node-problem-detector-config,
kubectl edit cm -n kube-system node-problem-detector-config
  • 在 configMap 的規則下添加異常日誌信息,規則遵循正則表達式。

自定義最佳實踐規則

  • 準備一個規則 yaml,例如,下面的規則將驗證你的 Pod 規範,以確保鏡像只來自受權的註冊處。
checks:
  imageFromUnauthorizedRegistry: warning

customChecks:
  imageFromUnauthorizedRegistry:
    promptMessage: When the corresponding rule does not match. Show that image from an unauthorized registry.
    category: Images
    target: Container
    schema:
      '$schema': http://json-schema.org/draft-07/schema
      type: object
      properties:
        image:
          type: string
          not:
            pattern: ^quay.io
  • 將上述規則保存爲 yaml,例如 rule.yaml

  • rule.yaml 運行 KubeEye。

root:# ke diag -f rule.yaml --kubeconfig ~/.kube/config
NAMESPACE     SEVERITY    NAME                      KIND         TIME                        MESSAGE
default       Warning     nginx                     Deployment   2020-11-27T17:18:31+08:00   [imageFromUnauthorizedRegistry]
kube-system   Warning     node-problem-detector     DaemonSet    2020-11-27T17:18:31+08:00   [livenessProbeMissing runAsPrivileged]
kube-system   Warning     calico-node               DaemonSet    2020-11-27T17:18:31+08:00   [cpuLimitsMissing runAsPrivileged]
kube-system   Warning     calico-kube-controllers   Deployment   2020-11-27T17:18:31+08:00   [cpuLimitsMissing livenessProbeMissing]
kube-system   Warning     nodelocaldns              DaemonSet    2020-11-27T17:18:31+08:00   [runAsPrivileged cpuLimitsMissing]
default       Warning     nginx                     Deployment   2020-11-27T17:18:31+08:00   [livenessProbeMissing cpuLimitsMissing]
kube-system   Warning     coredns                   Deployment   2020-11-27T17:18:31+08:00   [cpuLimitsMissing]

Roadmap

  • 支持更細粒度的巡檢項,例如集羣響應速度慢
  • 支持對巡檢結果生成集羣巡檢報告
  • 支持集羣巡檢報告導出爲 CSV 格式或 HTML 文件

你還但願 KubeEye 提供什麼樣的特性呢?歡迎來 Github 提交建議或需求~

GitHub 地址:https://github.com/kubesphere/kubeeye

參考連接

KubeEye Release:https://github.com/kubesphere/kubeeye/releases

KubeEye FAQ 文檔:https://github.com/kubesphere/kubeeye/blob/main/docs/FAQ.md

Node-Problem-Detector:https://github.com/kubernetes/node-problem-detector

關於 KubeSphere

KubeSphere 是在 Kubernetes 之上構建的容器混合雲,提供全棧的 IT 自動化運維的能力,簡化企業的 DevOps 工做流。

KubeSphere 已被 Aqara 智能家居、原本生活、新浪、中國人保壽險、華夏銀行、浦發硅谷銀行、四川航空、國藥集團、微衆銀行、紫金保險、Radore、ZaloPay 等海內外數千家企業採用。KubeSphere 提供了運維友好的嚮導式操做界面和豐富的企業級功能,包括多雲與多集羣管理、Kubernetes 資源管理、DevOps (CI/CD)、應用生命週期管理、微服務治理 (Service Mesh)、多租戶管理、監控日誌、告警通知、存儲與網絡管理、GPU support 等功能,幫助企業快速構建一個強大和功能豐富的容器雲平臺。

KubeSphere 官網https://kubesphere.io/ KubeSphere GitHubhttps://github.com/kubesphere/kubesphere

KubeSphere 微信公衆號

本文由博客一文多發平臺 OpenWrite 發佈!

相關文章
相關標籤/搜索