<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mathieu Dubois on Medium]]></title>
        <description><![CDATA[Stories by Mathieu Dubois on Medium]]></description>
        <link>https://medium.com/@duboismathieu?source=rss-d3f59aa67223------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*0IjSA9ZPn864q_hg.jpg</url>
            <title>Stories by Mathieu Dubois on Medium</title>
            <link>https://medium.com/@duboismathieu?source=rss-d3f59aa67223------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 12:46:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@duboismathieu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Database migrations on Kubernetes using Helm hooks]]></title>
            <link>https://itnext.io/database-migrations-on-kubernetes-using-helm-hooks-fb80c0d97805?source=rss-d3f59aa67223------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb80c0d97805</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[helm]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[database-migration]]></category>
            <dc:creator><![CDATA[Mathieu Dubois]]></dc:creator>
            <pubDate>Wed, 16 Sep 2020 06:18:56 GMT</pubDate>
            <atom:updated>2020-09-17T06:32:23.369Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>TL;DR: you can leverage the pre-install and pre-upgrade Helm hooks to run database migrations before your application is installed or updated. Keep reading to understand why simpler solutions might not be good ideas and a couple of gotchas when using Helm hooks.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*ZwakuQ2_fjUvQyUFP9qUiA.png" /></figure><p>If your application uses a relational database, your schema will evolve over time. Before deploying a new version of you application, you will have to ensure that the database schema is up to date. This article is not about how to generate and manage schema migrations (there are multiple tools to handle that) but how to apply them as part of the application deployment process on Kubernetes.</p><h3>Running migrations within the application</h3><p>You could decide to run migrations automatically as part of your service startup. This might seem like a good idea: it guarantees that your service will not start before the migrations are applied and removes the risk of running an application on an outdated schema.<br>But when running on Kubernetes, this poses a couple of issues:</p><ul><li>every time a new pod is created or restarted, it will try to run the migrations again. If your migration scripts are written correctly, this should not be an issue, but it’s not a clean design.</li><li>if the migration takes a while (think adding a column on a huge table), your pod might miss its readiness check and be killed before the migration is done. You could increase the initial delay for the pod’s readiness check, but then it makes it hard to understand if a pod is not starting because it’s applying database migrations or because of a different issue.</li><li>depending on the disruption budget of your deployment (how many pods can be added/removed at any point of time during a deployment), you may have 2 identical migrations running at the same time, resulting in conflicts and errors.</li></ul><h3>Using an init container</h3><p><a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/">Init containers</a> are containers that will run to completion before the regular containers in your pod start. This is useful to perform any setup that is required before your application can start (like downloading some secrets or config files for example). It could seem like a good idea to use init containers to run database migrations, but we would be facing the same issues as running them during application startup:</p><ul><li>more than one init container can run at the same time if multiple pods are created simultaneously</li><li>the init container will run every time a new pod is created</li></ul><h3>Running as job using Helm hooks</h3><h4>Kubernetes jobs</h4><p>First, let’s look at Kubernetes jobs. Jobs allow us to run 1 or multiple pods to completion. Contrary to pods in a deployment or a replicaset where K8s will recreate pods as they terminate to keep the number of requested pods running, pods in Job will not be recreated when they exit (unless they fail and the job is configured to restart on failure).<br>This is great to run a task that needs to run to completion only once and running database migrations is a perfect use case for them.</p><p>Now the challenge is to automatically run a job prior to deploying a new version of an application.</p><h4>Helm release lifecycle</h4><p><a href="https://helm.sh/">Helm</a> allows you to package all the K8s manifests that your application defines in a <em>Chart</em> to deploy them all at once, and uses templates to customize each deployment (allowing for example to deploy the same chart in multiple environments with different parameters).</p><p>Helm also provides <a href="https://helm.sh/docs/topics/charts_hooks/">hooks</a> to decide when resources are created during the deployment. We can leverage that to run the migration job before any resource is created/updated.</p><p>Here’s what our migration job would look like:</p><pre><strong>apiVersion</strong>: batch/v1<br><strong>kind</strong>: Job<br><strong>metadata</strong>:<br>  <strong>name</strong>: &quot;{{ .Release.Name }}&quot;<br>  <strong>labels</strong>:<br>  <strong>annotations</strong>:<br>    <strong>&quot;helm.sh/hook&quot;: </strong>pre-install,pre-upgrade<br>    <strong>&quot;helm.sh/hook-weight&quot;: </strong>&quot;-1&quot;<br>    <strong>&quot;helm.sh/hook-delete-policy&quot;: </strong>hook-succeeded<br><strong>spec</strong>:<br>  <strong>template</strong>:<br>    <strong>metadata</strong>:<br>      <strong>name</strong>: &quot;{{ .Release.Name }}&quot;<br>    <strong>spec</strong>:<br>      <strong>restartPolicy</strong>: Never<br>      <strong>containers</strong>:<br>      - <strong>name</strong>: db-migrations // we will have to build a dedicated<br>                            // docker image to run the migrations<br>        <strong>image</strong>: &quot;database-migrations&quot;</pre><p>The hooks are configured using annotations:</p><ul><li><strong>&quot;helm.sh/hook&quot;: </strong>pre-install,pre-upgrade tells helm to run this job before install and before upgrading the application.</li><li><strong>&quot;helm.sh/hook-weight&quot;: </strong>&quot;-1&quot; is used to define in which order helm should create resources that implement the same hook.</li><li><strong>&quot;helm.sh/hook-delete-policy&quot;: </strong>hook-succeeded tells helm to delete the job after it succeeded.</li></ul><p>The rest of the job is a regular pod template.</p><h3>Gotchas</h3><h4>Helm hooks gotchas</h4><p>Your migrations job will most likely require some configuration and or secrets to be able to run (db server address and credentials at the very least), but the job will be created <em>before</em> all the other resources in the chart.</p><p>This means that our job will not be able to mount a configmap created by the chart. We’ll need to create a configmap that implements the same hooks. Here’s an example of a configmap:</p><pre><strong>apiVersion</strong>: v1<br><strong>kind</strong>: ConfigMap<br><strong>metadata</strong>:<br>  <strong>name</strong>: db-migrations<br><strong>annotations</strong>:<br>    <strong>&quot;helm.sh/hook&quot;: </strong>pre-install,pre-upgrade<br>    <strong>&quot;helm.sh/hook-weight&quot;: </strong>&quot;-10&quot; // we use a smaller weight so it&#39;s<br>                                 // created before the job    <br>    <strong>&quot;helm.sh/hook-delete-policy&quot;: </strong>hook-succeeded<br><strong>data</strong>:<br>  DB_ADDR: {{ .Values.db.addr }}<br>  DB_NAME: {{ .Values.db.name }}</pre><h4>Deployment strategy and rollbacks</h4><p>By default, Kubernetes applies a rolling update strategy when updating a deployment. This means that during the deployment, there will be pods running both the previous and the new version of your application. <strong>This will require all migrations to be backwards compatible</strong> with at least the previous version.</p><p>If you ever need to rollback to a previous version of your application using the helm rollback command, the migration job for the version you’re re-deploying will run again. Trying to migrate <em>down </em>to a previous version of the schema during a rollback would likely cause the existing pods to fail. Finally, if you have to rollback to a much older version, you need to make sure that the current schema is backwards compatible with the version you plan to rollback to.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb80c0d97805" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/database-migrations-on-kubernetes-using-helm-hooks-fb80c0d97805">Database migrations on Kubernetes using Helm hooks</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Updating application config at runtime using Redis pub/sub]]></title>
            <link>https://medium.com/@duboismathieu/updating-application-config-at-runtime-using-redis-pub-sub-a52ae23c1e39?source=rss-d3f59aa67223------2</link>
            <guid isPermaLink="false">https://medium.com/p/a52ae23c1e39</guid>
            <category><![CDATA[feature-flags]]></category>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[configuration]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[golang-tutorial]]></category>
            <dc:creator><![CDATA[Mathieu Dubois]]></dc:creator>
            <pubDate>Wed, 09 Sep 2020 06:50:39 GMT</pubDate>
            <atom:updated>2020-09-09T06:50:39.188Z</atom:updated>
            <content:encoded><![CDATA[<h3>Updating application config at runtime using Redis pub/sub and Golang</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*hIFYvt3a9ua0ykrvFmdSZg.jpeg" /><figcaption>The Go gopher was designed by Renee French. (http://reneefrench.blogspot.com/)<br> The design is licensed under the Creative Commons 3.0 Attributions license.</figcaption></figure><p>When deploying an application or service, chances are you need to provide some sort of configuration. A common pattern is to leverage environment variables or a config file. This works fine for configuration that does not change often (like the port on which you http server is listening) but not so much for configuration that could change often and at any point in time (business logic configuration, feature flags etc.).</p><p>In this article we’ll see how we can leverage <a href="https://redis.io/topics/pubsub">Redis Pub/Sub</a> functionality to update configuration at runtime and in real time.</p><h3>Redis configuration</h3><p>We will use Redis as a key/value store and listen to changes for specific keys using Redis <a href="https://redis.io/topics/notifications">keyspace notifications</a>. Redis can publish events for specific operations affecting keys. These events are disabled by default and need to be activated in your Redis instance by running the following command:</p><pre>$ redis-cli config set notify-keyspace-events KA<br>OK</pre><p>This will enable all the keyspace events.</p><h3>Subscribing to events</h3><p>We’ll build this example in Go, using the <a href="https://github.com/go-redis/redis">go-redis</a> client.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/76c8ea76155de73271a2011f49be638e/href">https://medium.com/media/76c8ea76155de73271a2011f49be638e/href</a></iframe><p>If you run this program and then update the foo key by running redis-cli set foo test from another terminal, you’ll see the following message:</p><pre>received redis event: Message&lt;__keyspace@0__:foo: set&gt;</pre><p>You’ll notice that the event contains the key and the command that triggered the event, but not the value. We’ll need to fetch that new value manually. Our loop will now look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/20d42fbbdff1e827e3c482bdc1e1e56e/href">https://medium.com/media/20d42fbbdff1e827e3c482bdc1e1e56e/href</a></iframe><p>We’re now able to get the updated value for a single key as soon as it’s updated and detect if the key was deleted.</p><p>Running the updated program and updating the key in Redis again will now print:</p><pre>the new value for the key `foo` is: test</pre><h3>The Config Watcher</h3><p>Now, let’s try and make things a bit more generic. We’ll create a <em>configuration watcher</em> that will store the current value for all the configuration our service needs and will keep the local value updated.</p><p>For simplification, we’ll assume that all the configuration values are of the string type, but this could easily be extended to support any type of configuration value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/088b9d4e9b8fce66bb9ed76f83ca4b59/href">https://medium.com/media/088b9d4e9b8fce66bb9ed76f83ca4b59/href</a></iframe><h4>Making it foolproof</h4><p>The go-redis client detects connection issues and will automatically re-create the subscription if it drops. That’s great, but what if a config was updated while the subscription was done? Redis doesn’t handle these cases and the event will be lost. We can counter that by checking the config values at regular interval. The Watch method would then look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/07e69dc6368b2e0feb330c078aa2b069/href">https://medium.com/media/07e69dc6368b2e0feb330c078aa2b069/href</a></iframe><p>Now if an event is missed, the value will be out of sync of a maximum of 10 seconds or whatever refresh timeout is used.</p><h3>The Watcher in action: feature flag</h3><p>Now let’s see how to use the Watcher with a simple example: turning a feature flag on or off. Let’s say we’re working on a new awesome feature. The development is complete, but the feature cannot be launched yet because our product manager wants to release it on their birthday. We don’t want to wait another 2 months to push that code to production, so we’ll simply hide it behind a feature flag and will simply change a value in Redis to launch our new feature.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c81dde9f35df968170cf2025b8cea4c/href">https://medium.com/media/5c81dde9f35df968170cf2025b8cea4c/href</a></iframe><p>If you run this program and visit http://127.0.0.1:8080 you should see the Boring old feature :(. Now if you run the following command in your terminal:</p><pre>redis-cli set awesome_new_feature on</pre><p>and visit that page again, you should get the Exciting new stuff!!! message . If something goes terribly wrong, you can instantly rollback by running:</p><pre>redis-cli set awesome_new_feature off</pre><p>We now have a simple configuration watcher that allows us to update configuration at runtime without needing to re-deploy our service.</p><p>In a following article, we’ll see how we can leverage gRPC streams to abstract Redis.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a52ae23c1e39" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>