好用的預(yù)防 Kubernetes 配置錯(cuò)誤工具 - Datree
Datree 是一個(gè)可以檢測(cè) Kubernetes 資源清單錯(cuò)誤配置的 CLI 工具,可確保 Kubernetes 資源清單和 Helm Charts 遵循最佳實(shí)踐,防止開(kāi)發(fā)人員在 Kubernetes 配置中出現(xiàn)錯(cuò)誤,導(dǎo)致集群在生產(chǎn)中出現(xiàn)錯(cuò)誤。它比人工處理要有效得多,比如給開(kāi)發(fā)者發(fā)郵件,懇求他們?cè)O(shè)置各種限制,而這很可能會(huì)因?yàn)殚_(kāi)發(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)開(kāi)發(fā)人員在 CI 過(guò)程中去修復(fù)問(wèn)題,同時(shí)也會(huì)對(duì)這些規(guī)則背后的原因做出解釋。
安裝
只需要兩步即可開(kāi)始使用 Datree。
安裝最新版本的 Datre。
Linux & MacOS:curl https://get.datree.io | /bin/bashWindows: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 還提供了很多插件來(lái)增強(qiáng)體驗(yàn)。
Helm插件
Datree 的 Helm 插件可以通過(guò) Helm CLI 訪(fǎng)問(wèn),為 Helm 用戶(hù)提供無(wú)縫體驗(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 提供了開(kāi)箱即用的支持,允許你輕松掃描 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ī)則之外,你還可以編寫(xiě)任何希望的自定義規(guī)則,然后針對(duì)你的 Kubernetes 配置來(lái)檢查是否違反規(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=' ])?{
??????????????????????????????sh?'datree?test?*.yaml?--only-k8s-files'
????????????????????????}
????????????????}
????????????}
????????}
????}
}
倉(cāng)庫(kù)地址:https://github.com/datreeio/datree
