Implement Kubernetes security policies including NetworkPolicy, PodSecurityPolicy, and RBAC for production-grade security. Use when securing Kubernetes clusters, implementing network isolation, or enforcing pod security standards.
Add this skill
npx mdskills install sickn33/k8s-security-policiesComprehensive K8s security policy guide with excellent examples for NetworkPolicy, RBAC, and Pod Security Standards
1---2name: k8s-security-policies3description: Implement Kubernetes security policies including NetworkPolicy, PodSecurityPolicy, and RBAC for production-grade security. Use when securing Kubernetes clusters, implementing network isolation, or enforcing pod security standards.4---56# Kubernetes Security Policies78Comprehensive guide for implementing NetworkPolicy, PodSecurityPolicy, RBAC, and Pod Security Standards in Kubernetes.910## Do not use this skill when1112- The task is unrelated to kubernetes security policies13- You need a different domain or tool outside this scope1415## Instructions1617- Clarify goals, constraints, and required inputs.18- Apply relevant best practices and validate outcomes.19- Provide actionable steps and verification.20- If detailed examples are required, open `resources/implementation-playbook.md`.2122## Purpose2324Implement defense-in-depth security for Kubernetes clusters using network policies, pod security standards, and RBAC.2526## Use this skill when2728- Implement network segmentation29- Configure pod security standards30- Set up RBAC for least-privilege access31- Create security policies for compliance32- Implement admission control33- Secure multi-tenant clusters3435## Pod Security Standards3637### 1. Privileged (Unrestricted)38```yaml39apiVersion: v140kind: Namespace41metadata:42 name: privileged-ns43 labels:44 pod-security.kubernetes.io/enforce: privileged45 pod-security.kubernetes.io/audit: privileged46 pod-security.kubernetes.io/warn: privileged47```4849### 2. Baseline (Minimally restrictive)50```yaml51apiVersion: v152kind: Namespace53metadata:54 name: baseline-ns55 labels:56 pod-security.kubernetes.io/enforce: baseline57 pod-security.kubernetes.io/audit: baseline58 pod-security.kubernetes.io/warn: baseline59```6061### 3. Restricted (Most restrictive)62```yaml63apiVersion: v164kind: Namespace65metadata:66 name: restricted-ns67 labels:68 pod-security.kubernetes.io/enforce: restricted69 pod-security.kubernetes.io/audit: restricted70 pod-security.kubernetes.io/warn: restricted71```7273## Network Policies7475### Default Deny All76```yaml77apiVersion: networking.k8s.io/v178kind: NetworkPolicy79metadata:80 name: default-deny-all81 namespace: production82spec:83 podSelector: {}84 policyTypes:85 - Ingress86 - Egress87```8889### Allow Frontend to Backend90```yaml91apiVersion: networking.k8s.io/v192kind: NetworkPolicy93metadata:94 name: allow-frontend-to-backend95 namespace: production96spec:97 podSelector:98 matchLabels:99 app: backend100 policyTypes:101 - Ingress102 ingress:103 - from:104 - podSelector:105 matchLabels:106 app: frontend107 ports:108 - protocol: TCP109 port: 8080110```111112### Allow DNS113```yaml114apiVersion: networking.k8s.io/v1115kind: NetworkPolicy116metadata:117 name: allow-dns118 namespace: production119spec:120 podSelector: {}121 policyTypes:122 - Egress123 egress:124 - to:125 - namespaceSelector:126 matchLabels:127 name: kube-system128 ports:129 - protocol: UDP130 port: 53131```132133**Reference:** See `assets/network-policy-template.yaml`134135## RBAC Configuration136137### Role (Namespace-scoped)138```yaml139apiVersion: rbac.authorization.k8s.io/v1140kind: Role141metadata:142 name: pod-reader143 namespace: production144rules:145- apiGroups: [""]146 resources: ["pods"]147 verbs: ["get", "watch", "list"]148```149150### ClusterRole (Cluster-wide)151```yaml152apiVersion: rbac.authorization.k8s.io/v1153kind: ClusterRole154metadata:155 name: secret-reader156rules:157- apiGroups: [""]158 resources: ["secrets"]159 verbs: ["get", "watch", "list"]160```161162### RoleBinding163```yaml164apiVersion: rbac.authorization.k8s.io/v1165kind: RoleBinding166metadata:167 name: read-pods168 namespace: production169subjects:170- kind: User171 name: jane172 apiGroup: rbac.authorization.k8s.io173- kind: ServiceAccount174 name: default175 namespace: production176roleRef:177 kind: Role178 name: pod-reader179 apiGroup: rbac.authorization.k8s.io180```181182**Reference:** See `references/rbac-patterns.md`183184## Pod Security Context185186### Restricted Pod187```yaml188apiVersion: v1189kind: Pod190metadata:191 name: secure-pod192spec:193 securityContext:194 runAsNonRoot: true195 runAsUser: 1000196 fsGroup: 1000197 seccompProfile:198 type: RuntimeDefault199 containers:200 - name: app201 image: myapp:1.0202 securityContext:203 allowPrivilegeEscalation: false204 readOnlyRootFilesystem: true205 capabilities:206 drop:207 - ALL208```209210## Policy Enforcement with OPA Gatekeeper211212### ConstraintTemplate213```yaml214apiVersion: templates.gatekeeper.sh/v1215kind: ConstraintTemplate216metadata:217 name: k8srequiredlabels218spec:219 crd:220 spec:221 names:222 kind: K8sRequiredLabels223 validation:224 openAPIV3Schema:225 type: object226 properties:227 labels:228 type: array229 items:230 type: string231 targets:232 - target: admission.k8s.gatekeeper.sh233 rego: |234 package k8srequiredlabels235 violation[{"msg": msg, "details": {"missing_labels": missing}}] {236 provided := {label | input.review.object.metadata.labels[label]}237 required := {label | label := input.parameters.labels[_]}238 missing := required - provided239 count(missing) > 0240 msg := sprintf("missing required labels: %v", [missing])241 }242```243244### Constraint245```yaml246apiVersion: constraints.gatekeeper.sh/v1beta1247kind: K8sRequiredLabels248metadata:249 name: require-app-label250spec:251 match:252 kinds:253 - apiGroups: ["apps"]254 kinds: ["Deployment"]255 parameters:256 labels: ["app", "environment"]257```258259## Service Mesh Security (Istio)260261### PeerAuthentication (mTLS)262```yaml263apiVersion: security.istio.io/v1beta1264kind: PeerAuthentication265metadata:266 name: default267 namespace: production268spec:269 mtls:270 mode: STRICT271```272273### AuthorizationPolicy274```yaml275apiVersion: security.istio.io/v1beta1276kind: AuthorizationPolicy277metadata:278 name: allow-frontend279 namespace: production280spec:281 selector:282 matchLabels:283 app: backend284 action: ALLOW285 rules:286 - from:287 - source:288 principals: ["cluster.local/ns/production/sa/frontend"]289```290291## Best Practices2922931. **Implement Pod Security Standards** at namespace level2942. **Use Network Policies** for network segmentation2953. **Apply least-privilege RBAC** for all service accounts2964. **Enable admission control** (OPA Gatekeeper/Kyverno)2975. **Run containers as non-root**2986. **Use read-only root filesystem**2997. **Drop all capabilities** unless needed3008. **Implement resource quotas** and limit ranges3019. **Enable audit logging** for security events30210. **Regular security scanning** of images303304## Compliance Frameworks305306### CIS Kubernetes Benchmark307- Use RBAC authorization308- Enable audit logging309- Use Pod Security Standards310- Configure network policies311- Implement secrets encryption at rest312- Enable node authentication313314### NIST Cybersecurity Framework315- Implement defense in depth316- Use network segmentation317- Configure security monitoring318- Implement access controls319- Enable logging and monitoring320321## Troubleshooting322323**NetworkPolicy not working:**324```bash325# Check if CNI supports NetworkPolicy326kubectl get nodes -o wide327kubectl describe networkpolicy <name>328```329330**RBAC permission denied:**331```bash332# Check effective permissions333kubectl auth can-i list pods --as system:serviceaccount:default:my-sa334kubectl auth can-i '*' '*' --as system:serviceaccount:default:my-sa335```336337## Reference Files338339- `assets/network-policy-template.yaml` - Network policy examples340- `assets/pod-security-template.yaml` - Pod security policies341- `references/rbac-patterns.md` - RBAC configuration patterns342343## Related Skills344345- `k8s-manifest-generator` - For creating secure manifests346- `gitops-workflow` - For automated policy deployment347
Full transparency — inspect the skill content before installing.