https://github.com/zegl/kube-score
kube-score 是一款针对 Kubernetes 对象定义的静态代码分析的工具,其会输出一系列改进建议,以提升应用程序的安全性和容错能力。
官方支持多种集成方式,可以与CI、Helm、Kustomize等联动,也可以直接在Docker下执行,这里直接编译好的windows exe程序验证效果。写一个Deployment的测试yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.25.3
ports:
- containerPort: 80
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "200m"
memory: "256Mi"
执行:.\kube-score_1.20.0_windows_amd64.exe score .\app.yaml输出:
apps/v1/Deployment nginx-deployment 💥
path=C:\Users\Administrator\Downloads\app.yaml
[CRITICAL] Container Security Context User Group ID
· nginx -> Container has no configured security context
Set securityContext to run the container in a more secure context.
[CRITICAL] Container Image Pull Policy
· nginx -> ImagePullPolicy is not set to Always
It's recommended to always set the ImagePullPolicy to Always, to
make sure that the imagePullSecrets are always correct, and to
always get the image you want.
[CRITICAL] Pod NetworkPolicy
· The pod does not have a matching NetworkPolicy
Create a NetworkPolicy that targets this pod to control who/what
can communicate with this pod. Note, this feature needs to be
supported by the CNI implementation used in the Kubernetes cluster
to have an effect.
[CRITICAL] Container Security Context ReadOnlyRootFilesystem
· nginx -> Container has no configured security context
Set securityContext to run the container in a more secure context.
[CRITICAL] Container Ephemeral Storage Request and Limit
· nginx -> Ephemeral Storage limit is not set
Resource limits are recommended to avoid resource DDOS. Set
resources.limits.ephemeral-storage
· nginx -> Ephemeral Storage request is not set
Resource requests are recommended to make sure the application can
start and run without crashing. Set
resource.requests.ephemeral-storage
[CRITICAL] Deployment has PodDisruptionBudget
· No matching PodDisruptionBudget was found
It's recommended to define a PodDisruptionBudget to avoid
unexpected downtime during Kubernetes maintenance operations, such
as when draining a node.
[WARNING] Deployment has host PodAntiAffinity
· Deployment does not have a host podAntiAffinity set
It's recommended to set a podAntiAffinity that stops multiple pods
from a deployment from being scheduled on the same node. This
increases availability in case the node becomes unavailable.
对于这样一条yaml,输出了六条[CTICAL]级别的消息,其中包括:
详细的检查项清单在这里: https://github.com/zegl/kube-score/blob/master/README_CHECKS.md 涉其中及以下几类:
此外还有一些面向所有对象的检查项如label-values。相关配置项有默认开启(值为"default")和手动开启(值为"optional"),均可以在yaml中通过kube-score/ignore和kube-score/enable控制。
验证一下屏蔽检查项,在测试yaml的metadata.annotations下新增kube-score/ignore: deployment-has-poddisruptionbudget,再次执行,发现之前 [CRITICAL] Deployment has PodDisruptionBudget的报错已不再提示。
总结来说,kube-score 的检查主要聚焦于安全(Security)、稳定性(Stability)、规范化(Best Practices) 等通用维度,覆盖了业界通行的 Kubernetes 配置标准(如 CIS Benchmark),但在业务场景的精细化控制 方面仍存在不足,例如:
kube-score 作为基础合规性检查工具,整体设计和易用性方面值得肯定,适合作为K8s配置的“第一道防线”,但需结合业务定制化才能实现更高的可靠性和实用性。