How to override Appsettings in Kubernetes
In my previous posts, I talked about Helm and showed how it can be used to easily deploy applications to Kubernetes.
In this post I will show you, how you can dynamically configure your application using environment variables in Helm and override values in the appsettings.json file using environment variables.
Deploy a Microservice using Helm
You can find the code of the demo on GitHub. If you don’t know what Helm is or if you haven’t installed it yet, see Helm – Getting Started for more information.
You can run on Azure or locally, here I have used here with Azure kubernetes aks.
Open the ProductMicroservice-DotNET-Core-Master application and navigate to the Helm chart of the ProductMicroservice. You can find it under ProductMicroservice/ProductMicroservice/charts. The chart is in a folder called productmicroservice. Deploy this chart with Helm:
Open Windows Powershell as admin and go to this folder. To install this package use helm install [Name] [ChartName]. For me is ProductMicroservice application this can be done with the following code:
helm install product productmicroservice
If it is succeed, then package gets deployed within seconds. After it is finished, connect to the dashboard of your cluster.
In the dashboard, open the productmicroservice pod and you will see that there is an error.
RabbitMQ causes an Exception.
The application wants to open a connection to RabbitMQ. Currently, there is no RabbitMQ running in my Kubernetes cluster. Therefore, the exception occurs.
Overriding Appsettings with Environment Variables
The settings for RabbitMQ are in the appsettings.json file. There is also a flag to enable and disable the connection. By default, this flag is enabled.
"RabbitMq": {
"Hostname": "rabbitmq",
"QueueName": "CustomerQueue",
"UserName": "user",
"Password": "password"
"Enabled": true
}
Currently, we don’t want to use RabbitMQ, therefore we can disable it. Microsoft introduced the DefaultBuilder method which automatically reads environment variables, command-line arguments, and all variations of the appsettings.json files. This allows developers to use environment variables to override settings without changing the code.
The Startup.cs class is reading the RabbitMQ configs and then register the service depending on the value of the enabled flag:
var serviceClientSettingsConfig = Configuration.GetSection("RabbitMq");
var serviceClientSettings = serviceClientSettingsConfig.Get<RabbitMqConfig>();
services.Configure<RabbitMqConfig>(serviceClientSettingsConfig);
if(serviceClientSettings.Enabled)
{
services.AddHostedService<ProductUpdateSender>();
}
The enabled flag is in the RabbitMQ section of the appsettings.json file. To override it with an environment variable, we should to pass one with the same structure. Instead of braces,
Pass the Environment Variable using Helm
Helm allows us to add environment variables easily. Add in the values.yaml file use double underscores (__). This means that the name of the environment variable is rabbitmq__enabled and its value is false, as following code:
envvariables: rabbitmq__enabled: false
This passes the value as an environment variable into the deployment.yaml file.
env:
{{- $root := . }}
{{- range $key, $value := .Values.envvariables }}
- name:{{ $key }}
value: {{ $value | quote }}
{{- end }}
{{- $root := . }}
{{- range $ref, $values := .Values.secrets }}
{{- range $key, $value := $values }}
- name: {{ $key }}
valueFrom:
secretKeyRef:
name: {{ template "orderapi.fullname" $root }}-{{ $ref | lower }}
key: {{ $key }}
{{- end }}
{{- end }}
The code above iterates over the envvariables and secrets section and sets the values as environment variables.
Update the Microservice
Use Helm upgrade to update the deployment with the changes in your Helm package:
helm upgrade productmicroservice productmicroservice
After the changes are applied, open the dashboard and navigate to the productmicroservice pod.
You will see that the pod is running now. The environment variable is also displayed in the dashboard. If your application is not working as expected, check there first if all environment variables are present.
Under the Container productmicroservice, it should be seen:
Environment: rabbitmq_enabled : false,
The pod starts and the environment variable is displayed.
Test the application
To test the application, click either on “Start Port Forwarding” which will give you a localhost URL or you can open the Service and see the external IP of your service there.
open browser with url :20.166.217.101:80 , then Swagger UI should be loaded as follow image:
Conclusion
In this post we have override setting values with environment variables. This allows us to dynamically change the configuration during the deployment of your application. we used Helm to add an environment variable to override a value of the appsettings.json file.
You can find the code of the demo on my Github.
In my next post, I will explain, How to run Kubernetes Cluster locally
This post is part of “Kubernetes step by step”.