Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Kubernetes security policy expertise from a comprehensive 146-skill, 112-agent multi-agent orchestration system.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
SKILL.md
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## Purpose1112Implement defense-in-depth security for Kubernetes clusters using network policies, pod security standards, and RBAC.1314## When to Use This Skill1516- Implement network segmentation17- Configure pod security standards18- Set up RBAC for least-privilege access19- Create security policies for compliance20- Implement admission control21- Secure multi-tenant clusters2223## Pod Security Standards2425### 1. Privileged (Unrestricted)2627```yaml28apiVersion: v129kind: Namespace30metadata:31name: privileged-ns32labels:33pod-security.kubernetes.io/enforce: privileged34pod-security.kubernetes.io/audit: privileged35pod-security.kubernetes.io/warn: privileged36```3738### 2. Baseline (Minimally restrictive)3940```yaml41apiVersion: v142kind: Namespace43metadata:44name: baseline-ns45labels:46pod-security.kubernetes.io/enforce: baseline47pod-security.kubernetes.io/audit: baseline48pod-security.kubernetes.io/warn: baseline49```5051### 3. Restricted (Most restrictive)5253```yaml54apiVersion: v155kind: Namespace56metadata:57name: restricted-ns58labels:59pod-security.kubernetes.io/enforce: restricted60pod-security.kubernetes.io/audit: restricted61pod-security.kubernetes.io/warn: restricted62```6364## Network Policies6566### Default Deny All6768```yaml69apiVersion: networking.k8s.io/v170kind: NetworkPolicy71metadata:72name: default-deny-all73namespace: production74spec:75podSelector: {}76policyTypes:77- Ingress78- Egress79```8081### Allow Frontend to Backend8283```yaml84apiVersion: networking.k8s.io/v185kind: NetworkPolicy86metadata:87name: allow-frontend-to-backend88namespace: production89spec:90podSelector:91matchLabels:92app: backend93policyTypes:94- Ingress95ingress:96- from:97- podSelector:98matchLabels:99app: frontend100ports:101- protocol: TCP102port: 8080103```104105### Allow DNS106107```yaml108apiVersion: networking.k8s.io/v1109kind: NetworkPolicy110metadata:111name: allow-dns112namespace: production113spec:114podSelector: {}115policyTypes:116- Egress117egress:118- to:119- namespaceSelector:120matchLabels:121name: kube-system122ports:123- protocol: UDP124port: 53125```126127**Reference:** See `assets/network-policy-template.yaml`128129## RBAC Configuration130131### Role (Namespace-scoped)132133```yaml134apiVersion: rbac.authorization.k8s.io/v1135kind: Role136metadata:137name: pod-reader138namespace: production139rules:140- apiGroups: [""]141resources: ["pods"]142verbs: ["get", "watch", "list"]143```144145### ClusterRole (Cluster-wide)146147```yaml148apiVersion: rbac.authorization.k8s.io/v1149kind: ClusterRole150metadata:151name: secret-reader152rules:153- apiGroups: [""]154resources: ["secrets"]155verbs: ["get", "watch", "list"]156```157158### RoleBinding159160```yaml161apiVersion: rbac.authorization.k8s.io/v1162kind: RoleBinding163metadata:164name: read-pods165namespace: production166subjects:167- kind: User168name: jane169apiGroup: rbac.authorization.k8s.io170- kind: ServiceAccount171name: default172namespace: production173roleRef:174kind: Role175name: pod-reader176apiGroup: rbac.authorization.k8s.io177```178179**Reference:** See `references/rbac-patterns.md`180181## Pod Security Context182183### Restricted Pod184185```yaml186apiVersion: v1187kind: Pod188metadata:189name: secure-pod190spec:191securityContext:192runAsNonRoot: true193runAsUser: 1000194fsGroup: 1000195seccompProfile:196type: RuntimeDefault197containers:198- name: app199image: myapp:1.0200securityContext:201allowPrivilegeEscalation: false202readOnlyRootFilesystem: true203capabilities:204drop:205- ALL206```207208## Policy Enforcement with OPA Gatekeeper209210### ConstraintTemplate211212```yaml213apiVersion: templates.gatekeeper.sh/v1214kind: ConstraintTemplate215metadata:216name: k8srequiredlabels217spec:218crd:219spec:220names:221kind: K8sRequiredLabels222validation:223openAPIV3Schema:224type: object225properties:226labels:227type: array228items:229type: string230targets:231- target: admission.k8s.gatekeeper.sh232rego: |233package k8srequiredlabels234violation[{"msg": msg, "details": {"missing_labels": missing}}] {235provided := {label | input.review.object.metadata.labels[label]}236required := {label | label := input.parameters.labels[_]}237missing := required - provided238count(missing) > 0239msg := sprintf("missing required labels: %v", [missing])240}241```242243### Constraint244245```yaml246apiVersion: constraints.gatekeeper.sh/v1beta1247kind: K8sRequiredLabels248metadata:249name: require-app-label250spec:251match:252kinds:253- apiGroups: ["apps"]254kinds: ["Deployment"]255parameters:256labels: ["app", "environment"]257```258259## Service Mesh Security (Istio)260261### PeerAuthentication (mTLS)262263```yaml264apiVersion: security.istio.io/v1beta1265kind: PeerAuthentication266metadata:267name: default268namespace: production269spec:270mtls:271mode: STRICT272```273274### AuthorizationPolicy275276```yaml277apiVersion: security.istio.io/v1beta1278kind: AuthorizationPolicy279metadata:280name: allow-frontend281namespace: production282spec:283selector:284matchLabels:285app: backend286action: ALLOW287rules:288- from:289- source:290principals: ["cluster.local/ns/production/sa/frontend"]291```292293## Best Practices2942951. **Implement Pod Security Standards** at namespace level2962. **Use Network Policies** for network segmentation2973. **Apply least-privilege RBAC** for all service accounts2984. **Enable admission control** (OPA Gatekeeper/Kyverno)2995. **Run containers as non-root**3006. **Use read-only root filesystem**3017. **Drop all capabilities** unless needed3028. **Implement resource quotas** and limit ranges3039. **Enable audit logging** for security events30410. **Regular security scanning** of images305306## Compliance Frameworks307308### CIS Kubernetes Benchmark309310- Use RBAC authorization311- Enable audit logging312- Use Pod Security Standards313- Configure network policies314- Implement secrets encryption at rest315- Enable node authentication316317### NIST Cybersecurity Framework318319- Implement defense in depth320- Use network segmentation321- Configure security monitoring322- Implement access controls323- Enable logging and monitoring324325## Troubleshooting326327**NetworkPolicy not working:**328329```bash330# Check if CNI supports NetworkPolicy331kubectl get nodes -o wide332kubectl describe networkpolicy <name>333```334335**RBAC permission denied:**336337```bash338# Check effective permissions339kubectl auth can-i list pods --as system:serviceaccount:default:my-sa340kubectl auth can-i '*' '*' --as system:serviceaccount:default:my-sa341```342343344## Related Skills345346- `k8s-manifest-generator` - For creating secure manifests347- `gitops-workflow` - For automated policy deployment348