Write a policy that rejects any Deployment without CPU and memory limits and without a `team` label. Which tool and how?
IntermediateScenarioKubernetes Security
Answer
Situation read → An admission policy engine handles this. Kyverno writes it in YAML, Gatekeeper in Rego, or built-in ValidatingAdmissionPolicy in CEL.
What I'd do →
- Kyverno example:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata: { name: require-limits-and-team }
spec:
validationFailureAction: Audit
rules:
- name: check
match: { any: [ { resources: { kinds: [Deployment] } } ] }
validate:
message: "team label and resource limits are required"
pattern:
metadata: { labels: { team: "?*" } }
spec: { template: { spec: { containers: [ { resources: { limits: { cpu: "?*", memory: "?*" } } } ] } } }
- Start in
Audit, review policy reports, then switch toEnforce. - Run the same policies in CI with
kyverno applyso developers see failures before merge.
Why → Limits prevent noisy-neighbour denial of service; labels give ownership for incidents.
Interview tip → "Audit first, then enforce, and test in CI too."
More on Kubernetes Security
What are the 4Cs of cloud native security?BeginnerWhat happens to a request between `kubectl apply` and etcd from a security point of view?IntermediateExplain Role, ClusterRole, RoleBinding and ClusterRoleBinding.BeginnerHow do you check what a user or service account is allowed to do?BeginnerWhich RBAC permissions are effectively cluster-admin in disguise?AdvancedHow do service account tokens work now, and what is `automountServiceAccountToken`?Intermediate