KubeLinterでKubernetesのマニフェストを静的解析

スポンサーリンク

はじめに

KubernetesのLinterであるKubeLinterについて簡単に紹介して、実際に使ってみたいと思います。

KubeLinterとは

KubeLinterは、Kubernetesのマニフェスト(yamlファイル)とHelmチャートを静的解析するツール(linter)です。

GitHub - stackrox/kube-linter: KubeLinter is a static analysis tool that checks Kubernetes YAML files and Helm charts to ensure the applications represented in them adhere to best practices.
KubeLinter is a static analysis tool that checks Kubernetes YAML files and Helm charts to ensure the applications represented in them adhere to best practices. ...

セキュリティや本番環境で動かすためのベストプラクティスをチェックしてくれます。

公式のドキュメントにもありますが、2022/4/23時点ではKubeLinterはまだまだ開発の初期段階みたいなので、これから仕様が大きく変わる可能性もあることに注意してください。

KubeLinter
KubeLinter is a static analysis tool that checks Kubernetes YAML files and Helm charts to ensure the applications represented in them adhere to best practices.

インストール

Macであればbrewでインストールできます。

brew install kube-linter

使い方

kube-linter lintで静的解析ができます。

kube-linter lint [file path]

checkとtemplate

KubeLinterにはchecktemplateというものがあります。

公式のドキュメントには具体的な説明が見つからず、公式から参照されているブログで言及されていました。

https://rhuki.dev/2022/04/22/extending-kube-linter-to-build-a-custom-template/

a check is an input to a template.

a template is the parser that works on the resource to validate the check.

つまり、checktemplateのインプットとなり、templateはそのcheckを検証するパーサとなっています。

checktemplateの一覧は下記から確認できます。

kube-linter/checks.md at main · stackrox/kube-linter
KubeLinter is a static analysis tool that checks Kubernetes YAML files and Helm charts to ensure the applications represented in them adhere to best practices. ...
kube-linter/templates.md at main · stackrox/kube-linter
KubeLinter is a static analysis tool that checks Kubernetes YAML files and Helm charts to ensure the applications represented in them adhere to best practices. ...

コマンドでも確認可能です。

kube-linter checks list
kube-linter templates list

設定方法

設定ファイルを利用することで、チェックする内容をカスタマイズできます。

設定ファイルの指定には--configオプションを利用するか、作業ディレクトリに.kube-linter.yamlもしくは.kube-linter.ymlを作成する必要があります。

例えば、全てのcheckを無効化する場合は下記になります。

checks:
  doNotAutoAddDefaults: true
KubeLinter
KubeLinter is a static analysis tool that checks Kubernetes YAML files and Helm charts to ensure the applications represented in them adhere to best practices.

使ってみる

実際にKubeLinterを使ってみます。

下記のようなdeployment.ymlを作成します。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  labels:
    app: web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
          - containerPort: 80

まずはkube-linter lintをやってみます。

❯ kube-linter lint deployment.yml
KubeLinter 0.2.6

deployment.yml: (object: <no namespace>/web apps/v1, Kind=Deployment) The container "nginx" is using an invalid container image, "nginx". Please use images that are not blocked by the `BlockList` criteria : [".*:(latest)$" "^[^:]*$" "(.*/[^:]+)$"] (check: latest-tag, remediation: Use a container image with a specific tag other than latest.)

deployment.yml: (object: <no namespace>/web apps/v1, Kind=Deployment) container "nginx" does not have a read-only root file system (check: no-read-only-root-fs, remediation: Set readOnlyRootFilesystem to true in the container securityContext.)

deployment.yml: (object: <no namespace>/web apps/v1, Kind=Deployment) container "nginx" is not set to runAsNonRoot (check: run-as-non-root, remediation: Set runAsUser to a non-zero number and runAsNonRoot to true in your pod or container securityContext. Refer to https://kubernetes.io/docs/tasks/configure-pod-container/security-context/ for details.)

deployment.yml: (object: <no namespace>/web apps/v1, Kind=Deployment) container "nginx" has cpu request 0 (check: unset-cpu-requirements, remediation: Set CPU requests and limits for your container based on its requirements. Refer to https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#requests-and-limits for details.)

deployment.yml: (object: <no namespace>/web apps/v1, Kind=Deployment) container "nginx" has cpu limit 0 (check: unset-cpu-requirements, remediation: Set CPU requests and limits for your container based on its requirements. Refer to https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#requests-and-limits for details.)

deployment.yml: (object: <no namespace>/web apps/v1, Kind=Deployment) container "nginx" has memory request 0 (check: unset-memory-requirements, remediation: Set memory requests and limits for your container based on its requirements. Refer to https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#requests-and-limits for details.)

deployment.yml: (object: <no namespace>/web apps/v1, Kind=Deployment) container "nginx" has memory limit 0 (check: unset-memory-requirements, remediation: Set memory requests and limits for your container based on its requirements. Refer to https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#requests-and-limits for details.)

Error: found 7 lint errors

いくつかエラーが出ました。

エラーが出たcheckは下記の5つです。

  • latest-tag
  • no-read-only-root-fs
  • run-as-non-root
  • unset-cpu-requirements
  • unset-memory-requirements

エラーメッセージの中にはどう対応したらいいかと、参考にする公式ドキュメントのリンクが含まれています。

試しに下記の.kube-linter.yamlを作成してkube-linter lintしてみます。

checks:
  doNotAutoAddDefaults: true
❯ kube-linter lint deployment.yml
Warning: no checks enabled.

先ほどまで出ていたエラーがなくなりましたが、警告としてcheckがないと出ています。

作成した.kube-linter.yamlを削除してから、deployment.ymlを下記のように修正します。

修正したのは、

  • イメージのタグ
  • root以外のユーザーでの実行
  • ファイルシステムのReadOnly
  • リソースのrequestsとlimits

になります。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  labels:
    app: web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: nginx
        image: nginx:1.21
        ports:
          - containerPort: 80
        securityContext:
          runAsUser: 1000
          runAsGroup: 3000
          fsGroup: 2000
          readOnlyRootFilesystem: true
        resources:
          requests:
            memory: "64Mi"
            cpu: "500m"
          limits:
            memory: "64Mi"
            cpu: "500m"

kube-linter lintすると、全てのエラーが解消されたのがわかります。

❯ kube-linter lint deployment.yml
KubeLinter 0.2.6

No lint errors found!

まとめ

  • KubeLinterはKubernetesのマニフェストとHelmチャートの静的解析ができる

参考

タイトルとURLをコピーしました