一款超好用的 Kubernetes 配置錯誤檢測工具 Datree
Datree 是一個可以檢測 Kubernetes 資源清單錯誤配置的 CLI 工具,可確保 Kubernetes 資源清單和 Helm Charts 遵循最佳實踐,防止開發(fā)人員在 Kubernetes 配置中出現(xiàn)錯誤,導致集群在生產(chǎn)中出現(xiàn)錯誤。它比人工處理要有效得多,比如給開發(fā)者發(fā)郵件,懇求他們設置各種限制,而這很可能會因為開發(fā)者已經(jīng)不堪重負而被選擇性忘記~
Datree 為 Kubernetes 提供了一種策略執(zhí)行解決方案,對每一行代碼變更都會進行自動檢查,以防止違反規(guī)則和錯誤配置。當發(fā)現(xiàn)違反規(guī)則時,Datree 會產(chǎn)生一個警報,指導開發(fā)人員在 CI 過程中去修復問題,同時也會對這些規(guī)則背后的原因做出解釋。
安裝
只需要兩步即可開始使用 Datree。
- 安裝最新版本的 Datre。
- Linux & MacOS: curl https://get.datree.io | /bin/bash
- Windows: iwr -useb https://get.datree.io/windows_install.ps1 | iex
- 其他安裝方式(Homebrew、Docker等等)可以查看文檔 https://hub.datree.io/
- 將 Kubernetes 資源清單文件傳遞給 Datree 進行掃描。
datree test [k8s-manifest-file]
這樣就進行了一次檢查了??。
插件
Datree 還提供了很多插件來增強體驗。
Helm插件
Datree 的 Helm 插件可以通過 Helm CLI 訪問,為 Helm 用戶提供無縫體驗,執(zhí)行下面的命令即可啟用 Helm 插件:
helm plugin install https://github.com/datreeio/helm-datree
VSCode插件
允許你在 Kubernetes 配置上運行 Datree 測試,插件地址:https://marketplace.visualstudio.com/items?itemName=suyashsonawane.vscode-datree
Kustomize支持
Datree 為 Kustomize 提供了開箱即用的支持,允許你輕松掃描 Kustomization 文件以及它將為錯誤配置生成的資源。
在測試 kustomization 目錄時,Datree 將構(gòu)建 kustomization.yaml 中定義的資源的臨時副本,并對它們運行策略檢查。
只需將 kustomize 參數(shù)添加到 CLI 命令中即可,如下所示:
datree kustomize test [path] [cliArgs] -- [kustomizeArgs]
- path:表示所需 kustomize 目錄的路徑(包含 kustomization.yaml 文件)
- cliArgs(可選):Datree CLI 參數(shù)
- kustomizeArgs(可選):kustomize build命令的參數(shù),要查看支持的參數(shù)列表,請運行 kustomize build -h 或 kubectl kustomize -h
此外還可以配置 Git Hooks。
策略
每個策略檢查都將驗證你的 Kubernetes Schema,此外,還有很多 Datree 內(nèi)置的一些策略規(guī)則。這些規(guī)則涵蓋了各種 Kubernetes 資源和用例:
- Workload
- CronJob
- Containers
- Networking
- Security
- Deprecation
- Others
除了內(nèi)置的規(guī)則之外,你還可以編寫任何希望的自定義規(guī)則,然后針對你的 Kubernetes 配置來檢查是否違反規(guī)則,自定義規(guī)則引擎是基于 JSON Schema 的。更多的策略使用可以參考官方文檔:https://hub.datree.io。
CI/CD集成
像任何 linter 或靜態(tài)代碼分析工具一樣,Datree 的命令行工具可以與所有 CI/CD 平臺集成,自動掃描每行代碼變化并作為工作流程的一部分提供反饋。
比如在 Gitlab CI 中使用 Datree:
image: node:14
stages:
- test
test:
stage: test
script:
- curl https://get.datree.io | /bin/bash
- datree test ~/.datree/k8s-demo.yaml
在 Jenkins Pipeline 中使用 Datree:
pipeline{
agent any
stages{
stage('indentifying misconfigs using datree'){
steps{
script{
withEnv(['DATREE_TOKEN=<your-account-token>']) {
sh 'datree test *.yaml --only-k8s-files'
}
}
}
}
}
}
倉庫地址:https://github.com/datreeio/datree