K8s Configmap란?

  • 컨테이너에 필요한 환경 설정을 컨테이너와 분리해서 제공하는 기능
  • 컨테이 구성 정보를 한곳에 모아서 관리
  • Configmap을 단일진입점으로 Pod가 생성/재시작 할때마다 해당 Configmap을 참조하도록 하는게 가능
//형식
kubectl create configmap CONFIG_NAME --from-literal=id=jaon --from-literal=age=30
                                                   key value             key value 
kubectl create configmap CONFIG_NAME -- from-file=text.file
                                                    key  내용: value 
kubectl create configmap CONFIG_NAME --from-file=mydate=text.file
                                                   key     value
kubectl create configmap CONFIG_NAME --from-file=/config_dir/
                                              directory file전부

 

 

Configmap 생성 테스트

  • # kubectl create configmap test-config --from-literal=INTEVAL=2 --from-literal=OPTION=boy --from-file=config.dir/
    • configmap 생성
  • # kubectl describe configmaps test-config
  • # kubectl edit configmap test-config
    • configmap 수정

 

apiVersion: v1
kind: ConfigMap
metadata:
  name: config-test
  namespace: default
data:
  URL: localhost
  USER: myuser
  PASS: mypass
  DEBUG_MODE: info

  • yaml 형식으로 configmap 생성

 

 

Configmap Pod에 일부 configmap 적용 테스트

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: configmap-test
  name: configmap-test
spec:
  replicas: 3
  selector:
    matchLabels:
      app: configmap-test
  template:
    metadata:
      labels:
        app: configmap-test
    spec:
      containers:
      - image: nginx
        name: nginx
        env:
        - name: DEBUG_LEVEL
          valueFrom:
            configMapKeyRef:
              name: config-test
              key: DEBUG_MODE
        - name: URL
          valueFrom:
            configMapKeyRef:
              name: config-test
              key: URL

  • configmap에서 pod에 일부 적용된 환경변수 조회

 

 

Configmap Pod에 전체 configmap 적용 테스트

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: configmap-test
  name: configmap-test
spec:
  replicas: 3
  selector:
    matchLabels:
      app: configmap-test
  template:
    metadata:
      labels:
        app: configmap-test
    spec:
      containers:
      - image: nginx
        name: nginx
        envFrom:
        - configMapRef:
            name: config-test

  • configmap의 전체 내용을 pod에 적용

 

 

Configmap 볼륨 적용 테스트

apiVersion: v1
kind: Pod
metadata:
  name: configmap-volume-test
spec:
  containers:
  - image: ubuntu
    name: id-generator
    command:
    - sleep
    - "100000"
    env:
    - name: INTERVAL
      valueFrom:
        configMapKeyRef:
          name: test-config
          key: INTERVAL
    volumeMounts:
    - name: html
      mountPath: /webdata
  - image: nginx
    name: web-server
    ports:
    - containerPort: 80
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
      readOnly: true
    - name: config
      mountPath: /etc/nginx/conf.d
      readOnly: true
  volumes:
  - name: html
    emptyDir: {} #pod의 container간 volume mount (/webdata와 /usr/share/nginx/html path연결)
  - name: config
    configMap:
      name: test-config
      items:
      - key: nginx-config.conf #configmap test-conf의 key
        path: nginx-config.confs #생성할 파일이름
      - key: INTERVAL #configmap test-conf의 key
        path: IS_IS_INTERVAL

'클라우드 > K8s' 카테고리의 다른 글

Pod 스케줄링  (0) 2022.04.03
K8s Secret  (0) 2022.03.31
K8s 버전 업그레이드  (0) 2022.03.18
K8s 애플리케이션 변수관리  (0) 2022.03.17
K8s Self-healing  (0) 2022.03.17

애플리케이션에서 환경변수를 설정하는 방법은 3가지가 있다.

 

1. 환경변수를 pod에 저장하는 방법(kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/)

 

apiVersion: v1
kind: Pod
metadata:
  name: envar-demo
  labels:
    purpose: demonstrate-envars
spec:
  containers:
  - name: envar-demo-container
    image: gcr.io/google-samples/node-hello:1.0
    env:
    - name: DEMO_GREETING
      value: "Hello from the environment"
    - name: DEMO_FAREWELL
      value: "Such a sweet sorrow"

envars.yaml

 

 

 

2. Configmap환경변수를 ConfigMap 에 저장하는 방법

 

configmap 생성 및 조회

  • # kubectl create configmap map-name --from-file=test
  • # kubectl get configmaps -o yaml

 

apiVersion: v1
kind: Pod
metadata:
  name: envar-configmap
  labels:
    purpose: demonstrate-envars
spec:
  containers:
  - name: envar-demo-container
    image: gcr.io/google-samples/node-hello:1.0
    env:
    - name: DEMO_GREETING
      valueFrom:
        configMapKeyRef:
          name: map-name
          key: test

envars-configmap.yaml

 

configmap을 통한 환경번수 설정

 

전체 keylist를 환경변수에 설정

 

configmap을 활용한 디렉토리 마운트

/etc/config에 파일형태로 환경변수 저장

  • configmap에 3개의 key-value pairs를 추가할 경우 pod내에서도 이 설정이 1분마다 refresh되어 반영됨을 확인 할 수 있다.
    • key-value pairs의 삭제 또한 반영된다.
  • 마운트형태가 아닌 일반 configmap방식은 pod가 restart되어야 configmap의 변경사항이 반영된다.

 

3. 환경변수를 Secret에 저장하는 방법

  • bese64 인코딩된 형태로 데이터를 저장한다.
    • 비밀번호, OAuth 토큰 및 ssh 키등

secret 생성

  • # echo -n admin > username
  • # echo -n 242q3afsa2 > password
  • # kubectl create secret generic db-user-pass --from-file=u sername --from-file=password
  • # kubectl get secrets db-user-pass -o yaml
  • 디코딩
    • # echo YWRtaW4= | base64 --decode

secret를 환경변수로 pod생성

 

 

  • kubectl create secret generic db-secret --from-literal=' DB_Password=Passw0rd!0' --dry-run=client  -o yaml > secret-mysql.yaml
  • kubectl get secret db-secret -o yaml

'클라우드 > K8s' 카테고리의 다른 글

K8s Configmap  (0) 2022.03.31
K8s 버전 업그레이드  (0) 2022.03.18
K8s Self-healing  (0) 2022.03.17
K8s label  (0) 2022.03.17
K8s Network  (0) 2022.03.17

+ Recent posts