一款超好用的 Kubernetes 配置錯(cuò)誤檢測(cè)工具 Datree
Datree 是一個(gè)可以檢測(cè) Kubernetes 資源清單錯(cuò)誤配置的 CLI 工具,可確保 Kubernetes 資源清單和 Helm Charts 遵循最佳實(shí)踐,防止開發(fā)人員在 Kubernetes 配置中出現(xiàn)錯(cuò)誤,導(dǎo)致集群在生產(chǎn)中出現(xiàn)錯(cuò)誤。它比人工處理要有效得多,比如給開發(fā)者發(fā)郵件,懇求他們?cè)O(shè)置各種限制,而這很可能會(huì)因?yàn)殚_發(fā)者已經(jīng)不堪重負(fù)而被選擇性忘記~
Datree 為 Kubernetes 提供了一種策略執(zhí)行解決方案,對(duì)每一行代碼變更都會(huì)進(jìn)行自動(dòng)檢查,以防止違反規(guī)則和錯(cuò)誤配置。當(dāng)發(fā)現(xiàn)違反規(guī)則時(shí),Datree 會(huì)產(chǎn)生一個(gè)警報(bào),指導(dǎo)開發(fā)人員在 CI 過程中去修復(fù)問題,同時(shí)也會(huì)對(duì)這些規(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 進(jìn)行掃描。
datree test [k8s-manifest-file]
這樣就進(jìn)行了一次檢查了??。
插件
Datree 還提供了很多插件來增強(qiáng)體驗(yàn)。
Helm插件
Datree 的 Helm 插件可以通過 Helm CLI 訪問,為 Helm 用戶提供無縫體驗(yàn),執(zhí)行下面的命令即可啟用 Helm 插件:
helm plugin install https://github.com/datreeio/helm-datree
VSCode插件
允許你在 Kubernetes 配置上運(yùn)行 Datree 測(cè)試,插件地址:https://marketplace.visualstudio.com/items?itemName=suyashsonawane.vscode-datree
Kustomize支持
Datree 為 Kustomize 提供了開箱即用的支持,允許你輕松掃描 Kustomization 文件以及它將為錯(cuò)誤配置生成的資源。
在測(cè)試 kustomization 目錄時(shí),Datree 將構(gòu)建 kustomization.yaml 中定義的資源的臨時(shí)副本,并對(duì)它們運(yùn)行策略檢查。
只需將 kustomize 參數(shù)添加到 CLI 命令中即可,如下所示:
datree kustomize test [path] [cliArgs] -- [kustomizeArgs]
- path:表示所需 kustomize 目錄的路徑(包含 kustomization.yaml 文件)
- cliArgs(可選):Datree CLI 參數(shù)
- kustomizeArgs(可選):kustomize build命令的參數(shù),要查看支持的參數(shù)列表,請(qǐng)運(yùn)行 kustomize build -h 或 kubectl kustomize -h
此外還可以配置 Git Hooks。
策略
每個(gè)策略檢查都將驗(yàn)證你的 Kubernetes Schema,此外,還有很多 Datree 內(nèi)置的一些策略規(guī)則。這些規(guī)則涵蓋了各種 Kubernetes 資源和用例:
- Workload
- CronJob
- Containers
- Networking
- Security
- Deprecation
- Others
除了內(nèi)置的規(guī)則之外,你還可以編寫任何希望的自定義規(guī)則,然后針對(duì)你的 Kubernetes 配置來檢查是否違反規(guī)則,自定義規(guī)則引擎是基于 JSON Schema 的。更多的策略使用可以參考官方文檔:https://hub.datree.io。
CI/CD集成
像任何 linter 或靜態(tài)代碼分析工具一樣,Datree 的命令行工具可以與所有 CI/CD 平臺(tái)集成,自動(dòng)掃描每行代碼變化并作為工作流程的一部分提供反饋。
比如在 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