Kubernetes ConfigMaps and Secrets: What Are They and When to Use Them?
Tags: cloud, kubernetes, devops
Published: 2023-03-06 09:01:13

<!-- wp:paragraph -->
<p>Data breaches are almost always caused by human error, so security always starts with efficient container configuration. Kubernetes ConfigMaps and Secrets are two key objects that can help you store config data securely.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Read on to learn how ConfigMaps and Secrets work, discover if they interact with CAST AI, and what that means for your overall configuration security.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="h-what-are-kubernetes-configmaps">What are Kubernetes ConfigMaps?&nbsp;</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>A ConfigMap is an API object that stores configuration data for other objects</strong>. It lets you decouple environment-specific configuration from container images, so moving your applications gets easier.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The ConfigMap API resource stores config data as key-value pairs. That information can be consumed in pods or provide the configurations for other system components such as controllers.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Kubernetes ConfigMaps are mainly used to configure settings for containers running in a pod in the same namespace. You can write a pod spec that refers to a ConfigMap and configures the containers in that pod based on its ConfigMap data.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The ConfigMap object must exist before you reference it in a pod specification, or the pod won't start.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="h-why-use-kubernetes-configmaps">Why use Kubernetes ConfigMaps?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Many applications rely on configuration data used during either initialization or runtime. This process often requires adjusting values assigned to configuration parameters – and Kubernetes uses ConfigMaps to inject application pods with configuration data.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Keeping configuration data separate from your app code is one of the essential parts of the Twelve-factor Application methodology</strong><sup>1</sup>. It lets you easily change configuration depending on the environment (development, production, testing) and dynamically introduce changes at runtime.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A ConfigMap also doesn't provide secrecy or encryption. If the data you want to store in a ConfigMap is confidential, Kubernetes Secrets will be much better suited for the job.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="h-what-are-kubernetes-secrets"><span style="font-size: 48.832px;">What are Kubernetes Secrets?  </span></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Similar to ConfigMaps, Kubernetes Secrets are API objects specifically built to store confidential data.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Kubernetes Secrets contain small amounts of sensitive data, such as passwords, tokens, or keys</strong>. This type of information might be put in pod specs or a container image. However, thanks to a Secret, you can keep your confidential data separate from your app code.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can create a Secret independently of the pod that uses it. This ability reduces the risk of the data being exposed in the process of creating and editing your pods.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There are several types of Secrets to choose from, depending on the kind of data you wish to keep private. The most commonly used type is <kbd>Opaque</kbd> for handling arbitrary user-defined data. For basic authentication, the common choice is <kbd>basic-auth</kbd><code>.</code></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="configmaps-and-secrets-vs-cast-ai">ConfigMaps and Secrets vs CAST AI</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Connecting your cluster is a necessary step if you want to get CAST AI’s cost monitoring and <a href="https://cast.ai/cloud-cost-optimization/">optimization</a> recommendations. The same action unlocks security vulnerability scanning and best practice checks.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This requires installing a read-only agent, which sometimes raises questions about how it may affect your configuration security – so here’s what you need to know.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>First of all, you can review the agent’s code <a href="https://github.com/castai/k8s-agent">in GitHub</a> before running it.</p>
<!-- /wp:paragraph -->

<!-- wp:quote {"className":"is-style-plain"} -->
<blockquote class="wp-block-quote is-style-plain"><!-- wp:paragraph {"align":"center"} -->
<p class="has-text-align-center">The script runs a <em>kubectl</em> command which affects the cluster you connect to CAST AI, but it never touches any of your configuration data, including ConfigMaps and Secrets.&nbsp;</p>
<!-- /wp:paragraph --></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p><strong>When connecting the cluster for the first time, you can expect the agent to create the following elements:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>namespace/castai-agent</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>serviceaccount/castai-agent</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>clusterrole.rbac.authorization.k8s.io/castai-agent</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>clusterrolebinding.rbac.authorization.k8s.io/castai-agent</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>role.rbac.authorization.k8s.io/castai-agent</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>rolebinding.rbac.authorization.k8s.io/castai-agent</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>secret/castai-agent</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>configmap/castai-agent-autoscaler</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>deployment.apps/castai-agent</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Once installed, the agent pulls metadata about your workloads, their names, pods, and nodes to determine your cluster's efficiency. The agent's ClusterRole has only permission to get, list, and watch limited resources.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Moreover, the <a href="https://cast.ai/blog/how-does-the-read-only-cast-ai-agent-work-and-what-data-can-it-read/">read-only agent</a> can't change your configuration and poses no risk of downtime. Its CPU, memory, and network use is minimal and typically lower than the rounding error for compute.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>When you decide to remove it from your cluster, you can do it with a single command in the terminal:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>kubectl delete namespace castai-agent</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="enhance-kubernetes-configuration-security">Enhance Kubernetes configuration security&nbsp;</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>As mentioned, connecting your cluster to CAST AI lets you achieve two distinct sets of goals.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>What may have originally brought you to the platform are powerful cost and performance optimization ideas for your cluster. CAST AI users usually save around 65% off their regular Kubernetes bill.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>But apart from that, the platform also lets you automatically check and improve your cluster configuration security.&nbsp;</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The security report from CAST AI scans your cluster for potential vulnerabilities, compares its state against industry best practices, and suggests ways to secure it optimally. You can find its <a href="https://docs.cast.ai/docs/overview">documentation</a> here.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":18633,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://cast.ai/wp-content/uploads/2023/03/security-report-kubernetes-configmaps-1200x590.png" alt="" class="wp-image-18633"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph {"align":"left"} -->
<p class="has-text-align-left">You can use CAST AI's demo cluster to explore the security report without connecting your cluster. <a href="https://console.cast.ai/">Sign up</a> and it will be available on the main screen.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading" id="h-over-to-you">Over to you</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><strong>Kubernetes ConfigMaps and Secrets reduce the risk of exposing cluster data by decoupling application code and configurations.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>While ConfigMaps are sufficient for storing non-sensitive config details in key-value pairs, for sensitive data Secrets work better, as they allow for encryption and access restriction. Enabling RBAC lets you further limit who can retrieve your Secrets and ConfigMaps and reduce the odds of unauthorized configuration changes.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>By adding CAST AI’s <a href="https://cast.ai/cloud-security/">security report</a> to the strategy, you can quickly spot any potential vulnerabilities in your configurations and check your posture against the industry's standards.</p>
<!-- /wp:paragraph -->

<!-- wp:group {"style":{"spacing":{"padding":{"top":"2rem","right":"2rem","bottom":"2rem","left":"2rem"},"margin":{"top":"2rem","bottom":"2rem"}}},"backgroundColor":"cast-black","className":"pattern-cta-text-btn","layout":{"type":"flex","flexWrap":"wrap","justifyContent":"space-between","verticalAlignment":"center"}} -->
<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 {"style":{"spacing":{"padding":{"top":"0rem","right":"0rem","bottom":"0rem","left":"0rem"},"blockGap":"0.5rem"}},"layout":{"type":"constrained","contentSize":"520px"}} -->
<div class="wp-block-group" style="padding-top:0rem;padding-right:0rem;padding-bottom:0rem;padding-left:0rem"><!-- wp:heading {"level":5} -->
<h5 class="wp-block-heading" id="h-scan-your-cluster-now-and-find-out-how-to-best-secure-your-configuration">Scan your cluster now and find out how to <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-cast-green-color">best secure your configuration</mark></h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Get your cluster security report 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 {"className":"references","layout":{"inherit":true,"type":"constrained"}} -->
<div class="wp-block-group references"><!-- wp:heading {"level":4} -->
<h4 class="wp-block-heading" id="references">References</h4>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li><sup>[1]</sup> - &nbsp;<a href="https://12factor.net/">The Twelve-factor App</a></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></div>
<!-- /wp:group -->