[k8s] 10. Config Map / Secret
ConfigMap
컨피그맵은 다른 오브젝트가 사용할 구성을 저장할 수 있는 API 오브젝트이다.
key-value 형태로 configmap에 저장하며,
가능한 방법은
-
단순 텍스트로 임의로 key와 map을 지정
-
파일이름을 key, 파일의 내용을 value로 저장
-
key 이름을 임의로 지정, 파일의 내용만 value로 저장
# configmap 생성
$ kubectl create configmap jungik-config --from-literal=INTERVAL=2 from-literal=OPTION=boy --from-file=config.dir/
# configmap 조회
$ kubectl get configmaps jungik-config
NAME DATA AGE
jungik-config 3 15s
# describe로 자세한 조회 가능
# edit으로 수정 가능
ConfigMap 적용
컨테이너에 적용하기
다음은 단일 값을 가진 키와, 값이 구성 형식의 일부처럼 보이는 키를 가진 컨피그맵의 예시이다.
apiVersion: v1
kind: ConfigMap
metadata:
name: game-demo
data:
# 속성과 비슷한 키; 각 키는 간단한 값으로 매핑됨
player_initial_lives: "3"
ui_properties_file_name: "user-interface.properties"
# 파일과 비슷한 키
game.properties: |
enemy.types=aliens,monsters
player.maximum-lives=5
user-interface.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
다음은 game-demo
의 값을 사용하여 파드를 구성하는 파드 예시이다.
apiVersion: v1
kind: Pod
metadata:
name: configmap-demo-pod
spec:
containers:
- name: demo
image: alpine
command: ["sleep", "3600"]
env:
# 환경 변수 정의
- name: PLAYER_INITIAL_LIVES # demo pod 내 적용 될 환경변수의 이름
valueFrom:
configMapKeyRef:
name: game-demo # 이 값의 컨피그맵.
key: player_initial_lives # 가져올 키.
- name: UI_PROPERTIES_FILE_NAME
valueFrom:
configMapKeyRef:
name: game-demo
key: ui_properties_file_name
#envFrom: # 컨피그 키를 지정할 필요 없이
#- configMapRef: # 모든 컨피그를 다 가져오는
# name: game-demo # 방법
volumeMounts:
- name: config
mountPath: "/config" # volumemount할 경로
readOnly: true
volumes:
# 파드 레벨에서 볼륨을 설정한 다음, 해당 파드 내의 컨테이너에 마운트한다.
- name: config
configMap:
# 마운트하려는 컨피그맵의 이름을 제공한다.
name: game-demo
# 컨피그맵에서 파일로 생성할 키 배열
items:
- key: "game.properties"
path: "game.properties"
- key: "user-interface.properties"
path: "user-interface.properties"
Secret
ConfigMap이 컨테이너 구성 정보를 한곳에 모아서 관리하는 것이라면
Secret은 컨테이너가 사용하는 password, auth token, ssh key와 같은 중요한 정보를 저장하고 민감한 구성정보를 base64로 인코딩해서 한 곳에 모아서 관리하는 것이다.
민감하지 않은 일반 설정파일은 configMap을, 민감한 데이터는 secret을 사용
Available Commands
available commands에 따라 만드는 방법이 나뉜다.
- docker-registry : Create a secret for use with a Docker registry
$ kubectl create secret docker-registry my-secret --docker-username=tiger --docker-password=pass --docker-email=tiger@gmail.com
- generic : Create a secret from a local file, diredtory or literal value
$ kubectl create secret generic my-secret --from-literal=INTERVAL=2 --from-file=./test-web-config/
- tls : Create a TLS secret
$ kubectl create secret tls my-secret --cert=path/to/cert/file --key=path/to/key/file
Secret type
type | 의미 |
---|---|
Opaque | 임의의 사용자 정의 데이터 |
kubernetes.io/service-account-token | 서비스 어카운트 토큰 |
kubernetes.io/dockercfg | dockercfg 파일 |
kubernetes.io/dockerconfigjson | dockercfg.json 파일 |
kubernetes.io/basic-auth | 기본 인증을 위한 자격 증명 |
kubernetes.io/ssh-auth | SSH를 위한 자격 증명 |
kubernetes.io/tls | TLS 클라이언트나 서버를 위한 데이터 |
bootstrap.kubernetes.io/token | 부트스트랩 토큰 데이터 |
Secret을 환경변수(env)로 전달하기
configMap과 비교
# configMap env
apiVersion: v1
kind: Pod
metadata:
name: configmap-pod
spec:
containers:
- name: test-configmap
image: genid:env
env:
- name: INTERVAL
valueFrom:
configMapKeyRef:
name: jungik-config
key: INTERVAL
name: fakeid-generator
volumeMounts:
- name: config
mountPath: "/config"
# secret env
apiVersion: v1
kind: Pod
metadata:
name: secret-pod
spec:
containers:
- name: test-secret
image: genid:env
env:
- name: INTERVAL
valueFrom:
secretKeyRef: # 이 부분만 다름
name: jungik-secret
key: INTERVAL
volumeMounts:
- name: config
mountPath: "/config"
생성한 Secret을 컨테이너 volume mount로 전달하기
apiVersion: v1
kind: Pod
metadata:
name: configmap-pod
spec:
containers:
- name: seb-server
image: nginx:1.14
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: jungik-secret
items:
- key: nginx-config.conf
path: nginx-config.conf
댓글남기기