Secret
A Kubernetes resource that stores sensitive information
A Kubernetes resource that stores sensitive information
Kubernetes Secret is a resource that stores and manages sensitive information such as passwords, OAuth tokens, SSH keys, and TLS certificates. It is similar to a ConfigMap, but it is designed for sensitive data and is stored base64-encoded.
Secret types:
How Secrets are used:
Security considerations:
By default, Secrets are stored in plaintext in etcd. In production environments, the following are recommended:
Secret vs ConfigMap: A ConfigMap is for non-sensitive configuration data, and a Secret is for sensitive data. A Secret is mounted on tmpfs and loaded into memory without being written to disk.
# 1. kubectl로 Secret 생성 (권장)
kubectl create secret generic db-credentials \
--from-literal=username=admin \
--from-literal=password='S3cur3P@ss!'
# 2. YAML 파일로 생성 (base64 인코딩 필요)
# echo -n 'admin' | base64 → YWRtaW4=
# echo -n 'S3cur3P@ss!' | base64 → UzNjdXIzUEBzcyE=
apiVersion: v1
kind: Secret
metadata:
name: db-credentials
namespace: production
type: Opaque
data:
username: YWRtaW4= # base64 인코딩
password: UzNjdXIzUEBzcyE=
# 3. stringData 사용 (평문, 자동 인코딩)
apiVersion: v1
kind: Secret
metadata:
name: api-keys
type: Opaque
stringData:
api-key: "sk-1234567890abcdef"
webhook-secret: "whsec_abcdef123456"
---
# Pod에서 Secret 사용
apiVersion: v1
kind: Pod
metadata:
name: app-with-secrets
spec:
containers:
- name: app
image: my-app:latest
env:
# 개별 키를 환경변수로
- name: DB_USERNAME
valueFrom:
secretKeyRef:
name: db-credentials
key: username
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-credentials
key: password
# 전체 Secret을 환경변수로
envFrom:
- secretRef:
name: api-keys
optional: false
# 파일로 마운트
volumeMounts:
- name: db-creds-volume
mountPath: /etc/secrets
readOnly: true
volumes:
- name: db-creds-volume
secret:
secretName: db-credentials
defaultMode: 0400 # 읽기 전용 권한
# TLS Secret 생성
kubectl create secret tls my-tls-secret \
--cert=./tls.crt \
--key=./tls.key
# YAML 형식
apiVersion: v1
kind: Secret
metadata:
name: my-tls-secret
type: kubernetes.io/tls
data:
tls.crt: LS0tLS1CRUdJTi... # base64 인코딩된 인증서
tls.key: LS0tLS1CRUdJTi... # base64 인코딩된 개인키
---
# Ingress에서 TLS Secret 사용
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-app-ingress
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "true"
spec:
tls:
- hosts:
- my-app.example.com
secretName: my-tls-secret
rules:
- host: my-app.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app
port:
number: 80
# External Secrets Operator 설치
helm install external-secrets \
external-secrets/external-secrets \
-n external-secrets \
--create-namespace
# SecretStore 설정 (AWS Secrets Manager 연결)
apiVersion: external-secrets.io/v1beta1
kind: SecretStore
metadata:
name: aws-secretsmanager
namespace: production
spec:
provider:
aws:
service: SecretsManager
region: ap-northeast-2
auth:
jwt:
serviceAccountRef:
name: external-secrets-sa
---
# ExternalSecret 정의 (AWS에서 동기화)
apiVersion: external-secrets.io/v1beta1
kind: ExternalSecret
metadata:
name: db-credentials
namespace: production
spec:
refreshInterval: 1h # 1시간마다 동기화
secretStoreRef:
name: aws-secretsmanager
kind: SecretStore
target:
name: db-credentials # 생성될 K8s Secret 이름
creationPolicy: Owner
data:
- secretKey: username
remoteRef:
key: production/database # AWS Secret 이름
property: username
- secretKey: password
remoteRef:
key: production/database
property: password
# kubectl get externalsecret -n production
# NAME STORE REFRESH STATUS
# db-credentials aws-secretsmanager 1h SecretSynced
Security team: "It looks like the DB password was pushed to Git, right?"
Developer: "I encoded it in base64 in the Secret YAML and pushed it..."
Security team: "base64 is not encryption. Anyone can decode it. Use Sealed Secrets or External Secrets Operator so that only the encrypted form is stored in Git."
Developer: "Understood. I'll move it to AWS Secrets Manager and sync it with External Secrets."
Interviewer: "What are the security limitations of Kubernetes Secrets?"
Candidate: "First, they are stored in plaintext in etcd by default. You need to enable encryption at rest with EncryptionConfiguration. Second, base64 is just encoding, not encryption. Third, every Pod and user with access to a Secret can see its value, so RBAC configuration is important."
Interviewer: "So how do you improve on that?"
Candidate: "I use a dedicated secret management tool such as HashiCorp Vault or AWS Secrets Manager. If you sync external Secrets into K8s Secrets with External Secrets Operator, you don't have to store sensitive information in the source repository."
Operations team: "I updated the Secret, but it isn't reflected in the Pod."
Developer: "A Secret injected as an environment variable requires a Pod restart. With volume mounts, the kubelet updates it periodically, but it takes about 1 minute by default. To apply it immediately, do a rolling restart of the Pod."
Operations team: "Can't we make it restart automatically?"
Developer: "If you install a controller like Reloader, it automatically rolls out the Deployment when a Secret changes. Or you can put the Secret hash in an annotation so that the Pod is recreated when it changes."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free