ConfigMap으로 Redis 구성하기

ConfigMap으로 Redis 구성하기

이 페이지는 ConfigMap을 사용해 Redis를 구성하는 실제 세상 예시를 제공하며, 파드가 ConfigMap을 사용하도록 구성 작업을 바탕으로 이어져요.

출처: 문서

본문

목표 (Objectives)

  • Redis 구성 값이 있는 ConfigMap 만들기
  • 생성된 ConfigMap을 마운트하고 사용하는 Redis 파드 만들기
  • 구성이 올바르게 적용되었는지 확인하기

시작하기 전에

Kubernetes 클러스터가 있어야 하고 kubectl 명령줄 도구가 클러스터와 통신하도록 구성되어 있어야 해요. 이 튜토리얼은 컨트롤 플레인 호스트가 아닌 노드가 최소 두 개 있는 클러스터에서 실행하는 것을 권장해요. 아직 클러스터가 없다면 minikube를 이용해 만들거나, 아래 Kubernetes 플레이그라운드 중 하나를 사용할 수 있어요.

버전을 확인하려면 kubectl version을 입력해요. 이 페이지에 표시된 예시는 kubectl 1.14 이상에서 동작해요. 파드가 ConfigMap을 사용하도록 구성을 이해해야 해요.

실제 예시: ConfigMap으로 Redis 구성하기

다음 단계에 따라 ConfigMap에 저장된 데이터로 Redis 캐시를 구성해요.

먼저 빈 구성 블록이 있는 ConfigMap을 만들어요.

cat <<EOF >./example-redis-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: example-redis-config
data:
  redis-config: ""
EOF

위에서 만든 ConfigMap과 Redis 파드 매니페스트를 적용해요.

kubectl apply -f example-redis-config.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/pods/config/redis-pod.yaml

Redis 파드 매니페스트의 내용을 살펴보고 다음을 주목해요.

  • spec.volumes[1]config라는 볼륨을 만들어요.
  • spec.volumes[1].configMap.items[0]keypathexample-redis-config ConfigMap의 redis-config 키를 config 볼륨에 redis.conf라는 파일로 노출해요.
  • config 볼륨은 그런 다음 spec.containers[0].volumeMounts[1]에 의해 /redis-master에 마운트돼요.

이것은 사실상 위 example-redis-config ConfigMap의 data.redis-config 데이터를 파드 안 /redis-master/redis.conf로 노출하는 효과를 가져요.

apiVersion: v1
kind: Pod
metadata:
  name: redis
spec:
  containers:
  - name: redis
    image: redis:8.0.2
    command:
      - redis-server
      - "/redis-master/redis.conf"
    env:
    - name: MASTER
      value: "true"
    ports:
    - containerPort: 6379
    resources:
      limits:
        cpu: "0.1"
    volumeMounts:
    - mountPath: /redis-master-data
      name: data
    - mountPath: /redis-master
      name: config
  volumes:
    - name: data
      emptyDir: {}
    - name: config
      configMap:
        name: example-redis-config
        items:
        - key: redis-config
          path: redis.conf

생성된 객체를 확인해요.

kubectl get pod/redis configmap/example-redis-config

다음 출력을 볼 수 있어요.

NAME        READY   STATUS    RESTARTS   AGE
pod/redis   1/1     Running   0          8s

NAME                             DATA   AGE
configmap/example-redis-config   1      14s

example-redis-config ConfigMap의 redis-config 키를 비워 둔 것을 기억하세요.

kubectl describe configmap/example-redis-config

redis-config 키를 볼 수 있어요.

Name:         example-redis-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
redis-config:

kubectl exec로 파드에 들어가 redis-cli 도구를 실행해 현재 구성을 확인해요.

kubectl exec -it pod/redis -- redis-cli

maxmemory를 확인해요.

127.0.0.1:6379> CONFIG GET maxmemory

기본값 0을 보여줘야 해요.

1) "maxmemory"
2) "0"

비슷하게 maxmemory-policy를 확인해요.

127.0.0.1:6379> CONFIG GET maxmemory-policy

기본값 noeviction을 보여줘야 해요.

1) "maxmemory-policy"
2) "noeviction"

이제 example-redis-config ConfigMap에 몇 가지 구성 값을 추가해요.

apiVersion: v1
kind: ConfigMap
metadata:
  name: example-redis-config
data:
  redis-config: |
    maxmemory 2mb
    maxmemory-policy allkeys-lru

업데이트된 ConfigMap을 적용해요.

kubectl apply -f example-redis-config.yaml

ConfigMap이 업데이트되었는지 확인해요.

kubectl describe configmap/example-redis-config

방금 추가한 구성 값을 볼 수 있어요.

Name:         example-redis-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
redis-config:
----
maxmemory 2mb
maxmemory-policy allkeys-lru

kubectl exec를 통한 redis-cli로 Redis 파드를 다시 확인해 구성이 적용되었는지 봐요.

kubectl exec -it pod/redis -- redis-cli

maxmemory를 확인해요.

127.0.0.1:6379> CONFIG GET maxmemory

기본값 0에 그대로 있어요.

1) "maxmemory"
2) "0"

비슷하게 maxmemory-policy도 기본 설정인 noeviction에 그대로 있어요.

127.0.0.1:6379> CONFIG GET maxmemory-policy

반환:

1) "maxmemory-policy"
2) "noeviction"

구성 값이 변경되지 않았어요. 파드가 관련 ConfigMap에서 업데이트된 값을 가져오려면 재시작되어야 하기 때문이에요. 파드를 삭제하고 다시 만들어요.

kubectl delete pod redis
kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/pods/config/redis-pod.yaml

이제 마지막으로 구성 값을 다시 확인해요.

kubectl exec -it pod/redis -- redis-cli

maxmemory를 확인해요.

127.0.0.1:6379> CONFIG GET maxmemory

이제 업데이트된 값 2097152를 반환해야 해요.

1) "maxmemory"
2) "2097152"

비슷하게 maxmemory-policy도 업데이트되었어요.

127.0.0.1:6379> CONFIG GET maxmemory-policy

이제 원하는 값인 allkeys-lru를 반영해요.

1) "maxmemory-policy"
2) "allkeys-lru"

생성한 리소스를 삭제해 작업을 정리해요.

kubectl delete pod/redis configmap/example-redis-config

다음 단계

더 알아보기 (Learn more)