Authentication & Authorization
Control who can access the Kubernetes API and what actions they are allowed to perform.
Key Topics
- • Authentication
- • RBAC
- • Roles and ClusterRoles
- • RoleBindings
Learn how to secure Kubernetes clusters and workloads using RBAC, Service Accounts, Secrets, Network Policies, Pod Security Standards, Security Contexts, admission controls, and security best practices.
Kubernetes Security
RBAC & Access Control
Pod Security
Network Policies
Secrets Management
Security Monitoring
Introduction
Kubernetes security involves protecting the control plane, cluster infrastructure, applications, network communication, and sensitive data.
A secure Kubernetes environment typically uses multiple layers of protection instead of relying on a single security mechanism. Access controls, workload restrictions, network isolation, and secret management all work together.
Kubernetes provides built-in mechanisms such as RBAC, Secrets, Network Policies, Security Contexts, and Pod Security Standards that can be combined with infrastructure and operational security controls.
Security Fundamentals
Kubernetes security includes access control, workload hardening, network isolation, and protection of sensitive data.
Control who can access the Kubernetes API and what actions they are allowed to perform.
Key Topics
Restrict workload privileges and apply security controls to Pods and containers.
Key Topics
Control communication between workloads and restrict unnecessary ingress and egress traffic.
Key Topics
Protect sensitive configuration such as passwords, tokens, certificates, and API credentials.
Key Topics
Access Control
RBAC controls which identities can perform specific actions on Kubernetes resources. Permissions are defined using Roles or ClusterRoles and assigned through RoleBindings or ClusterRoleBindings.
The principle of least privilege is important when creating RBAC permissions. Users, applications, and Service Accounts should receive only the permissions required for their intended tasks.
RBAC Role Example
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
rules:
- apiGroups: [""]
resources:
- pods
verbs:
- get
- list
- watchPermissions
A Role defines permissions, while a RoleBinding assigns those permissions to users, groups, or Service Accounts.
Namespace-scoped Roles should be used when permissions do not need cluster-wide access.
RoleBinding Example
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
subjects:
- kind: ServiceAccount
name: app-service-account
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.ioWorkload Identity
Kubernetes workloads can use Service Accounts to authenticate to the Kubernetes API.
If a workload does not need Kubernetes API access, avoid unnecessarily mounting a Service Account token. When access is required, grant only the specific permissions needed by the application.
Disable Automatic Token Mounting
apiVersion: v1
kind: Pod
metadata:
name: example-app
spec:
automountServiceAccountToken: false
containers:
- name: app
image: nginxWorkload Protection
Pod Security Standards define security profiles that can help restrict potentially dangerous workload configurations.
Kubernetes defines Privileged, Baseline, and Restricted security profiles. The appropriate policy depends on the workload and its operational requirements.
Pod Security Admission can apply these standards at the namespace level using enforce, audit, or warn modes.
Pod Security Labels
apiVersion: v1
kind: Namespace
metadata:
name: production
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/audit: restricted
pod-security.kubernetes.io/warn: restrictedContainer Hardening
A Security Context defines privilege and access-control settings for Pods and containers.
Security settings can control the user that runs the process, privilege escalation, Linux capabilities, filesystem access, and other workload-level security options.
Security Context Example
securityContext:
runAsNonRoot: true
runAsUser: 1000
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALLNetwork Security
Network Policies define how Pods are allowed to communicate with other workloads and network destinations.
Policies can control ingress traffic, egress traffic, or both. The networking implementation in the cluster must support NetworkPolicy enforcement.
A default-deny approach can help create an allow-list model where only explicitly permitted communication is allowed.
Default Deny Example
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny
spec:
podSelector: {}
policyTypes:
- Ingress
- EgressSensitive Data
Kubernetes Secrets are designed for confidential information such as passwords, API tokens, certificates, and SSH keys.
Secret data should be protected with appropriate access controls. Base64 encoding is not encryption, so encoded Secret manifests should still be treated as sensitive.
Encryption at rest can be configured to protect Secret data stored in the Kubernetes control plane.
Secret Example
apiVersion: v1 kind: Secret metadata: name: database-secret type: Opaque stringData: username: appuser password: secure-password
Defense in Depth
A strong Kubernetes security posture combines multiple controls across the cluster instead of depending on a single mechanism.
Protect access to the Kubernetes API using authentication, authorization, TLS, and least-privilege access controls.
Restrict container privileges, apply Pod Security Standards, and configure Security Contexts.
Control traffic between Pods and external systems using Network Policies and infrastructure controls.
Protect sensitive data using Secrets, encryption at rest, restricted access, and careful secret handling.
Protect worker nodes, kubelet access, operating systems, and isolate sensitive workloads where required.
Use logging, monitoring, and audit mechanisms to detect unusual behavior and investigate security events.
kubectl
These commands can help inspect access controls, identities, Secrets, Network Policies, and workload configuration.
kubectl get rolesList Roles in the current namespace.
kubectl get rolebindingsList RoleBindings in the current namespace.
kubectl auth can-i create podsCheck whether the current identity can perform an action.
kubectl get serviceaccountsList ServiceAccounts in the current namespace.
kubectl get secretsList Secret resources in the current namespace.
kubectl get networkpoliciesList NetworkPolicy resources.
kubectl get pods -o wideInspect Pods and the nodes where they are scheduled.
kubectl describe pod <pod-name>Inspect Pod configuration and runtime information.
Learning Roadmap
Follow this progression to build a strong understanding of Kubernetes security concepts.
Learn the difference between control plane security, workload security, network security, and data protection.
Understand Roles, ClusterRoles, RoleBindings, ClusterRoleBindings, and the principle of least privilege.
Learn how workloads authenticate and avoid mounting service account credentials when they are unnecessary.
Use Kubernetes Secrets for confidential data and restrict access to sensitive resources.
Use Pod Security Standards and Security Contexts to reduce unnecessary container privileges.
Use Network Policies to define allowed ingress and egress communication between workloads.
Best Practices
Grant users, applications, and Service Accounts only the permissions required for their responsibilities.
Apply appropriate Pod Security Standards and restrict unnecessary privileges.
Where application requirements allow, avoid running containers with unnecessary root privileges.
Use Network Policies to limit communication between workloads and external destinations.
Restrict access to Secrets and avoid exposing sensitive values in source code or logs.
Review RBAC permissions, Service Accounts, workload configuration, and cluster security settings periodically.
Continue Learning
Learn Kubernetes architecture, clusters, nodes, Pods, Deployments, Services, and kubectl fundamentals.
🌐Learn Kubernetes Services, DNS, traffic routing, networking, and Network Policies.
📈Learn manual scaling, HPA, VPA, node autoscaling, resource management, and Kubernetes operations.
Next Step
Practice creating RBAC policies, configuring Security Contexts, applying Pod Security Standards, protecting Secrets, and restricting application traffic using Network Policies.