I’m using kubectl to deploy ASP.Net core apps to K8S cluster.
At the current moment I hardcode container PORTs and ConnectionString for DataBase like this:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mydeploy
spec:
replicas: 1
selector:
matchLabels:
app: mydeploy
template:
metadata:
labels:
app: mydeploy
spec:
containers:
- name: mydeploy
image: harbor.com/my_app:latest
env:
- name: MyApp__ConnectionString
value: "Host=postgres-ap-svc;Port=5432;Database=db;Username=postgres;Password=password"
ports:
- containerPort: 5076
But the good solution I think – to use such variables from appsettings.json
file (ASP.Net config file).
So my question – How to load this vars from JSON file and use in yaml file?
JSON file is like this:
{
"MyApp": {
"ConnectionString": "Host=postgres-ap-svc;Port=5432;Database=db;Username=postgres;Password=password"
},
"ClientBaseUrls": {
"MyApp": "http://mydeploy-svc:5076/api",
}
}
3
Answers
You can use following command to create configmap and then mount it to your contianer
For mounting you should add a volume to your deployment or sts like this:
And then mount it to your container:
Also if you want you can use the config map as your environment variables source like this:
I’m putting all the comments as a community wiki answer for better usability.
If you run
printenv
in a container and see:appsettings.json={ "MyApp": { "ConnectionString": "Host=postgres-ap-svc;Port=5432;Database=db;Username=postgres;Password=password" }, "ClientBaseUrls": { "MyApp": "http://mydeploy-svc:5076/api", } }
instead of something like:
"MyApp__ConnectionString":"..."
and"ClientBaseUrls__MyApp":"..."
you should create the configmap using
--from-env-file
option instead.For example:
kubectl create configmap appsettings --from-env-file=appsettings.json --dry-run -o yaml
And useenvFrom
like mentioned in the post kubernetes.io/docs/tasks/configure-pod-container/.You can try using literal mode:
If this doesn’t work you may need to write separate JSON-YAML parser.