はじめに
KubernetesのLinterであるKubeLinterについて簡単に紹介して、実際に使ってみたいと思います。
KubeLinterとは
KubeLinterは、Kubernetesのマニフェスト(yamlファイル)とHelmチャートを静的解析するツール(linter)です。
セキュリティや本番環境で動かすためのベストプラクティスをチェックしてくれます。
公式のドキュメントにもありますが、2022/4/23時点ではKubeLinterはまだまだ開発の初期段階みたいなので、これから仕様が大きく変わる可能性もあることに注意してください。
インストール
Macであればbrew
でインストールできます。
brew install kube-linter
使い方
kube-linter lint
で静的解析ができます。
kube-linter lint [file path]
checkとtemplate
KubeLinterにはcheck
とtemplate
というものがあります。
公式のドキュメントには具体的な説明が見つからず、公式から参照されているブログで言及されていました。
a 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 checks list
kube-linter templates list
設定方法
設定ファイルを利用することで、チェックする内容をカスタマイズできます。
設定ファイルの指定には--config
オプションを利用するか、作業ディレクトリに.kube-linter.yaml
もしくは.kube-linter.yml
を作成する必要があります。
例えば、全てのcheck
を無効化する場合は下記になります。
checks:
doNotAutoAddDefaults: true
使ってみる
実際に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チャートの静的解析ができる