How do I inject environment variables into AI containers with ConfigMap?
In this guide, we will demonstrate how to inject environment variables into Kubernetes AI containers using a ConfigMap with Pulumi. ConfigMaps are a Kubernetes resource used to store non-confidential data in key-value pairs. They can be used to inject environment variables into your containerized applications.
Steps:
- Create a ConfigMap to store environment variables.
- Deploy a Pod that uses the ConfigMap to set environment variables.
Below is the Pulumi program written in TypeScript to accomplish this:
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
// Create a ConfigMap with environment variables
const configMap = new k8s.core.v1.ConfigMap("env-config", {
metadata: {
name: "env-config",
},
data: {
"ENV_VAR_1": "value1",
"ENV_VAR_2": "value2",
},
});
// Deploy a Pod that uses the ConfigMap to set environment variables
const pod = new k8s.core.v1.Pod("ai-pod", {
metadata: {
name: "ai-pod",
},
spec: {
containers: [{
name: "ai-container",
image: "your-ai-image:latest",
env: [
{
name: "ENV_VAR_1",
valueFrom: {
configMapKeyRef: {
name: configMap.metadata.name,
key: "ENV_VAR_1",
},
},
},
{
name: "ENV_VAR_2",
valueFrom: {
configMapKeyRef: {
name: configMap.metadata.name,
key: "ENV_VAR_2",
},
},
},
],
}],
},
});
Key Points:
- ConfigMap: Used to store environment variables in key-value pairs.
- Pod: Deploys a container and uses the ConfigMap to set environment variables.
- Environment Variables: Injected into the container using
valueFrom
withconfigMapKeyRef
.
Summary:
We created a ConfigMap to store environment variables and deployed a Pod that uses this ConfigMap to set environment variables for its container. This approach allows you to manage environment variables separately from your container images, making your deployments more flexible and easier to manage.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.