본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

MongoDB 스케일러

원문 보기 위키 갱신

MongoDB 쿼리의 결과 개수에 따라 워크로드를 자동으로 늘리고 줄이고 싶을 때 사용하는 스케일러예요. 쿼리가 몇 개를 반환하는지에 따라 파드 수가 조정되는 원리를 살펴볼게요.

출처: 문서

본문

트리거 명세 (Trigger Specification)

이 명세는 MongoDB 쿼리 결과를 기준으로 스케일링하는 mongodb 트리거를 설명해요.

triggers:
  - type: mongodb
    metadata:
      # name of an environment variable containing a valid MongoDB connection string
      connectionStringFromEnv: MongoDB_CONNECTION_STRING
      # Required: database name
      dbName: "test"
      # Required: collection name
      collection: "test_collection"
      # Required: query expr, used by filter data
      query: '{"region":"eu-1","state":"running","plan":"planA"}'
      # Required: according to the number of query result, to scale the TargetRef
      queryValue: "1"
      # Optional: according to the number of query result, the scaler is active
      activationQueryValue: "1"

연결 문자열 대신 연결 파라미터를 명시적으로 구성할 수도 있어요.

triggers:
  - type: mongodb
    metadata:
      # scheme of the MongoDB server. if using MongoDB Altas, you can set it to "mongodb+srv"
      scheme: "mongodb"
      # host name of the MongoDB server. Example of mongodb service: "mongodb-svc.<namespace>.svc.cluster.local"
      host: mongodb-svc.default.svc.cluster.local
      # port number of the MongoDB server.
      port: "27017"
      # username credential for connecting to the MongoDB server
      username: test_user
      # name of an environment variable containing a valid password for connecting to the MongoDB server
      passwordFromEnv: MongoDB_Password
      # Required: database name
      dbName: "test"
      # Required: collection name
      collection: "test_collection"
      # Required: query expr, used by filter data
      query: '{"region":"eu-1","state":"running","plan":"planA"}'
      # Required: according to the number of query result, to scale the TargetRef
      queryValue: "1"
      # Optional: according to the number of query result, the scaler is active
      activationQueryValue: "1"

파라미터 목록: mongodb 트리거는 항상 다음 정보를 요구해요.

  • dbName - 데이터베이스의 이름이에요.
  • collection - 컬렉션의 이름이에요.
  • query - 단일 숫자 값을 반환해야 하는 MongoDB 쿼리예요.
  • queryValue - 스케일링이 발생해야 하는 시점을 정의하는 임계값이에요. 이 값은 float일 수 있어요.
  • activationQueryValue - 스케일러를 활성화하는 목표 값이에요. 활성화(activation)에 대해 더 알고 싶다면 여기를 참고해요. (기본값: 0, 선택 사항). 이 값은 float일 수 있어요.

MongoDB 서버에 연결하려면 다음 중 하나를 제공해요.

  • connectionStringFromEnv - MongoDB 서버에 연결하기 위한 유효한 MongoDB 연결 문자열을 담은 환경 변수의 이름이에요.

또는 더 자세한 연결 파라미터를 명시적으로 제공할 수도 있어요(실행 시 연결 문자열이 자동으로 생성돼요).

  • scheme - MongoDB 서버의 스킴이에요. MongoDB Atlas를 사용한다면 mongodb+srv로 설정할 수 있어요. (기본값: mongodb, 선택 사항)
  • host - MongoDB 서버의 호스트 이름이에요.
  • port - MongoDB 서버의 포트 번호예요.
  • username - MongoDB 데이터베이스에 인증할 사용자 이름이에요.
  • passwordFromEnv - MongoDB 서버에 연결하기 위한 비밀번호 자격 증명을 담은 환경 변수의 이름이에요.

연결 문자열로 구성할 때는 다음 URL 형식을 사용해요.

mongodb[+srv]://<username>:<password>@mongodb-svc.<namespace>.svc.cluster.local:27017/<database_name>

인증 파라미터 (Authentication Parameters)

환경 변수 대신 TriggerAuthentication이나 ClusterTriggerAuthentication 구성을 통해 연결 문자열이나 비밀번호 인증을 사용해 MongoDB 서버에 인증할 수도 있어요.

연결 문자열 인증(Connection String Authentication):

  • connectionString - MongoDB 서버의 연결 문자열이에요.

비밀번호 인증(Password Authentication):

  • scheme - MongoDB 서버의 스킴이에요. MongoDB Atlas를 사용한다면 mongodb+srv로 설정할 수 있어요. (기본값: mongodb, 선택 사항)
  • host - MongoDB 서버의 호스트 이름이에요.
  • port - MongoDB 서버의 포트 번호예요.
  • username - MongoDB 데이터베이스에 인증할 사용자 이름이에요.
  • password - 구성된 사용자가 MongoDB 서버에 로그인할 때 사용할 비밀번호예요.
  • dbName - 데이터베이스의 이름이에요.

예시 (Example)

TriggerAuthentication을 사용하는 MongoDB 스케일 트리거로 scaled Job을 배포하는 예시예요.

apiVersion: keda.sh/v1alpha1
kind: ScaledJob
metadata:
  name: mongodb-job
spec:
  jobTargetRef:
    template:
      spec:
        containers:
          - name: mongodb-update
            image: 1314520999/mongodb-update:latest
            args:
            - --dataBase=test
            - --collection=test_collection
            imagePullPolicy: IfNotPresent
        restartPolicy: Never
    backoffLimit: 1
  pollingInterval: 30             # Optional. Default: 30 seconds
  maxReplicaCount: 30             # Optional. Default: 100
  successfulJobsHistoryLimit: 0   # Optional. Default: 100. How many completed jobs should be kept.
  failedJobsHistoryLimit: 10      # Optional. Default: 100. How many failed jobs should be kept.
  triggers:
    - type: mongodb
      metadata:
        dbName: "test"
        collection: "test_collection"
        query: '{"region":"eu-1","state":"running","plan":"planA"}'
        queryValue: "1"
      authenticationRef:
        name: mongodb-trigger
---
apiVersion: keda.sh/v1alpha1
kind: TriggerAuthentication
metadata:
  name: mongodb-trigger
spec:
  secretTargetRef:
    - parameter: connectionString
      name: mongodb-secret
      key: connect
---
apiVersion: v1
kind: Secret
metadata:
  name: mongodb-secret
type: Opaque
data:
  connect: bW9uZ29kYjovL3Rlc3RfdXNlcjp0ZXN0X3Bhc3N3b3JkQG1vbmdvZGItc3ZjLm1vbmdvREIuc3ZjLmNsdXN0ZXIubG9jYWw6MjcwMTcvdGVzdA==

더 알아보기 (Learn more)

쿼리 결과 개수를 목표 값과 비교해 스케일링하는 방식이라, 특정 조건의 문서가 늘어날 때 작업을 확장하는 시나리오에 잘 맞아요. 연결 문자열이나 개별 파라미터 중 편한 방식으로 인증을 구성하면 돼요.