متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 133Kubernetes · Workloads

How to use a ConfigMap for app settings

The same image should run in dev, staging and production — only the settings differ (log level, feature flags, an Nginx config file). A ConfigMap holds those settings outside the image, as environment variables or as files. This how-to creates one, uses it both ways, and solves the classic surprise: you changed the ConfigMap and the app did not notice.

  • 5 الدروس
  • 2 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

133.1

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
deployment.yaml (part)
      containers:
        - name: api
          envFrom:
            - configMapRef:
                name: api-config
kubectl rollout restart deploy/api -n shop

الأوامر في هذا الدرس

133.2

Step by step

  1. Create it as YAML (keep it in Git; not secrets — those go into Secrets):
api-config.yaml
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: stripe

Values are always strings — quote "true" and numbers.

  1. As environment variables — all keys (envFrom), or one key:
deployment.yaml (part)
          env:
            - name: LOG_LEVEL
              valueFrom:
                configMapKeyRef: { name: api-config, key: LOG_LEVEL }
  1. As files — every key becomes a file in the folder:
deployment.yaml (part)
          volumeMounts:
            - name: config
              mountPath: /etc/shop
              readOnly: true
      volumes:
        - name: config
          configMap:
            name: api-config
            items:
              - key: app.yaml
                path: app.yaml

The app reads /etc/shop/app.yaml.

  1. 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
  1. Changes and restarts:
Used asAfter a change
Env variablesNo change until the Pods restart: kubectl rollout restart deploy/api.
Mounted filesUpdated 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.

الأوامر في هذا الدرس

133.3

Check that it worked

kubectl get configmap api-config -n shop -o yaml
kubectl exec deploy/api -n shop -- printenv LOG_LEVEL
kubectl exec deploy/api -n shop -- cat /etc/shop/app.yaml

The Pod sees the values you set.

الأوامر في هذا الدرس

133.4

Common problems

ProblemCauseFix
Changed ConfigMap, app still oldEnv variables are read at start only.kubectl rollout restart; or a generator with a hash name.
Mounted file never updatesMounted with subPath.Mount the whole folder, or restart the Pods.
CreateContainerConfigError: configmap "…" not foundWrong name or namespace.ConfigMaps are per namespace; check the name.
YAML error on true / numbersValues must be strings.Quote them: "true", "300".
A secret ended up in a ConfigMapEasy to do, but readable by everyone with view.Move it to a Secret; rotate it.
ConfigMap too bigLimit is 1 MiB.Ship big files in the image or a volume.

الأوامر في هذا الدرس

133.5

Go further

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).

كل الأوامر في هذا الفصل