ConfigMap creation methods¶
ConfigMap is a Kubernetes object used to decploy configuration artifacts from image content, allowing for the separation of code and configuration.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md] It functions as a centralized storage for one or more configuration files or key-value pairs, which can be injected into containers at runtime.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md] This abstraction reduces code coupling, as the same container image can be deployed across different environments (e.g., Development, Production) simply by swapping the associated ConfigMap.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md]
There are several primary methods to create a ConfigMap, ranging from imperative CLI commands to declarative YAML manifests.
1. Importing a file from the CLI¶
You can create a ConfigMap directly from an existing file using the kubectl create configmap command with the --from-file flag.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md] This is useful for injecting configuration scripts or properties files.
Syntax:
[kubectl](<./kubectl.md>) create [ConfigMap](<./configmap.md>) <map-name> --from-file=<filename>
Example:
Creating a map named pg-initsql from a file named initdb.sql.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md]
[kubectl](<./kubectl.md>) create [ConfigMap](<./configmap.md>) pg-initsql --from-file=initdb.sql
2. Creating Key-Value pairs from the CLI¶
Using the --from-literal flag allows you to define specific key-value pairs without needing a source file.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md] This method is ideal for simple environment variables or connection strings.
Syntax:
[kubectl](<./kubectl.md>) create [ConfigMap](<./configmap.md>) <map-name> --from-literal=<key>=<value>
Example:
Creating a map named pg-connect with database host and port information.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md]
[kubectl](<./kubectl.md>) create [ConfigMap](<./configmap.md>) pg-connect \
--from-literal=host=127.0.0.1 \
--from-literal=port=5432
3. Defining files via YAML¶
For a declarative Infrastructure as Code approach, you can define a ConfigMap in a YAML manifest.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md] This method uses the data field to store file content, often utilizing a pipe symbol (|) to handle multi-line strings while preserving formatting.
Example:
Creating initdb-yaml with an embedded SQL script.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md]
apiVersion: v1
kind: [ConfigMap](<./configmap.md>)
metadata:
name: initdb-yaml
labels:
app: db
data:
initdb.sql: |
DROP TABLE IF EXISTS posts CASCADE;
CREATE TABLE posts ( ... );
Once defined, the resource is applied using:
[kubectl](<./kubectl.md>) apply -f initdb-configmap.yaml
4. Defining Key-Value pairs via YAML¶
Similar to the CLI literal method, YAML allows you to explicitly list key-value pairs under the data section.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md]
Example:
Creating initdb-kv-yaml with user credentials.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md]
apiVersion: v1
kind: [ConfigMap](<./configmap.md>)
metadata:
name: initdb-kv-yaml
labels:
app: db
data:
PG_USER: postgres
PG_PASSWORD: postgres
Application in Pods¶
After creation, ConfigMaps are consumed by Pods primarily in two ways:
1. Environment Variables: Injecting specific keys as environment variables using configMapKeyRef.
2. Mounted Volumes: Mounting the entire ConfigMap or specific keys as files at a specific path (e.g., /docker-entrypoint-initdb.d) using the volumes and volumeMounts fields.^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md]
Sources¶
- ^[400-devops-06-kubernetes-k8s-ithelp-day18-readme.md]