ENع
→K8s config
الدرس 11 · الحاويات وKubernetes

Config, secrets and storage in Kubernetes

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةIntermediate
عن هذا الدرس

A good container image is the same in testing and in production. What changes between them lives outside the image. Kubernetes keeps plain settings in a ConfigMap, passwords and keys in a Secret, and data that must outlive a Pod on a PersistentVolume, reached through a PersistentVolumeClaim. Follow a six-line Python app as it reads a setting, reads a password, writes to a volume, loses its Pod without losing its data, and picks up a changed setting after a restart. Each new Pod adds one line to the same file.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 11 · تفاعلي
01 / 10

Keep the image the same

Build one container image and run it everywhere. What differs between test and production, the settings, the passwords and the saved data, lives outside the image, in Kubernetes objects that the Pod plugs into.

one image, everywhere

settings        → ConfigMap
passwords, keys → Secret
data that lasts → PersistentVolume
                  (through a claim)
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Keep the image the same

    Build one container image and run it everywhere. What differs between test and production, the settings, the passwords and the saved data, lives outside the image, in Kubernetes objects that the Pod plugs into.

    one image, everywhere
    
    settings        → ConfigMap
    passwords, keys → Secret
    data that lasts → PersistentVolume
                      (through a claim)
  2. A ConfigMap holds settings

    A ConfigMap is a Kubernetes object that stores plain settings as key-value pairs. The kubectl create configmap command makes one, named app-config, with one key: GREETING=Hello. It is not secret: anyone allowed to read it sees the values.

    kubectl create configmap app-config --from-literal=GREETING=Hello
    
    ConfigMap app-config
      GREETING: Hello
    
    plain, non-secret settings · up to 1 MiB
  3. Settings arrive as variables

    When the Pod starts, Kubernetes copies GREETING into the container as an environment variable. Line 2 reads it with os.environ. The image never mentions Hello, so the same image can greet differently elsewhere.

    env:
    - name: GREETING
      valueFrom:
        configMapKeyRef:
          name: app-config
          key: GREETING
    
    greeting = os.environ["GREETING"]
      → greeting = "Hello"
  4. A Secret holds a password

    A Secret holds small sensitive values, such as a database password. Its values are stored base64-encoded: s3cr3t becomes czNjcjN0. Base64 is not encryption. By default Secrets sit unencrypted in etcd, the cluster's database, so limit who can read them.

    kubectl create secret generic db --from-literal=password=s3cr3t
    
    Secret db
      password: czNjcjN0   (base64 of "s3cr3t")
    
    base64 hides nothing: anyone can decode it
    by default stored unencrypted in etcd
  5. The Secret becomes a file

    The Secret is mounted into the Pod as a file, /etc/db/password, held in memory (tmpfs) and never written to the node's disk. Line 3 opens it like any file. The password is never baked into the image.

    volumes:
    - name: db
      secret:
        secretName: db
    volumeMounts:
    - name: db
      mountPath: /etc/db
      readOnly: true
    
    password = open("/etc/db/password").read()
      → password = "s3cr3t"  (decoded for you)
  6. Claim a lasting volume

    Files written inside a container are lost when it stops. For data that must last, the Pod uses a PersistentVolumeClaim asking for 1Gi, ReadWriteOnce. Kubernetes binds it, one to one, to a PersistentVolume: a piece of real storage.

    kind: PersistentVolumeClaim
    spec:
      accessModes: [ReadWriteOnce]
      resources:
        requests:
          storage: 1Gi
    
    claim data  ⇄  PersistentVolume (1Gi)
    ReadWriteOnce: read-write from one node
  7. Write to the volume

    The volume is mounted in the Pod at /data. Lines 4 and 5 open /data/visits.txt for appending and add the greeting. The file lives on the PersistentVolume, not inside the container, and now holds one line: Hello.

    volumes:
    - name: data
      persistentVolumeClaim:
        claimName: data
    volumeMounts:
    - name: data
      mountPath: /data
    
    with open("/data/visits.txt", "a") as f:
        f.write(greeting + "\n")
      /data/visits.txt:  Hello
  8. The Pod goes, the data stays

    Now delete the Pod. Its Deployment starts a replacement, web-2, and the same claim mounts the same volume again. Files inside the old container are gone, but line 1 is still there, and web-2 runs the app and adds its own Hello.

    kubectl delete pod web-1
    
    web-1  deleted (its container files are lost)
    web-2  started by the Deployment
           claim data → the same volume
           runs the app once: appends a line
    /data/visits.txt:  Hello  ✓ still there
                       Hello  (from web-2)
  9. Change a setting, restart

    Edit the ConfigMap to GREETING=Salaam. Environment variables are read only when a container starts, so run a rollout restart. The new Pod, web-3, reads Salaam and appends it: the file holds all three lines. The image never changed.

    kubectl edit configmap app-config   (GREETING: Salaam)
    kubectl rollout restart deployment/web
    
    env vars are not updated in a running Pod
    (a ConfigMap mounted as files is, after a delay)
    
    web-3: greeting = "Salaam"
    /data/visits.txt:  Hello / Hello / Salaam
  10. Three kinds of outside

    Settings go in a ConfigMap, passwords and keys in a Secret, and data that must outlive a Pod on a PersistentVolume, reached through a claim. You change any of them without rebuilding the image.

    ConfigMap         env vars or files · plain
    Secret            env vars or files · sensitive
    PVC → PV          a mounted folder · lasts
    
    the image stays the same everywhere

اختبار يتاح بعد الخطوة الأخيرة.