configmaps 更新时如何在 kubernetes 的应用程序中获取通知
How to get notification inside an application in kubernetes when configmaps get updated
我在 kubernetes
中有一个应用程序 运行ning,它有一个通过 configmaps
挂载的文件。现在,当这个文件(来自 configmap)更新时(假设通过 kubectl update configmaps xyz
命令),我想从应用程序内部执行一些操作。
假设我使用以下命令创建了一个配置映射:
kubectl create configmap myy-config --from-file=config.json
我的 Deployment 是这样创建的:
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
现在,如果我这样做 kubectl exec -it <pod> sh
我可以看到该文件。如果我使用 kubectl edit configmap my-config
编辑 configmap 并更改内容,我的 pod 中的应用程序 运行ning 不会收到文件更改通知。我正在为应用程序使用 GO Lang,它没有收到文件 /myapp/config/config.json
上的 fsnotify,即使我可以看到编辑后文件已更改。
如果我 运行 我的笔记本电脑上有相同的应用程序,当然,代码会获取 fsnotify 并且我的应用程序会更新它的配置。来自 kubernetes 的相同代码与来自 configmap 的文件,它不起作用。我已经阅读了其他 SOF 问题 和其他各种问题,但没有什么特别针对我面临的问题的解决方案。
我知道该文件(来自 configmap)是一个符号链接,实际文件位于名为 ..data/config.json
的文件夹中。我也尝试添加该文件,但仍然没有收到 fsnotify 信号。是否有可能为来自应用程序中的 configmap(以及机密)的文件获取 fsnotify 信号?如果是这样,有人可以帮助我并展示如何做吗(最好是在 GO lang 中)?
听起来 Reloader 就是您要找的人。它将监视 configmap/secret 并更新与其相关的部署。
您可能遇到了问题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: [..]
看来您需要跟踪符号链接链并观察它。由于您的应用程序是用 go
编写的,您可以只使用 spf13/viper
since the feature WatchConfig and Kubernetes was added.
或者,您最有可能在 changes of a ConfigMap. This requires configuring some access rules 上预先收到 Kubernetes API 的通知。
我在 kubernetes
中有一个应用程序 运行ning,它有一个通过 configmaps
挂载的文件。现在,当这个文件(来自 configmap)更新时(假设通过 kubectl update configmaps xyz
命令),我想从应用程序内部执行一些操作。
假设我使用以下命令创建了一个配置映射:
kubectl create configmap myy-config --from-file=config.json
我的 Deployment 是这样创建的:
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
现在,如果我这样做 kubectl exec -it <pod> sh
我可以看到该文件。如果我使用 kubectl edit configmap my-config
编辑 configmap 并更改内容,我的 pod 中的应用程序 运行ning 不会收到文件更改通知。我正在为应用程序使用 GO Lang,它没有收到文件 /myapp/config/config.json
上的 fsnotify,即使我可以看到编辑后文件已更改。
如果我 运行 我的笔记本电脑上有相同的应用程序,当然,代码会获取 fsnotify 并且我的应用程序会更新它的配置。来自 kubernetes 的相同代码与来自 configmap 的文件,它不起作用。我已经阅读了其他 SOF 问题
我知道该文件(来自 configmap)是一个符号链接,实际文件位于名为 ..data/config.json
的文件夹中。我也尝试添加该文件,但仍然没有收到 fsnotify 信号。是否有可能为来自应用程序中的 configmap(以及机密)的文件获取 fsnotify 信号?如果是这样,有人可以帮助我并展示如何做吗(最好是在 GO lang 中)?
听起来 Reloader 就是您要找的人。它将监视 configmap/secret 并更新与其相关的部署。
您可能遇到了问题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: [..]
看来您需要跟踪符号链接链并观察它。由于您的应用程序是用 go
编写的,您可以只使用 spf13/viper
since the feature WatchConfig and Kubernetes was added.
或者,您最有可能在 changes of a ConfigMap. This requires configuring some access rules 上预先收到 Kubernetes API 的通知。