はじめに
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. - stackrox/kube-linter
セキュリティや本番環境で動かすためのベストプラクティスをチェックしてくれます。
公式のドキュメントにもありますが、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でインストールできます。
1brew install kube-linter使い方
kube-linter lintで静的解析ができます。
1kube-linter lint [file path]checkとtemplate
KubeLinterにはcheckとtemplateというものがあります。
公式のドキュメントには具体的な説明が見つからず、公式から参照されているブログで言及されていました。
unknown linka check is an input to a template.
a template is the parser that works on the resource to validate the check.
つまり、checkはtemplateのインプットとなり、templateはそのcheckを検証するパーサとなっています。
checkとtemplateの一覧は下記から確認できます。
kube-linter/docs/generated/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. - stackrox/kube-linter
kube-linter/docs/generated/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. - stackrox/kube-linter
コマンドでも確認可能です。
1kube-linter checks list1kube-linter templates list設定方法
設定ファイルを利用することで、チェックする内容をカスタマイズできます。
設定ファイルの指定には--configオプションを利用するか、作業ディレクトリに.kube-linter.yamlもしくは.kube-linter.ymlを作成する必要があります。
例えば、全てのcheckを無効化する場合は下記になります。
1checks:
2 doNotAutoAddDefaults: trueKubeLinter
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を作成します。
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: web
5 labels:
6 app: web
7spec:
8 replicas: 1
9 selector:
10 matchLabels:
11 app: web
12 template:
13 metadata:
14 labels:
15 app: web
16 spec:
17 containers:
18 - name: nginx
19 image: nginx
20 ports:
21 - containerPort: 80まずはkube-linter lintをやってみます。
1❯ kube-linter lint deployment.yml
2KubeLinter 0.2.6
3
4deployment.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.)
5
6deployment.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.)
7
8deployment.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.)
9
10deployment.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.)
11
12deployment.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.)
13
14deployment.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.)
15
16deployment.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.)
17
18Error: 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してみます。
1checks:
2 doNotAutoAddDefaults: true1❯ kube-linter lint deployment.yml
2Warning: no checks enabled.先ほどまで出ていたエラーがなくなりましたが、警告としてcheckがないと出ています。
作成した.kube-linter.yamlを削除してから、deployment.ymlを下記のように修正します。
修正したのは、
- イメージのタグ
- root以外のユーザーでの実行
- ファイルシステムのReadOnly
- リソースのrequestsとlimits
になります。
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: web
5 labels:
6 app: web
7spec:
8 replicas: 1
9 selector:
10 matchLabels:
11 app: web
12 template:
13 metadata:
14 labels:
15 app: web
16 spec:
17 containers:
18 - name: nginx
19 image: nginx:1.21
20 ports:
21 - containerPort: 80
22 securityContext:
23 runAsUser: 1000
24 runAsGroup: 3000
25 fsGroup: 2000
26 readOnlyRootFilesystem: true
27 resources:
28 requests:
29 memory: "64Mi"
30 cpu: "500m"
31 limits:
32 memory: "64Mi"
33 cpu: "500m"kube-linter lintすると、全てのエラーが解消されたのがわかります。
1❯ kube-linter lint deployment.yml
2KubeLinter 0.2.6
3
4No lint errors found!まとめ
- KubeLinterはKubernetesのマニフェストとHelmチャートの静的解析ができる
参考
- 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.
- Introduction
- Extending kube-linter To Build A Custom Template – Red Hat UK & Ireland Developer Advocacy




