0Pricing
Docker & Kubernetes for Developers · บทเรียน

ConfigMaps และ Secrets สำหรับการกำหนดค่า

จัดการการกำหนดค่าแอปพลิเคชันและข้อมูลละเอียดอ่อนอย่างปลอดภัยด้วย ConfigMaps และ Secrets ภายใน Kubernetes

ConfigMaps และ Secrets สำหรับการกำหนดค่า เป็นบทเรียน Docker & Kubernetes for Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Docker & Kubernetes for Developers และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Docker & Kubernetes for Developers มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Configuration Challenges

Managing application configuration and sensitive data can be tricky, especially in dynamic environments like Kubernetes.

Hardcoding values into images makes them less reusable. Storing secrets directly in Git is a security risk.

Kubernetes offers two powerful resources to solve these challenges: ConfigMaps and Secrets.

Meet ConfigMaps

A ConfigMap is an API object used to store non-sensitive configuration data in key-value pairs.

Think of it as a central place for settings like database hostnames, logging levels, or API endpoints.

  • Separates configuration from application code.
  • Allows easy updates without rebuilding images.
  • Can be consumed as environment variables or mounted files.

ConfigMap: Literal Values

You can create a ConfigMap directly from literal key-value pairs using YAML. This is great for simple, direct settings.

Here's an example for an application's settings:

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-settings
data:
  log_level: INFO
  feature_flag_a: "true"
  api_url: http://backend-service/api

ConfigMap: From Files

For more complex configurations, you can create a ConfigMap from an entire file.

If you have a config.properties file, its content can be directly embedded into the ConfigMap. The resulting ConfigMap would look like this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config-from-file
data:
  config.properties: |
    database.host=db-service
    database.port=5432
    application.name=MyWebApp

ConfigMap as Env Vars

The most common way to use a ConfigMap is by injecting its data as environment variables into your Pods.

This allows your application to read configuration values directly.

apiVersion: v1
kind: Pod
metadata:
  name: my-app-pod
spec:
  containers:
  - name: my-container
    image: nginx
    env:
    - name: LOG_LEVEL
      valueFrom:
        configMapKeyRef:
          name: app-settings
          key: log_level
    - name: API_URL
      valueFrom:
        configMapKeyRef:
          name: app-settings
          key: api_url

ConfigMap as Volume Mounts

ConfigMap data can also be mounted as files into a Pod's filesystem. This is ideal for applications that read configuration from files.

Each key in the ConfigMap becomes a file in the specified mountPath. For example, log_level would be at /etc/config/log_level.

apiVersion: v1
kind: Pod
metadata:
  name: my-app-pod-volume
spec:
  containers:
  - name: my-container
    image: nginx
    volumeMounts:
    - name: config-volume
      mountPath: "/etc/config"
  volumes:
  - name: config-volume
    configMap:
      name: app-settings

Introducing Secrets

A Secret is similar to a ConfigMap but is designed for sensitive data like passwords, API keys, or TLS certificates.

Secrets are stored in Kubernetes in base64 encoded format, but this is NOT encryption. It's just encoding for safe transport.

  • Provides a mechanism to distribute sensitive data.
  • Accessed like ConfigMaps (env vars or volume mounts).
  • Should always be managed with care and access controls.

Secret: Literal Values

Secrets are created similarly to ConfigMaps, but their data values are base64 encoded. This encoding is for safe transport, not encryption.

For example, if your password is "my-secure-password", its base64 encoding is "bXktc2VjdXJlLXBhc3N3b3Jk".

apiVersion: v1
kind: Secret
metadata:
  name: db-credentials
type: Opaque
data:
  username: dXNlcg== # 'user' base64 encoded
  password: bXktc2VjdXJlLXBhc3N3b3Jk # 'my-secure-password' base64 encoded

Secret as Env Vars

Secrets can be consumed as environment variables, just like ConfigMaps. Kubernetes automatically decodes the base64 value for the container.

This makes sensitive data available to your application without hardcoding it directly in the image.

apiVersion: v1
kind: Pod
metadata:
  name: my-db-app
spec:
  containers:
  - name: my-container
    image: my-app:1.0
    env:
    - name: DB_USERNAME
      valueFrom:
        secretKeyRef:
          name: db-credentials
          key: username
    - name: DB_PASSWORD
      valueFrom:
        secretKeyRef:
          name: db-credentials
          key: password

Secret as Volume Mounts

Mounting Secrets as files is often preferred for sensitive data, as it limits the exposure of the secret to the application process.

The mounted files will contain the decoded (plain-text) secret values, and you can set them to be read-only.

apiVersion: v1
kind: Pod
metadata:
  name: my-secure-app
spec:
  containers:
  - name: my-container
    image: my-app:1.0
    volumeMounts:
    - name: secret-volume
      mountPath: "/etc/secrets"
      readOnly: true
  volumes:
  - name: secret-volume
    secret:
      secretName: db-credentials

ConfigMaps vs. Secrets

You need to store an API key for a third-party service and a configuration setting for your application's logging level. Which Kubernetes resources would you use for each?

Recap: Config & Secrets

We've learned how ConfigMaps and Secrets help manage configuration and sensitive data in Kubernetes.

  • ConfigMaps store non-sensitive key-value pairs.
  • Secrets store sensitive data, base64 encoded (not encrypted).
  • Both can be consumed as environment variables or mounted files in Pods.
  • Using them separates configuration from application logic, improving flexibility and security.

คำถามที่พบบ่อย

บทเรียน “ConfigMaps และ Secrets สำหรับการกำหนดค่า” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ConfigMaps และ Secrets สำหรับการกำหนดค่า” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Docker & Kubernetes for Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Docker & Kubernetes for Developers มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ConfigMaps และ Secrets สำหรับการกำหนดค่า”

จัดการการกำหนดค่าแอปพลิเคชันและข้อมูลละเอียดอ่อนอย่างปลอดภัยด้วย ConfigMaps และ Secrets ภายใน Kubernetes คุณปฏิบัติ Docker & Kubernetes for Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Docker & Kubernetes for Developers หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Docker & Kubernetes for Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “ConfigMaps และ Secrets สำหรับการกำหนดค่า” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Docker & Kubernetes for Developers นี้ได้ไหม

ได้ บทเรียน Docker & Kubernetes for Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. วอลุ่มถาวรและคำขอวอลุ่มถาวร
  2. การจัดการแอปพลิเคชันแบบมีสถานะด้วย StatefulSets
  3. ConfigMaps และ Secrets สำหรับการกำหนดค่า
  4. คลาสพื้นที่จัดเก็บและการจัดสรรแบบไดนามิก
← กลับไปที่ Docker & Kubernetes for Developers