クブ管理者とリソースをクリーンアップ



アバウト
設定済みのTTL (生存する時間)後にKubernetesリソースをクリーンアップする

実装
アプリケーションは、エスカレーションされた権限を持つ展開として展開されます.APIサーバにAPIリクエストを聞き、内部のスケジュールキューを持っていて、規則マッチでリソースを削除します.

参考資料
https://codeberg.org/hjacobs/kube-janitor.git

インストール
# pick files from https://codeberg.org/hjacobs/kube-janitor/src/branch/main/deploy/
# update rules.yaml as necessary
> cat rules.yaml
rules:
# remove deployments and statefulsets with a "demo" label set after 3 days
- id: cleanup-demo-objects
  resources:
    - deployments
    - statefulsets
  jmespath: "(spec.template.metadata.labels.demo)"
  ttl: 3d
# remove all deployments and jobs named "pr-*" after 6 hours
- id: cleanup-pr-deployments
  resources:
    - deployments
    - jobs
  jmespath: "starts_with(metadata.name, 'pr-')"
  ttl: 6h
# delete all resources within the "temp-*" namespace after 3 days
- id: cleanup-temp-namespaces
  resources:
    - namespaces
  jmespath: "starts_with(metadata.name, 'temp-')"
  ttl: 3d
# delete all PVCs which are not mounted and not referenced by StatefulSets after 4 days
- id: remove-unused-pvcs
  resources:
    - persistentvolumeclaims
  jmespath: "_context.pvc_is_not_mounted && _context.pvc_is_not_referenced"
  ttl: 4d

> kubectl apply -k .

構成
このツールを使用する3つの方法があります
  • オブジェクトをaで注釈するjanitor/ttl 注釈.CI/CDシナリオに便利です> kubectl annotate deploy test-app-dep janitor/ttl=24h
  • オブジェクトをaで注釈するjanitor/expires 注釈.ジョブの問題解決に便利> kubectl annotate deploy nginx janitor/expires=2022-03-31
  • ルールファイルを更新します> kubectl edit configmap kube-janitor ). これは、政策執行のために適用可能なサーバー側の自動化です
  • 参照rules.yaml 例えば
  • jmespathを使用してルールを記述するthis )
  • 注意:
  • 名前空間レベルのクリーンアップは動作しません、そして、この強化を加えるPRがあります

  • 詳しい情報
  • Sample usage in tekton pipeline scenario
  • ほとんどすべての複雑さなしでKubernetes CRDとしてほとんど働いているパイソンアプリケーションを見たいと思っています.