Automating Configuration Updates in Kubernetes with Reloader
Tags: kubernetes, docker, reloader, tutorial
Published: 2023-06-23 06:29:44

Managing and updating application configurations in a Kubernetes environment can be a complex and time-consuming task. Changes to ConfigMaps and Secrets often require manual intervention, which can lead to errors and downtime. However, with the help of a powerful tool called Reloader, this process can be automated. Reloader is an open-source Kubernetes controller that watches for changes in ConfigMaps and Secrets and triggers rolling upgrades for associated resources, ensuring that applications stay up-to-date. 

In this blog post, we will explore how to deploy and utilize Reloader in a Kubernetes cluster, along with code snippets to illustrate its usage.

## Understanding Reloader


[Reloader](https://github.com/stakater/Reloader) is designed to simplify the process of updating application configurations in Kubernetes. It monitors ConfigMaps and Secrets for changes and triggers rolling upgrades for associated resources such as Deployments, StatefulSets, DaemonSets, and more. Reloader eliminates the need for manual intervention and reduces the risk of errors during the configuration update process.

## Deployment of Reloader on Kubernetes

To leverage the benefits of Reloader, we need to deploy it in our Kubernetes cluster. There are multiple deployment options available, including using manifests and Helm. In this section, we will explore both methods.


## 1. Deployment Using Manifests

Deploying Reloader using manifests involves the following steps:

## Step 1: Apply vanilla manifests 

Apply vanilla manifests by changing RELEASE-NAME placeholder provided in manifest with a proper value and apply it by running the command given below:

```
kubectl apply -f https://raw.githubusercontent.com/stakater/Reloader/master/deployments/kubernetes/reloader.yaml
```


```
kubectl apply -k https://github.com/stakater/Reloader/deployments/kubernetes
```


By default, Reloader gets deployed in default namespace and watches changes secrets and configmaps in all namespaces.

Reloader can be configured to ignore the resources secrets and configmaps by passing the following args (spec.template.spec.containers.args) to its container :

| Args | Description |
|------| ---------|
|--resources-to-ignore=configMaps |To ignore configMaps
| --resources-to-ignore=secrets | To ignore secrets |

## Deployment Using Helm

Helm is a package manager for Kubernetes that simplifies the deployment and management of applications. To deploy Reloader using Helm, follow these steps:

## Step 1: Add the Reloader Helm repository

```
helm repo add stakater https://stakater.github.io/stakater-charts
helm repo update
helm install stakater/reloader # For helm3 add --generate-name flag or set the release name
```

Note: By default reloader watches in all namespaces. To watch in single namespace, please run following command. It will install reloader in test namespace which will only watch Deployments, Daemonsets Statefulsets and Rollouts in test namespace.

```
helm install stakater/reloader --set reloader.watchGlobally=false --namespace test # For helm3 add --generate-name flag or set the release name
```




## ConfigMap and Secret Reloading with Reloader:

Reloader supports both ConfigMap and Secret reloading. Let's explore each of them in detail.

## i. Reloading ConfigMaps:

ConfigMaps store configuration data that can be consumed by applications running in Kubernetes. Reloader monitors ConfigMaps for changes and automatically triggers rolling upgrades for associated resources. Here's an example of how to enable ConfigMap reloading with Reloader:

## Step 1: Annotate the resource with Reloader's annotation:

```
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-app-config
  annotations:
    reloader.stakater.com/auto: "true"
```


## Step 2: Apply the ConfigMap

```
kubectl apply -f configmap.yaml
```


## Step 3: Update the ConfigMap

Modify the ConfigMap's data:

## ii. Reloading Secrets

Secrets store sensitive information, such as passwords, API keys, and certificates. Reloader can also watch for changes in Secrets and trigger rolling upgrades accordingly. Here's how to enable Secret reloading with Reloader:

## Step 1: Annotate the Secret with Reloader's annotation:

```
apiVersion: v1
kind: Secret
metadata:
  name: my-app-secret
  annotations:
    reloader.stakater.com/auto: "true"
```


## Step 2: Apply the Secret:

```
kubectl apply -f secret.yaml
```

## Step 3: Update the Secret:

Modify the Secret's data.

## Use-Cases

Assume we have a Deployment called "my-app" and a ConfigMap called "my-app-config" that stores the application's configuration. To enable Reloader for this Deployment, we need to add an annotation to the Deployment's metadata:

## 1. Adding Reloader annotation to Deployment metadata:

```
kind: Deployment
metadata:
  annotations:
    reloader.stakater.com/auto: "true"
spec:
  template:
    metadata:
      ...
```

## 2. Restricting discovery of resources using reloader.stakater.com/search annotation

This annotation tells Reloader to only trigger rolling upgrades when changes occur in ConfigMaps or Secrets that are explicitly annotated with reloader.stakater.com/match: "true".

```
kind: Deployment
metadata:
  annotations:
    reloader.stakater.com/search: "true"
spec:
  template:
    ...
```

Specifying specific ConfigMap or Secret to trigger rolling upgrade:

```
kind: Deployment
metadata:
  annotations:
    configmap.reloader.stakater.com/reload: "my-app-config"
spec:
  template:
```

## Conclusion

Reloader is a powerful tool that automates rolling upgrades in Kubernetes by monitoring changes in ConfigMaps and Secrets. It simplifies the process of keeping applications up-to-date and minimizes manual intervention. In this blog post, we covered the deployment of Reloader using manifests and Helm, along with examples of ConfigMap and Secret reloading. We also explored a real-world use case where Reloader enables automatic rolling upgrades based on changes in associated ConfigMaps.

By leveraging Reloader's capabilities, you can enhance the reliability and efficiency of your Kubernetes deployments. Happy automating with Reloader in your Kubernetes cluster!