Cloud-Native Configuration
Video
Overriding Configuration At Runtime
ASP.NETCore provides a machanism for overriding the appsettings.json
at runtime. This provides a simple way to "inject" our runtime configuration from a Kubernetes/OpenShift ConfigMap or Secret.
- Modify the
Program.cs
to add an optional configuration file to be loadedcsharppublic static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseSerilog() .ConfigureAppConfiguration((hostingContext, config) => { config.AddJsonFile("/tmp/config/runtimesettings.json", optional: true, reloadOnChange: false); }) // SNIP - Remaining Host Builder
- Now, when we deploy our application on OpenShift, it will look for a file in
/tmp/config
calledruntimesettings.json
and those settings will override any settings in ourappsettings.json
. - We can define our Deployment or DeploymentConfig such that we mount a ConfigMap or Secret containing that JSON configuration in that location