When deploying .NET applications in Kubernetes, configuration management plays a vital role. Sensitive data like database connection strings and app settings must be handled securely and efficiently. This blog post walks you through configuring a .NET application using Kubernetes ConfigMaps and Secrets, particularly when dealing with nested keys like MongoDb:ConnectionString.
Handling Nested Keys in Kubernetes
In Kubernetes, colons (:) are not allowed in keys. For .NET applications, replace colons with double underscores (__). This aligns with the way environment variables are mapped to configuration keys in .NET.
Example ConfigMap
Here’s an example ConfigMap with a MongoDB connection string:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
MongoDb__ConnectionString: mongodb://root:mypassword@mongodb.servername:27017
Accessing ConfigMap Values in .NET
In your .NET application, the key MongoDb__ConnectionString will be automatically interpreted as MongoDb:ConnectionString by the configuration system.
builder.Services.Configure<NoSqlDatabaseConfiguration>(
builder.Configuration.GetSection("MongoDb"));
Using Kubernetes ConfigMap in Deployments
To use the ConfigMap in a Kubernetes deployment, expose it as an environment variable:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 1
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: my-image
env:
- name: MongoDb__ConnectionString
valueFrom:
configMapKeyRef:
name: my-config
key: MongoDb__ConnectionString
Using Secrets for Sensitive Data
Sensitive information, such as connection strings, should be stored in a Kubernetes Secret instead of a ConfigMap.
Creating a Secret
Here’s how to create a Secret for the MongoDB connection string:
apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque
data:
MongoDb__ConnectionString: bW9uZ29kYjovL3Jvb3Q6bXlwYXNzd29yZEAtbW9uZ29kYi5zZXJ2ZXJuYW1lOjI3MDE3
Note: The value for
MongoDb__ConnectionStringmust be base64-encoded. Use the following command to encode your connection string:echo -n "mongodb://root:mypassword@mongodb.servername:27017" | base64
Using Secrets in Deployments
Reference the Secret in your Kubernetes deployment:
env:
- name: MongoDb__ConnectionString
valueFrom:
secretKeyRef:
name: my-secret
key: MongoDb__ConnectionString
Verifying the Configuration in .NET
Inject IOptions<NoSqlDatabaseConfiguration> or IOptionsMonitor<NoSqlDatabaseConfiguration> to verify the configuration:
public class MyService
{
private readonly NoSqlDatabaseConfiguration _config;
public MyService(IOptions<NoSqlDatabaseConfiguration> options)
{
_config = options.Value;
}
public void DisplayConfig()
{
Console.WriteLine($"Connection String: {_config.ConnectionString}");
Console.WriteLine($"Database Name: {_config.DatabaseName}");
}
}
Conclusion
Using Kubernetes ConfigMaps and Secrets with double underscores (__) ensures compatibility with .NET applications and allows you to securely and effectively manage configuration. With these steps, your application is better equipped for production-grade deployments in Kubernetes.
For more best practices, visit the Kubernetes Documentation.
