هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
kubectl create configmap api-config -n shop --from-literal=LOG_LEVEL=info --from-literal=FEATURE_NEW_CHECKOUT=true --dry-run=client -o yaml > api-config.yaml
kubectl apply -f api-config.yaml containers:
- name: api
envFrom:
- configMapRef:
name: api-configkubectl rollout restart deploy/api -n shopStep by step
- Create it as YAML (keep it in Git; not secrets — those go into Secrets):
apiVersion: v1
kind: ConfigMap
metadata:
name: api-config
namespace: shop
data:
LOG_LEVEL: info
FEATURE_NEW_CHECKOUT: "true"
app.yaml: |
cache:
ttl: 300
payments:
provider: stripeValues are always strings — quote "true" and numbers.
- As environment variables — all keys (
envFrom), or one key:
env:
- name: LOG_LEVEL
valueFrom:
configMapKeyRef: { name: api-config, key: LOG_LEVEL }- As files — every key becomes a file in the folder:
volumeMounts:
- name: config
mountPath: /etc/shop
readOnly: true
volumes:
- name: config
configMap:
name: api-config
items:
- key: app.yaml
path: app.yamlThe app reads /etc/shop/app.yaml.
- From existing files (an
nginx.conf, a folder of configs):
kubectl create configmap nginx-conf -n shop --from-file=nginx.conf --dry-run=client -o yaml > nginx-conf.yaml- Changes and restarts:
| Used as | After a change |
|---|---|
| Env variables | No change until the Pods restart: kubectl rollout restart deploy/api. |
| Mounted files | Updated after up to about a minute — the app must re-read them. subPath mounts are never updated. |
Kustomize's configMapGenerator (or a hash annotation in Helm) adds a hash to the name, so every change rolls the Pods automatically.
Check that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Changed ConfigMap, app still old | Env variables are read at start only. | kubectl rollout restart; or a generator with a hash name. |
| Mounted file never updates | Mounted with subPath. | Mount the whole folder, or restart the Pods. |
CreateContainerConfigError: configmap "…" not found | Wrong name or namespace. | ConfigMaps are per namespace; check the name. |
YAML error on true / numbers | Values must be strings. | Quote them: "true", "300". |
| A secret ended up in a ConfigMap | Easy to do, but readable by everyone with view. | Move it to a Secret; rotate it. |
| ConfigMap too big | Limit is 1 MiB. | Ship big files in the image or a volume. |
Go further
- Command pages: kubectl, kustomize.
- Next how-tos: Create a Kubernetes Secret safely, Use Kustomize overlays per environment, Roll back a deployment.
- Learn it: CKA — ConfigMaps and Secrets, CKA — Kustomize.
1You changed a ConfigMap used as environment variables. Why does the app still use the old value?
Env variables are read only at container start; restart the Pods.
2Should a database password be in a ConfigMap?
No — use a Secret (and restrict who can read it).