ConfigMap linked to POD , deployment - unix1998/technical_notes GitHub Wiki
A ConfigMap in Kubernetes and OpenShift is used to store configuration data that can be consumed by pods or deployments. However, creating a ConfigMap alone does not apply it to any pods or deployments. You need to reference the ConfigMap in your pod or deployment YAML file to make use of it.
Here’s how you can link a ConfigMap to a Deployment or Pod YAML file:
Step 1: Create a ConfigMap
First, create a ConfigMap YAML file. For example, let's create a ConfigMap that stores some configuration data:
apiVersion: v1
kind: ConfigMap
metadata:
name: example-config
data:
example.property: "example value"
another.property: "another value"
Apply this ConfigMap with the following command:
oc apply -f configmap.yaml
Step 2: Link ConfigMap in a Deployment YAML
Next, you can reference this ConfigMap in your Deployment YAML file. Here’s an example of how to do this:
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-deployment
spec:
replicas: 3
selector:
matchLabels:
app: example
template:
metadata:
labels:
app: example
spec:
containers:
- name: example-container
image: nginx
env:
- name: EXAMPLE_PROPERTY
valueFrom:
configMapKeyRef:
name: example-config
key: example.property
- name: ANOTHER_PROPERTY
valueFrom:
configMapKeyRef:
name: example-config
key: another.property
Step 3: Link ConfigMap in a Pod YAML
If you want to link a ConfigMap directly in a Pod YAML file, you can do it similarly:
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: example-container
image: nginx
env:
- name: EXAMPLE_PROPERTY
valueFrom:
configMapKeyRef:
name: example-config
key: example.property
- name: ANOTHER_PROPERTY
valueFrom:
configMapKeyRef:
name: example-config
key: another.property
Applying the Deployment or Pod
Apply the Deployment or Pod YAML file with the following command:
oc apply -f deployment.yaml
or
oc apply -f pod.yaml
Volumes
In addition to environment variables, you can also mount ConfigMaps as files inside the containers:
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-deployment
spec:
replicas: 3
selector:
matchLabels:
app: example
template:
metadata:
labels:
app: example
spec:
containers:
- name: example-container
image: nginx
volumeMounts:
- name: config-volume
mountPath: /etc/config
subPath: example.property
volumes:
- name: config-volume
configMap:
name: example-config
This example mounts the example.property
key from the example-config
ConfigMap as a file in /etc/config/example.property
.
In summary, to use a ConfigMap, you need to reference it in your Deployment or Pod YAML file. This can be done using environment variables or by mounting the ConfigMap as a volume. Once you link the ConfigMap to your deployment or pod, you can apply the deployment or pod configuration, and the containers will have access to the configuration data stored in the ConfigMap.