본문 바로가기

K8S

Percona Distribution for MongoDB - 샤딩 (3/3)

Gisida님의 Database Operator In Kubernetes study(=DOIK) 스터디 진행 중 테스트 한 내용입니다.

Percona Distribution for MongoDB - 샤딩 (3/3)
1. MongoDB 샤드 구성
2. MongoDB 샤드정보 확인
3. MongoDB 샤딩 테스트


https://www.percona.com/doc/kubernetes-operator-for-psmongodb/expose.html

  • Shard : 데이터베이스의 Replica Set
  • Mongos : 클라이언트 애플리케이션의 쿼리를 처리하는 라우터
  • Config Servers : Replica Set 의 메타데이터와 샤드 클러스터의 정보를 저장
  • 샤드 접근 : mongos Pods - query routers, which acts as an entry point for client applications,

1. MongoDB 샤드 구성

▶ 샤드구성 및 테스트를 위해 이전에 생성하였던 파드들 삭제

$ kubectl delete psmdb $MYNICK-db
$ kubectl get pod -l app.kubernetes.io/instance=miny-db -owide

 

▶ 파드삭제 확인 후 PVC삭제

$ kubectl delete pvc -l app.kubernetes.io/instance=miny-db

 

▶ yaml 파일 확인

$ ls -lrt  ~/DOIK/4/mycluster2.yaml
-rw-r--r-- 1 root root 14657 Jun 19 11:24 /root/DOIK/4/mycluster2.yaml

 

클러스터 생성 : 복제 셋 2개(rs-0, rs1), mongos(파드 3개), cfg(파드 3개)

$ MYNICK=miny
$ MYCLUSTERNAME=$MYNICK envsubst < ~/DOIK/4/mycluster2.yaml | kubectl apply -f -

 

 클러스터 생성 정보 확인

$ kubectl get psmdb
NAME      ENDPOINT   STATUS         AGE
miny-db              initializing   2m2s

 

클러스터 파드정보 확인

$ kubectl get pod -owide
NAME                                               READY   STATUS    RESTARTS   AGE    IP            NODE     NOMINATED NODE   READINESS GATES
miny-db-cfg-0                                      1/1     Running   0          75s    172.16.3.6    k8s-w3   <none>           <none>
miny-db-cfg-1                                      1/1     Running   0          55s    172.16.1.8    k8s-w1   <none>           <none>
miny-db-cfg-2                                      1/1     Running   0          35s    172.16.2.11   k8s-w2   <none>           <none>
miny-db-rs0-0                                      1/1     Running   0          75s    172.16.1.6    k8s-w1   <none>           <none>
miny-db-rs0-1                                      1/1     Running   0          55s    172.16.3.9    k8s-w3   <none>           <none>
miny-db-rs0-2                                      1/1     Running   0          32s    172.16.2.12   k8s-w2   <none>           <none>
miny-db-rs1-0                                      1/1     Running   0          75s    172.16.2.8    k8s-w2   <none>           <none>
miny-db-rs1-1                                      1/1     Running   0          53s    172.16.3.10   k8s-w3   <none>           <none>
miny-db-rs1-2                                      1/1     Running   0          30s    172.16.1.10   k8s-w1   <none>           <none>

$ kubectl get sts -owide
NAME          READY   AGE   CONTAINERS   IMAGES
miny-db-cfg   3/3     90s   mongod       percona/percona-server-mongodb:5.0.7-6
miny-db-rs0   3/3     90s   mongod       percona/percona-server-mongodb:5.0.7-6
miny-db-rs1   3/3     90s   mongod       percona/percona-server-mongodb:5.0.7-6

 

 클러스터 서비스 정보 확인

$ kubectl get svc
NAME             TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)     AGE
miny-db-cfg      ClusterIP   None          <none>        27017/TCP   108s
miny-db-mongos   ClusterIP   10.200.1.49   <none>        27017/TCP   17s
miny-db-rs0      ClusterIP   None          <none>        27017/TCP   108s
miny-db-rs1      ClusterIP   None          <none>        27017/TCP   108s

 

 클러스터 엔드포인트, 엔드포인트슬라이스 정보 확인

$ kubectl get endpoints,endpointslice
NAME                       ENDPOINTS                                              AGE
endpoints/miny-db-cfg      172.16.1.8:27017,172.16.2.11:27017,172.16.3.6:27017    2m1s
endpoints/miny-db-mongos   172.16.1.11:27017,172.16.3.11:27017                    30s
endpoints/miny-db-rs0      172.16.1.6:27017,172.16.2.12:27017,172.16.3.9:27017    2m1s
endpoints/miny-db-rs1      172.16.1.10:27017,172.16.2.8:27017,172.16.3.10:27017   2m1s

NAME                                                  ADDRESSTYPE   PORTS   ENDPOINTS                            AGE
endpointslice.discovery.k8s.io/miny-db-cfg-hnbz5      IPv4          27017   172.16.3.6,172.16.1.8,172.16.2.11    2m1s
endpointslice.discovery.k8s.io/miny-db-mongos-fbtc6   IPv4          27017   172.16.1.11,172.16.3.11              30s
endpointslice.discovery.k8s.io/miny-db-rs0-fjgc4      IPv4          27017   172.16.1.6,172.16.3.9,172.16.2.12    2m1s
endpointslice.discovery.k8s.io/miny-db-rs1-m6ts6      IPv4          27017   172.16.2.8,172.16.3.10,172.16.1.10   2m1s

 

(옵션) 설치 리소스 확인

$ kubectl get all --namespace=psmdb

 

2. MongoDB 샤드정보 확인

mongos 라우터 접속 서비스 정보 확인

$ kubectl get svc miny-db-mongos
NAME             TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)     AGE
miny-db-mongos   ClusterIP   10.200.1.49   <none>        27017/TCP   5m41s

 

[터미널1] 클러스터 접속(ADMIN_USER)

kubectl exec -it myclient1 -- mongo --quiet "mongodb://userAdmin:userAdmin123456@miny-db-mongos.psmdb.svc.cluster.local/admin?ssl=false"
mongos> db
admin
mongos> show dbs
admin   0.000GB
config  0.003GB

 

[터미널2] 클러스터 접속(CLUSTER_USER)

$ kubectl exec -it myclient2 -- mongo --quiet "mongodb://clusterAdmin:clusterAdmin123456@miny-db-mongos.psmdb.svc.cluster.local/admin?ssl=false"
mongos> use config
switched to db config

 

 [터미널2] 샤드 목록 정보 확인

mongos> db.shards.find().pretty()
{{
        "_id" : "rs0",
        "host" : "rs0/miny-db-rs0-0.miny-db-rs0.psmdb.svc.cluster.local:27017,miny-db-rs0-1.miny-db-rs0.psmdb.svc.cluster.local:27017,miny-db-rs0-2.miny-db-rs0.psmdb.svc.cluster.local:27017",
        "state" : 1,
        "topologyTime" : Timestamp(1655635914, 2)
}
{
        "_id" : "rs1",
        "host" : "rs1/miny-db-rs1-0.miny-db-rs1.psmdb.svc.cluster.local:27017,miny-db-rs1-1.miny-db-rs1.psmdb.svc.cluster.local:27017,miny-db-rs1-2.miny-db-rs1.psmdb.svc.cluster.local:27017",
        "state" : 1,
        "topologyTime" : Timestamp(1655635917, 2)
}

 

 샤드 클러스터 상태 확인 : 기본 정보, 샤드 정보, 밸런서 정보, 샤딩 설정이 된 컬렉션 정보, 청크 정보 등 출력

mongos> sh.help()
mongos> sh.status({"verbose":1})  # 모든 정보 출력
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("62aeff70e9827296b1244a76")
  }
  shards:               # 샤드 정보, 샤드의 상태 state 정보 
        {  "_id" : "rs0",  "host" : "rs0/miny-db-rs0-0.miny-db-rs0.psmdb.svc.cluster.local:27017,miny-db-rs0-1.miny-db-rs0.psmdb.svc.cluster.local:27017,miny-db-rs0-2.miny-db-rs0.psmdb.svc.cluster.local:27017",  "state" : 1,  "topologyTime" : Timestamp(1655635914, 2) }
        {  "_id" : "rs1",  "host" : "rs1/miny-db-rs1-0.miny-db-rs1.psmdb.svc.cluster.local:27017,miny-db-rs1-1.miny-db-rs1.psmdb.svc.cluster.local:27017,miny-db-rs1-2.miny-db-rs1.psmdb.svc.cluster.local:27017",  "state" : 1,  "topologyTime" : Timestamp(1655635917, 2) }
  active mongoses:      # 라우터 정보
        "5.0.7-6" : 3
  autosplit:
        Currently enabled: yes
  balancer:             # 밸런서 정보 : 활성화 여부, 현재 작동 여부, 밸런싱 실패한 횟수나 24시간 이내에 몇번이나 '청크 이동 migration'이 있었는지 확인 >> 불필요한 청크 이동 점검 확인
        Currently enabled: yes
        Currently running: no
        Failed balancer rounds in last 5 attempts: 0
        Migration results for the last 24 hours: 
                322 : Success
  databases:            # 데이터베이스 정보 : 샤드 클러스터에서 admin 과 config 를 제외한 데이터베이스 정보를 표시
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                rs0     702
                                rs1     322
                        too many chunks to print, use verbose if you want to force print

 

 (옵션) 설정 서버에 저장된 메타데이터 확인

mongos> show collections
changelog  # 메타메이터가 변경된 내용을 기록한 목록
chunks     # 샤딩된 컬렉션의 청크 정보, 어떤 샤드에 어떤 범위로 있는지 확인 가능
collections   # 샤드 클러스터 컬렉션 목록
image_collection  #
lockpings  # 샤드 클러스터의 구성원이 서로의 연결상태를 확인한 일시가 있는 목록
locks      # 컬렉션 잠금에 대한 목록. 서로 다른 mongos 가 보낸 명령 충돌을 방지한다
migrations
mongos     # 실행중인 라우터 mongos 목록
reshardingOperations
shards     # 샤드 클러스터에 등록된 샤드 목록
tags
transactions
version    # 샤드 클러스터 메타데이터 전체에 대한 버전 정보, 동기화를 위한 필요

mongos> db.changelog.find().pretty()  # 메타메이터가 변경된 내용을 기록한 목록
mongos> db.chunks.find().pretty()     # 샤딩된 컬렉션의 청크 정보, 어떤 샤드에 어떤 범위로 있는지 확인 가능

 

3. MongoDB 샤딩 작동 테스트

▶ [터미널1] 클러스터 접속 (miny)

$ kubectl exec -it myclient1 -- mongo --quiet "mongodb://miny:qwe123@$MYNICK-db-mongos.psmdb.svc.cluster.local/admin?ssl=false"
mongos> db
admin
mongos> show dbs
admin   0.000GB
config  0.004GB

 

 [터미널1] miny 테이터베이스 선택(없으면 데이터베이스 생성됨)

mongos> use miny
switched to db miny

 

 [터미널1] 도큐먼트 추가

mongos> db.test.insertOne({ hello: 'world' })
{
        "acknowledged" : true,
        "insertedId" : ObjectId("62af0410e9a3c358d161d33e")
}

 

 [터미널1] 콜렉션에서 도큐먼트 조회

mongos> db.test.find()
{ "_id" : ObjectId("62af0410e9a3c358d161d33e"), "hello" : "world" }
mongos> 
mongos> db.test.find({},{_id:0})
{ "hello" : "world" }

 

[터미널2] 클러스터 접속(CLUSTER_USER)

$ kubectl exec -it myclient2 -- mongo --quiet "mongodb://clusterAdmin:clusterAdmin123456@miny-db-mongos.psmdb.svc.cluster.local/admin?ssl=false"
mongos> use config
switched to db config

 

 [터미널2] 샤드 클러스터 상태 확인 : 기본 정보, 샤드 정보, 밸런서 정보, 샤딩 설정이 된 컬렉션 정보, 청크 정보 등 출력

mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "minCompatibleVersion" : 5,
        "currentVersion" : 6,
        "clusterId" : ObjectId("62aeff70e9827296b1244a76")
  }
  shards:
        {  "_id" : "rs0",  "host" : "rs0/miny-db-rs0-0.miny-db-rs0.psmdb.svc.cluster.local:27017,miny-db-rs0-1.miny-db-rs0.psmdb.svc.cluster.local:27017,miny-db-rs0-2.miny-db-rs0.psmdb.svc.cluster.local:27017",  "state" : 1,  "topologyTime" : Timestamp(1655635914, 2) }
        {  "_id" : "rs1",  "host" : "rs1/miny-db-rs1-0.miny-db-rs1.psmdb.svc.cluster.local:27017,miny-db-rs1-1.miny-db-rs1.psmdb.svc.cluster.local:27017,miny-db-rs1-2.miny-db-rs1.psmdb.svc.cluster.local:27017",  "state" : 1,  "topologyTime" : Timestamp(1655635917, 2) }
  active mongoses:
        "5.0.7-6" : 3
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled: yes
        Currently running: no
        Failed balancer rounds in last 5 attempts: 0
        Migration results for the last 24 hours: 
                512 : Success
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                rs0     512
                                rs1     512
                        too many chunks to print, use verbose if you want to force print
        {  "_id" : "miny",  "primary" : "rs1",  "partitioned" : false,  "version" : {  "uuid" : UUID("19512d28-7dff-4546-ba4e-4bbe326de163"),  "timestamp" : Timestamp(1655637008, 3),  "lastMod" : 1 } }

 

miny 데이터베이스에서 샤딩을 활성화

mongos> sh.enableSharding("miny")
{
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1655637193, 2),
                "signature" : {
                        "hash" : BinData(0,"BTU65ZaIuCdE3HXqddNVutErGkE="),
                        "keyId" : NumberLong("7110901718166011919")
                }
        },
        "operationTime" : Timestamp(1655637193, 2)
}

 

샤딩 테스트를 위한 chunksize 변경 : 기본 64MB -> 1MB

mongos> db.settings.save({_id: "chunksize", value: 1})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "chunksize" })
mongos> db.settings.find()
{ "_id" : "chunksize", "value" : 1 }

 

[터미널1]  샤딩 활성화를 위해서 샤딩하려는 키에 해시 인덱스를 생성

mongos> db.test.createIndex({"username" : "hashed"})
## 혹은 인덱스 생성
mongos> db.test.createIndex({"username" : 1})

 

 [터미널2] 클러스터 접속(CLUSTER_USER) : 이제 "username" 으로 컬렉션을 샤딩할 수 있다

mongos> sh.shardCollection("miny.test", {"username" : "hashed"})
## 혹은
mongos> sh.shardCollection("miny.test", {"username" : 1})
## 몇 분 기다렸다가 다시 샤드 클러스터 상태 확인 : 휠씬 많은 정보가 표시됨
mongos> sh.status()

 

[터미널1] 클러스터 접속(miny) : 대량의 도큐먼트 생성  (10분 넘게 소요)

mongos> for (i=0; i<100000; i++) {db.test.insert({"username" : "user"+i, "created_at" : new Date()})}

 

[터미널2] 클러스터 접속(CLUSTER_USER) :  콜렉션 조회

$ kubectl exec -it myclient3 -- mongo --quiet "mongodb://miny:qwe123@miny-db-mongos.psmdb.svc.cluster.local/admin?ssl=false"
mongos> use miny
switched to db miny
mongos> db.test.find({},{_id:0})
~
~
mongos> db.test.count()
6605
mongos> db.test.count()
6639
mongos> db.test.count()
6653

 

 [터미널2]  데이터가 여러 샤드에 분산됐으므로 몇 가지 쿼리를 시도해서 확인 : 쿼리 정상 작동 확인

mongos> db.test.find({username: "user1234"})

 

쿼리 explain 확인

mongos>  db.test.find({username: "user1234"})
{ "_id" : ObjectId("62af058ee9a3c358d161d811"), "username" : "user1234", "created_at" : ISODate("2022-06-19T11:16:30.865Z") }
mongos> 
mongos> db.test.find({username: "user1234"}).explain()
{
        "queryPlanner" : {
                "mongosPlannerVersion" : 1,
                "winningPlan" : {
                        "stage" : "SINGLE_SHARD",
                        "shards" : [
                                {
                                        "shardName" : "rs1",
                                        "connectionString" : "rs1/miny-db-rs1-0.miny-db-rs1.psmdb.svc.cluster.local:27017,miny-db-rs1-1.miny-db-rs1.psmdb.svc.cluster.local:27017,miny-db-rs1-2.miny-db-rs1.psmdb.svc.cluster.local:27017",
                                        "serverInfo" : {
                                                "host" : "miny-db-rs1-0",
                                                "port" : 27017,
                                                "version" : "5.0.7-6",
                                                "gitVersion" : "5215aa3f853ea97640cdabf0b48861b0d53bfac3"
                                        },
                                        "namespace" : "miny.test",
                                        "indexFilterSet" : false,
                                        "parsedQuery" : {
                                                "username" : {
                                                        "$eq" : "user1234"
                                                }
                                        },
                                        "queryHash" : "379E82C5",
                                        "planCacheKey" : "44E4BA17",
                                        "maxIndexedOrSolutionsReached" : false,
                                        "maxIndexedAndSolutionsReached" : false,
                                        "maxScansToExplodeReached" : false,
                                        "winningPlan" : {
                                                "stage" : "FETCH",
                                                "filter" : {
                                                        "username" : {
                                                                "$eq" : "user1234"
                                                        }
                                                },
                                                "inputStage" : {
                                                        "stage" : "IXSCAN",
                                                        "keyPattern" : {
                                                                "username" : "hashed"
                                                        },
                                                        "indexName" : "username_hashed",
                                                        "isMultiKey" : false,
                                                        "isUnique" : false,
                                                        "isSparse" : false,
                                                        "isPartial" : false,
                                                        "indexVersion" : 2,
                                                        "direction" : "forward",
                                                        "indexBounds" : {
                                                                "username" : [
                                                                        "[8720327145141812260, 8720327145141812260]"
                                                                ]
                                                        }
                                                }
                                        },
                                        "rejectedPlans" : [
                                                {
                                                        "stage" : "FETCH",
                                                        "inputStage" : {
                                                                "stage" : "IXSCAN",
                                                                "keyPattern" : {
                                                                        "username" : 1
                                                                },
                                                                "indexName" : "username_1",
                                                                "isMultiKey" : false,
                                                                "multiKeyPaths" : {
                                                                        "username" : [ ]
                                                                },
                                                                "isUnique" : false,
                                                                "isSparse" : false,
                                                                "isPartial" : false,
                                                                "indexVersion" : 2,
                                                                "direction" : "forward",
                                                                "indexBounds" : {
                                                                        "username" : [
                                                                          "[\"user1234\", \"user1234\"]"
                                                                        ]
                                                                }
                                                        }
                                                }
                                        ]
                                }
                        ]
                }
        },
        "serverInfo" : {
                "host" : "miny-db-mongos-0",
                "port" : 27017,
                "version" : "5.0.7-6",
                "gitVersion" : "5215aa3f853ea97640cdabf0b48861b0d53bfac3"
        },
        "serverParameters" : {
                "internalQueryFacetBufferSizeBytes" : 104857600,
                "internalQueryFacetMaxOutputDocSizeBytes" : 104857600,
                "internalLookupStageIntermediateDocumentMaxSizeBytes" : 104857600,
                "internalDocumentSourceGroupMaxMemoryBytes" : 104857600,
                "internalQueryMaxBlockingSortMemoryUsageBytes" : 104857600,
                "internalQueryProhibitBlockingMergeOnMongoS" : 0,
                "internalQueryMaxAddToSetBytes" : 104857600,
                "internalDocumentSourceSetWindowFieldsMaxMemoryBytes" : 104857600
        },
        "command" : {
                "find" : "test",
                "filter" : {
                        "username" : "user1234"
                },
                "lsid" : {
                        "id" : UUID("0cfa7055-cc02-4b84-b8d0-c6288c29cac9")
                },
                "$clusterTime" : {
                        "clusterTime" : Timestamp(1655637511, 16),
                        "signature" : {
                                "hash" : BinData(0,"S2V182rlL4p+XCcOqBiYTcOXY3A="),
                                "keyId" : NumberLong("7110901718166011919")
                        }
                },
                "$db" : "miny"
        },
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1655637511, 54),
                "signature" : {
                        "hash" : BinData(0,"S2V182rlL4p+XCcOqBiYTcOXY3A="),
                        "keyId" : NumberLong("7110901718166011919")
                }
        },
        "operationTime" : Timestamp(1655637511, 54)
}



mongos> db.test.find({username: "user9851"}).explain()
{
        "queryPlanner" : {
                "mongosPlannerVersion" : 1,
                "winningPlan" : {
                        "stage" : "SINGLE_SHARD",
                        "shards" : [
                                {
                                        "shardName" : "rs1",
                                        "connectionString" : "rs1/miny-db-rs1-0.miny-db-rs1.psmdb.svc.cluster.local:27017,miny-db-rs1-1.miny-db-rs1.psmdb.svc.cluster.local:27017,miny-db-rs1-2.miny-db-rs1.psmdb.svc.cluster.local:27017",
                                        "serverInfo" : {
                                                "host" : "miny-db-rs1-0",
                                                "port" : 27017,
                                                "version" : "5.0.7-6",
                                                "gitVersion" : "5215aa3f853ea97640cdabf0b48861b0d53bfac3"
                                        },
                                        "namespace" : "miny.test",
                                        "indexFilterSet" : false,
                                        "parsedQuery" : {
                                                "username" : {
                                                        "$eq" : "user9851"
                                                }
                                        },
                                        "queryHash" : "379E82C5",
                                        "planCacheKey" : "44E4BA17",
                                        "maxIndexedOrSolutionsReached" : false,
                                        "maxIndexedAndSolutionsReached" : false,
                                        "maxScansToExplodeReached" : false,
                                        "winningPlan" : {
                                                "stage" : "FETCH",
                                                "filter" : {
                                                        "username" : {
                                                                "$eq" : "user9851"
                                                        }
                                                },
                                                "inputStage" : {
                                                        "stage" : "IXSCAN",
                                                        "keyPattern" : {
                                                                "username" : "hashed"
                                                        },
                                                        "indexName" : "username_hashed",
                                                        "isMultiKey" : false,
                                                        "isUnique" : false,
                                                        "isSparse" : false,
                                                        "isPartial" : false,
                                                        "indexVersion" : 2,
                                                        "direction" : "forward",
                                                        "indexBounds" : {
                                                                "username" : [
                                                                        "[-8090243295106338627, -8090243295106338627]"
                                                                ]
                                                        }
                                                }
                                        },
                                        "rejectedPlans" : [
                                                {
                                                        "stage" : "FETCH",
                                                        "inputStage" : {
                                                                "stage" : "IXSCAN",
                                                                "keyPattern" : {
                                                                        "username" : 1
                                                                },
                                                                "indexName" : "username_1",
                                                                "isMultiKey" : false,
                                                                "multiKeyPaths" : {
                                                                        "username" : [ ]
                                                                },
                                                                "isUnique" : false,
                                                                "isSparse" : false,
                                                                "isPartial" : false,
                                                                "indexVersion" : 2,
                                                                "direction" : "forward",
                                                                "indexBounds" : {
                                                                        "username" : [
                                                                          "[\"user9851\", \"user9851\"]"
                                                                        ]
                                                                }
                                                        }
                                                }
                                        ]
                                }
                        ]
                }
        },
        "serverInfo" : {
                "host" : "miny-db-mongos-0",
                "port" : 27017,
                "version" : "5.0.7-6",
                "gitVersion" : "5215aa3f853ea97640cdabf0b48861b0d53bfac3"
        },
        "serverParameters" : {
                "internalQueryFacetBufferSizeBytes" : 104857600,
                "internalQueryFacetMaxOutputDocSizeBytes" : 104857600,
                "internalLookupStageIntermediateDocumentMaxSizeBytes" : 104857600,
                "internalDocumentSourceGroupMaxMemoryBytes" : 104857600,
                "internalQueryMaxBlockingSortMemoryUsageBytes" : 104857600,
                "internalQueryProhibitBlockingMergeOnMongoS" : 0,
                "internalQueryMaxAddToSetBytes" : 104857600,
                "internalDocumentSourceSetWindowFieldsMaxMemoryBytes" : 104857600
        },
        "command" : {
                "find" : "test",
                "filter" : {
                        "username" : "user9851"
                },
                "lsid" : {
                        "id" : UUID("0cfa7055-cc02-4b84-b8d0-c6288c29cac9")
                },
                "$clusterTime" : {
                        "clusterTime" : Timestamp(1655637531, 97),
                        "signature" : {
                                "hash" : BinData(0,"SUDWaCT/o/FNAS+KkrAevS7dosI="),
                                "keyId" : NumberLong("7110901718166011919")
                        }
                },
                "$db" : "miny"
        },
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1655637532, 85),
                "signature" : {
                        "hash" : BinData(0,"Z70Dtg6rAkZhlOM0ykjg1R6hUlM="),
                        "keyId" : NumberLong("7110901718166011919")
                }
        },
        "operationTime" : Timestamp(1655637532, 85)
}



mongos> db.test.find({username: "user9851"}).explain()
{
        "queryPlanner" : {
                "mongosPlannerVersion" : 1,
                "winningPlan" : {
                        "stage" : "SINGLE_SHARD",
                        "shards" : [
                                {
                                        "shardName" : "rs1",
                                        "connectionString" : "rs1/miny-db-rs1-0.miny-db-rs1.psmdb.svc.cluster.local:27017,miny-db-rs1-1.miny-db-rs1.psmdb.svc.cluster.local:27017,miny-db-rs1-2.miny-db-rs1.psmdb.svc.cluster.local:27017",
                                        "serverInfo" : {
                                                "host" : "miny-db-rs1-0",
                                                "port" : 27017,
                                                "version" : "5.0.7-6",
                                                "gitVersion" : "5215aa3f853ea97640cdabf0b48861b0d53bfac3"
                                        },
                                        "namespace" : "miny.test",
                                        "indexFilterSet" : false,
                                        "parsedQuery" : {
                                                "username" : {
                                                        "$eq" : "user9851"
                                                }
                                        },
                                        "queryHash" : "379E82C5",
                                        "planCacheKey" : "44E4BA17",
                                        "maxIndexedOrSolutionsReached" : false,
                                        "maxIndexedAndSolutionsReached" : false,
                                        "maxScansToExplodeReached" : false,
                                        "winningPlan" : {
                                                "stage" : "FETCH",
                                                "filter" : {
                                                        "username" : {
                                                                "$eq" : "user9851"
                                                        }
                                                },
                                                "inputStage" : {
                                                        "stage" : "IXSCAN",
                                                        "keyPattern" : {
                                                                "username" : "hashed"
                                                        },
                                                        "indexName" : "username_hashed",
                                                        "isMultiKey" : false,
                                                        "isUnique" : false,
                                                        "isSparse" : false,
                                                        "isPartial" : false,
                                                        "indexVersion" : 2,
                                                        "direction" : "forward",
                                                        "indexBounds" : {
                                                                "username" : [
                                                                        "[-8090243295106338627, -8090243295106338627]"
                                                                ]
                                                        }
                                                }
                                        },
                                        "rejectedPlans" : [
                                                {
                                                        "stage" : "FETCH",
                                                        "inputStage" : {
                                                                "stage" : "IXSCAN",
                                                                "keyPattern" : {
                                                                        "username" : 1
                                                                },
                                                                "indexName" : "username_1",
                                                                "isMultiKey" : false,
                                                                "multiKeyPaths" : {
                                                                        "username" : [ ]
                                                                },
                                                                "isUnique" : false,
                                                                "isSparse" : false,
                                                                "isPartial" : false,
                                                                "indexVersion" : 2,
                                                                "direction" : "forward",
                                                                "indexBounds" : {
                                                                        "username" : [
                                                                          "[\"user9851\", \"user9851\"]"
                                                                        ]
                                                                }
                                                        }
                                                }
                                        ]
                                }
                        ]
                }
        },
        "serverInfo" : {
                "host" : "miny-db-mongos-0",
                "port" : 27017,
                "version" : "5.0.7-6",
                "gitVersion" : "5215aa3f853ea97640cdabf0b48861b0d53bfac3"
        },
        "serverParameters" : {
                "internalQueryFacetBufferSizeBytes" : 104857600,
                "internalQueryFacetMaxOutputDocSizeBytes" : 104857600,
                "internalLookupStageIntermediateDocumentMaxSizeBytes" : 104857600,
                "internalDocumentSourceGroupMaxMemoryBytes" : 104857600,
                "internalQueryMaxBlockingSortMemoryUsageBytes" : 104857600,
                "internalQueryProhibitBlockingMergeOnMongoS" : 0,
                "internalQueryMaxAddToSetBytes" : 104857600,
                "internalDocumentSourceSetWindowFieldsMaxMemoryBytes" : 104857600
        },
        "command" : {
                "find" : "test",
                "filter" : {
                        "username" : "user9851"
                },
                "lsid" : {
                        "id" : UUID("0cfa7055-cc02-4b84-b8d0-c6288c29cac9")
                },
                "$clusterTime" : {
                        "clusterTime" : Timestamp(1655637531, 97),
                        "signature" : {
                                "hash" : BinData(0,"SUDWaCT/o/FNAS+KkrAevS7dosI="),
                                "keyId" : NumberLong("7110901718166011919")
                        }
                },
                "$db" : "miny"
        },
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1655637532, 85),
                "signature" : {
                        "hash" : BinData(0,"Z70Dtg6rAkZhlOM0ykjg1R6hUlM="),
                        "keyId" : NumberLong("7110901718166011919")
                }
        },
        "operationTime" : Timestamp(1655637532, 85)
}
mongos> 
mongos> 
mongos> 
mongos> db.test.find().explain()
{
        "queryPlanner" : {
                "mongosPlannerVersion" : 1,
                "winningPlan" : {
                        "stage" : "SINGLE_SHARD",
                        "shards" : [
                                {
                                        "shardName" : "rs1",
                                        "connectionString" : "rs1/miny-db-rs1-0.miny-db-rs1.psmdb.svc.cluster.local:27017,miny-db-rs1-1.miny-db-rs1.psmdb.svc.cluster.local:27017,miny-db-rs1-2.miny-db-rs1.psmdb.svc.cluster.local:27017",
                                        "serverInfo" : {
                                                "host" : "miny-db-rs1-0",
                                                "port" : 27017,
                                                "version" : "5.0.7-6",
                                                "gitVersion" : "5215aa3f853ea97640cdabf0b48861b0d53bfac3"
                                        },
                                        "namespace" : "miny.test",
                                        "indexFilterSet" : false,
                                        "parsedQuery" : {

                                        },
                                        "queryHash" : "8B3D4AB8",
                                        "planCacheKey" : "D542626C",
                                        "maxIndexedOrSolutionsReached" : false,
                                        "maxIndexedAndSolutionsReached" : false,
                                        "maxScansToExplodeReached" : false,
                                        "winningPlan" : {
                                                "stage" : "SHARDING_FILTER",
                                                "inputStage" : {
                                                        "stage" : "COLLSCAN",
                                                        "direction" : "forward"
                                                }
                                        },
                                        "rejectedPlans" : [ ]
                                }
                        ]
                }
        },
        "serverInfo" : {
                "host" : "miny-db-mongos-0",
                "port" : 27017,
                "version" : "5.0.7-6",
                "gitVersion" : "5215aa3f853ea97640cdabf0b48861b0d53bfac3"
        },
        "serverParameters" : {
                "internalQueryFacetBufferSizeBytes" : 104857600,
                "internalQueryFacetMaxOutputDocSizeBytes" : 104857600,
                "internalLookupStageIntermediateDocumentMaxSizeBytes" : 104857600,
                "internalDocumentSourceGroupMaxMemoryBytes" : 104857600,
                "internalQueryMaxBlockingSortMemoryUsageBytes" : 104857600,
                "internalQueryProhibitBlockingMergeOnMongoS" : 0,
                "internalQueryMaxAddToSetBytes" : 104857600,
                "internalDocumentSourceSetWindowFieldsMaxMemoryBytes" : 104857600
        },
        "command" : {
                "find" : "test",
                "filter" : {

                },
                "lsid" : {
                        "id" : UUID("0cfa7055-cc02-4b84-b8d0-c6288c29cac9")
                },
                "$clusterTime" : {
                        "clusterTime" : Timestamp(1655637544, 73),
                        "signature" : {
                                "hash" : BinData(0,"kr5X6ehe5R+HYkQurVYUNazLpzM="),
                                "keyId" : NumberLong("7110901718166011919")
                        }
                },
                "$db" : "miny"
        },
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1655637545, 25),
                "signature" : {
                        "hash" : BinData(0,"xiO9RwpmXoPnf2ThxbS8C1YdhKM="),
                        "keyId" : NumberLong("7110901718166011919")
                }
        },
        "operationTime" : Timestamp(1655637545, 25)
}

 

[터미널2] 클러스터 접속(CLUSTER_USER) : 클러스터 내 모든 샤드정보 출력

mongos> db.shards.find()
{ "_id" : "rs0", "host" : "rs0/miny-db-rs0-0.miny-db-rs0.psmdb.svc.cluster.local:27017,miny-db-rs0-1.miny-db-rs0.psmdb.svc.cluster.local:27017,miny-db-rs0-2.miny-db-rs0.psmdb.svc.cluster.local:27017", "state" : 1, "topologyTime" : Timestamp(1655635914, 2) }
{ "_id" : "rs1", "host" : "rs1/miny-db-rs1-0.miny-db-rs1.psmdb.svc.cluster.local:27017,miny-db-rs1-1.miny-db-rs1.psmdb.svc.cluster.local:27017,miny-db-rs1-2.miny-db-rs1.psmdb.svc.cluster.local:27017", "state" : 1, "topologyTime" : Timestamp(1655635917, 2) }

 

클러스터가 알고 있는 모든 샤딩 데이터베이스 출력 : enableSharding 실행된 데이터베이스이며, partitioned 가 true

mongos> db.databases.find()
{ "_id" : "miny", "primary" : "rs1", "partitioned" : true, "version" : { "uuid" : UUID("19512d28-7dff-4546-ba4e-4bbe326de163"), "timestamp" : Timestamp(1655637008, 3), "lastMod" : 1 } }

 

샤딩된 컬렉션 출력

mongos> db.collections.find().pretty()
{
        "_id" : "config.system.sessions",
        "lastmodEpoch" : ObjectId("62af00757528961fdb9a7bbb"),
        "lastmod" : ISODate("2022-06-19T10:54:45.361Z"),
        "timestamp" : Timestamp(1655636085, 4),
        "uuid" : UUID("38908292-3367-441f-b4a1-d046eb08154e"),
        "key" : {
                "_id" : 1
        },
        "unique" : false,
        "noBalance" : false
}
{
        "_id" : "miny.test",
        "lastmodEpoch" : ObjectId("62af0551a67700e48f89a99c"),
        "lastmod" : ISODate("2022-06-19T11:15:29.461Z"),
        "timestamp" : Timestamp(1655637329, 12),
        "uuid" : UUID("98747d3a-4fd0-4aa9-8e1c-912191cc5db5"),
        "key" : {
                "username" : "hashed"
        },
        "unique" : false,
        "noBalance" : false
}

 

모든 컬렉션 내의 청크 기록

mongos> db.chunks.find().skip(1).limit(3).pretty()
{
        "_id" : ObjectId("62af007ee9827296b1245dcd"),
        "uuid" : UUID("38908292-3367-441f-b4a1-d046eb08154e"),
        "min" : {
                "_id" : {
                        "id" : UUID("00400000-0000-0000-0000-000000000000")
                }
        },
        "max" : {
                "_id" : {
                        "id" : UUID("00800000-0000-0000-0000-000000000000")
                }
        },
        "shard" : "rs1",
        "lastmod" : Timestamp(3, 0),
        "history" : [
                {
                        "validAfter" : Timestamp(1655636100, 19),
                        "shard" : "rs1"
                },
                {
                        "validAfter" : Timestamp(1655636085, 4),
                        "shard" : "rs0"
                }
        ]
}
{
        "_id" : ObjectId("62af007ee9827296b1245dce"),
        "uuid" : UUID("38908292-3367-441f-b4a1-d046eb08154e"),
        "min" : {
                "_id" : {
                        "id" : UUID("00800000-0000-0000-0000-000000000000")
                }
        },
        "max" : {
                "_id" : {
                        "id" : UUID("00c00000-0000-0000-0000-000000000000")
                }
        },
        "shard" : "rs1",
        "lastmod" : Timestamp(4, 0),
        "history" : [
                {
                        "validAfter" : Timestamp(1655636102, 8),
                        "shard" : "rs1"
                },
                {
                        "validAfter" : Timestamp(1655636085, 4),
                        "shard" : "rs0"
                }
        ]
}
{
        "_id" : ObjectId("62af007ee9827296b1245dcf"),
        "uuid" : UUID("38908292-3367-441f-b4a1-d046eb08154e"),
        "min" : {
                "_id" : {
                        "id" : UUID("00c00000-0000-0000-0000-000000000000")
                }
        },
        "max" : {
                "_id" : {
                        "id" : UUID("01000000-0000-0000-0000-000000000000")
                }
        },
        "shard" : "rs1",
        "lastmod" : Timestamp(5, 0),
        "history" : [
                {
                        "validAfter" : Timestamp(1655636103, 10),
                        "shard" : "rs1"
                },
                {
                        "validAfter" : Timestamp(1655636085, 4),
                        "shard" : "rs0"
                }
        ]
}

 

분할과 마이그레이션 기록 확인

mongos> db.changelog.find().pretty()

mongos> db.test.getShardDistribution()
Shard rs1 at rs1/miny-db-rs1-0.miny-db-rs1.psmdb.svc.cluster.local:27017,miny-db-rs1-1.miny-db-rs1.psmdb.svc.cluster.local:27017,miny-db-rs1-2.miny-db-rs1.psmdb.svc.cluster.local:27017
 data : 1.91MiB docs : 30523 chunks : 1
 estimated data per chunk : 1.91MiB
 estimated docs per chunk : 30523

Shard rs0 at rs0/miny-db-rs0-0.miny-db-rs0.psmdb.svc.cluster.local:27017,miny-db-rs0-1.miny-db-rs0.psmdb.svc.cluster.local:27017,miny-db-rs0-2.miny-db-rs0.psmdb.svc.cluster.local:27017
 data : 1.19MiB docs : 19050 chunks : 1
 estimated data per chunk : 1.19MiB
 estimated docs per chunk : 19050

Totals
 data : 3.1MiB docs : 49573 chunks : 2
 Shard rs1 contains 61.54% data, 61.57% docs in cluster, avg obj size on shard : 65B
 Shard rs0 contains 38.45% data, 38.42% docs in cluster, avg obj size on shard : 65B