Kubernetes - Reload your ASP.NET Core configuration when a configmap changes–DOTNET_USE_POLLING_FILE_WATCHER
In a previous post I showed how you can use ConfigMaps in Kubernetes. This allows you to update for example our appsettings.json without the need to upload a new container image and update our pods.
Sounds great! However there is one challenge with this approach. In Kubernetes a config map is mounted as a symlink, and .NET is not able to pick up changes applied to the original file by just knowing about the symlink. I mentioned some workarounds but an out-of-the-box solution was not available at that time.
DOTNET_USE_POLLING_FILE_WATCHER
Now a solution to the problem above is available through the DOTNET_USE_POLLING_FILE_WATCHER environment variable. By setting this value to “true” or “1”, we can instruct .NET to not use the default FileSystemWatcher but using a polling mechanism instead.
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: example-api-deployment | |
spec: | |
selector: | |
matchLabels: | |
app: example-api | |
replicas: 2 | |
strategy: | |
type: RollingUpdate | |
rollingUpdate: | |
maxUnavailable: 1 | |
template: | |
metadata: | |
labels: | |
app: example-api | |
spec: | |
containers: | |
- name: example-api | |
image: my.azurecr.io/example:latest | |
env: | |
- name: DOTNET_USE_POLLING_FILE_WATCHER | |
value: "true" | |
ports: | |
- containerPort: 80 | |
volumeMounts: | |
- name: appsettings-volume | |
mountPath: /app/appsettings.json | |
subPath: appsettings.json | |
volumes: | |
- name: appsettings-volume | |
configMap: | |
name: example-configmap |
More information: dotnet watch