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

1/16/2019

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)?

-- Amudhan
configmap
go
kubernetes

2 Answers

3/6/2019

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.

-- webwurst
Source: StackOverflow

1/18/2019

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

-- Cindy
Source: StackOverflow