Kubernetes: ConfigMap and Secrets – data auto-reload in pods
Tags: kubernetes, tutorial
Published: 2020-02-14 16:07:06

---
title: Kubernetes: ConfigMap and Secrets – data auto-reload in pods
published: true
date: 2020-02-13 10:05:25 UTC
tags: Kubernetes,tutorial
canonical_url: https://rtfm.co.ua/en/kubernetes-configmap-and-secrets-data-auto-reload-in-pods/
---

![](https://rtfm.co.ua/wp-content/uploads/2017/06/kuber-1.png)W have a ConfigMap for our Gorush service (see the [Kubernetes: running a push-server with Gorush behind an AWS LoadBalancer](https://rtfm.co.ua/en/kubernetes-running-a-push-server-with-gorush-behind-an-aws-loadbalancer/) post).

The issue is that if change a value in this ConfigMap or Secrets -it will not be applied to already running pods.

There are various solitons like mount data as volumes and re-attach those volumes after data has been changed, or to create a brand new ConfigMap with a new name each time on a new change, and re-map it to the pods.

But let’s try to use the [Reloader](https://github.com/stakater/Reloader) – it will auto-discover all ConfgiMaps and Secrets and will watch for their changes. Once it will find that a ConfigMap was updated – Reloader will re-create related pods.

### Running Reloader in Kubernetes

Create a service and related objects – configs, roles, etc:

```
$ kubectl apply -f https://raw.githubusercontent.com/stakater/Reloader/master/deployments/kubernetes/reloader.yaml
deployment.apps/reloader-reloader created
clusterrole.rbac.authorization.k8s.io/reloader-reloader-role created
clusterrolebinding.rbac.authorization.k8s.io/reloader-reloader-role-binding created
serviceaccount/reloader-reloader created
```

It will be deployed to the в _default_ namespace, and by default will watch for all ConfigMap and Secrets in all namespaces, but you can limit this behavior using object’s annotations, see the full doc [here>>>](https://github.com/stakater/Reloader#configmap).

Check the service’s pod:

```
$ kubectl get pod
NAME                                 READY   STATUS    RESTARTS   AGE
reloader-reloader-7684d7d4b5-rwp6x   1/1     Running   0          112s
```

### ConfigMap auto-reload

Now, let’s check an existing value from the Gorush service:

```
$ curl push.example.com/api/config
...
ios:
enabled: true
key_path: /data/ssl/apns-crt.p12
key_base64: ""
...
```

And its ConfigMap:

```
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: gorush-config
  namespace: gorush
data:
  # stat
  stat.engine: redis
  stat.redis.host: redis:6379
  ios.enabled: "true"
  ios.key_path: /data/ssl/apns-crt.p12
...
```

Update its deployment and add to the annotations one of the following string –  `reloader.stakater.com/auto: "true"` or `configmap.reloader.stakater.com/reload: "gorush-config"`, or you’d like to watch only this particular ConfigMap:

```
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: gorush
  namespace: gorush
  annotations:
    configmap.reloader.stakater.com/reload: "gorush-config"
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: gorush
        tier: frontend
...
```

Apply the deployment:

```
$ kubectl apply -f gorush-deployment.yaml
deployment.extensions/gorush configured
```

Add some changes to the Gorush’ cConfigMap, for example, let’s change `ios.enabled: "true"` to the `ios.enabled: "false"`:

```
$ kubectl apply -f gorush-configmap.yaml
configmap/gorush-config configured
configmap/gorush-config-file unchanged
```

Check events:

```
$ kubectl get events -n gorush
...
7s          Normal    ScalingReplicaSet        deployment/gorush              Scaled up replica set gorush-b85d7dc64 to 1
7s          Normal    ScalingReplicaSet        deployment/gorush              Scaled down replica set gorush-7b8695bf6f to 0
```

Replica-sets:

```
$ kubectl -n gorush get rs
NAME                DESIRED   CURRENT   READY   AGE
gorush-7b8695bf6f   0         0         0       10m
gorush-88cdd9b4d    0         0         0       20m
gorush-b85d7dc64    1         1         1       51s
redis-7d5844c58d    1         1         1       5d
```

And Gorush’ settings now:

```
$ curl push.example.com/api/config
...
ios:
enabled: false
key_path: /data/ssl/apns-crt.p12
key_base64: ""
key_type: pem
...
```

Done.

### Similar posts

- <small>01/30/2020</small> [Kubernetes: could not find the requested resource (get services http:heapster:) and running metrics-server](https://rtfm.co.ua/en/kubernetes-could-not-find-the-requested-resource-get-services-httpheapster-and-running-metrics-server/)  
- <small>03/27/2019</small> [Kubernetes: running Minikube on Arch Linux](https://rtfm.co.ua/en/kubernetes-running-minikube-on-arch-linux/)
- <small>08/15/2019</small> [Kubernetes: part 3 – AWS EKS overview and manual EKS cluster set up](https://rtfm.co.ua/en/kubernetes-part-3-aws-eks-overview-and-manual-eks-cluster-set-up/)  
- <small>09/03/2019</small> [Kubernetes: part 4 – AWS EKS authentification, aws-iam-authenticator and AWS IAM](https://rtfm.co.ua/en/kubernetes-part-4-aws-eks-authentification-aws-iam-authenticator-and-aws-iam/)