ConfigMaps
ConfigMaps allow you to decouple configuration artifacts from image content to keep containerized applications portable. They store non-confidential data in key-value pairs and can be consumed by pods as environment variables, command-line arguments, or configuration files.
ConfigMaps provide:
- Configuration Management: Store application configuration separately from code
- Environment Flexibility: Use different configurations for different environments
- Runtime Updates: Update configuration without rebuilding container images
- Portability: Keep applications portable across different environments
In this lab, you'll learn about ConfigMaps by creating one for our retail store's UI component and seeing how it connects to backend services.
Creating ConfigMap
Let's create a ConfigMap for our retail store's UI component. The UI needs to know where to find the backend services:
apiVersion: v1
kind: ConfigMap
metadata:
name: ui
data:
RETAIL_UI_ENDPOINTS_CATALOG: http://catalog.catalog.svc:80
RETAIL_UI_ENDPOINTS_CARTS: http://carts.carts.svc:80
RETAIL_UI_ENDPOINTS_ORDERS: http://orders.orders.svc:80
RETAIL_UI_ENDPOINTS_CHECKOUT: http://checkout.checkout.svc:80
kind: ConfigMap: Tells Kubernetes what type of resource to create
metadata.name: Unique identifier for this ConfigMap within the namespace
data: Key-value pairs containing the configuration data
Apply the ConfigMap configuration:
Exploring ConfigMap
Now let's examine the ConfigMap we just created:
NAME DATA AGE
kube-root-ca.crt 1 2m51s
ui 4 2m50s
Get detailed information about the ConfigMap:
Name: ui
Namespace: ui
Labels: <none>
Annotations: <none>
Data
====
RETAIL_UI_ENDPOINTS_CARTS:
----
http://carts.carts.svc:80
RETAIL_UI_ENDPOINTS_CATALOG:
----
http://catalog.catalog.svc:80
RETAIL_UI_ENDPOINTS_CHECKOUT:
----
http://checkout.checkout.svc:80
RETAIL_UI_ENDPOINTS_ORDERS:
----
http://orders.orders.svc:80
BinaryData
====
Events: <none>
This shows:
- Data section - The key-value pairs stored in the ConfigMap
- Labels - Metadata tags for organization
- Annotations - Additional metadata
Using ConfigMaps in Pods
Now let's create a pod that uses our ConfigMap. We'll update our UI pod to use the configuration:
apiVersion: v1
kind: Pod
metadata:
name: ui-pod
namespace: ui
labels:
app.kubernetes.io/name: ui
app.kubernetes.io/component: service
app.kubernetes.io/created-by: eks-workshop
spec:
containers:
- name: ui
image: public.ecr.aws/aws-containers/retail-store-sample-ui:0.4.0
ports:
- containerPort: 8080
envFrom:
- configMapRef:
name: ui
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "200m"
envFrom.configMapRef: Loads all key-value pairs from the ConfigMap as environment variables
Apply the updated pod configuration:
Testing the Configuration
Let's verify that our pod can now access the configuration:
RETAIL_UI_ENDPOINTS_CATALOG=http://catalog.catalog.svc:80
You can also see all the ConfigMap environment variables:
RETAIL_UI_ENDPOINTS_CATALOG=http://catalog.catalog.svc:80
RETAIL_UI_ENDPOINTS_CARTS=http://carts.carts.svc:80
RETAIL_UI_ENDPOINTS_ORDERS=http://orders.orders.svc:80
RETAIL_UI_ENDPOINTS_CHECKOUT=http://checkout.checkout.svc:80
Key Points to Remember
- ConfigMaps store non-confidential configuration data
- They decouple configuration from container images
- Can be consumed as environment variables or mounted as files
- Allow the same image to work across different environments
- Have a 1MB size limit per ConfigMap