Securing Kubernetes takes planning at several levels at once. The more of your workloads run in containers, the more your applications and data depend on how well the cluster is locked down.
Understanding the Kubernetes security model
Kubernetes security works on four layers:
- Cluster security: the cluster infrastructure itself
- Workload security: the individual applications
- Network security: which traffic is allowed to flow
- Data security: sensitive information such as credentials
RBAC (role-based access control)
Role-based access control decides who can reach which resources and what they can do with them. It is usually the first control to get right.
Creating secure RBAC policies
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: production
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
Good RBAC practice:
- Follow the principle of least privilege
- Use namespace-specific roles when possible
- Audit and review permissions regularly
- Avoid cluster-admin unless there is no other way
Network policies
Network policies control which pods can talk to each other, much like internal firewalls for your applications.
Example network policy
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all-ingress
namespace: secure-namespace
spec:
podSelector: {}
policyTypes:
- Ingress
Pod security standards
Kubernetes Pod Security Standards let you define security policies for your workloads.
Security context best practices
apiVersion: v1
kind: Pod
metadata:
name: secure-pod
spec:
securityContext:
runAsNonRoot: true
runAsUser: 1000
fsGroup: 2000
containers:
- name: app
image: myapp:latest
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
Image security
Container images are a common way in for attackers. To reduce that risk:
- Use trusted base images from reputable sources.
- Scan images for vulnerabilities with tools like Trivy or Clair.
- Sign images with a tool like Cosign.
- Use admission controllers to enforce image policies.
Secrets management
Never store sensitive data in plain text in your Kubernetes manifests:
apiVersion: v1
kind: Secret
metadata:
name: app-secret
type: Opaque
data:
database-password: <base64-encoded-password>
External secret managers
For sensitive data, consider an external secret manager such as:
- HashiCorp Vault
- AWS Secrets Manager
- Azure Key Vault
- External Secrets Operator
Monitoring and auditing
Log and monitor these areas:
- Audit logging of every API server request
- Runtime security monitoring of container behaviour
- Regular compliance scans and security assessments
- Alerting and response when something suspicious happens
Cluster hardening
Control plane
- Enable audit logging
- Restrict access to the API server
- Use TLS everywhere
- Apply security updates regularly
Nodes
- Keep the attack surface small
- Use read-only root filesystems
- Run runtime security tooling
- Patch the operating system regularly
Compliance and governance
Enterprise deployments usually have to meet one or more of these frameworks:
- CIS Kubernetes Benchmark
- NIST Cybersecurity Framework
- SOC 2 Type II
- PCI DSS (for payment processing)
Implementation checklist
- RBAC policies implemented and reviewed
- Network policies defined for all namespaces
- Pod Security Standards enforced
- Image scanning integrated into CI/CD
- Secrets management solution deployed
- Monitoring and alerting configured
- Regular security assessments scheduled
Conclusion
Kubernetes security needs regular reviews, updates and monitoring after the initial setup. Start with RBAC, network policies and pod security, then add the other tools on top.
Developers and operators share responsibility for security with the security team, so build these practices into your development process.
Additional resources
Questions about putting these practices in place? Talk to our engineers.
