1. 배경

레디스 클러스터에 주기적으로 부하를 넣어야 하는데 마땅한 방법이 없어 급히 자작한 파이썬 코드임

 

2. 구성

  • 레디스 클러스터도 쿠버네티스에 파드로 실행 중
  • 본 샘플도 컨테이너화되어 있음

 

3. redis_client.py

import rediscluster
import os
import random
from time import sleep

REDIS_LOAD_COUNT=100

REDIS_NODE=os.getenv('REDIS_NODE')
print("[] Redis node =", REDIS_NODE)
REDIS_LOAD_COUNT==int(os.getenv('REDIS_LOAD_COUNT'))
print("[] Redis load count =", REDIS_LOAD_COUNT)
REDIS_LOAD_SLEEP1=int(os.getenv('REDIS_LOAD_SLEEP1'))
print("[] Redis load sleep2 =", REDIS_LOAD_SLEEP1)
REDIS_LOAD_SLEEP2=int(os.getenv('REDIS_LOAD_SLEEP2'))
print("[] Redis load sleep2 =", REDIS_LOAD_SLEEP2)

startup_nodes = [{ "host": REDIS_NODE, "port": "6379" }]
client = rediscluster.RedisCluster(startup_nodes=startup_nodes, decode_responses=True, skip_full_coverage_check=True)

print("[] Redis cluster slot")
print(client.cluster('slots'))

print("[] Load test")
cnt = 0

while True:
    cnt += 1;
    randomNumber = random.randint(1,1000);
    client.set('key'+str(randomNumber), 'value'+str(randomNumber))
    print(client.get('key'+str(randomNumber)))
    sleeptime = random.randint(REDIS_LOAD_SLEEP1,REDIS_LOAD_SLEEP2)
    sleep(sleeptime)

    if cnt == REDIS_LOAD_COUNT:
        break 

 

4. Dockerfile

FROM python:3.11

RUN pip install redis-py-cluster

RUN mkdir /python
COPY redis_client.py /python

WORKDIR /python
CMD [ "python", "redis_client.py" ]

 

5. 실행 YAML

  • REDIS_NODE : 연결할 레디스 노드, 나는 서비스 이름을 지정함
  • REDIS_LOAD_COUNT : 부하를 입력할 횟수 (부하 => set)
  • REDIS_LOAD_SLEEP1 : set 입력 횟수 사이의 최저 대기시간 (랜덤시간 대기, 초)
  • REDIS_LOAD_SLEEP2 : set 입력 횟수 사이의 최고 대기시간 (랜덤시간 대기, 초)
  • set을 넣고 다음 set을 넣기까지의 대기 시간은 REDIS_LOAD_SLEEP1(초) ~ REDIS_LOAD_SLEEP2(초) 사이다.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redisc
  namespace: backend
  labels:
    app: redisc
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redisc
  template:
    metadata:
      labels:
        app: redisc
    spec:
      containers:
      - name: redisc
        image: 224983703333.dkr.ecr.ap-northeast-2.amazonaws.com/redis/redisc:0.20
        imagePullPolicy: Always
        resources:
          requests:
            memory: "128Mi"
            cpu: "100m"
          limits:
            memory: "256Mi"
            cpu: "150m"
        ports:
        - containerPort: 80
        env:
        - name: REDIS_NODE
          value: redis-cluster-svc
        - name: REDIS_LOAD_COUNT
          value: "1000"
        - name: REDIS_LOAD_SLEEP1
          value: "3"
        - name: REDIS_LOAD_SLEEP2
          value: "10"