자격증 학원 › CKA › 워크로드 · 스케줄링

강의 9 / 28 · 약 20분

ConfigMap 과 Secret

설정을 이미지 밖으로 — 환경변수로 주입하기, 파일로 마운트하기, Secret 의 base64 는 암호화가 아니라는 사실.

이 강의를 마치면

  • ConfigMap · Secret 을 명령형으로 만들 수 있다
  • 값을 환경변수(env · envFrom)와 볼륨 파일로 주입할 수 있다
  • Secret 값을 꺼내 디코드할 수 있고, base64 가 암호화가 아님을 안다

만들기

# ConfigMap
kubectl create configmap app-config --from-literal=APP_MODE=prod --from-literal=LOG=info
kubectl create configmap nginx-conf --from-file=nginx.conf          # 키 = 파일 이름
kubectl create configmap nginx-conf --from-file=main.conf=nginx.conf   # 키 이름 지정
kubectl create configmap env-cfg --from-env-file=app.env            # KEY=VALUE 줄들

# Secret
kubectl create secret generic db-cred --from-literal=user=admin --from-literal=password='S3cr3t!'
kubectl create secret tls web-tls --cert=tls.crt --key=tls.key
kubectl create secret docker-registry regcred --docker-server=… --docker-username=… --docker-password=…

셸에서 ! · $ 같은 특수문자가 든 값은 작은따옴표로 감쌉니다.

Secret 값 보기

kubectl get secret db-cred -o yaml
kubectl get secret db-cred -o jsonpath='{.data.password}' | base64 -d

base64 는 암호화가 아닙니다. 인코딩일 뿐이라 누구나 되돌립니다. Secret 을 지키는 것은 ① RBAC(누가 get secrets 를 할 수 있나)와 ② etcd 저장 시 암호화 설정입니다. YAML 로 직접 쓸 때는 stringData: 를 쓰면 평문으로 적어도 저장할 때 base64 로 바뀝니다.

주입 방법 1 — 환경변수

spec:
  containers:
  - name: app
    image: busybox:1.37
    command: ["sleep", "3600"]
    env:
    - name: APP_MODE                  # 키 하나씩
      valueFrom:
        configMapKeyRef:
          name: app-config
          key: APP_MODE
    - name: DB_PASSWORD
      valueFrom:
        secretKeyRef:
          name: db-cred
          key: password
    envFrom:                          # 전부 한꺼번에
    - configMapRef:
        name: app-config
    - secretRef:
        name: db-cred
      prefix: DB_                     # 이름 앞에 붙일 접두어(선택)

주입 방법 2 — 볼륨(파일)

spec:
  containers:
  - name: web
    image: nginx:1.27
    volumeMounts:
    - name: cfg
      mountPath: /etc/app            # 키마다 파일 하나: /etc/app/APP_MODE
      readOnly: true
    - name: conf
      mountPath: /etc/nginx/conf.d/main.conf
      subPath: main.conf             # 폴더를 덮지 않고 파일 하나만 끼워 넣기
  volumes:
  - name: cfg
    configMap:
      name: app-config
  - name: conf
    configMap:
      name: nginx-conf
      items:                         # 원하는 키만, 원하는 파일 이름으로
      - key: main.conf
        path: main.conf
  - name: secret-vol
    secret:
      secretName: db-cred            # ⚠️ Secret 은 name 이 아니라 secretName

Secret 볼륨은 secretName, ConfigMap 볼륨은 name — 시험장 단골 오타입니다.

확인

kubectl exec app -- env | grep APP_MODE
kubectl exec app -- cat /etc/app/APP_MODE
kubectl exec app -- ls /etc/app

갱신과 불변 설정

  • 볼륨으로 마운트한 값은 ConfigMap 을 고치면 잠시 뒤 파일이 바뀝니다(subPath 로 마운트한 파일은 제외).
  • 환경변수는 컨테이너가 시작될 때 한 번 읽습니다. 바꾸려면 kubectl rollout restart deployment ….
  • immutable: true 를 주면 내용을 바꿀 수 없습니다(실수 방지·성능). 바꾸려면 지우고 새로 만듭니다.
  • 참조하는 ConfigMap 이 없으면: 환경변수 참조 → CreateContainerConfigError, 볼륨 참조 → ContainerCreating + FailedMount. 없는 것을 만들어 주면 kubelet 이 곧 다시 시도합니다.

⌨️ 직접 해 보기

브라우저 실습 터미널에서 풀고 자동 채점을 받습니다.

✅ 확인 문제

과정 목차