# Use Environment Variables to Customize Microservices
This tutorial describes how to use environment variables to customize the behavior of microservices. Using environment variables as a configuration mechanism allows the same Linux image to run in development and production environments. This is especially useful when managing sensitive information via Kubernetes Secrets.
The entando.json
file makes environment variables available to a microservice in a docker-based bundle. There are two options:
- Inject the variables using a key-value pair
- Inject a reference to an existing Kubernetes Secret
This tutorial will demonstrate both of these options. The microservice will receive one environment value directly as plain text in the pod YAML and the other indirectly through a referenced Secret.
# Prerequisites
# Add Environment Variables to the Microservice
- To determine YOUR-BUNDLE-ID, run the following command. Supply the full bundle URL, remembering to update the placeholders with your Docker organization and your bundle name.
ent ecr get-bundle-id docker://registry.hub.docker.com/YOUR-ORG/YOUR-BUNDLE
- Create a Secret named
YOUR-BUNDLE-ID-my-secret
with key-value pairmySecretKey=mySecretValue
. Replace YOUR-BUNDLE-ID with the output of the previous step.
ent kubectl create secret generic YOUR-BUNDLE-ID-my-secret --from-literal=mySecretKey=mySecretValue -n entando
- Insert the following
env
section into the microservice inentando.json
, remembering to replace YOUR-BUNDLE-ID. By convention, environment variables are all caps and K8s resource names are hyphenated.
"env": [
{ "name": "SIMPLE_VAR",
"value": "mySimpleValue"
},
{ "name": "SECRET_VAR",
"secretKeyRef": {
"name": "YOUR-BUNDLE-ID-my-secret",
"key": "mySecretKey"
}
}
]
- Build and deploy the updated bundle
# Verify the Environment Variables
Retrieve your microservice pod name. Use
ent kubectl get pods
or your cluster management tool to find the name.The pod name begins with 'pn', followed by two generated alpha-numeric strings, your docker organization name, microservice name, etc.
E.g. YOUR-MICROSERVICE-POD-NAME =pn-ccfcefa6-615bc3ba-dockerorg-conference-ms-deploymentvpsgj
Shell into the pod, using the name from above:
ent kubectl exec -it YOUR-MICROSERVICE-POD-NAME -- /bin/bash
- Confirm the environment variables are present:
echo SIMPLE_VAR=$SIMPLE_VAR, SECRET_VAR=$SECRET_VAR
Expected output:
SIMPLE_VAR=mySimpleValue, SECRET_VAR=mySecretValue
Congratulations!
You have learned how to use environment variables with microsevices on the Entando Platform!