Running a web workload on Kubernetes with default Pod settings leaves too many privileges enabled: writable container layers, Linux capabilities you never use, and unrestricted east-west cluster networking.
Here is the minimal baseline we enforce for services deployed to our K3s cluster.
1. Run Rootless with a Read-Only Root Filesystem
Using an unprivileged base image such as nginxinc/nginx-unprivileged ensures the process listens on an unprivileged port (8080) and never binds UID 0. Pairing that with readOnlyRootFilesystem: true prevents an attacker from dropping binaries or modifying served static files at runtime.
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
runAsNonRoot: true
runAsUser: 101
capabilities:
drop:
- ALL
Because Nginx still needs scratch space for its PID file and proxy/temp buffers, mount memory-backed emptyDir volumes exclusively where required:
volumeMounts:
- name: nginx-cache
mountPath: /var/cache/nginx
- name: nginx-tmp
mountPath: /tmp
2. Default-Deny Egress with Kubernetes NetworkPolicy
A static blog or frontend container has no legitimate reason to initiate outbound TCP connections to internal databases, cloud metadata services, or arbitrary internet hosts.
By declaring both Ingress and Egress in policyTypes and omitting any egress allow rules, Kubernetes blocks 100% of outbound traffic from the pod while still allowing Traefik in kube-system to route HTTPS traffic into port 8080:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: security-blog-netpol
namespace: security
spec:
podSelector:
matchLabels:
app: security-blog
policyTypes:
- Ingress
- Egress
ingress:
- from:
- namespaceSelector: {}
ports:
- protocol: TCP
port: 8080
Takeaway: Combining unprivileged containers, read-only root filesystems, and egress-denied
NetworkPolicymanifests shrinks both the blast radius and lateral movement surface for web-facing pods.