Catch the risky manifest before it reaches the cluster
The problem. Admission policies stop a bad object at the API server, but by then the change is merged, the pipeline has run and someone is waiting. A scanner on the manifest itself catches the same mistakes at review time: a privileged container, an image without a tag, no resource limits. The CKS names two tools for it: kubesec and KubeLinter.
kubesec: a score
kubesec scan FILE checks each object against rules with points. Risky settings are critical (negative points): privileged -30, SYS_ADMIN -30, hostNetwork / hostPID / hostIPC -9, the docker socket -9, allowPrivilegeEscalation -7, secrets in env vars -5. Hardening passes with positive points: a service account +3, AppArmor +3, and +1 each for runAsNonRoot, readOnlyRootFilesystem, capabilities drop ALL, seccomp, requests and limits. A hardening rule you did not meet is listed under advise. Score 0 or less = exit code 2.
$ kubesec scan web.yaml
[
{
"object": "Deployment/web.shop",
"valid": true,
"fileName": "web.yaml",
"message": "Failed with a score of -30 points",
"score": -30,
"scoring": {
"critical": [
{
"id": "Privileged",
"selector": "containers[] .securityContext .privileged == true",
"reason": "Privileged containers can allow almost completely unrestricted host access",
"points": -30
}
],
"advise": [
{
"id": "ApparmorAny",
...
The output is JSON, so in a pipeline you ask it questions: kubesec scan web.yaml | jq -r '.[0].scoring.critical[].id'.
KubeLinter: findings with a fix
kube-linter lint PATH (a file or a directory) runs named checks and prints one line per finding, with the check and its remediation, then fails:
$ kube-linter lint web.yaml
KubeLinter 0.8.3
web.yaml: (object: shop/web apps/v1, Kind=Deployment) The container "web" is using an invalid container
image, "nginx:latest". 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.)
web.yaml: (object: shop/web apps/v1, Kind=Deployment) container "web" is privileged (check:
privileged-container, remediation: Do not run your container as privileged unless it is required.)
...
Error: found 7 lint errors
| kubesec | KubeLinter | |
|---|---|---|
| Output | a score per object, JSON | a finding per problem, plain or JSON |
| Fails when | the score is 0 or less (exit 2) | any finding (exit 1) |
| Looks at | one object at a time | the object and its neighbours (Service selectors, ServiceAccounts) |
| Silence one | --rules picks rules | --exclude CHECK, or the annotation ignore-check.kube-linter.io/<check> |
Read the scores critically. A kubesec score above 0 is not "secure": it is a sum, and one privileged container (-30) outweighs every hardening point. KubeLinter does not know your app: an ignore annotation with a reason is better than a check that everyone learns to skip.
In an interview: "static analysis in CI finds the risky manifest before review is over; admission policies stop whatever still gets through".
On the exam: run the tool on the file you are given, read which rule or check fails, fix the manifest, and run it again until it is clean.