학습 내용

Secret이란?

컨테이너가 사용하는 password, auth token, ssh key 같은 중요한 정보를 저장하는 리소스. 민감한 구성 정보를 base64로 인코딩해서 한 곳에 모아 관리한다.

ConfigMap vs Secret:

  • ConfigMap: 민감하지 않은 일반 설정 파일
  • Secret: 민감한 데이터 (비밀번호, 토큰, 키 등)

Secret 데이터 전달 방법

  1. Environment Variable (환경변수)
  2. Volume Mount (파일로 마운트)
  3. Command-line Argument

Secret 생성

kubectl create secret generic NAME --from-literal=key1=value1
kubectl create secret generic NAME --from-file=source

Available Commands:

  • generic: 일반 목적의 secret
  • docker-registry: Docker 레지스트리 인증 정보
  • tls: TLS 인증서

Secret 사용 방식

1. 환경변수로 전달 (secretKeyRef) ConfigMap의 configMapKeyRef와 동일한 구조. secretKeyRef로 변경하면 된다.

2. 볼륨 마운트로 전달 ConfigMap의 볼륨 마운트와 동일한 구조. configMap: 대신 secret:으로 변경.

Secret 용량 제한

Secret은 etcd에 저장되므로, value가 커지면 메모리 사용량이 증가한다. Secret의 최대 크기는 1MB.


실습 예제 (GitHub 237summit)

Secret을 환경변수로 전달 — genid-env-secret.yaml

apiVersion: v1
kind: Pod
metadata:
  name: genid-env-secret
spec:
  containers:
  - image: smlinux/genid:env
    env:
    - name: INTERVAL
      valueFrom:
        secretKeyRef:
          name: ttabae-secret
          key: INTERVAL
    name: fakeid-generator
    volumeMounts:
    - name: html
      mountPath: /webdata
  - image: nginx:1.14
    name: web-server
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
      readOnly: true
    ports:
    - containerPort: 80
  volumes:
  - name: html
    emptyDir: {}

ConfigMap 예제(genid.yaml)와 비교하면, configMapKeyRefsecretKeyRef, ttabae-configttabae-secret으로 변경된 것만 다르다.

Secret을 볼륨 마운트로 전달 — genid-volume-secret.yaml

apiVersion: v1
kind: Pod
metadata:
  name: genid-volume-secret
spec:
  containers:
  - image: smlinux/genid:env
    env:
    - name: INTERVAL
      valueFrom:
        secretKeyRef:
          name: ttabae-secret
          key: INTERVAL
    name: fakeid-generator
    volumeMounts:
    - name: html
      mountPath: /webdata
  - image: nginx:1.14
    name: web-server
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
      readOnly: true
    - name: config
      mountPath: /etc/nginx/conf.d
      readOnly: true
    ports:
    - containerPort: 80
  volumes:
  - name: html
    emptyDir: {}
  - name: config
    secret:
      secretName: ttabae-secret
      items:
      - key: nginx-config.conf
        path: nginx-config.conf

ConfigMap 볼륨 예제와 비교하면, configMap:secret:, name:secretName:으로 변경된 것만 다르다.

Secret용 nginx 설정 — genid-web-config/nginx-config.conf

server {
    listen   80;
    server_name  www.example.com;
 
    gzip on;
    gzip_types text/plain application/xml;
 
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

ConfigMap vs Secret 비교 요약

구분ConfigMapSecret
용도일반 설정 정보민감한 정보 (비밀번호, 토큰 등)
인코딩평문base64 인코딩
환경변수 refconfigMapKeyRefsecretKeyRef
전체 주입configMapRef + envFromsecretRef + envFrom
볼륨 키configMap:name:secret:secretName:
크기 제한1MB1MB
저장소etcdetcd (암호화 미적용 시 평문)

핵심 정리

  • Secret = 민감한 구성 정보를 base64 인코딩하여 관리. ConfigMap과 사용법은 거의 동일.
  • Secret은 etcd에 저장되며, 기본적으로 암호화되지 않음. etcd 암호화 설정을 별도로 해야 안전.
  • 환경변수 방식: secretKeyRef 사용. 볼륨 마운트 방식: secret: + secretName: 사용.
  • ConfigMap → Secret 전환 시 YAML에서 바꿔야 할 부분: configMapKeyRefsecretKeyRef, configMap:secret:, name:secretName:
  • Secret 최대 크기는 1MB.

체크리스트

  • Secret을 생성하는 방법(generic, docker-registry, tls)을 구분할 수 있는가?
  • ConfigMap YAML을 Secret YAML로 전환할 때 변경 포인트를 알고 있는가?
  • Secret이 etcd에 평문 저장되는 보안 이슈를 인지하고 있는가?
  • base64 인코딩/디코딩 명령어를 사용할 수 있는가? (echo -n 'value' | base64)

참고 링크