dougengqiu8031 2019-01-16 15:17 采纳率: 100%
浏览 72
已采纳

当配置映射更新时,如何在kubernetes中的应用程序内部获取通知

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

  • 写回答

2条回答 默认 最新

  • doureng5668 2019-03-06 18:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用