{"id":23139,"date":"2024-07-11T23:35:24","date_gmt":"2024-07-11T20:35:24","guid":{"rendered":"https:\/\/kifarunix.com\/?p=23139"},"modified":"2024-07-11T23:35:29","modified_gmt":"2024-07-11T20:35:29","slug":"step-by-step-guide-to-using-configmaps-in-kubernetes","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/step-by-step-guide-to-using-configmaps-in-kubernetes\/","title":{"rendered":"Step-by-Step Guide to Using ConfigMaps in Kubernetes"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1063\" height=\"604\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2024\/07\/kubernetes-configmaps.png?v=1720729516\" alt=\"Step-by-Step Guide to Using ConfigMaps in Kubernetes\" class=\"wp-image-23167\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2024\/07\/kubernetes-configmaps.png?v=1720729516 1063w, https:\/\/kifarunix.com\/wp-content\/uploads\/2024\/07\/kubernetes-configmaps-768x436.png?v=1720729516 768w\" sizes=\"(max-width: 1063px) 100vw, 1063px\" \/><\/figure>\n\n\n\n<p>This tutorial provides a step-by-step guide to using ConfigMaps in Kubernetes. <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/configuration\/configmap\/\" target=\"_blank\" rel=\"noreferrer noopener\">ConfigMaps<\/a> in Kubernetes are a powerful tool for managing configuration data separately from application code. They allow you to decouple configuration settings such as environment variables, command-line arguments, or configuration files from your containerized applications. In this guide, we&#8217;ll explore how to create, manage, and use ConfigMaps effectively in your Kubernetes deployments.<\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#how-to-use-config-maps-in-kubernetes-deployments-pods\">How to Use ConfigMaps in Kubernetes Deployments\/Pods<\/a><ul><li><a href=\"#what-is-a-config-map-in-kubernetes\">What is a ConfigMap in Kubernetes?<\/a><\/li><li><a href=\"#creating-a-config-map-in-kubernetes\">Creating a ConfigMap in Kubernetes<\/a><ul><li><a href=\"#creating-config-maps-from-literal-values\">Creating ConfigMaps from Literal Values<\/a><\/li><li><a href=\"#creating-config-map-from-a-file\">Creating ConfigMap from a File<\/a><\/li><li><a href=\"#creating-a-config-map-from-a-directory\">Creating a ConfigMap from a Directory<\/a><\/li><li><a href=\"#creating-config-map-from-an-environment-variables-file\">Creating ConfigMap from an Environment Variables File<\/a><\/li><li><a href=\"#create-a-config-map-from-a-yaml-manifest-file\">Create a ConfigMap from a YAML manifest file<\/a><\/li><\/ul><\/li><li><a href=\"#viewing-and-describing-config-maps\">Viewing and Describing ConfigMaps<\/a><ul><li><a href=\"#listing-available-config-maps-in-kubernetes-cluster\">Listing Available ConfigMaps in Kubernetes Cluster<\/a><\/li><li><a href=\"#show-more-details-about-config-maps\">Show More Details About ConfigMaps<\/a><\/li><\/ul><\/li><li><a href=\"#using-config-maps-in-kubernetes-pods\">Using ConfigMaps in Kubernetes Pods<\/a><ul><li><a href=\"#mounting-config-maps-as-volumes\">Mounting ConfigMaps as Volumes<\/a><\/li><li><a href=\"#using-config-maps-as-environment-variables\">Using ConfigMaps as Environment Variables<\/a><\/li><\/ul><\/li><li><a href=\"#updating-kubernetes-config-maps\">Updating Kubernetes ConfigMaps<\/a><ul><li><a href=\"#update-config-map-using-kubectl-apply-or-edit\">Update ConfigMap Using kubectl apply or edit<\/a><\/li><li><a href=\"#directly-with-kubectl-replace-or-patch\">Directly with kubectl replace or patch<\/a><\/li><\/ul><\/li><li><a href=\"#deleting-config-maps-in-kubernetes\">Deleting ConfigMaps in Kubernetes<\/a><ul><li><a href=\"#delete-a-specific-config-map-by-name\">Delete a Specific ConfigMap by Name<\/a><\/li><li><a href=\"#delete-all-config-maps-in-a-namespace\">Delete All ConfigMaps in a Namespace<\/a><\/li><li><a href=\"#delete-config-maps-using-manifest-file\">Delete ConfigMaps Using Manifest File<\/a><\/li><\/ul><\/li><li><a href=\"#conclusion\">Conclusion<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-use-config-maps-in-kubernetes-deployments-pods\">How to Use ConfigMaps in Kubernetes Deployments\/Pods<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"what-is-a-config-map-in-kubernetes\">What is a ConfigMap in Kubernetes?<\/h3>\n\n\n\n<p>ConfigMap is Kubernetes API object that is used, unlike <a href=\"https:\/\/kifarunix.com\/how-to-use-secrets-in-kubernetes-applications\/\" target=\"_blank\" rel=\"noreferrer noopener\">secrets<\/a>, to store and manage non-sensitive configuration data in key-value pairs. ConfigMap provides a way to decouple configuration artifacts from the application code thus making containerized applications portable. In short, it provides a way to inject configurations into Pods without modifying the container image or source code directly.<\/p>\n\n\n\n<p>ConfigMap is often used to store configuration parameters such as:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Environment variables<\/strong>: You can inject ConfigMap data into a container as environment variables, which allows you to configure how the application behaves without changing its container image.<\/li>\n\n\n\n<li><strong>Configuration files<\/strong>: ConfigMaps can also be mounted as volumes into containers, allowing applications to read configuration files directly from the volume.<\/li>\n\n\n\n<li><strong>Command Line arguments<\/strong>: ConfigMaps can also store command-line arguments that can be passed to containers in Kubernetes. This is particularly useful when applications need to start with specific runtime options or flags.<\/li>\n<\/ol>\n\n\n\n<p>ConfigMaps are useful when you want to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Centralize configuration<\/strong>: Instead of embedding configuration directly into application code, container images or Kubernetes manifests, ConfigMaps provide a centralized way to manage configuration data.<\/li>\n\n\n\n<li><strong>Override configuration<\/strong>: You can update ConfigMaps independently of the application code, which makes it easier to change configurations without rebuilding or redeploying the containers.<\/li>\n<\/ul>\n\n\n\n<p>Are you getting started with Kubernetes and you need a book that offers a beginner-friendly approach to mastering Kubernetes architecture and core concepts such as Pods, Deployments, Services, StatefulSets, Ingress, ConfigMaps, and more? Look no further, The Kubernetes Book: 2024 Edition by Nigel Poulton is the best bet.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-amazon wp-block-embed-amazon\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"The Kubernetes Book: 2024 Edition\" type=\"text\/html\" width=\"1200\" height=\"550\" frameborder=\"0\" allowfullscreen style=\"max-width:100%\" src=\"https:\/\/read.amazon.com\/kp\/card?preview=inline&#038;linkCode=ll1&#038;ref_=k4w_oembed_liMP8KzGhphum6&#038;asin=B072TS9ZQZ&#038;tag=dc42a8f60962-20\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"creating-a-config-map-in-kubernetes\">Creating a ConfigMap in Kubernetes<\/h3>\n\n\n\n<p>Kubernetes offers multiple ways to create ConfigMaps, <strong>imperatively<\/strong> using <strong>kubectl create configmap<\/strong> command or <strong>declaratively<\/strong> using the <strong>manifest file<\/strong>.<\/p>\n\n\n\n<p>The name of a ConfigMap must be a valid&nbsp;<a href=\"https:\/\/kubernetes.io\/docs\/concepts\/overview\/working-with-objects\/names#dns-subdomain-names\" target=\"_blank\" rel=\"noopener\">DNS subdomain name<\/a>. It must consist of lower case alphanumeric characters, &#8216;-&#8216; or &#8216;.&#8217;, and must start and end with an alphanumeric character (e.g. &#8216;example.com&#8217;).<\/p>\n\n\n\n<p>Regex used for validation is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>'<strong>&#91;a-z0-9](&#91;-a-z0-9]*&#91;a-z0-9])?(\\.&#91;a-z0-9](&#91;-a-z0-9]*&#91;a-z0-9])?)*<\/strong>'<\/code><\/pre>\n\n\n\n<p>Therefore, configmaps with underscore on their names will fail to create, for example.<\/p>\n\n\n\n<p>You can specify the respective namespace for the ConfigMaps (<strong>-n|\u2013namespace &lt;namespace&gt;<\/strong>) when creating them imperatively or specify it in the manifest yaml file.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"creating-config-maps-from-literal-values\">Creating ConfigMaps from Literal Values<\/h4>\n\n\n\n<p>You can create a ConfigMap using literal key-value pairs with <strong>kubectl create configmap<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl create configmap database-host --from-literal=db_host=mariadb --from-literal=db_port=3306<\/code><\/pre>\n\n\n\n<p>The command creates a Kubernetes ConfigMap called <code>database-host<\/code> with two key-value pairs:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>db_host<\/code>: <code>mariadb<\/code><\/li>\n\n\n\n<li><code>db_port<\/code>: <code>3306<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"creating-config-map-from-a-file\">Creating ConfigMap from a File<\/h4>\n\n\n\n<p>You can create a ConfigMap from a file by passing the <strong>&#8211;from-file<\/strong> option to the <strong>kubectl create configmap<\/strong> command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat database-host<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>db_host: mariadb\ndb_port: 3306<\/code><\/pre>\n\n\n\n<p>Hence;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl create configmap database-host --from-file=.\/database-host -n apps<\/code><\/pre>\n\n\n\n<p>This will create the configmap in the <strong>apps<\/strong> namespace.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"creating-a-config-map-from-a-directory\">Creating a ConfigMap from a Directory<\/h4>\n\n\n\n<p>When you have multiple files that you want to store as different keys in a configmap, you can put them under a specific directory and create them right from the directory. When creating a configmap in Kubernetes based on a directory, each file whose basename (filename without the path) corresponds to a valid key in the directory will be packaged into the configmap.<\/p>\n\n\n\n<p>Let\u2019s say you have a directory named&nbsp;<code>database-host<\/code>&nbsp;with the following files:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>db_host<\/strong> containing db host name<\/li>\n\n\n\n<li><strong>db_port<\/strong>&nbsp;containing&nbsp;<code>port number<\/code><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>tree database-host<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>database-host\/\n\u251c\u2500\u2500 db_host\n\u2514\u2500\u2500 db_port\n\n1 directory, 2 files\n<\/code><\/pre>\n\n\n\n<p>To create a Kubernetes ConfigMap named&nbsp;<code>datbase-host<\/code>&nbsp;from this directory, where each file becomes a key-value pair in the ConfigMap:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl create configmap database-host --from-file=database-host -n apps<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"creating-config-map-from-an-environment-variables-file\">Creating ConfigMap from an Environment Variables File<\/h4>\n\n\n\n<p>This allows you to store multiple environment variables in a single file.<\/p>\n\n\n\n<p>Create a text file that contains your environment variables in the format&nbsp;<code>KEY=VALUE<\/code>, each on a new line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat  database-host<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>DB_HOST=mariadb\nDB_PORT=3306<\/code><\/pre>\n\n\n\n<p>Then use&nbsp;<code>kubectl create configmap<\/code>&nbsp;with the&nbsp;<code>--from-env-file<\/code>&nbsp;option followed by the path to your environment file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl create configmap database-host --from-env-file=database-host -n apps<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"create-a-config-map-from-a-yaml-manifest-file\">Create a ConfigMap from a YAML manifest file<\/h4>\n\n\n\n<p>Here is the sample YAML file to create a ConfigMap named <code>database-host<\/code> with the <code>DB_HOST<\/code> and <code>DB_PORT<\/code> values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat database-host.yaml<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>apiVersion: v1\nkind: ConfigMap\nmetadata:\n  name: database-host\n  namespace: apps\ndata:\n  DB_HOST: mariadb\n  DB_PORT: \"3306\"\n<\/code><\/pre>\n\n\n\n<p>Apply the ConfigMap using:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl apply -f database-host.yaml<\/code><\/pre>\n\n\n\n<p>A sample configuration;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat web-page.html<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>apiVersion: v1\nkind: ConfigMap\nmetadata:\n  name: web-page\n  namespace: apps\ndata:\n  index.html: |\n    &lt;!DOCTYPE html&gt;\n    &lt;html&gt;\n    &lt;head&gt;\n      &lt;title&gt;Welcome to my website&lt;\/title&gt;\n    &lt;\/head&gt;\n    &lt;body&gt;\n      &lt;h1&gt;Hello, Kubernetes!&lt;\/h1&gt;\n      &lt;p&gt;This is an updated message.&lt;\/p&gt;\n    &lt;\/body&gt;\n    &lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"viewing-and-describing-config-maps\">Viewing and Describing ConfigMaps<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"listing-available-config-maps-in-kubernetes-cluster\">Listing Available ConfigMaps in Kubernetes Cluster<\/h4>\n\n\n\n<p>List all ConfigMaps in the default namespace of the cluster:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kubectl get configmaps<\/pre>\n\n\n\n<p>List all ConfigMaps in a specific namespace in the cluster:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl get configmaps &#91;-n|--namespace] &lt;namespace&gt;<\/code><\/pre>\n\n\n\n<p>For example;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl get configmaps -n apps<\/code><\/pre>\n\n\n\n<p>List all ConfigMaps in the all namespaces of the cluster:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl get configmaps &#91;-A|--all-namespaces]<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>NAMESPACE          NAME                                                   DATA   AGE\napps               database-host                                          2      13h\napps               html-page                                              1      23d\napps               kube-root-ca.crt                                       1      23d\napps               nginx-config                                           1      3d3h\ncalico-apiserver   kube-root-ca.crt                                       1      23d\ncalico-system      active-operator                                        1      23d\ncalico-system      cni-config                                             1      23d\ncalico-system      kube-root-ca.crt                                       1      23d\ncalico-system      tigera-ca-bundle                                       2      23d\ndefault            kube-root-ca.crt                                       1      23d\nkube-node-lease    kube-root-ca.crt                                       1      23d\nkube-public        cluster-info                                           1      23d\nkube-public        kube-root-ca.crt                                       1      23d\nkube-system        coredns                                                1      23d\nkube-system        extension-apiserver-authentication                     6      23d\nkube-system        kube-apiserver-legacy-service-account-token-tracking   1      23d\nkube-system        kube-proxy                                             2      23d\nkube-system        kube-root-ca.crt                                       1      23d\nkube-system        kubeadm-config                                         1      23d\nkube-system        kubelet-config                                         1      23d\nrm                 kube-root-ca.crt                                       1      21d\ntigera-operator    kube-root-ca.crt                                       1      23d\n<\/code><\/pre>\n\n\n\n<p>To display the ConfigMap in specific output, for example, in yaml format, pass the <strong>-o|&#8211;output=<\/strong> option to kubectl get command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl get configmap database-host -n apps -o yaml<\/code><\/pre>\n\n\n\n<p>Read more on <strong>kubectl get &#8211;help<\/strong> to learn more on different command line options to use.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"show-more-details-about-config-maps\">Show More Details About ConfigMaps<\/h4>\n\n\n\n<p>You can use the <strong>kubectl describe configmap<\/strong> command to show more details of a ConfigMap.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl describe configmap &lt;configmap-name&gt; &#91;-n|--namespace &lt;namespace&gt;]<\/code><\/pre>\n\n\n\n<p>For example, to show more details about a ConfigMap, <strong>kube-root-ca.crt<\/strong>, in the default namespace;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl describe configmap kube-root-ca.crt<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>Name:         kube-root-ca.crt\nNamespace:    default\nLabels:       &lt;none&gt;\nAnnotations:  kubernetes.io\/description:\n                Contains a CA bundle that can be used to verify the kube-apiserver when using internal endpoints such as the internal service IP or kubern...\n\nData\n====\nca.crt:\n----\n-----BEGIN CERTIFICATE-----\nMIIDBTCCAe2gAwIBAgIIFgTDp0Lsj6IwDQYJKoZIhvcNAQELBQAwFTETMBEGA1UE\nAxMKa3ViZXJuZXRlczAeFw0yNDA2MTYxNzA1MzdaFw0zNDA2MTQxNzEwMzdaMBUx\nEzARBgNVBAMTCmt1YmVybmV0ZXMwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK\nAoIBAQCz5W7a15BtNoLfNBAGWZQIXa1sCYR8KRLkNb4HohbvfRtLJbh4+Dq6jycB\neacNgKV03Pv7LsnBjYVboiYsYfo2CIcKDxkPueHcCFhaizqbN4643AxFDh2YoODy\nimqV0IlPNs6WfLpZl8CWCMsPg1Wf5ebAloPozncUmAFxreTFSRnhXXHtVrG6v55F\nPxU7Y2VHIGRhJ6EkamKWKOWwlwU916z51+pW0IobQb1RzDmu0AYD9bLZ8ANjqC7I\nAgBOKSUeN3wMP3\/ZO5eO1F37AFnkegECrdubfCB4C5oDbeB6yJYOPBWd8rpU0KtW\nKnLhPm+p4hqlj0ns+tp\/GdsNghdTAgMBAAGjWTBXMA4GA1UdDwEB\/wQEAwICpDAP\nBgNVHRMBAf8EBTADAQH\/MB0GA1UdDgQWBBSyzdSJXP3ttEm6Ppf3K9FUQIe5tTAV\nBgNVHREEDjAMggprdWJlcm5ldGVzMA0GCSqGSIb3DQEBCwUAA4IBAQCVkHwwTwkg\nvVGLmQJUnHdNUgPGnphxyXKaDFnXCCOZ+Gz+j6c2Apg4Xq2VUq1yQaiiUisg5hVq\nMaoSRO13gmnCnp6aLChH3wyywV2GHQFBIae+mfwhYr+8dV9qqj\/ntCMbXkda0x+D\nDxDK5ecfa5JoVDQa+do6MHy0Jp0+LlhwUnzw9owXqSajsfwDn+GrpQFRHswggS61\nTbtjICDpMJ0xQkG5rMP3nikGXvl1CTDGWM1R6FfGI\/Rls5M+5WvVTOh0YtVPzKMm\nBdBKrQt21kXVV9\/UFugSQt7l+8FggjZR1\/0n3IaGIA1ldXkpPDbhLdu45GFHb0lK\nghpt9pOKoQHp\n-----END CERTIFICATE-----\n\n\nBinaryData\n====\n\nEvents:  &lt;none&gt;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"using-config-maps-in-kubernetes-pods\">Using ConfigMaps in Kubernetes Pods<\/h3>\n\n\n\n<p>You can configure Kubernetes Pods to use ConfigMaps via volumes or environment variables.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"mounting-config-maps-as-volumes\">Mounting ConfigMaps as Volumes<\/h4>\n\n\n\n<p>You can mount ConfigMaps as volumes inside Pods.<\/p>\n\n\n\n<p>To demonstrate this, let create a basic Nginx deployment that mounts the html page as a config map!<\/p>\n\n\n\n<p>Prepare Nginx web page. Here is our sample page.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat index.html<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n    &lt;style&gt;\n        body {\n            font-family: Arial, sans-serif;\n            text-align: center;\n            padding: 50px;\n        }\n        h1 {\n            color: #009688;\n        }\n        p {\n            color: #607d8b;\n        }\n        .container {\n            margin-top: 30px;\n            display: flex;\n            justify-content: center;\n            align-items: center;\n        }\n        .kube-logo {\n            width: 150px;\n            height: 150px;\n            margin-right: 20px;\n        }\n    &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h1&gt;Welcome to our Kubernetes Cluster!&lt;\/h1&gt;\n    &lt;p&gt;Our cluster is running smoothly and efficiently.&lt;\/p&gt;\n    &lt;div class=\"container\"&gt;\n        &lt;img class=\"kube-logo\" src=\"https:\/\/raw.githubusercontent.com\/kubernetes\/kubernetes\/master\/logo\/logo.png\" alt=\"Kubernetes Logo\"&gt;\n        &lt;p&gt;Kubernetes orchestrates our applications with ease, ensuring high availability and scalability.&lt;\/p&gt;\n    &lt;\/div&gt;\n    &lt;p&gt;Feel free to explore and innovate!&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<p>Now, based on the configuration file, let&#8217;s create a web page ConfigMap;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl create configmap web-page --from-file=.\/index.html -n apps<\/code><\/pre>\n\n\n\n<p>This is how configmap will be set as;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl get configmaps web-page -n apps -o yaml<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>apiVersion: v1\ndata:\n  index.html: |\n    &lt;!DOCTYPE html&gt;\n    &lt;html lang=\"en\"&gt;\n    &lt;head&gt;\n        &lt;meta charset=\"UTF-8\"&gt;\n        &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n        &lt;style&gt;\n            body {\n                font-family: Arial, sans-serif;\n                text-align: center;\n                padding: 50px;\n            }\n            h1 {\n                color: #009688;\n            }\n            p {\n                color: #607d8b;\n            }\n            .container {\n                margin-top: 30px;\n                display: flex;\n                justify-content: center;\n                align-items: center;\n            }\n            .kube-logo {\n                width: 150px;\n                height: 150px;\n                margin-right: 20px;\n            }\n        &lt;\/style&gt;\n    &lt;\/head&gt;\n    &lt;body&gt;\n        &lt;h1&gt;Welcome to our Kubernetes Cluster!&lt;\/h1&gt;\n        &lt;p&gt;Our cluster is running smoothly and efficiently.&lt;\/p&gt;\n        &lt;div class=\"container\"&gt;\n            &lt;img class=\"kube-logo\" src=\"https:\/\/raw.githubusercontent.com\/kubernetes\/kubernetes\/master\/logo\/logo.png\" alt=\"Kubernetes Logo\"&gt;\n            &lt;p&gt;Kubernetes orchestrates our applications with ease, ensuring high availability and scalability.&lt;\/p&gt;\n        &lt;\/div&gt;\n        &lt;p&gt;Feel free to explore and innovate!&lt;\/p&gt;\n    &lt;\/body&gt;\n    &lt;\/html&gt;\nkind: ConfigMap\nmetadata:\n  creationTimestamp: \"2024-07-11T17:48:07Z\"\n  name: web-page\n  namespace: apps\n  resourceVersion: \"5523425\"\n  uid: c404b62a-bae2-4122-ac3e-7ab3c87d3510\n<\/code><\/pre>\n\n\n\n<p>Next, create an Nginx deployment manifest file and define how to mount the web page configmap as a volume.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat nginx.yaml<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: nginx-app\n  namespace: apps\nspec:\n  replicas: 1\n  selector:\n    matchLabels:\n      app: nginx\n  template:\n    metadata:\n      labels:\n        app: nginx\n    spec:\n      containers:\n      - name: nginx\n        image: nginx:latest\n        ports:\n        - containerPort: 80\n        volumeMounts:\n        - name: nginx-web-page\n          mountPath: \/usr\/share\/nginx\/html\n          readOnly: true\n      volumes:\n      - name: nginx-web-page\n        configMap:\n          name: web-page\n<\/code><\/pre>\n\n\n\n<p>In summary, this configuration allow Nginx app Pods to mount a ConfigMap named <strong>web-page<\/strong> as a volume at <strong>\/usr\/share\/nginx\/html<\/strong>, allowing the nginx container to serve content from the <strong>index.html<\/strong> file stored in the ConfigMap.<\/p>\n\n\n\n<p>Apply the configuration;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl apply -f nginx.yaml<\/code><\/pre>\n\n\n\n<p>Check the Pods;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl get pods  -n apps -l app=nginx<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>NAME                         READY   STATUS    RESTARTS   AGE\nnginx-app-7598db6dbf-svxhm   1\/1     Running   0          22s\n<\/code><\/pre>\n\n\n\n<p>Let&#8217;s try to access the page;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl exec -it nginx-app-7598db6dbf-svxhm -n apps -- curl localhost<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n    &lt;style&gt;\n        body {\n            font-family: Arial, sans-serif;\n            text-align: center;\n            padding: 50px;\n        }\n        h1 {\n            color: #009688;\n        }\n        p {\n            color: #607d8b;\n        }\n        .container {\n            margin-top: 30px;\n            display: flex;\n            justify-content: center;\n            align-items: center;\n        }\n        .kube-logo {\n            width: 150px;\n            height: 150px;\n            margin-right: 20px;\n        }\n    &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h1&gt;Welcome to our Kubernetes Cluster!&lt;\/h1&gt;\n    &lt;p&gt;Our cluster is running smoothly and efficiently.&lt;\/p&gt;\n    &lt;div class=\"container\"&gt;\n        &lt;img class=\"kube-logo\" src=\"https:\/\/raw.githubusercontent.com\/kubernetes\/kubernetes\/master\/logo\/logo.png\" alt=\"Kubernetes Logo\"&gt;\n        &lt;p&gt;Kubernetes orchestrates our applications with ease, ensuring high availability and scalability.&lt;\/p&gt;\n    &lt;\/div&gt;\n    &lt;p&gt;Feel free to explore and innovate!&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<p>You can expose the Deployment to test it outside Kubernetes cluster.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl expose deployment nginx-app --port=80 --target-port=80 --type=NodePort --namespace=apps<\/code><\/pre>\n\n\n\n<p>Check the services to retrieve the port to access your pods externally;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl get service -n apps<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>NAME        TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE\nnginx-app   NodePort   10.109.64.134   <none>        80:32239\/TCP   1m\n<\/code><\/pre>\n\n\n\n<p>You can access the app via port 32239 via the worker nodes IP;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1168\" height=\"516\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2024\/07\/nginx-app-mount-configmap-volume.png?v=1720721931\" alt=\"Using ConfigMaps in Kubernetes Pods\" class=\"wp-image-23153\" style=\"width:820px;height:auto\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2024\/07\/nginx-app-mount-configmap-volume.png?v=1720721931 1168w, https:\/\/kifarunix.com\/wp-content\/uploads\/2024\/07\/nginx-app-mount-configmap-volume-768x339.png?v=1720721931 768w\" sizes=\"(max-width: 1168px) 100vw, 1168px\" \/><\/figure>\n\n\n\n<p>And that is it!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"using-config-maps-as-environment-variables\">Using ConfigMaps as Environment Variables<\/h4>\n\n\n\n<p>You can use ConfigMaps as environment variables value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat mariadb.yaml<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>apiVersion: apps\/v1\nkind: Deployment\nmetadata:\n  name: mariadb-deployment\n  namespace: apps\nspec:\n  replicas: 2\n  selector:\n    matchLabels:\n      app: mariadb\n  template:\n    metadata:\n      labels:\n        app: mariadb\n    spec:\n      containers:\n      - name: mariadb\n        image: mariadb:latest\n        ports:\n        - containerPort: 3306\n        env:\n        - name: MARIADB_ROOT_PASSWORD\n          valueFrom:\n            configMapKeyRef:\n              name: db-root-pwd\n              key: db-pwd\n<\/code><\/pre>\n\n\n\n<p>Apply and confirm the credentials.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl apply -f mariadb.yaml<\/code><\/pre>\n\n\n\n<p>After that, check the pods;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl get pods -l app=mariadb -n apps<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>NAME                                  READY   STATUS    RESTARTS   AGE\nmariadb-deployment-776dcbcbcc-68vsk   1\/1     Running   0          1m\nmariadb-deployment-776dcbcbcc-wg7mv   1\/1     Running   0          1m\n<\/code><\/pre>\n\n\n\n<p>Check the availability of the password within the Pods;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl exec -it mariadb-deployment-776dcbcbcc-68vsk -n apps -- env | grep MARIADB_ROOT_PASSWORD<\/code><\/pre>\n\n\n\n<p>Sample output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>MARIADB_ROOT_PASSWORD<\/strong>=p@ssw0rd<\/code><\/pre>\n\n\n\n<p>Confirm that the login to MariaDB as root user and defined password works.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl exec -it mariadb-deployment-776dcbcbcc-68vsk -n apps -- mariadb -u root -p<\/code><\/pre>\n\n\n\n<p>If the password is correct, then you should drop into MariaDB!<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>Enter password: \nWelcome to the MariaDB monitor.  Commands end with ; or \\g.\nYour MariaDB connection id is 8\nServer version: 11.4.2-MariaDB-ubu2404 mariadb.org binary distribution\n\nCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.\n\nType 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.\n\nMariaDB [(none)]&gt;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"updating-kubernetes-config-maps\">Updating Kubernetes ConfigMaps<\/h3>\n\n\n\n<p>Updating a ConfigMap in Kubernetes can be approached in a few different ways:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"update-config-map-using-kubectl-apply-or-edit\">Update ConfigMap Using kubectl apply or edit<\/h4>\n\n\n\n<p>Using <code>kubectl apply<\/code> or <code>kubectl edit<\/code> commands allows you to update a ConfigMap directly from the command line or by editing a YAML file.<\/p>\n\n\n\n<p><strong>kubectl apply<\/strong> command updates a ConfigMap by applying a new configuration defined in a YAML file. It merges changes into the existing ConfigMap.<\/p>\n\n\n\n<p>For example, if you have updated your ConfigMap manifest file, then apply the changes;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl apply -f updated-configmap.yaml<\/code><\/pre>\n\n\n\n<p><strong>kubectl edit<\/strong> command opens the current ConfigMap in your default text editor, allowing you to modify it interactively. After saving your changes and exiting the editor, Kubernetes applies the updated configuration.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl edit configmap &lt;configmap_name&gt; -n &lt;namespace&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"directly-with-kubectl-replace-or-patch\">Directly with kubectl replace or patch<\/h4>\n\n\n\n<p>You can also update a ConfigMap directly with <code>kubectl replace<\/code> or <code>kubectl patch<\/code> commands.<\/p>\n\n\n\n<p><strong>kubectl replace<\/strong> command replaces the current ConfigMap with the one defined in a YAML file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl replace -f updated-configmap.yaml<\/code><\/pre>\n\n\n\n<p>Use caution as it overrides the existing ConfigMap completely.<\/p>\n\n\n\n<p><strong>kubectl patch<\/strong> command applies changes to a ConfigMap without replacing the entire object. It&#8217;s useful for making small modifications or additions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl patch configmap &lt;configmap_name&gt; -n &lt;namespace&gt; --patch \"$(cat patch.yaml)\"<\/code><\/pre>\n\n\n\n<p>Sample patch.yaml file;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat patch.yaml<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>data:\n  index.html: |\n    &lt;!DOCTYPE html&gt;\n    &lt;html lang=\"en\"&gt;\n    &lt;head&gt;\n        &lt;meta charset=\"UTF-8\"&gt;\n        &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n        &lt;style&gt;\n            body {\n                font-family: Arial, sans-serif;\n                text-align: center;\n                padding: 50px;\n            }\n            h1 {\n                color: #009688;\n            }\n            p {\n                color: #607d8b;\n            }\n            .container {\n                margin-top: 30px;\n                display: flex;\n                justify-content: center;\n                align-items: center;\n            }\n            .kube-logo {\n                width: 150px;\n                height: 150px;\n                margin-right: 20px;\n            }\n        &lt;\/style&gt;\n    &lt;\/head&gt;\n    &lt;body&gt;\n        &lt;h1&gt;Welcome to our Kubernetes Cluster!&lt;\/h1&gt;\n        &lt;p&gt;Our cluster is running smoothly and efficiently.&lt;\/p&gt;\n        &lt;div class=\"container\"&gt;\n            &lt;img class=\"kube-logo\" src=\"https:\/\/raw.githubusercontent.com\/kubernetes\/kubernetes\/master\/logo\/logo.png\" alt=\"Kubernetes Logo\"&gt;\n            &lt;p&gt;Kubernetes orchestrates our applications with ease, ensuring high availability and scalability.&lt;\/p&gt;\n        &lt;\/div&gt;\n        &lt;p&gt;Feel free to explore and innovate!&lt;\/p&gt;\n    &lt;\/body&gt;\n    &lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"deleting-config-maps-in-kubernetes\">Deleting ConfigMaps in Kubernetes<\/h3>\n\n\n\n<p>Before deleting a ConfigMap, understand the impact on applications using it. ConfigMaps provide configuration data to Pods, so deleting a ConfigMap without considering its usage can cause application failures or downtime.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"delete-a-specific-config-map-by-name\">Delete a Specific ConfigMap by Name<\/h4>\n\n\n\n<p>To delete a specific ConfigMap by its name:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl delete configmap &lt;configmap_name&gt; -n &lt;namespace&gt;<\/code><\/pre>\n\n\n\n<p>Replace <code>&lt;configmap_name&gt;<\/code> with the name of the ConfigMap you want to delete and <code>&lt;namespace&gt;<\/code> with the namespace where the ConfigMap resides.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"delete-all-config-maps-in-a-namespace\">Delete All ConfigMaps in a Namespace<\/h4>\n\n\n\n<p>To delete all ConfigMaps within a namespace, you can use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl delete configmap --all -n &lt;namespace&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"delete-config-maps-using-manifest-file\">Delete ConfigMaps Using Manifest File<\/h4>\n\n\n\n<p>If the ConfigMap was created using a manifest file (<code>yaml<\/code>&nbsp;or&nbsp;<code>json<\/code>), you can delete it by applying the same manifest file with the&nbsp;<code>delete<\/code>&nbsp;action:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl delete -f config-manifest.yaml<\/code><\/pre>\n\n\n\n<p>Ensure that\u00a0<code>config-manifest.yaml<\/code>\u00a0contains the correct definition of the ConfigMap you want to delete.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h3>\n\n\n\n<p>ConfigMaps in Kubernetes are essential for managing configuration data separately from application code,, hence providing flexibility in managing containerized environments.<\/p>\n\n\n\n<p>In this blog post, we&#8217;ve explored the foundational concepts of ConfigMaps, from creation methods such as literal values, files, to YAML manifests. We&#8217;ve also delved into practical usage scenarios, including mounting ConfigMaps as volumes and using them as environment variables within Pods, updating and deleting ConfigMaps.<\/p>\n\n\n\n<p>For further information, check the documentation, <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/configuration\/configmap\/\" target=\"_blank\" rel=\"noreferrer noopener\">Kubernetes ConfigMaps page<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial provides a step-by-step guide to using ConfigMaps in Kubernetes. ConfigMaps in Kubernetes are a powerful tool for managing configuration data separately from application<\/p>\n","protected":false},"author":10,"featured_media":23167,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[1668,1076,121],"tags":[7565,7567,7568,7566],"class_list":["post-23139","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kubernetes","category-containers","category-howtos","tag-configmaps-in-kubernetes","tag-create-configmaps-kubernetes","tag-update-configmaps-in-kubernetes","tag-use-configmaps-in-kubernetes","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/23139"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=23139"}],"version-history":[{"count":24,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/23139\/revisions"}],"predecessor-version":[{"id":23169,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/23139\/revisions\/23169"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/23167"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=23139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=23139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=23139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}