{"id":452,"date":"2018-10-17T03:00:05","date_gmt":"2018-10-17T03:00:05","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw93\/?p=452"},"modified":"2018-10-17T07:51:50","modified_gmt":"2018-10-17T07:51:50","slug":"helm-tips-and-tricks-updating-an-app-that-uses-configmap","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw93\/index.php\/2018\/10\/17\/helm-tips-and-tricks-updating-an-app-that-uses-configmap\/","title":{"rendered":"Helm Tips and Tricks: Updating an App that uses ConfigMap"},"content":{"rendered":"<h5>Take a deep dive into Best Practices in Kubernetes Networking<\/h5>\n<p>From overlay networking and SSL to ingress controllers and network security policies, we&#8217;ve seen many users get hung up on Kubernetes networking challenges. In this video recording, we dive into Kubernetes networking, and discuss best practices for a wide variety of deployment options.<\/p>\n<p><a href=\"https:\/\/rancher.com\/events\/2018\/kubernetes-networking-masterclass-june-online-meetup\/\" target=\"blank\">Watch the video<\/a><\/p>\n<p>This February I got a chance to attend the Helm Summit in Portland. It was filled with amazing talks covering various topics about Helm. I attended almost all of them, and also got the opportunity to present a Lightning Talk myself. The talk was about a Helm trick\/tip that I came across and found very useful, so I thought I\u2019d share with you all as an article.<\/p>\n<p>Helm lets us manage Kubernetes applications effortlessly. Helm Charts make installation and upgradation of Kubernetes applications easier. In this article, I want to focus on one out of the many benefits of using Helm: How Helm makes updating an app that uses ConfigMap very easy.<\/p>\n<h2>Updating a deployment<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/rancher.com\/img\/blog\/2018\/updating-a-deployment.png\" alt=\"Updating a Deployment\" \/><\/p>\n<p>This is a sample manifest of a deployment. Let\u2019s assume this is being used to run an app. You specify the pod template under spec.template section. Now if you want to update the app, it is a must that something from this spec.template section changes. Meaning, for a change in container image, the deployment will get updated, but not for a change in spec.replicas.<\/p>\n<h2>Updating a deployment that uses a ConfigMap<\/h2>\n<p>Some apps require certain configuration files and values. It\u2019s not recommended to have these configuration values\/files baked into the container image. This is so, because otherwise every time your configuration file changes, you\u2019d have to recreate the container image. Kubernetes provides a great way of managing configuration files\/values, with the ConfigMap resource.<\/p>\n<p>There are 2 ways to expose ConfigMap data to a pod,<\/p>\n<ol>\n<li>Env vars<\/li>\n<li>Volume mounts<\/li>\n<\/ol>\n<p>We\u2019re going to focus on the volume mounts way of exposing ConfigMap.<\/p>\n<p>I\u2019ve created a very simple chart to use as an example to go over this. Within that chart I have the following manifest for a ConfigMap:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/rancher.com\/img\/blog\/2018\/volume-mount-1.png\" alt=\"Manifest for a ConfigMap\" \/><\/p>\n<p>As you can see, the name of the ConfigMap is nginx-cm, and a file called default.conf is reading its data. This default.conf is an nginx configuration file.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/rancher.com\/img\/blog\/2018\/volume-mount-2.png\" alt=\"Nginx Configuration File\" \/><\/p>\n<p>Now I want to use this ConfigMap nginx-cm for my app. So I\u2019m going to expose it via Volume Mounts in the deployment manifest for my app.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/rancher.com\/img\/blog\/2018\/volume-mount-3.png\" alt=\"Volume Mounts\" \/><\/p>\n<p>As shown in the manifest above, we need to add the ConfigMap under Volumes section, and give it a unique name (config-volume as shown in the example manifest). Then, we add this volume under volume mounts in the containers section. The volumeMounts.mountPath field is the exact location in the container where the configuration file will be made available to the container.<\/p>\n<p>So using these manifests, we can have an app running that uses the content of nginx configuration file made available by the ConfigMap.<\/p>\n<p>Now let\u2019s say it\u2019s time to change the nginx configuration file. Changes to this config file should be followed by an update to the ConfigMap too, otherwise our app that uses the ConfigMap won\u2019t use the new content.<\/p>\n<p>We can for sure use the kubectl update command for updating the ConfigMap. This should be followed by an update to the deployment as well. Will kubectl update work for the deployment too?<\/p>\n<p>I got this message when I tried.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/rancher.com\/img\/blog\/2018\/volume-mount-4.png\" alt=\"Deployment Unchanged\" \/><\/p>\n<p>This is because the deployment\u2019s spec.template portion has not changed even after updating the ConfigMap resource. Even though the ConfigMap\u2019s data section changed, that didn\u2019t cause any changes in the deployment\u2019s spec.template<br \/>\nOne workaround for this is to delete all the pods being managed by the deployment, and then the deployment will create new pods that use the updated configMap. But I didn\u2019t quite like this approach, as you have to delete all the pods manually. So I started looking for better solutions. That\u2019s when I came across this Helm trick.<br \/>\n<a href=\"https:\/\/github.com\/kubernetes\/helm\/blob\/master\/docs\/charts_tips_and_tricks.md#automatically-roll-deployments-when-configmaps-or-secrets-change\">https:\/\/github.com\/kubernetes\/helm\/blob\/master\/docs\/charts_tips_and_tricks.md#automatically-roll-deployments-when-configmaps-or-secrets-change<\/a><\/p>\n<p>As you can see under annotations, you can provide path to your configmap file, and pass it to sha256 sum function. This updates the annotation section everytime the configmap file changes, thus in turn updating spec.template portion of the deployment. I found this very helpful, because your configuration file contents can keep on changing quite frequently. So because of this trick, Helm ensures that your app will keep reflecting those changes promptly.<\/p>\n<p>I have created <a href=\"https:\/\/github.com\/mrajashree\/helm-cm1\">this sample Helm chart<\/a> to show how this trick can be implemented.<\/p>\n<p>Do check it out and hopefully you\u2019ll find it useful for your apps too! Also feel free to add more to it, and maybe share any such tips\/tricks that you have come across as well!<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/rancher.com\/img\/bio\/rajashree-mandaogane.jpg\" alt=\"Rajashree Mandaogane\" width=\"100\" height=\"100\" \/><\/p>\n<p>Rajashree Mandaogane<\/p>\n<p>Software Engineer<\/p>\n<p><a href=\"https:\/\/rancher.com\/blog\/2018\/2018-07-10-helm-tips-and-tricks-updating-app-with-configmap\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Take a deep dive into Best Practices in Kubernetes Networking From overlay networking and SSL to ingress controllers and network security policies, we&#8217;ve seen many users get hung up on Kubernetes networking challenges. In this video recording, we dive into Kubernetes networking, and discuss best practices for a wide variety of deployment options. Watch the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw93\/index.php\/2018\/10\/17\/helm-tips-and-tricks-updating-an-app-that-uses-configmap\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Helm Tips and Tricks: Updating an App that uses ConfigMap&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-452","post","type-post","status-publish","format-standard","hentry","category-kubernetes"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/comments?post=452"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/452\/revisions"}],"predecessor-version":[{"id":477,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/posts\/452\/revisions\/477"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/media?parent=452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/categories?post=452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw93\/index.php\/wp-json\/wp\/v2\/tags?post=452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}