Post

Part 5: ConfigMap과 Secret - 설정 및 민감정보 관리

Part 5: ConfigMap과 Secret - 설정 및 민감정보 관리

Part 5: 설정 및 데이터 관리

15. ConfigMap과 Secret

15.1 ConfigMap (설정 관리)

용도:

  • 애플리케이션 설정
  • 환경 변수
  • 설정 파일

ConfigMap 생성 방법:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 1. literal 값으로 생성
kubectl create configmap app-config \
  --from-literal=ENV=production \
  --from-literal=LOG_LEVEL=info

# 2. 파일에서 생성
kubectl create configmap app-config \
  --from-file=app.properties \
  --from-file=config/

# 3. env 파일에서 생성
kubectl create configmap app-config \
  --from-env-file=.env.production

# 4. YAML로 생성
kubectl apply -f configmap.yaml

ConfigMap YAML 예시:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
  namespace: default
data:
  # Key-value 형식
  server.port: "8080"
  server.timeout: "30"
  ENV: "production"
  LOG_LEVEL: "info"
  # 파일 형식
  application.yaml: |
    server:
      port: 8080
      ssl: true
    database:
      host: localhost
      port: 5432

CKA Mock Exam - Q.4 (ConfigMap 생성 및 Deployment 업데이트):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 문제: ConfigMap을 생성하고 Deployment의 환경 변수로 설정

# 1. ConfigMap 생성
kubectl create configmap app-config -n cm-namespace \
  --from-literal=ENV=production \
  --from-literal=LOG_LEVEL=info

# 2. ConfigMap 확인
kubectl get configmap app-config -n cm-namespace -o yaml

# 3. Deployment에 ConfigMap 적용 (kubectl set env 사용)
kubectl set env deployment/cm-webapp -n cm-namespace \
  --from=configmap/app-config

# 또는 직접 수정
kubectl edit deployment cm-webapp -n cm-namespace
# spec.template.spec.containers[].envFrom에 추가:
#   - configMapRef:
#       name: app-config

# 4. Rollout 상태 확인
kubectl rollout status deployment/cm-webapp -n cm-namespace

# 5. Pod에서 환경 변수 확인
kubectl exec -n cm-namespace deployment/cm-webapp -- env | grep -E 'ENV|LOG_LEVEL'

15.2 Secret (민감 정보 관리)

종류:

  • Opaque: 기본, 임의의 데이터
  • kubernetes.io/service-account-token: ServiceAccount 토큰
  • kubernetes.io/dockercfg: Docker 설정
  • kubernetes.io/dockerconfigjson: Docker 설정 (JSON)
  • kubernetes.io/basic-auth: 기본 인증
  • kubernetes.io/ssh-auth: SSH 인증
  • kubernetes.io/tls: TLS 인증서
1
2
3
4
5
6
7
8
apiVersion: v1
kind: Secret
metadata:
  name: db-secret
type: Opaque
stringData: # base64 인코딩 자동
  username: admin
  password: securepassword

15.3 Volume으로 마운트

ConfigMap:

1
2
3
4
5
6
7
volumeMounts:
  - name: config
    mountPath: /etc/config
volumes:
  - name: config
    configMap:
      name: app-config

Secret:

1
2
3
4
5
6
7
8
volumeMounts:
  - name: secrets
    mountPath: /etc/secrets
    readOnly: true
volumes:
  - name: secrets
    secret:
      secretName: db-secret

15.4 환경 변수로 주입

ConfigMap에서:

1
2
3
4
5
6
7
8
containers:
  - name: app
    env:
      - name: SERVER_PORT
        valueFrom:
          configMapKeyRef:
            name: app-config
            key: server.port

Secret에서:

1
2
3
4
5
6
7
8
containers:
  - name: app
    env:
      - name: DB_PASSWORD
        valueFrom:
          secretKeyRef:
            name: db-secret
            key: password

학습 정리

핵심 개념

  1. ConfigMap은 애플리케이션 설정을 컨테이너 이미지와 분리하여 관리
  2. Secret은 민감한 정보를 안전하게 저장
  3. 환경 변수 또는 볼륨으로 Pod에 주입 가능
  4. 설정 변경 시 이미지 재빌드 불필요

다음 단계

  • ConfigMap으로 설정 관리 이해
  • Secret으로 민감 정보 관리 이해
  • Volume과 환경 변수 주입 방법 학습
  • 스토리지 학습 → Part 6으로 이동

실습 과제

  1. ConfigMap 생성 및 사용
1
2
3
4
5
6
7
8
9
10
# ConfigMap 생성 (명령형)
kubectl create configmap app-config \
  --from-literal=server.port=8080 \
  --from-literal=server.timeout=30

# ConfigMap 확인
kubectl get configmap app-config -o yaml

# ConfigMap을 환경 변수로 사용
kubectl run nginx --image=nginx --env=SERVER_PORT=configMap:app-config:server.port
  1. Secret 생성 및 사용
1
2
3
4
5
6
7
8
9
10
# Secret 생성 (명령형)
kubectl create secret generic db-secret \
  --from-literal=username=admin \
  --from-literal=password=securepassword

# Secret 확인 (base64 인코딩됨)
kubectl get secret db-secret -o yaml

# Secret 디코딩
kubectl get secret db-secret -o jsonpath='{.data.password}' | base64 -d
  1. ConfigMap을 파일로 마운트
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
apiVersion: v1
kind: Pod
metadata:
  name: configmap-pod
spec:
  containers:
    - name: nginx
      image: nginx
      volumeMounts:
        - name: config
          mountPath: /etc/config
  volumes:
    - name: config
      configMap:
        name: app-config
1
2
3
4
5
6
# Pod 생성
kubectl apply -f configmap-pod.yaml

# 마운트된 파일 확인
kubectl exec configmap-pod -- ls /etc/config
kubectl exec configmap-pod -- cat /etc/config/server.port

추가 학습 자료


This post is licensed under CC BY 4.0 by the author.