要使用Go安装,请运行以下命令:
GO111MODULE=on go install golang.stackrox.io/kube-linter/cmd/kube-linter
否则,从Releases下载最新的二进制文件并将其添加到您的 PATH。
[hidecontent type="logged" desc="隐藏内容:登录后可查看"]
要使用 Homebrew 或 LinuxBrew 安装,请运行以下命令:
brew install kube-linter
从源代码安装 KubeLinter 非常简单,只需执行以下步骤:
首先,克隆 KubeLinter 存储库。
git clone git@github.com:stackrox/kube-linter.git
然后,编译源代码。这将为每个平台创建 kube-linter 二进制文件并将它们放在.gobin
文件夹中。
make build
最后,您就可以开始使用 KubeLinter 了。验证您的版本以确保您已成功安装 KubeLinter。
.gobin/kube-linter version
有几层测试。每一层都有望通过。
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
。此文件有两个生产准备问题和一个安全问题:
安全问题:
生产准备:
未设置容器的 CPU 限制,这可能会使其消耗过多的 CPU。
未设置容器的内存限制,这可能会使其消耗过多的内存
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 并通过运行以下命令来检查此文件:
kube-linter lint pod.yaml
KubeLinter 运行其默认检查并报告建议。下面是我们之前命令的输出。
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 your container's securityContext.)
pod.yaml: (object: <no namespace>/security-context-demo /v1, Kind=Pod) container "sec-ctx-demo" has cpu limit 0 (check: unset-cpu-requirements, remediation: Set your container's CPU requests and limits depending on its requirements. See https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/ #requests-and-limits for more details.)
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 your container's memory requests and limits depending on its requirements. See https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/ #requests-and-limits for more details.)
Error: found 3 lint errors
要了解有关使用和配置 KubeLinter 的更多信息,请访问文档页面。
以下是用户编写的关于 KubeLinter 的教程。如果您有想要添加到此列表中的,请发送 PR!
[/hidecontent]