← Back to list

Kubernetes Security Best Practices — Secure Your Workloads

Introduction to Kubernetes Security

AXIOMIO Social in AXIOMIO · 2026-04-24 06:48 · 1 claps · 12.5 min read
#kubernetes #security #best-practices #api
Open on Medium ↗
Wiki topics: ☁️ · DevOps & Cloud

Kubernetes Security Best Practices — Secure Your Workloads

Introduction to Kubernetes Security

Kubernetes has become the go-to platform for running cloud-native applications. However, its flexibility and distributed architecture inherently expand the attack surface compared to traditional virtual machines.

Kubernetes security isn’t about flipping a single switch — it requires a combination of hardening controls, secure CI/CD processes, image provenance verification, runtime detection, network segmentation, and operational best practices.

In this article, we’ll explore the critical questions: what to protect, who is responsible for each control, and how to effectively approach secure Kubernetes operations.

Why Kubernetes Security is Different

Kubernetes differs from traditional infrastructure in ways that significantly affect the threat model:

  • Privileged and exposed control plane APIs: Developers and automation have access to these highly privileged endpoints. If an API credential or controller is compromised, attackers can gain cluster-wide control.
  • Ephemeral and mobile workloads: Container images move through CI pipelines, registries, and nodes, making supply chain integrity essential.
  • Increased east-west traffic: Microservices communicate extensively within clusters, requiring fine-grained network segmentation.
  • Multi-tenancy and shared components: Failures in isolation can have wide-reaching impact.

Attackers often target image registries, misconfigured RBAC, exposed kubelets, unencrypted etcd databases, permissive admission controls, and CI/CD pipelines. Therefore, defenses must combine identity and access management, supply chain verification, runtime detection, secure pod and node defaults, and continuous auditing.

Key Security Principles for Kubernetes

  • Adopt defense in depth: Layer multiple controls so a single failure won’t lead to a full breach.
  • Enforce least privilege: Grant minimal necessary permissions to users, service accounts, and workloads. Treat automation identities with the same care as human users.
  • Shift left: Integrate static scanning, Software Bill of Materials (SBOM) generation, and policy checks into CI pipelines — only verified artifacts should reach the cluster.
  • Use immutable and reproducible artifacts: Build images deterministically, generate and sign SBOMs, and require provenance verification before deployment.
  • Continuous verification and telemetry: Collect audit logs, runtime events, and network telemetry to automate detection and response.
  • Define clear ownership: Separate responsibilities between platform/SRE teams (cluster-level controls, nodes, API server, etcd) and application teams (images, manifests, runtime behavior), codifying this in runbooks and policy-as-code.

Kubernetes Cluster Hardening

Cluster hardening targets securing the control plane, nodes, and the sensitive components that store cluster state.

API Server Security Configuration

The API server is your cluster’s main gatekeeper. Key configurations include:

  • Enable authentication and consistent authorization: Use OpenID Connect (OIDC) or corporate identity providers for human users, favoring short-lived tokens whenever possible. Avoid unauthenticated access and legacy static tokens.
  • Enforce RBAC and disable ABAC: Use Role and ClusterRole to scope permissions. Audit all subjects bound to high-privilege roles.
  • Enable API audit logging: Configure an audit policy tailored to your environment, capturing request metadata, user info, impersonation, and response codes. Route these logs to centralized immutable storage for retention and forensic analysis.
  • Restrict admitted operations via admission controllers: Enable PodSecurity admission (or use Gatekeeper with OPA for richer policies), NodeRestriction, ValidatingAdmissionWebhook, and MutatingAdmissionWebhook as needed.
  • Control API server network access: Restrict the API server endpoint to trusted networks and management hosts. On cloud providers, leverage private endpoints or authorized IP ranges.

Example kube-apiserver flags commonly used:

— authorization-mode=RBAC — audit-log-path=/var/log/kubernetes/audit.log — audit-policy-file=/etc/kubernetes/audit-policy.yaml — tls-cert-file=/etc/kubernetes/pki/apiserver.crt — advertise-address=<control-plane-ip>

Securing etcd and Control Plane

The etcd database holds crucial cluster state, including Secrets (unless encrypted at rest). Protect it by:

  • Encrypting data at rest using Kubernetes encryption configuration for Secrets. Use encryption providers like KMS with separate keys, and rotate keys regularly.
  • Using TLS client/server certificates to secure etcd, limiting access strictly to control-plane components. Do not expose etcd to worker nodes or public networks.
  • Backing up etcd securely with encrypted and integrity-checked backups, tested regularly for restorability.
  • Hardening control-plane hosts by using dedicated or managed control planes, minimizing installed packages, applying patches promptly, and monitoring host-level logs.

Kubelet and Node Hardening

Kubelets are powerful agents capable of creating pods, accessing container logs, and running commands. Harden kubelets and nodes by:

  • Enabling kubelet authentication and authorization using client certificates and configuring — authentication-token-webhook and — authorization-mode=Webhook or RBAC.
  • Restricting read-only and read/write kubelet ports to prevent anonymous access, limiting access through firewall rules or cloud security groups.
  • Running nodes with OS-level security features: up-to-date patches, anti-malware software, kernel livepatching if available, and disabling unused services.
  • Applying node-level security policies like SELinux or AppArmor, mounting filesystems read-only when feasible, disabling SSH where unnecessary, and managing node access via bastion hosts.
  • Isolating workloads by node role using taints and tolerations or node selectors to separate high-risk workloads from critical or sensitive nodes.

Example commands and checks:

kubectl get nodes -o wide curl -k https://<node-ip>:10250/healthz # Verify kubelet access restrictions

Check kubelet systemd or manifest files for authentication and authorization flags.

Example Commands and Audit Checks

Quick commands for validating critical controls:

  • Check RBAC bindings for cluster-admin usage:

kubectl get clusterrolebindings -o json | jq ‘.items[] | select(.roleRef.name==”cluster-admin”)’

  • Verify anonymous access is disabled:

kubectl auth can-i — list — as=system:anonymous

  • Check for etcd encryption configuration (on control plane):

cat /etc/kubernetes/encryption-config.yaml

  • Run kube-bench to evaluate CIS benchmark compliance:

kubectl apply -f https://raw.githubusercontent.com/aquasecurity/kube-bench/main/job.yaml kubectl logs job/kube-bench -n kube-bench

  • Verify PodSecurity Admission mode and level (for Kubernetes >=1.23):

kubectl get podsecuritypolicy # If using legacy PSP

Or check PSA annotations per pod:

kubectl get pods — all-namespaces -o custom-columns=NODE:.spec.nodeName,PSP:.metadata.annotations.’pod-security.kubernetes.io/enforce’

Identity and Access Management

Identity is foundational for a secure cluster. Weak or overly permissive identities lead to privilege escalation and stolen credentials.

Best Practices for RBAC

  • Design roles around least privilege. Create narrow, namespaced Roles for application teams, reserving ClusterRoles for platform operations.
  • Avoid binding many subjects to cluster-admin.
  • Use role aggregation sparingly, binding Roles to specific service accounts representing CI pipelines or operators.
  • Regularly audit RoleBindings and ClusterRoleBindings, removing orphaned service accounts and stale bindings.

Example namespaced Role for a CI job that pulls secrets and updates deployments:

apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: ci-deployer namespace: prod rules:

  • apiGroups: [“”] resources: [“secrets”, “configmaps”] verbs: [“get”, “list”]
  • apiGroups: [“apps”] resources: [“deployments”] verbs: [“get”, “update”]

Using OIDC and Service Accounts

  • Use OIDC federation for human users to avoid long-lived static kubeconfig files. Issue short-lived tokens through your identity provider.
  • Prefer Workload Identity features (cloud provider offerings) or projected service account tokens to tie Kubernetes ServiceAccounts to cloud identities and reduce credential sprawl.
  • Rotate service account tokens regularly and minimize use of the default service account. Create dedicated ServiceAccounts per application with minimal privileges.

Implementing Least Privilege Access

  • Define required permissions for each workload and enforce them via policy-as-code.
  • Scan for over-permissive bindings using tools like kube-score or rakkess.
  • Integrate CI checks that verify manifest RBAC before merging changes.
  • Clearly map ownership: platform teams manage ClusterRoles; application teams own namespace Roles and runtime security contexts. Document these boundaries.

Image and Supply Chain Security

Security starts in the CI pipeline. A compromised image or malicious dependency can undermine all cluster hardening.

Image Scanning Techniques

  • Scan at multiple stages: developer workstation, CI build, registry, and just before runtime.
  • Use vulnerability scanners that detect CVEs in operating system packages, application dependencies, and container layers.
  • Fail CI pipelines when critical vulnerabilities are discovered unless exceptions are formally documented.
  • Generate SBOMs (e.g., SPDX or CycloneDX formats) for every image to support downstream verification.

Popular open-source scanners include Trivy, Clair, and Grype. Example scanning command:

trivy image — format json — output trivy-report.json myregistry/myapp:sha

Signed Images and Registry Management

  • Use container signature frameworks like cosign or Notary to sign images and verify signatures before deployment.
  • Use attestation mechanisms to record build provenance such as builder identity, commit ID, and SBOM.
  • Restrict registry access: enforce authentication for push and pull, control network access, and scan new images before promotion.
  • Use immutable tags in production (avoid :latest) and implement lifecycle policies to remove stale or vulnerable images.

CI/CD Integration Patterns for Supply Chain Security

  • Adopt a gated promotion model: images built in CI are scanned, signed, and stored in a registry; only signed and scanned images are promoted to production.
  • Embed policy checks in pipelines (for example, use Grafeas attestations or Open Policy Agent constraints) to block merges or pushes that lack necessary SBOMs or exceed vulnerability thresholds.

Example simplified GitHub Actions snippet:

  • name: Build image uses: docker/build-push-action@v2

  • name: Generate SBOM run: trivy image — format cyclonedx — output sbom.xml myimage:sha

  • name: Sign image run: cosign sign — key cosign.key myregistry/myimage:sha

Pod and Workload Hardening

Reducing the attack surface inside pods improves resilience if an image is malicious or a container is compromised.

Pod Security Standards and Admission Controls

  • Use Pod Security Admission (PSA) with levels: restricted, baseline, and privileged.
  • Enforce namespace-level policies matching risk tolerance — for example, baseline for developer namespaces and restricted for production.
  • For richer, customizable policies, leverage OPA Gatekeeper or Kyverno to enforce rules like disallowing privileged containers, requiring read-only root filesystems, and enforcing resource limits.

Example PSA manifest for a namespace:

apiVersion: v1 kind: Namespace metadata: name: prod annotations: pod-security.kubernetes.io/enforce: “restricted” pod-security.kubernetes.io/enforce-version: “latest”

Using Seccomp and Capability Restrictions

  • Apply a default seccomp profile and drop unnecessary Linux capabilities in pod specs.
  • Only add capabilities when absolutely necessary.

Example security context snippet:

securityContext: runAsNonRoot: true readOnlyRootFilesystem: true allowPrivilegeEscalation: false capabilities: drop: — ALL

For seccomp:

securityContext: seccompProfile: type: RuntimeDefault

Resource Limits and PodSecurityPolicy Alternatives

  • Always set resource requests and limits to prevent noisy neighbors and DoS-like exhaustion.
  • The deprecated PodSecurityPolicy (PSP) has been replaced by Pod Security Admission, Gatekeeper/OPA policies, or Kyverno. Use these alternatives to enforce workload security policies.
  • Provide reusable policy templates that platform and developer teams can apply and test.

Network Security and Segmentation

Network segmentation limits lateral movement and reduces blast radius in case of compromise.

NetworkPolicy Examples

  • Implement a deny-by-default posture by applying a default NetworkPolicy that denies all ingress and egress traffic.
  • Add allow policies for required communication between namespaces and pods using namespace and pod selectors.

Example NetworkPolicy allowing traffic only from a specific namespace:

apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-from-monitoring namespace: prod spec: podSelector: matchLabels: app: myapp ingress: — from: — namespaceSelector: matchLabels: name: monitoring

  • Be aware that NetworkPolicy enforcement depends on the capabilities of your chosen CNI plugin.

CNI Considerations

  • Choose a CNI that supports NetworkPolicy and egress controls you need, such as Calico, Cilium, Kube-router, or Weave.
  • Cilium offers advanced eBPF-based visibility and L7 policies.
  • Understand performance and operational trade-offs: some CNIs require additional components or kernel features.
  • Keep your CNI updated, monitor its health, and test network policies in staging before rolling out to production.

Ingress and Egress Controls

  • Restrict egress wherever possible to limit data exfiltration and access to attacker infrastructure.
  • Use egress network policies or dedicated egress gateways/proxies for centralized control and logging.
  • For ingress, terminate TLS at trusted ingress controllers, enable WAF features if needed, and authenticate any external API traffic before it reaches sensitive services.

Secrets Management

Kubernetes Secrets stored in etcd rely on encryption and RBAC for protection. For stronger guarantees, use external secret stores.

Secure Storage Solutions (KMS, Vault, SealedSecrets)

  • Use cloud KMS services or external secret managers like HashiCorp Vault, AWS Secrets Manager, or GCP Secret Manager.
  • Integrate secrets via CSI drivers or external secrets operators to avoid storing plaintext secrets in manifests or etcd.
  • Bitnami SealedSecrets enables encrypting secrets for safe Git storage. The sealed secret can be stored publicly and unsealed only by the controller in the cluster.
  • Mount secrets as files rather than environment variables when possible, with strict file permissions.

Secrets Rotation and Access Policies

  • Rotate secrets regularly and automate rotation for service credentials and keys.
  • Enforce strict access policies limiting which ServiceAccounts and namespaces have access to specific secrets.
  • Audit secret access and avoid logging secrets in application logs.
  • Use short-lived credentials where possible and prefer workload identity to avoid secrets altogether when cloud provider identity features are available.

Runtime Security and Threat Detection

Prevention alone isn’t enough — you must detect and respond to anomalies during runtime.

Runtime Protection Tools and Patterns

  • Deploy runtime security agents that detect suspicious behavior such as unexpected system calls, file modifications, container execs, or privilege escalations.
  • Popular tools include Falco, eBPF-based monitoring (such as in Cilium with Hubble), and commercial Endpoint Detection and Response (EDR) platforms that support containers.
  • Implement host- and container-level intrusion detection and integrate alerts into your SIEM.
  • Profile normal behavior for critical services and tune anomaly detection rules to reduce false positives.

Breach Detection Using eBPF, Falco, and EDR

  • Falco provides rule-based detection widely used in Kubernetes environments.
  • eBPF delivers high-fidelity observability with low overhead; combining eBPF monitoring with correlation engines enriches alert context.
  • EDR solutions aware of containers can trace process trees, network flows, and file operations across hosts and containers, improving response speed.

Logging, Monitoring, and Auditing

Good telemetry is critical for detection, forensic investigation, and compliance.

Audit Policy Configuration

  • Configure Kubernetes audit policies to capture necessary events without producing unmanageable log volumes.
  • At a minimum, log authentication attempts, RBAC changes, secret access, and admission controller rejections.
  • Use dynamic sampling for low-risk events to reduce noise.

What to Log for Detection and Forensics

  • Collect and centralize logs and telemetry from:
  • API server audit logs
  • Kubelet logs
  • Container stdout/stderr
  • Node system logs
  • Network flow logs (CNI or cloud VPC logs)
  • Registry events
  • Preserve image metadata and SBOMs to link running workloads to their build origins.

Ensure timestamps and host identifiers are synchronized across logs to enable accurate event correlation.

Integration with Monitoring Tools

  • Forward security telemetry to SIEM, SOAR, or monitoring platforms such as Prometheus, Grafana, Elasticsearch, or OpenSearch.
  • Create dashboards and automated alerts for high-priority events like privilege changes, unsigned deployments, or anomalous outbound traffic.

CI/CD and GitOps Security

Pipelines are the path to production — securing them is essential to prevent supply chain attacks.

Policy-as-Code and Pre-Deploy Scanning

  • Implement policies as code using OPA Gatekeeper or Kyverno to block non-compliant manifests.
  • Enforce image scanning, SBOM availability, signature validation, resource limits, and PodSecurity validations within PRs and pipelines.

SLSA and SBOM in Pipeline Security

  • Follow SLSA (Supply-chain Levels for Software Artifacts) guidelines to establish strong build provenance.
  • Generate and store SBOMs for every artifact; sign build attestations.
  • Design pipelines to produce immutable artifacts with attestations that can be verified before deployment.

Best Practices for Safe Pipelines

  • Limit CI pipeline permissions: use ephemeral credentials, restrict registry access, and minimize token scopes.
  • Require code reviews for pipeline configuration changes.
  • Scan third-party actions and plugins for supply-chain risks.
  • Design CI jobs to be idempotent, produce reproducible artifacts, and manage versions in immutable registries with proper retention and promotion workflows.

Compliance and Benchmarking

Using compliance frameworks and benchmarks helps provide measurable targets and progress tracking.

CIS Kubernetes Benchmark Overview

  • The CIS Kubernetes Benchmark provides detailed configuration recommendations across control plane, nodes, policies, and runtime.
  • Automate assessments using tools like kube-bench.
  • Prioritize remediating high- and medium-severity findings first, assigning owners and timelines.

Mapping to NIST and Other Standards

  • Map Kubernetes controls to broader frameworks such as NIST SP 800–53, ISO 27001, and SOC 2.
  • Focus on identity management, change control, logging, and incident response controls that overlap across standards.

Automated Compliance Scanning Tools

  • Utilize automated tools like kube-bench, Polaris, and commercial offerings to continuously scan clusters against CIS and internal policies.
  • Integrate scan results into ticketing systems to track remediation work.

Incident Response and Forensics

Preparing for worst-case scenarios is a must: detection, containment, eradication, and recovery.

Detection and Indicators of Compromise

Common indicators include:

  • Unexpected service accounts or ClusterRoleBindings.
  • New pods mounting hostPath volumes.
  • Container images from unknown registries.
  • Sudden spikes in image pulls.
  • Suspicious container exec sessions.
  • Unusual outbound network connections.

Containment and Remediation Steps

  • Isolate compromised workloads by cordoning or evicting nodes, or applying restrictive network policies.
  • Revoke credentials associated with compromised identities.
  • Rotate secrets and keys.
  • Block access to suspect images in the registry.
  • Replace compromised images with signed, trusted ones.
  • Scale down affected deployments and restore from trustworthy backups if needed.

Forensic Log Collection and Analysis

  • Collect API server audit logs, kubelet logs, host logs, container filesystems (as available), and network captures from relevant time windows.
  • Preserve etcd backups for integrity checks.
  • Use immutable log stores and export copies for thorough forensic analysis.

Prioritization of Security Fixes

Resources are always limited, so prioritize actions that reduce the greatest risk quickly.

Quick Wins vs. Long-term Projects

  • Quick wins: Enable audit logging, enforce RBAC and rotate cluster-admin credentials, enable etcd encryption for Secrets, set resource limits, apply PodSecurity Admission enforcement, enable image scanning in CI, and implement default deny NetworkPolicies.
  • Long-term projects: Build full supply-chain attestation with SLSA, adopt zero-trust network architecture, migrate to workload identity models, deploy cluster-wide runtime EDR, and automate remediation workflows.

Risk-based Prioritization Matrix

Score risks by their potential impact, ease of exploitation, and detectability. Address high-impact, easily exploitable misconfigurations first, such as:

  • Public API servers with anonymous authentication
  • Unencrypted etcd
  • Overly permissive RBAC

Tie fixes to SLAs and track metrics like mean time to detect and mean time to remediate.

Tools and Resources

Categories of Security Tools

Open-source and commercial tools cover categories such as:

  • Static image scanners (e.g., Trivy, Clair)
  • Compliance scanners (e.g., kube-bench)
  • Policy engines (OPA Gatekeeper, Kyverno)
  • Runtime detection (Falco, eBPF tools, Cilium Hubble)
  • Secret managers (HashiCorp Vault, External Secrets)
  • CNIs with policy support (Cilium, Calico)
  • Incident response and EDR platforms

Comparison of Free and Paid Solutions

  • Open-source tools offer transparency and rapid adoption but may require significant integration and operational effort.
  • Paid solutions often provide enterprise features: managed sensors, correlation engines, professional support, and compliance reporting.
  • Evaluate total cost, integration needs, and ability to reduce detection and remediation times when choosing.

Automation and Policy Enforcement Examples

  • Automate policy enforcement with Gatekeeper constraints blocking privileged pods or missing image signatures.
  • For example, constraints can reject pods lacking required labels or restrict allowed container registries.

Downloadable Security Checklist and Audit Scripts Available

Use curated checklists and audit scripts to expedite hardening:

  • RBAC audits
  • Kube-bench assessments
  • Image scanning pipeline validations
  • NetworkPolicy existence checks

Templates are provided for Pod Security Admission annotations, NetworkPolicy examples, and CI/service account RBAC roles.

Note: For reused manifest templates or curated content, ensure proper attribution and where possible, adapt them to your environment. Always use manifests targeting current Kubernetes API versions and test changes in staging before production rollout.

Conclusion

Summary of Key Best Practices

Securing Kubernetes requires a holistic approach: configuration hardening, identity and access management, supply-chain verification, pod and workload restrictions, network segmentation, secrets management, runtime detection, and comprehensive telemetry.

Prioritize least privilege, defense in depth, and automation. Embed security controls into CI/CD pipelines, and use policy-as-code for consistent enforcement.

References


메타데이터
post_id
ec4d1d61baab
slug
kubernetes-security-best-practices-secure-your-workloads-ec4d1d61baab
url
https://blog.axiomio.com/kubernetes-security-best-practices-secure-your-workloads-ec4d1d61baab
canonical_url
https://blog.axiomio.com/kubernetes-security-best-practices-secure-your-workloads-ec4d1d61baab
author_url
https://medium.com/@Axiom_IO
status
ok
fetched_at
2026-06-10 08:17:25