Implement Kubernetes security policies including

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.
--- name: k8s-security-policies description: 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. --- # Kubernetes Security Policies Comprehensive guide for implementing NetworkPolicy, PodSecurityPolicy, RBAC, and Pod Security Standards in Kubernetes. ## Purpose Implement defense-in-depth security for Kubernetes clusters using network policies, pod security standards, and RBAC. ## When to Use This Skill - Implement network segmentation - Configure pod security standards - Set up RBAC for least-privilege access
Overview
Comprehensive guide for implementing NetworkPolicy, PodSecurityPolicy, RBAC, and Pod Security Standards in Kubernetes.
Manual install (npx, ZIP)
npx skills add wshobson/agents --skill k8s-security-policies -g -a claude-codemkdir -p ~/.claude/skills && curl -sL https://codeload.github.com/wshobson/agents/tar.gz/4236bb91f839 \
| tar -xz -C ~/.claude/skills --strip-components=4 agents-4236bb91f839/plugins/kubernetes-operations/skills/k8s-security-policiesNew-Item -ItemType Directory -Force "$HOME\.claude\skills" | Out-Null; Invoke-WebRequest "https://m.agentalley.io/zip/20e6f1a48ba51f774edb/k8s-security-policies.zip" -OutFile "$env:TEMP\k8s-security-policies.zip"; Expand-Archive "$env:TEMP\k8s-security-policies.zip" -DestinationPath "$HOME\.claude\skills" -ForceConnects with Agent Alley MCP · agentalley.io/mcp
Files included
SKILL.md7.0 KBassets/1 file · 3.1 KBreferences/1 file · 4.1 KB
Read the files here
Show the full text348 lines
Kubernetes Security Policies
Comprehensive guide for implementing NetworkPolicy, PodSecurityPolicy, RBAC, and Pod Security Standards in Kubernetes.
Purpose
Implement defense-in-depth security for Kubernetes clusters using network policies, pod security standards, and RBAC.
When to Use This Skill
- Implement network segmentation
- Configure pod security standards
- Set up RBAC for least-privilege access
- Create security policies for compliance
- Implement admission control
- Secure multi-tenant clusters
Pod Security Standards
1. Privileged (Unrestricted)
apiVersion: v1
kind: Namespace
metadata:
name: privileged-ns
labels:
pod-security.kubernetes.io/enforce: privileged
pod-security.kubernetes.io/audit: privileged
pod-security.kubernetes.io/warn: privileged
2. Baseline (Minimally restrictive)
apiVersion: v1
kind: Namespace
metadata:
name: baseline-ns
labels:
pod-security.kubernetes.io/enforce: baseline
pod-security.kubernetes.io/audit: baseline
pod-security.kubernetes.io/warn: baseline
3. Restricted (Most restrictive)
apiVersion: v1
kind: Namespace
metadata:
name: restricted-ns
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/audit: restricted
pod-security.kubernetes.io/warn: restricted
Network Policies
Default Deny All
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-all
namespace: production
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
Allow Frontend to Backend
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-frontend-to-backend
namespace: production
spec:
podSelector:
matchLabels:
app: backend
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 8080
Allow DNS
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns
namespace: production
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
name: kube-system
ports:
- protocol: UDP
port: 53
Reference: See assets/network-policy-template.yaml
RBAC Configuration
Role (Namespace-scoped)
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
namespace: production
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
ClusterRole (Cluster-wide)
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: secret-reader
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "watch", "list"]
RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: production
subjects:
- kind: User
name: jane
apiGroup: rbac.authorization.k8s.io
- kind: ServiceAccount
name: default
namespace: production
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
Reference: See references/rbac-patterns.md
Pod Security Context
Restricted Pod
apiVersion: v1
kind: Pod
metadata:
name: secure-pod
spec:
securityContext:
runAsNonRoot: true
runAsUser: 1000
fsGroup: 1000
seccompProfile:
type: RuntimeDefault
containers:
- name: app
image: myapp:1.0
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
Policy Enforcement with OPA Gatekeeper
ConstraintTemplate
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
name: k8srequiredlabels
spec:
crd:
spec:
names:
kind: K8sRequiredLabels
validation:
openAPIV3Schema:
type: object
properties:
labels:
type: array
items:
type: string
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
package k8srequiredlabels
violation[{"msg": msg, "details": {"missing_labels": missing}}] {
provided := {label | input.review.object.metadata.labels[label]}
required := {label | label := input.parameters.labels[_]}
missing := required - provided
count(missing) > 0
msg := sprintf("missing required labels: %v", [missing])
}
Constraint
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sRequiredLabels
metadata:
name: require-app-label
spec:
match:
kinds:
- apiGroups: ["apps"]
kinds: ["Deployment"]
parameters:
labels: ["app", "environment"]
Service Mesh Security (Istio)
PeerAuthentication (mTLS)
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: default
namespace: production
spec:
mtls:
mode: STRICT
AuthorizationPolicy
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: allow-frontend
namespace: production
spec:
selector:
matchLabels:
app: backend
action: ALLOW
rules:
- from:
- source:
principals: ["cluster.local/ns/production/sa/frontend"]
Best Practices
- Implement Pod Security Standards at namespace level
- Use Network Policies for network segmentation
- Apply least-privilege RBAC for all service accounts
- Enable admission control (OPA Gatekeeper/Kyverno)
- Run containers as non-root
- Use read-only root filesystem
- Drop all capabilities unless needed
- Implement resource quotas and limit ranges
- Enable audit logging for security events
- Regular security scanning of images
Compliance Frameworks
CIS Kubernetes Benchmark
- Use RBAC authorization
- Enable audit logging
- Use Pod Security Standards
- Configure network policies
- Implement secrets encryption at rest
- Enable node authentication
NIST Cybersecurity Framework
- Implement defense in depth
- Use network segmentation
- Configure security monitoring
- Implement access controls
- Enable logging and monitoring
Troubleshooting
NetworkPolicy not working:
# Check if CNI supports NetworkPolicy
kubectl get nodes -o wide
kubectl describe networkpolicy <name>
RBAC permission denied:
# Check effective permissions
kubectl auth can-i list pods --as system:serviceaccount:default:my-sa
kubectl auth can-i '*' '*' --as system:serviceaccount:default:my-sa
Related Skills
k8s-manifest-generator- For creating secure manifestsgitops-workflow- For automated policy deployment
| 1 | |
| 2 | name k8s-security-policies |
| 3 | description 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 | |
| 5 | |
| 6 | # Kubernetes Security Policies |
| 7 | |
| 8 | Comprehensive guide for implementing NetworkPolicy, PodSecurityPolicy, RBAC, and Pod Security Standards in Kubernetes. |
| 9 | |
| 10 | ## Purpose |
| 11 | |
| 12 | Implement defense-in-depth security for Kubernetes clusters using network policies, pod security standards, and RBAC. |
| 13 | |
| 14 | ## When to Use This Skill |
| 15 | |
| 16 | Implement network segmentation |
| 17 | Configure pod security standards |
| 18 | Set up RBAC for least-privilege access |
| 19 | Create security policies for compliance |
| 20 | Implement admission control |
| 21 | Secure multi-tenant clusters |
| 22 | |
| 23 | ## Pod Security Standards |
| 24 | |
| 25 | ### 1. Privileged (Unrestricted) |
| 26 | |
| 27 | |
| 28 | apiVersion: v1 |
| 29 | kind: Namespace |
| 30 | metadata: |
| 31 | name: privileged-ns |
| 32 | labels: |
| 33 | pod-security.kubernetes.io/enforce: privileged |
| 34 | pod-security.kubernetes.io/audit: privileged |
| 35 | pod-security.kubernetes.io/warn: privileged |
| 36 | |
| 37 | |
| 38 | ### 2. Baseline (Minimally restrictive) |
| 39 | |
| 40 | |
| 41 | apiVersion: v1 |
| 42 | kind: Namespace |
| 43 | metadata: |
| 44 | name: baseline-ns |
| 45 | labels: |
| 46 | pod-security.kubernetes.io/enforce: baseline |
| 47 | pod-security.kubernetes.io/audit: baseline |
| 48 | pod-security.kubernetes.io/warn: baseline |
| 49 | |
| 50 | |
| 51 | ### 3. Restricted (Most restrictive) |
| 52 | |
| 53 | |
| 54 | apiVersion: v1 |
| 55 | kind: Namespace |
| 56 | metadata: |
| 57 | name: restricted-ns |
| 58 | labels: |
| 59 | pod-security.kubernetes.io/enforce: restricted |
| 60 | pod-security.kubernetes.io/audit: restricted |
| 61 | pod-security.kubernetes.io/warn: restricted |
| 62 | |
| 63 | |
| 64 | ## Network Policies |
| 65 | |
| 66 | ### Default Deny All |
| 67 | |
| 68 | |
| 69 | apiVersion: networking.k8s.io/v1 |
| 70 | kind: NetworkPolicy |
| 71 | metadata: |
| 72 | name: default-deny-all |
| 73 | namespace: production |
| 74 | spec: |
| 75 | podSelector: {} |
| 76 | policyTypes: |
| 77 | - Ingress |
| 78 | - Egress |
| 79 | |
| 80 | |
| 81 | ### Allow Frontend to Backend |
| 82 | |
| 83 | |
| 84 | apiVersion: networking.k8s.io/v1 |
| 85 | kind: NetworkPolicy |
| 86 | metadata: |
| 87 | name: allow-frontend-to-backend |
| 88 | namespace: production |
| 89 | spec: |
| 90 | podSelector: |
| 91 | matchLabels: |
| 92 | app: backend |
| 93 | policyTypes: |
| 94 | - Ingress |
| 95 | ingress: |
| 96 | - from: |
| 97 | - podSelector: |
| 98 | matchLabels: |
| 99 | app: frontend |
| 100 | ports: |
| 101 | - protocol: TCP |
| 102 | port: 8080 |
| 103 | |
| 104 | |
| 105 | ### Allow DNS |
| 106 | |
| 107 | |
| 108 | apiVersion: networking.k8s.io/v1 |
| 109 | kind: NetworkPolicy |
| 110 | metadata: |
| 111 | name: allow-dns |
| 112 | namespace: production |
| 113 | spec: |
| 114 | podSelector: {} |
| 115 | policyTypes: |
| 116 | - Egress |
| 117 | egress: |
| 118 | - to: |
| 119 | - namespaceSelector: |
| 120 | matchLabels: |
| 121 | name: kube-system |
| 122 | ports: |
| 123 | - protocol: UDP |
| 124 | port: 53 |
| 125 | |
| 126 | |
| 127 | **Reference:** See `assets/network-policy-template.yaml` |
| 128 | |
| 129 | ## RBAC Configuration |
| 130 | |
| 131 | ### Role (Namespace-scoped) |
| 132 | |
| 133 | |
| 134 | apiVersion: rbac.authorization.k8s.io/v1 |
| 135 | kind: Role |
| 136 | metadata: |
| 137 | name: pod-reader |
| 138 | namespace: production |
| 139 | rules: |
| 140 | - apiGroups: [""] |
| 141 | resources: ["pods"] |
| 142 | verbs: ["get", "watch", "list"] |
| 143 | |
| 144 | |
| 145 | ### ClusterRole (Cluster-wide) |
| 146 | |
| 147 | |
| 148 | apiVersion: rbac.authorization.k8s.io/v1 |
| 149 | kind: ClusterRole |
| 150 | metadata: |
| 151 | name: secret-reader |
| 152 | rules: |
| 153 | - apiGroups: [""] |
| 154 | resources: ["secrets"] |
| 155 | verbs: ["get", "watch", "list"] |
| 156 | |
| 157 | |
| 158 | ### RoleBinding |
| 159 | |
| 160 | |
| 161 | apiVersion: rbac.authorization.k8s.io/v1 |
| 162 | kind: RoleBinding |
| 163 | metadata: |
| 164 | name: read-pods |
| 165 | namespace: production |
| 166 | subjects: |
| 167 | - kind: User |
| 168 | name: jane |
| 169 | apiGroup: rbac.authorization.k8s.io |
| 170 | - kind: ServiceAccount |
| 171 | name: default |
| 172 | namespace: production |
| 173 | roleRef: |
| 174 | kind: Role |
| 175 | name: pod-reader |
| 176 | apiGroup: rbac.authorization.k8s.io |
| 177 | |
| 178 | |
| 179 | **Reference:** See `references/rbac-patterns.md` |
| 180 | |
| 181 | ## Pod Security Context |
| 182 | |
| 183 | ### Restricted Pod |
| 184 | |
| 185 | |
| 186 | apiVersion: v1 |
| 187 | kind: Pod |
| 188 | metadata: |
| 189 | name: secure-pod |
| 190 | spec: |
| 191 | securityContext: |
| 192 | runAsNonRoot: true |
| 193 | runAsUser: 1000 |
| 194 | fsGroup: 1000 |
| 195 | seccompProfile: |
| 196 | type: RuntimeDefault |
| 197 | containers: |
| 198 | - name: app |
| 199 | image: myapp:1.0 |
| 200 | securityContext: |
| 201 | allowPrivilegeEscalation: false |
| 202 | readOnlyRootFilesystem: true |
| 203 | capabilities: |
| 204 | drop: |
| 205 | - ALL |
| 206 | |
| 207 | |
| 208 | ## Policy Enforcement with OPA Gatekeeper |
| 209 | |
| 210 | ### ConstraintTemplate |
| 211 | |
| 212 | |
| 213 | apiVersion: templates.gatekeeper.sh/v1 |
| 214 | kind: ConstraintTemplate |
| 215 | metadata: |
| 216 | name: k8srequiredlabels |
| 217 | spec: |
| 218 | crd: |
| 219 | spec: |
| 220 | names: |
| 221 | kind: K8sRequiredLabels |
| 222 | validation: |
| 223 | openAPIV3Schema: |
| 224 | type: object |
| 225 | properties: |
| 226 | labels: |
| 227 | type: array |
| 228 | items: |
| 229 | type: string |
| 230 | targets: |
| 231 | - target: admission.k8s.gatekeeper.sh |
| 232 | rego: | |
| 233 | package k8srequiredlabels |
| 234 | violation[{"msg": msg, "details": {"missing_labels": missing}}] { |
| 235 | provided := {label | input.review.object.metadata.labels[label]} |
| 236 | required := {label | label := input.parameters.labels[_]} |
| 237 | missing := required - provided |
| 238 | count(missing) > 0 |
| 239 | msg := sprintf("missing required labels: %v", [missing]) |
| 240 | } |
| 241 | |
| 242 | |
| 243 | ### Constraint |
| 244 | |
| 245 | |
| 246 | apiVersion: constraints.gatekeeper.sh/v1beta1 |
| 247 | kind: K8sRequiredLabels |
| 248 | metadata: |
| 249 | name: require-app-label |
| 250 | spec: |
| 251 | match: |
| 252 | kinds: |
| 253 | - apiGroups: ["apps"] |
| 254 | kinds: ["Deployment"] |
| 255 | parameters: |
| 256 | labels: ["app", "environment"] |
| 257 | |
| 258 | |
| 259 | ## Service Mesh Security (Istio) |
| 260 | |
| 261 | ### PeerAuthentication (mTLS) |
| 262 | |
| 263 | |
| 264 | apiVersion: security.istio.io/v1beta1 |
| 265 | kind: PeerAuthentication |
| 266 | metadata: |
| 267 | name: default |
| 268 | namespace: production |
| 269 | spec: |
| 270 | mtls: |
| 271 | mode: STRICT |
| 272 | |
| 273 | |
| 274 | ### AuthorizationPolicy |
| 275 | |
| 276 | |
| 277 | apiVersion: security.istio.io/v1beta1 |
| 278 | kind: AuthorizationPolicy |
| 279 | metadata: |
| 280 | name: allow-frontend |
| 281 | namespace: production |
| 282 | spec: |
| 283 | selector: |
| 284 | matchLabels: |
| 285 | app: backend |
| 286 | action: ALLOW |
| 287 | rules: |
| 288 | - from: |
| 289 | - source: |
| 290 | principals: ["cluster.local/ns/production/sa/frontend"] |
| 291 | |
| 292 | |
| 293 | ## Best Practices |
| 294 | |
| 295 | **Implement Pod Security Standards** at namespace level |
| 296 | **Use Network Policies** for network segmentation |
| 297 | **Apply least-privilege RBAC** for all service accounts |
| 298 | **Enable admission control** (OPA Gatekeeper/Kyverno) |
| 299 | **Run containers as non-root** |
| 300 | **Use read-only root filesystem** |
| 301 | **Drop all capabilities** unless needed |
| 302 | **Implement resource quotas** and limit ranges |
| 303 | **Enable audit logging** for security events |
| 304 | **Regular security scanning** of images |
| 305 | |
| 306 | ## Compliance Frameworks |
| 307 | |
| 308 | ### CIS Kubernetes Benchmark |
| 309 | |
| 310 | Use RBAC authorization |
| 311 | Enable audit logging |
| 312 | Use Pod Security Standards |
| 313 | Configure network policies |
| 314 | Implement secrets encryption at rest |
| 315 | Enable node authentication |
| 316 | |
| 317 | ### NIST Cybersecurity Framework |
| 318 | |
| 319 | Implement defense in depth |
| 320 | Use network segmentation |
| 321 | Configure security monitoring |
| 322 | Implement access controls |
| 323 | Enable logging and monitoring |
| 324 | |
| 325 | ## Troubleshooting |
| 326 | |
| 327 | **NetworkPolicy not working:** |
| 328 | |
| 329 | |
| 330 | # Check if CNI supports NetworkPolicy |
| 331 | kubectl get nodes -o wide |
| 332 | kubectl describe networkpolicy <name> |
| 333 | |
| 334 | |
| 335 | **RBAC permission denied:** |
| 336 | |
| 337 | |
| 338 | # Check effective permissions |
| 339 | kubectl auth can-i list pods --as system:serviceaccount:default:my-sa |
| 340 | kubectl auth can-i '*' '*' --as system:serviceaccount:default:my-sa |
| 341 | |
| 342 | |
| 343 | |
| 344 | ## Related Skills |
| 345 | |
| 346 | `k8s-manifest-generator` - For creating secure manifests |
| 347 | `gitops-workflow` - For automated policy deployment |
| 348 |
Details
- Updated
- Sep 21, 2026
- License
- MIT
- Stars on the repo
- ★ 39,857
- Checked
- Source
- wshobson/agents ↗
- Creator
- wshobson · 179 listings
Similar skills
See all →
SkillSecurity guidance and vulnerability review for codebasesSecurity audit
MCPYou need to audit Active Directory security postureAd security reviewer
SkillApply modern web development best practices for security
SkillHelps Claude write secure web applicationsSecure Coding Guide for Web Applications
MCPReviews completed implementation against governing securitySecurity reviewer
SkillAccount-based marketing for B2B SaaSAccount-Based Marketing OperationsMore from wshobson
SkillMaster C/.NET backend development patterns for building
SkillImplement WCAG 2.2 compliant interfaces with mobileAccessibility compliance
SkillMaster Material Design 3 and Jetpack Compose patternsAndroid Mobile Design
SkillMigrate from AngularJS to Angular using hybrid modeAngular migration
SkillUnderstand anti-reversing, obfuscationAnti reversing techniques
SkillBuild production Apache Airflow DAGs with best practicesApache Airflow DAG Patterns