Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get notification inside an application in kubernetes when configmaps get updated

I have an application running inside kubernetes which has a file mounted through configmaps. Now, from inside the application I want to perform some action when this file (from configmap) gets updated (lets say through kubectl update configmaps xyz command).

Lets say I have created a configmap using the following command:

kubectl create configmap myy-config --from-file=config.json

and I have my Deployment created like this:

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: myapp
spec:
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        -
          image: "xyz"
          name: myapp
          ports:
            -
              containerPort: 5566
          volumeMounts:
            -
              mountPath: /myapp/config
              name: config
      dnsPolicy: ClusterFirstWithHostNet
      volumes:
        -
          name: config
          configMap:
            name: my-config

Now, if I do kubectl exec -it <pod> sh I can see the file. If I edit the configmap using kubectl edit configmap my-config and change the content, the application running in my pod doesn't get the file changed notification. I am using GO Lang for the application and it doesn't receive the fsnotify on the file /myapp/config/config.json even though I can see that after the edit, the file has changed.

If I run the same application in my laptop, of course, the code gets the fsnotify and my application updates it configuration. The same code from within the kubernetes with the file coming from configmap, it doesn't work. I have read other SOF questions like this and various others, but nothing specifically has solution for the problem I face.

I understand that the file (which comes from the configmap) is a symlink and the actual file is in a folder called ..data/config.json. I tried to add that file also, but still not getting the fsnotify signal. Is it possible to get fsnotify signal for files which come from configmap (as well as secrets) within the application? If so, can someone please help me and show how to do it (preferably in GO lang)?

like image 519
Amudhan Avatar asked Jan 16 '19 15:01

Amudhan


People also ask

What happens when ConfigMap is updated?

When the config map is updated, a new version is created for that configMap. All the deployments that were attached to the configMap get a rolling update with the latest configMap version tied to it.

Do we need to restart pod after ConfigMap change?

ConfigMaps consumed as environment variables are not updated automatically and require a pod restart. Note: A container using a ConfigMap as a subPath volume mount will not receive ConfigMap updates.

Can a pod update a ConfigMap?

Updating the config map in Kubernetes POD requires the restart of POD. A possible approach to auto-update configmap inside pod without restart is mounting it's content as volume. Kubernetes auto-updates the config map into POD if mounted as a volume, however, it has a limitation like it won't work if subpath used.

Can we edit ConfigMap in Kubernetes?

Method to edit ConfigMaps in Kubernetes using kubectl You can alter the file as per your work needs.


2 Answers

You might be experience a problem like this:

When a ConfigMap changes, the real path to the config files it contains changed, but this is kinda “hidden” by 2 levels of symlinks: [..]

So it seems you need to follow the chain of symlinks and watch that. Since your application is written in go you could just use spf13/viper since the feature WatchConfig and Kubernetes was added.

Alternatively you can get notified by the Kubernetes API on changes of a ConfigMap. This requires configuring some access rules upfront most probably.

like image 180
webwurst Avatar answered Oct 22 '22 15:10

webwurst


Sounds like the Reloader is the one you are looking for. It will watch the configmap/secret and update the deployment related to it.

like image 35
Cindy Avatar answered Oct 22 '22 13:10

Cindy