Dev./Kubernetes & Helm

Kubernetes: ConfigMap

Ivan'show 2023. 9. 18.
728x90
반응형

Configmap

Configmap 은 key - value 쌍으로 기밀이 아닌 데이터를 저장하는데 사용하는 API 오브젝트이다. 파드는 볼륨에서 환경 변수, 커맨드-라인 인수 또는 구성 파일로 ConfigMap 을 사용할 수 있다.

k create configmap fortune-config --from-literal=sleep-interval=15
k get configmap
k get configmap fortune-config -o yaml
k get configmap fortune-config -o yaml > fortune-config.yaml
# fortune-config.yaml : 부수적인 부분들 삭제 후
apiVersion: v1
data:
  sleep-interval: "15"
kind: ConfigMap
metadata:
  name: fortune-config

파드 생성하는 yaml 파일 만들고 실행

k create -f fortune-config.yaml

생성한 configmap 확인

k get cm

폴더를 생성해서 conf 파일로 만들고 참고해서 파일 만들기

k create cm ${config map name} --from-file ${path of files}
k create cm fortune-config --from-file configmap-files/

config map 이 생성되어 있는 상황에서 ingress 에 외부에서 접근할 수 있는 경로를 하나 더 파고

# fortune-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: fortune
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  # ingressClassName: nginx-example
  rules:
  - host: lion.fortune.com
    http:
      paths:
      - backend:
          service:
            name: fortune-nodeport
            port:
              number: 80
        path: /
        pathType: Prefix
      - backend:
          service:
            name: fortune-configmap
            port:
              number: 80
        path: /config
        pathType: Prefix

파드와 서비스를 연결시키면

# fortune-svc-configmap.yaml
apiVersion: v1
kind: Service
metadata:
  name: fortune-configmap
spec:
  type: NodePort
  selector: # app 으로 레이블을 달아서 endpoints 에 넣어줄 수 있다.
    app: fortune-configmap
  ports:
      # 기본적으로 그리고 편의상 `targetPort` 는 `port` 필드와 동일한 값으로 설정된다.
    - port: 80
      targetPort: 80
k create -f fortune-svc-configmap.yaml
# fortune-pod-configmap-volume.yaml
apiVersion: v1
kind: Pod
metadata:
  name: fortune-configmap-v
spec:
  containers:
  - name: html-generator
    image: ivaninitworld/fortune:env # 이미지
    env:
    - name: INTERVAL
      valueFrom: 
        configMapKeyRef:
          name: fortune-config
          key: sleep-interval
    volumeMounts: 
    - name: html
      mountPath: /var/www

  - name: web-server
    # command: ["sleep"]  # error 확인용
    # args: ["infinity"]  # error 확인용
    image: nginx:alpine
    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: {}
  - name: config
    configMap:
      name: fortune-config
      items:
      - key: nginx-config.conf
        path: gzip.conf
k create -f fortune-pod-configmap-volume.yaml
k get endpoints fortune-configmap 
# 
kimminhyeok@Ivans-Mac configmap % k get endpoints fortune-configmap                
NAME                ENDPOINTS   AGE
fortune-configmap   <none>      83m
k label po fortune-configmap-volume app=fortune-configmap
kimminhyeok@Ivans-Mac configmap % k get endpoints fortune-configmap                               
NAME                ENDPOINTS         AGE
fortune-configmap   10.244.0.244:80   83m

파드에 서비스의 app 정보를 레이블로 달게 되면 자동으로 endpoints 로 잡혀서 접근이 가능하게 된다.

/etc/hosts 에 주소값만 저장이 되어 있다면 curl 로 호출이 가능해 진다.

curl <http://lion.fortune.com/config>
# result
kimminhyeok@Ivans-Mac configmap % curl <http://lion.fortune.com/config>
You will be married within a year.

To label it

k label po {po name} {label info}
k label po fortune-configmap-volume app=fortune-configmap

Error check

중간에 계속해서 web-server 컨테이너에서 에러가 발생했는데,

kimminhyeok@Ivans-Mac configmap % k create -f fortune-pod-configmap-volume.yaml        
pod/fortune-configmap-v created
kimminhyeok@Ivans-Mac configmap % k  get po
NAME                  READY   STATUS   RESTARTS     AGE
fortune-configmap-v   1/2     Error    1 (2s ago)   2s

de-bug 과정에서 web-server 컨테이너의 로그로 에러(오타…)를 찾아 수정했었다.

k logs fortune-configmap-v -c web-server

연결 확인 코드

fortune-configmap-v 파드의 web-server 컨테이너에 접속해서 conf.d 폴더에 뭐가 있는지 확인

k exec fortune-configmap-v -c web-server -- ls /etc/nginx/conf.d

# 확인
gzip.conf

만약 nginx-config.conf 에 변경이 생기면 아래의 방법으로 리로드를 한다.

k exec fortune-configmap-v -c web-server -- nginx -s reload

그리고 헤더값에 내용을 추가해서 호출하면 변경 내용 확인 가능

curl -H "Accept-Encoding: gzip" -I <http://lion.fortune.com/config>
728x90
반응형

댓글