【智簡聯接,萬物互聯】華為云·云享專家董昕:Serverless和微服務下, IoT的變革蓄勢待發
909
2025-04-02
一 前言
kubeEye 是一款 kubernetes 安全及配置問題檢測工具,針對部署在 K8s 集群中的業務應用進行配置檢測使用 OPA,針對集群部署的 Node 使用Node-Problem-Detector進行檢測,同時除了系統內置有根據大多數業界常見場景的預定義規則,還支持用戶自定義規則來進行集群檢測。
二 架構
KubeEye 通過調用 Kubernetes API,通過匹配資源中的關鍵字和容器語法的規則匹配來獲取集群診斷數據,詳見架構圖。
其中針對 Node 節點檢測,需要在被檢測 Node 主機上安裝
三 特點
3.1 特性
KubeEye 根據行業最佳實踐審查你的工作負載 yaml 規范,幫助你使你的集群穩定。
KubeEye 可以發現你的集群控制平面的問題,包括 kube-apiserver/kube-controller-manager/etcd 等。
KubeEye 可以幫助你檢測各種節點問題,包括內存/CPU/磁盤壓力,意外的內核錯誤日志等。
3.2 檢查項
四 部署
kubeEye 本身使用 Golang 編寫,可使用編譯好的二進制可執行文件進行相關組件安裝。
4.1 安裝
wget https://github.com/kubesphere/kubeeye/releases/download/v0.3.0/kubeeye-0.3.0-linux-amd64.tar.gz tar -zxvf kubeeye-0.3.0-linux-amd64.tar.gz mv kubeeye /usr/bin/
git clone https://github.com/kubesphere/kubeeye.git cd kubeeye make installke
4.2 安裝NPD
針對集群 Node 主機的檢測,kubeEye 采用Node-problem-Detector ,需要在 Node 主機節點進行安裝,kubeeye 封裝安裝命令,可以進行一鍵安裝。
??注意:這將在你的集群上安裝 npd,只有當你想要詳細的節點報告時才需要。
[root@VM-48-7-centos ~]# kubeeye install -e npd kube-system ConfigMap node-problem-detector-config created kube-system DaemonSet node-problem-detector created
其主要在 kube-system 名稱空間創建 node-problem-detector-config 的 ConfigMap 和 node-problem-detector DaemonSet。
4.3 集群中運行kubeEye
kubeEye 除了可以一次性使用工具運行,同時 kubeEye 也是一個 Operator,可以運行在集群內部,進行長久的持續對集群進行檢測。
kubectl apply -f https://raw.githubusercontent.com/kubesphere/kubeeye/main/deploy/kubeeye.yaml kubectl apply -f https://raw.githubusercontent.com/kubesphere/kubeeye/main/deploy/kubeeye_insights.yaml
$ kubectl get clusterinsight -o yaml apiVersion: v1 items: - apiVersion: kubeeye.kubesphere.io/v1alpha1 kind: ClusterInsight metadata: name: clusterinsight-sample namespace: default spec: auditPeriod: 24h status: auditResults: auditResults: - resourcesType: Node resultInfos: - namespace: "" resourceInfos: - items: - level: waring message: KubeletHasNoSufficientMemory reason: kubelet has no sufficient memory available - level: waring message: KubeletHasNoSufficientPID reason: kubelet has no sufficient PID available - level: waring message: KubeletHasDiskPressure reason: kubelet has disk pressure name: kubeeyeNode
五 測試
5.1 命令選項
[root@VM-48-7-centos ~]# kubeeye -h KubeEye finds various problems on Kubernetes cluster. Usage: ke [command] Available Commands: audit audit resources from the cluster completion generate the autocompletion script for the specified shell help Help about any command install A brief description of your command uninstall A brief description of your command Flags: -f, --config string Specify the path of kubeconfig. -h, --help help for ke --kubeconfig string Paths to a kubeconfig. Only required if out-of-cluster. --master --kubeconfig (Deprecated: switch to --kubeconfig) The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.
可以看到 kubeeye 目前主要支持兩個命令,一個為 install package 例如 NPD,另外一個執行 audit,對集群應用進行配置掃描。
5.2 audit
[root@VM-48-7-centos ~]# kubeeye audit KIND NAMESPACE NAME MESSAGE Deployment dddd jenkins-1644220286 [NoCPULimits ImagePullPolicyNotAlways NoMemoryLimits NoPriorityClassName NotReadOnlyRootFilesystem NotRunAsNonRoot] Deployment jenkins jenkins-1644220286 [NoCPULimits ImagePullPolicyNotAlways NoMemoryLimits NoPriorityClassName NotReadOnlyRootFilesystem NotRunAsNonRoot] Deployment smartkm-api-k8s velero [ImageTagIsLatest NoLivenessProbe NoPriorityClassName NotReadOnlyRootFilesystem NoReadinessProbe NotRunAsNonRoot] DaemonSet smartkm-api-k8s restic [ImageTagIsLatest NoLivenessProbe NoPriorityClassName NotReadOnlyRootFilesystem NoReadinessProbe NotRunAsNonRoot] Node minikube [KernelHasNoDeadlock FilesystemIsNotReadOnly KubeletHasSufficientMemory KubeletHasNoDiskPressure KubeletHasSufficientPID] Event kube-system node-problem-detector-dmsws.16d844532f662318 [Failed to pull image "k8s.gcr.io/node-problem-detector/node-problem-detector:v0.8.7": rpc error: code = Unknown desc = Error response from daemon: Get https://k8s.gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)] Event kube-system node-problem-detector-dmsws.16d844532f66703e [Error: ErrImagePull] Event kube-system node-problem-detector-dmsws.16d84453351b8b19 [Error: ImagePullBackOff]
5.3 添加自定義檢查規則
我們利用命令查看預定義 OPA 檢查規則。
kubectl get cm -n kube-system node-problem-detector-config -oyaml
同時也可以根據自己業務創建自定義檢查規則。
創建 OPA 規則存放目錄
mkdir opa
添加自定義 OPA 規則文件
注意:為檢查工作負載設置的 OPA 規則, package 名稱必須是 kubeeye_workloads_rego 為檢查 RBAC 設置的 OPA 規則, package 名稱必須是 kubeeye_RBAC_rego 為檢查節點設置的 OPA 規則, package 名稱必須是 kubeeye_nodes_rego
以下為檢查鏡像倉庫地址規則,保存以下規則到規則文件 imageRegistryRule.rego
package kubeeye_workloads_rego deny[msg] { resource := input type := resource.Object.kind resourcename := resource.Object.metadata.name resourcenamespace := resource.Object.metadata.namespace workloadsType := {"Deployment","ReplicaSet","DaemonSet","StatefulSet","Job"} workloadsType[type] not workloadsImageRegistryRule(resource) msg := { "Name": sprintf("%v", [resourcename]), "Namespace": sprintf("%v", [resourcenamespace]), "Type": sprintf("%v", [type]), "Message": "ImageRegistryNotmyregistry" } } workloadsImageRegistryRule(resource) { regex.match("^myregistry.public.kubesphere/basic/.+", resource.Object.spec.template.spec.containers[_].image) }
使用額外的規則運行 kubeeye
提示:kubeeye 將讀取指定目錄下所有 .rego 結尾的文件
kubeeye audit -p ./opa
六 問題排查
NPD 安裝異常,默認使用 k8s.gcr.io,如果安裝服務器無法連通公網可使用我的鏡像倉庫:1832990/node-problem-detector:v0.8.7。
kubEye 安裝使用默認使用主機$HOME/.kube/config 文件,如果不存在 K8s config 文件,則無法正常運行。
參考鏈接
https://github.com/kubesphere/kubeeye/
Kubernetes 鏡像服務
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。