Kubernetes Security: 10 Best Practices from the Industry and Community
Tags: cloud, kubernetes, security, devops
Published: 2022-09-30 06:45:29

<!-- wp:paragraph -->
<p>Kubernetes requires extensive configuration, and keeping container security at the right level is always challenging. One of the best ways to tighten your clusters' security is by implementing tactics that have become industry-standard.<sup>4</sup> Here are 10 most important ones.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="h-10-best-practices-for-kubernetes-security">10 best practices for Kubernetes security</h2>
<!-- /wp:heading -->

<!-- wp:heading -->
<h2 id="h-1-don-t-keep-secrets-in-an-environment-variable">1. Don’t keep secrets in an environment variable</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>It's a good practice to have your objects use a secret in an environment variable since other parts of your system can access environment variables. That's why it's best to use secrets as files or take advantage of a secretKeyRef to minimize potential attacks.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="h-2-use-strong-kubernetes-authentication-and-authorization">2. Use Strong Kubernetes Authentication and Authorization</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Integrating Kubernetes with a third-party authentication provider is smart, as it delivers extra security features like multi-factor authentication.&nbsp;Avoid managing users at the API server level to make access to the control plane more secure. Use Identity and Access Management (IAM) solution from the cloud service provider if you’re running a managed Kubernetes service like EKS, GKE, AKS, or similar. Go for OpenID Connect (OIDC) alongside an SSO provider you got used to if CSP IAM isn’t an option.<sup>3</sup>&nbsp;&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="h-3-disable-the-net-raw-capability">3. Disable the NET_RAW capability</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If your K8s containers don't drop the NET_RAW capability, you risk enabling a wide range of networking exploits from within the cluster. It's best to use Policy Enforcement solutions like Open Policy Agents, Kyverno, or Kubernetes Pod Security admission controller to ensure you are following best practices. Use drop for ALL or NET_RAW capabilities in the pod's securityContext definition to ensure that the NET_RAW capability is disabled.<sup>1,2</sup></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="h-4-avoid-sharing-the-host-s-ipc-or-network-namespace">4. Avoid sharing&nbsp; the host's IPC or network namespace&nbsp;</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Check if your pods are sharing the host's IPC or network namespace. Sharing namespaces for pod and host interprocess communication may lead to opening access to shared information. Pods should not be allowed access to the host namespaces.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>And sharing pod and host network namespaces enables network access to the host network from the pod, which breaks network isolation. That's why you better set the hostNetwork parameter to false in PodSecurityPolicy.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="h-5-use-kubernetes-role-based-access-control-rbac">5. Use Kubernetes Role-Based Access Control (RBAC)</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>RBAC helps to define who has access to the Kubernetes API and on what permissions. In Kubernetes 1.6 and higher, RBAC is usually enabled by default. Since Kubernetes combines authorization controllers, make sure to disable the legacy Attribute Based Access Control (ABAC) when enabling RBAC.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Pick namespace-specific permissions over cluster-wide permissions. Even when debugging, don't grant cluster administrator privileges. Otherwise, your container security may be compromised.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="h-6-avoid-usage-of-root-file-system-for-container-security">6. Avoid usage of root file system for container security</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Are your containers running without a read-only root file system? Using a read-only file system prevents malicious binaries from writing to a system or a system takeover by attackers. You can ensure that containers use only the read-only file system by setting readOnlyRootFilesystem to true in Pod securityContext definition.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="h-7-isolate-your-kubernetes-nodes">7. Isolate your Kubernetes nodes</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Kubernetes nodes should never be exposed directly to public networks. Ideally, these nodes are located on a separate network and - if possible - without any direct connections to the general corporate network.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can achieve this by keeping Kubernetes control and data traffic isolated. Otherwise, they both flow through the same pipe. Open access to the data plane means that access is open to the control plane as well. Configure nodes with an ingress controller and set it to only allow connections from the master node via the specified port through the network access control list (ACL).</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="h-8-avoid-running-containers-in-privileged-mode">8. Avoid running containers in privileged mode</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Does your deployment have containers running in privileged (root) mode? This allows the container to gain access to critical resources of the host. Avoid running containers in privileged mode or enable the podSecurityPolicy with the privileged parameter set to false. This will ensure that containers can't run processes that require root privileges on the host.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="h-9-check-for-unsafe-proc-mount">9. Check for unsafe /proc mount&nbsp;</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Deployments with unsafe /proc mount (procMount=Unmasked) will allow bypassing the default masking behavior of the container runtime. Setting a container with Unmasked /proc mount type can potentially expose the host information to the container and, as a result, cause a potential information leakage or container escape. Set procMount=Default to ensure the container doesn't unsafely expose any parts of /proc.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="h-10-have-a-rolling-update-strategy">10. Have a rolling update strategy</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Another smart move is developing a rolling update strategy. Rolling updates allow deployment updates to minimize your application downtime by incrementally updating pod instances with new ones. See <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#rolling-update-deployment">this page in the Kubernetes docs</a> for more details.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 id="h-achieving-kubernetes-security-is-challenging">Achieving Kubernetes security is challenging</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The Kubernetes ecosystem is constantly changing - and so are its security and configuration parts. Keeping up with these changes takes time and effort, stealing away engineers from other tasks screaming for their attention.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Another challenge teams experience around Kubernetes security is the prioritization of security issues - depending on the application's size, prioritizing issues may become time-consuming.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>But automation is here to change that.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>CAST AI's Cloud Security feature constantly checks clusters against industry best practices, Kubernetes recommendations, and container security system benchmarks - and prioritizes them automatically to get you started right away.</p>
<!-- /wp:paragraph -->

<!-- wp:group {"layout":{"type":"flex","flexWrap":"wrap","justifyContent":"space-between","verticalAlignment":"center"},"style":{"spacing":{"padding":{"top":"2rem","right":"2rem","bottom":"2rem","left":"2rem"},"margin":{"top":"2rem","bottom":"2rem"}}},"backgroundColor":"cast-black","className":"pattern-cta-text-btn"} -->
<div class="wp-block-group pattern-cta-text-btn has-cast-black-background-color has-background" style="margin-top:2rem;margin-bottom:2rem;padding-top:2rem;padding-right:2rem;padding-bottom:2rem;padding-left:2rem"><!-- wp:group {"layout":{"type":"constrained","contentSize":"520px"},"style":{"spacing":{"padding":{"top":"0rem","right":"0rem","bottom":"0rem","left":"0rem"},"blockGap":"0.5rem"}}} -->
<div class="wp-block-group" style="padding-top:0rem;padding-right:0rem;padding-bottom:0rem;padding-left:0rem"><!-- wp:heading {"level":5} -->
<h5 id="h-check-your-kubernetes-cluster-against-configuration-and-security-best-practices">Check your Kubernetes cluster against <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-cast-green-color">configuration and security</mark> best practices</h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Connect your cluster and see your costs in 5 min, no credit card required.</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->

<!-- wp:buttons {"layout":{"type":"flex","justifyContent":"left","orientation":"horizontal","flexWrap":"nowrap"}} -->
<div class="wp-block-buttons"><!-- wp:button {"width":100} -->
<div class="wp-block-button has-custom-width wp-block-button__width-100"><a class="wp-block-button__link wp-element-button" href="https://console.cast.ai/?screen_hint=SIGNUP" target="_blank" rel="noreferrer noopener">Get started</a></div>

<!-- /wp:group -->

<!-- wp:group {"layout":{"inherit":true,"type":"constrained"},"className":"references"} -->
<div class="wp-block-group references"><!-- wp:heading {"level":4} -->
<h4 id="references">References</h4>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li><sup>[1]</sup> - <a href="https://kubernetes.io/docs/concepts/security/pod-security-admission/" target="_blank" rel="noreferrer noopener">https://kubernetes.io/docs/concepts/security/pod-security-admission/</a></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><sup>[2]</sup> - <a href="https://kubernetes.io/docs/concepts/security/pod-security-standards/" target="_blank" rel="noreferrer noopener">https://kubernetes.io/docs/concepts/security/pod-security-standards/</a></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><sup>[3]</sup> - <a href="https://kubernetes.io/docs/reference/access-authn-authz/authentication/#authentication-strategies" target="_blank" rel="noreferrer noopener">https://kubernetes.io/docs/reference/access-authn-authz/authentication/#authentication-strategies</a> </li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><sup>[4]</sup> - <a href="https://www.nsa.gov/Press-Room/Cybersecurity-Advisories-Guidance/smdsearch11747/kubernetes/">https://www.nsa.gov/Press-Room/Cybersecurity-Advisories-Guidance/smdsearch11747/kubernetes/</a></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></div>
<!-- /wp:group -->