Kubernetes 静态分析
KubeLinter 分析 Kubernetes YAML 文件、Helm Chart 和 Kustomize 清单,并根据各种最佳实践(重点关注生产就绪性和安全性)对其进行检查。
KubeLinter 运行合理的默认检查,旨在为你提供有关 Kubernetes YAML 文件、Helm Chart 和 Kustomize 清单的有用信息。这有助于团队尽早且频繁地检查安全错误配置和 DevOps 最佳实践。一些常见的示例包括以非 root 用户运行容器、强制最小权限原则,以及仅将敏感信息存储在 Secret 中。
KubeLinter 是可配置的,因此你可以根据组织内要遵循的策略,启用和禁用检查,以及创建自己的自定义检查。
当 lint 检查失败时,KubeLinter 会报告如何解决任何潜在问题的建议,并返回非零退出码。
请访问 https://docs.kubelinter.io 获取有关安装、使用和配置 KubeLinter 的详细文档。
Kube-linter 二进制文件可在此处找到:https://github.com/stackrox/kube-linter/releases/latest
要使用 Go 进行安装,请运行以下命令:
go install golang.stackrox.io/kube-linter/cmd/kube-linter@latest
或者,从 Releases 下载最新二进制文件,并将其添加到你的 PATH 中。
要使用 Homebrew 或 LinuxBrew 进行安装,请运行以下命令:
brew install kube-linter
nix-shell -p kube-linter
docker pull stackrox/kube-linter:latest
从源码安装 KubeLinter 只需遵循以下步骤:
首先,克隆 KubeLinter 仓库。
git clone [email protected]:stackrox/kube-linter.git
然后,编译源代码。这将会为每个平台创建 kube-linter 二进制文件,并将它们放置在 .gobin 目录中。
make build
最后,你就可以开始使用 KubeLinter 了。验证你的版本,以确保你已成功安装 KubeLinter。
.gobin/kube-linter version
KubeLinter 的测试分为多个层次,每一层都应通过。
go 单元测试:
make test
端到端集成测试:
make e2e-test
最后,使用 bats-core 进行端到端集成测试:
make e2e-bats
KubeLinter 镜像由 cosign 签名。我们建议在使用镜像前进行验证。
安装 cosign 后,你可以使用 KubeLinter 公钥 通过以下命令验证 KubeLinter 镜像:
cat kubelinter-cosign.pub
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEl0HCkCRzYv0qH5QiazoXeXe2qwFX
DmAszeH26g1s3OSsG/focPWkN88wEKQ5eiE95v+Z2snUQPl/mjPdvqpyjA==
-----END PUBLIC KEY-----
cosign verify --key kubelinter-cosign $IMAGE_NAME
KubeLinter 还提供 cosign 无密钥签名。
你可以使用以下命令验证 KubeLinter 镜像:
# NOTE: Keyless signatures are NOT PRODUCTION ready.
COSIGN_EXPERIMENTAL=1 cosign verify $IMAGE_NAME
在最基本的形式下,使用 KubeLinter 检查(Lint)你的 YAML 文件只需两个步骤。
找到你想要测试其安全性和生产就绪最佳实践的 YAML 文件:
运行以下命令:
kube-linter lint /path/to/your/yaml.yaml
请考虑以下示例 Pod 规范文件 pod.yaml。该文件存在两个生产就绪问题和 一个安全问题:
安全问题:
生产就绪问题:
未设置容器的内存限制,这可能导致它消耗过多内存。
apiVersion: v1
kind: Pod
metadata:
name: security-context-demo
spec:
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
volumes:
- name: sec-ctx-vol
emptyDir: {}
containers:
- name: sec-ctx-demo
image: busybox
resources:
requests:
memory: "64Mi"
cpu: "250m"
command: [ "sh", "-c", "sleep 1h" ]
volumeMounts:
- name: sec-ctx-vol
mountPath: /data/demo
securityContext:
allowPrivilegeEscalation: false
将上面的 YAML 复制到 pod.yaml 文件中,并通过运行以下命令对该文件进行 lint 检查:
kube-linter lint pod.yaml
KubeLinter 会运行其默认检查并报告建议。以下是上述命令的输出。
pod.yaml: (object: <no namespace>/security-context-demo /v1, Kind=Pod) The container "sec-ctx-demo" is using an invalid container image, "busybox". 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.)
pod.yaml: (object: <no namespace>/security-context-demo /v1, Kind=Pod) container "sec-ctx-demo" does not have a read-only root file system (check: no-read-only-root-fs, remediation: Set readOnlyRootFilesystem to true in the container securityContext.)
pod.yaml: (object: <no namespace>/security-context-demo /v1, Kind=Pod) container "sec-ctx-demo" has memory limit 0 (check: unset-memory-requirements, remediation: Set memory 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 3 lint errors
KubeLinter 支持在单次运行中生成多种输出格式。这对于同时生成人类可读和机器可读的报告非常有用:
kube-linter lint \
--format sarif --output kube-linter.sarif \
--format json --output kube-linter.json \
--config .kube-linter.yaml \
pod.yaml
此命令将会:
kube-linter.sarif 中生成 SARIF 格式的报告kube-linter.json 中生成 JSON 格式的报告注意: 多种格式需要显式指定 --output 标志。若要将单一格式输出到 stdout,只需使用一个 --format 标志,无需 --output。
有关使用多种输出格式的更多详细信息,请参阅文档。
要了解更多关于使用和配置 KubeLinter 的信息,请访问文档页面。
以下是用户撰写的关于 KubeLinter 的教程。如果你有想添加到这个列表的内容,请提交 PR!
KubeLinter 根据 Apache License 2.0 获得许可。
如果你想与 KubeLinter 社区(包括维护者和其他用户)互动,可以在此加入 Slack 工作区。
未来命令用法、标志和配置文件格式可能会有破坏性变更。不过,我们鼓励你使用 KubeLinter 测试你的环境 YAML 文件,看看哪里会出问题,并贡献代码。
提醒一下,所有参与 KubeLinter 社区的行为都受我们的行为准则约束。