sodcalculator

10 Tech How-To

How to Secure Cloud Native Apps from Code to Cluster

Guide 10-59839 · Updated · Step by step

This guide walks you through securing a cloud native application at every stage of its lifecycle, from writing the first line of code to running it in a Kubernetes cluster. You’ll get a practical, layered approach covering code, build, image, deployment, and runtime security, with specific steps you can apply today. No prior security expertise is needed, but basic familiarity with containers and Kubernetes will help.

What You’ll Need

  • A source code repository (e.g., GitHub, GitLab, Bitbucket)
  • A container runtime (Docker, containerd, or similar)
  • Access to a Kubernetes cluster (minikube, kind, or a cloud provider)
  • Command-line tools: kubectl, docker, git, curl
  • Basic knowledge of YAML and shell commands

Step-by-Step

  1. Secure your code dependencies. Start by scanning your application’s dependencies for known vulnerabilities. Use a software composition analysis (SCA) tool like npm audit, pip-audit, or trivy fs to identify vulnerable libraries. Fix or update any critical or high severity issues before proceeding. Also, pin your dependencies to exact versions (not ranges) to avoid pulling in unexpected changes.
  2. Enforce code review and branch protection. In your Git provider, enable branch protection for the main branch. Require at least one approval for pull requests, and run automated security checks (linting, SAST, dependency scanning) as part of your CI pipeline. Block merges if any security check fails.
  3. Run static application security testing (SAST). Integrate a SAST tool (e.g., Semgrep, CodeQL, SonarQube) into your CI pipeline. These tools scan your source code for common vulnerabilities like SQL injection, XSS, or hardcoded secrets. Configure the scanner to fail the build on critical findings.
  4. Scan for secrets in code. Use a secret scanner (e.g., gitleaks, trufflehog) to detect accidentally committed API keys, passwords, or tokens. Run it as a pre-commit hook and in CI. If a secret is found, rotate it immediately and remove it from history (using tools like BFG).
  5. Build a minimal container image. Use a small base image like alpine or distroless to reduce the attack surface. Avoid including package managers, compilers, or debug tools in the final image. Copy only the compiled binary or runtime files needed to run the app.
  6. Scan the container image. Before pushing to a registry, scan the image for vulnerabilities using a tool like trivy image, grype, or anchore. Fix any critical vulnerabilities by updating base images or patching dependencies. Also, check that the image is not running as root (see step 9).
  7. Sign and verify images. Use cosign (part of the Sigstore project) to sign your container images. Then, configure your Kubernetes cluster to verify the signature before pulling an image, using tools like Kyverno or OPA Gatekeeper. This ensures only trusted images are deployed.
  8. Scan the IaC and Kubernetes manifests. Run a static scan on your Kubernetes YAML files and Infrastructure as Code (e.g., Terraform) using tools like kube-score, checkov, or polaris. These tools flag misconfigurations like running as root, missing resource limits, or insecure service accounts.
  9. Apply least privilege to the container. In your Kubernetes deployment, set securityContext to run the container as a non-root user, with a read-only root filesystem, and drop all Linux capabilities. Example:
    securityContext:
      runAsNonRoot: true
      runAsUser: 10001
      readOnlyRootFilesystem: true
      capabilities:
        drop: ["ALL"]
  10. Use network policies. Define Kubernetes NetworkPolicies to restrict traffic between pods. By default, all pods can communicate; use policies to allow only the necessary connections. For example, allow ingress to your web frontend from the ingress controller, but block direct access to your database pod.
  11. Secure the supply chain with admission control. Use an admission controller like OPA/Gatekeeper or Kyverno to enforce security policies at deployment time. For example, require all images to come from an approved registry, block privileged containers, and enforce resource limits.
  12. Enable runtime security monitoring. Install a runtime security tool like Falco to detect anomalous behavior inside containers (e.g., shell execution, unexpected file access). Configure alerts for suspicious activity and integrate with your SIEM or logging system.
  13. Manage secrets properly. Never store secrets in environment variables or Kubernetes Secrets as plain text. Use a secrets management solution like HashiCorp Vault, AWS Secrets Manager, or Sealed Secrets, and inject them into pods via mounted volumes or sidecar containers.
  14. Regularly audit and update. Schedule periodic security audits of your cluster and application. Keep Kubernetes, container runtime, and all dependencies updated to the latest stable versions, and re-scan images and dependencies on a regular basis.

Common Mistakes

  • Ignoring vulnerabilities in base images and only scanning application dependencies.
  • Running containers as root, which gives attackers full control of the host if they escape.
  • Using the default namespace for all workloads, making it harder to apply network policies and RBAC.
  • Storing secrets in environment variables or Kubernetes Secrets without encryption.
  • Applying security only at the cluster level and forgetting about code-level issues like SQL injection.
  • Forgetting to scan third-party images pulled directly from public registries.

Tips

  • Start with the OWASP Top 10 for web applications to prioritize code-level fixes.
  • Use a multi-stage Docker build to keep the final image small and clean.
  • Enable Kubernetes RBAC and use separate service accounts for each application with minimal permissions.
  • Set resource requests and limits for every container to prevent resource exhaustion attacks.
  • Use a private container registry with access controls to avoid pulling from untrusted sources.
  • Automate security scans in CI so they run on every commit, not just before release.

Troubleshooting

  • Image pull fails due to signature verification: Ensure the signing key is correctly configured in the admission controller. Test the signature locally with cosign verify.
  • Container crashes because of read-only filesystem: Identify which directory the app needs to write to (e.g., /tmp) and mount an emptyDir volume there.
  • Network policy blocks legitimate traffic: Use kubectl describe networkpolicy and check pod labels. Test with a temporary permissive policy to isolate the issue.
  • Secrets not injected correctly: Verify the secret name and key in the deployment YAML. Check the secret exists in the correct namespace with kubectl get secrets.
  • SAST tool produces many false positives: Tune the rules to your language and framework. Focus on fixing critical and high confidence findings first.
  • Falco alerts too noisy: Adjust Falco rules to your application’s normal behavior, or use a baseline profile to suppress expected activities.

Frequently Asked Questions

What is the most important step in securing cloud native apps?

There is no single most important step; security must be layered. However, starting with dependency scanning and running as non-root are two high-impact, easy-to-implement measures that prevent many common attacks.

Do I need a dedicated security team to do this?

No. Many of these steps can be automated with open-source tools and integrated into your existing CI/CD pipeline. A security team helps with policy and incident response, but the practices here are achievable by any development team.

How often should I scan for vulnerabilities?

At minimum, scan on every commit or pull request. Also schedule nightly or weekly scans of your images and dependencies, as new vulnerabilities are disclosed regularly.

Can I use Kubernetes Secrets for all secrets?

Kubernetes Secrets are better than plaintext, but they are only base64-encoded by default. For production, use a dedicated secrets manager and inject secrets via a sidecar or CSI driver to avoid storing them in etcd.

What is the difference between SAST and DAST?

SAST (Static Application Security Testing) scans source code without executing it. DAST (Dynamic Application Security Testing) tests a running application. Both are valuable; use SAST early in development and DAST on staging.

Is it safe to use public container images?

Public images can be risky if not maintained. Always scan them, pin to a specific digest, and prefer images from official or well-known publishers. Consider copying them to your private registry after scanning.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →