시크릿은 파드의 컨테이너에서 참조하여 사용하게 된다. 환경 변수로 전달하는 방법과 볼륨으로 마운트하여 사용하는 방법이 있다.
- 환경 변수로 전달
- 시크릿의 특정 키 전달: spec.containers[].env[].valueForm.secretKeyRef.name/key
- 시크릿 전체 키 전달: spec.containers[].envFrom[].secretRef.name
- 볼륨으로 마운트
- 시크릿의 특정 키 마운트: spec.volumes[].secret.items[].key/path
- 시크릿 전체 키 마운트: spec.volumes[].secret.secretName
1. 환경변수로 특정 키 전달
아래 매니페스트와 같이 env[] 에 환경변수들을 이름과 값으로 하나씩 직접 지정한다. sample-db-auth 라는 시크릿에 username 키로 담긴 값을 DB_USERNAME 이라는 환경변수로 세팅하는 것이다.
apiVersion: v1
kind: Pod
metadata:
name: sample-secret-single-env
spec:
containers:
- name: secret-container
image: nginx:1.27
env:
- name: DB_USERNAME
valueFrom:
secretKeyRef:
name: sample-db-auth
key: username
$ kubectl apply -f sample-secret-single-env.yaml
$ kubectl exec -it sample-secret-single-env -- env | grep DB_USER
DB_USERNAME=root
2. 환경변수로 전체 키 전달
직접 환경변수명과 불러올 시크릿 값을 지정하지 않고 시크릿 전체를 환경변수로 그대로 등록하는 방법이다. 간단하긴 하지만, 매니페스트만 봐서는 어떤 환경변수가 설정되는지 알 수 없다는 단점이 있다.
아래 매니페스트처럼 간단히 envFrom[] 에 가져올 시크릿들의 참조를 걸어주면 된다.
apiVersion: v1
kind: Pod
metadata:
name: sample-secret-multi-env
spec:
containers:
- name: secret-container
image: nginx:1.27
envFrom:
- secretRef:
name: sample-db-auth
$ kubectl apply -f sample-secret-multi-env.yaml
$ kubectl exec -it sample-secret-multi-env -- env
...
password=rootpassword
username=root
...
여러 시크릿들을 가져오는 경우 key 가 동일하여 충돌할 가능성이 있다. 이런 경우에는 prefix 를 붙여 충돌을 방지할 수 있다.
apiVersion: v1
kind: Pod
metadata:
name: sample-secret-prefix-env
spec:
containers:
- name: secret-container
image: nginx:1.27
envFrom:
- secretRef:
name: sample-db-auth
prefix: DB1_
- secretRef:
name: sample-db-auth
prefix: DB2_
$ kubectl apply -f sample-secret-prefix-env.yaml
$ kubectl exec -it sample-secret-prefix-env -- env | egrep "^DB"
DB1_password=rootpassword
DB1_username=root
DB2_password=rootpassword
DB2_username=root
3. 볼륨으로 특정 키 마운트
시크릿의 정보를 컨테이너의 볼륨으로 마운트 하는 방식으로, spec.containers[].volumeMounts[] 에 마운트 할 볼륨명과 마운트 경로를 지정하고, spec.volumes[] 에 생성할 볼륨명과 값을 가져올 시크릿 및 읽을 key, 그리고 마운트 경로에 생성할 파일명(path)을 지정한다. 직접 마운트 파일명을 지정한다는 것이 특징이다.
다음과 같은 매니페스트로 생성한다.
apiVersion: v1
kind: Pod
metadata:
name: sample-secret-single-volume
spec:
containers:
- name: secret-container
image: nginx:1.27
volumeMounts:
- name: config-volume
mountPath: /config
volumes:
- name: config-volume
secret:
secretName: sample-db-auth
items:
- key: username
path: username.txt
$ kubectl apply -f sample-secret-single-volume.yaml
$ kubectl exec -it sample-secret-single-volume -- cat /config/username.txt
root
4. 볼륨으로 전체 키 마운트
시크릿의 전체 정보를 컨테이너의 볼륨으로 마운트하는 방식으로, spec.containers[].volumeMounts[] 에 마운트 할 볼륨명과 마운트 경로를 지정하고, spec.volumes[] 에 생성할 볼륨명과 가져올 시크릿을 지정한다. 마운트 경로에 생성되는 파일명은 시크릿의 key 와 동일하다. 매니페스트는 간단해지지만 매니페스트만으로는 시크릿의 어떤 key-value 가 마운트되는지 알 수 없다는 단점이 있다.
다음과 같은 매니페스트로 생성한다.
apiVersion: v1
kind: Pod
metadata:
name: sample-secret-multi-volume
spec:
containers:
- name: secret-container
image: nginx:1.27
volumeMounts:
- name: config-volume
mountPath: /config
volumes:
- name: config-volume
secret:
secretName: sample-db-auth
$ kubectl apply -f sample-secret-multi-volume.yaml
$ kubectl exec -it sample-secret-multi-volume -- ls /config
password username
'Kubernetes' 카테고리의 다른 글
| [Kubernets] ConfigMap 을 생성하는 3가지 방법 (0) | 2026.01.11 |
|---|---|
| [Kubernetes] 운영중에 Secret 을 업데이트하기 (0) | 2026.01.11 |
| [Kubernetes] TLS, Docker 레지스트리, Basic, SSH 시크릿 생성 (0) | 2026.01.10 |
| [Kubernetes] 범용 Secret 을 생성하는 4가지 방법(Opaque) (0) | 2026.01.07 |
| [Kubernetes] Secret 이해 (0) | 2026.01.07 |
