ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [MongoDB] Linux 서버 샤딩 (Sharding) 구성하기
    BackEnd/MongoDB 2021. 10. 7. 16:17

    샤딩 개념의 이해

    위 사진은 몽고DB 의 샤딩구성요소이다.

     

    • Mongos
      • 라우터의 역할을 하는 몽구스는 애플리케이션과 샤드 클러스터 간의 인터페이스를 제공한다.
      • 별도의 데이터를 가지고 있지 않으며 설정서버의 메타 정보로 데이터를 각 샤드에 전달한다.
    • Config Server
      • 설정 서버는 클러스터의 메타 데이터와 설정을 저장한다.
      • Mongos의 DB path 도 설정서버를 바라본다.
      • Replica set 구성으로 설정해야한다.
    • Shard
      • 데이터 저장 단위가 되는 기본 인스턴스
      • Replica set 구성으로 설정해야한다.
      • 1개 이상의 인스턴스를 설정할 수 있다.

     

    Replica Set (복제) 개념 이해

    구성요소

    • Primary
      • Master Mongo 서버, 쓰기만 하는 인스턴스
      • 본인 DB 에도 적재한다.
      • Secondary 에 복제하는 역할이다.
    • Secondary
      • 읽기를 지원하는 DB 이며 Primary 가 될 수 있는 자격을 가졌다.
    • Arbiter
      • 일종의 감시자이며 Primary 가 죽으면 Secondary 를 투표하는 역할을 지닌다.

     

    샤딩 방식 종류

    - 샤딩 설정시 특정 컬렉션의 인덱스를 기준으로 Range 샤딩, Hashed 샤딩 설정을 할 수 있다.

    - Range 는 인덱스의 값에 따라서, Hashed 는 HashFunction 을 통해서 샤딩이 결정된다.

    - 본 설명에서는 Hashed 방식으로 설정하였다.

    Range 방식

     

    Hashed 방식

    sh.shardCollection(“db.collection", {"_id": "hashed"})
    sh.shardCollection(“db. collection", {"_id": 1})

     

     

    샤딩 구성 하기

    - MongoDB 공식 홈페이지 https://www.mongodb.com/try/download/community

     

    MongoDB Community Download

    Download the Community version of MongoDB's non-relational database server from MongoDB's download center.

    www.mongodb.com

    에서 CentOS 7 기준 community Server 파일을 다운받아 구성하였다. 

    wget os별_MongoDB_downlink
    tar -xvf 다운파일

     

    Host 및 Port 정보

    - 전체 인스턴스 구조를 다음표 처럼 구성할 것이다.

    1. 폴더 구성

    폴더 구조

    - Mongos 는 데이터가 존재 하지 않는 구조로 구성하고

    - Config Server, Shard01, Shard02, Shard03 는 레플리카 셋 구성을 바탕으로 한 폴더구조로 구성한다.

    - 위 구조를 기준으로 .conf 파일을 수정하고, 각 경로 설정을 변경해준다.

     

     

    2. conf 파일 수정

    • Shard01, 02, 03 Server .conf 파일 - 각각 다음 정보를 참고하여 작성한다.
    # where to write logging data.
    systemLog:
      destination: file
      logAppend: true
      path: /mongodbPath/shard01/log/rs0.log
    
    # Where and how to store data.
    storage:
      dbPath: /mongodbPath/shard01/rs0
      journal:
        enabled: true
      engine: wiredTiger
      wiredTiger:
        engineConfig:
          cacheSizeGB: 16
    
    # how the process runs
    processManagement:
      fork: true
      pidFilePath: /mongodbPath/shard01/rs0.pid
      timeZoneInfo: /usr/share/zoneinfo
    
    # network interfaces
    net:
      port: 27054
      bindIp: ::,0.0.0.0
    
    #security:
    #  keyFile: /mongodbPath/shard01/mongodb-keyfile
    #  authorization: enabled
    
    #operationProfiling:
    
    replication:
      replSetName: "shard01"
    
    sharding:
      clusterRole: shardsvr

     

    • config Server .conf 파일 - replica 구성에 맞추어 각각 다음을 참고하여 작성한다.
    # where to write logging data.
    systemLog:
      destination: file
      logAppend: true
      path: /mongodbPath/configSvr/log/config01.log
    
    # Where and how to store data.
    storage:
      dbPath: /mongodbPath/configSvr/rs0
      journal:
        enabled: true
      engine: wiredTiger
      wiredTiger:
        engineConfig:
          cacheSizeGB: 16
    
    # how the process runs
    processManagement:
      fork: true
      pidFilePath: /mongodbPath/configSvr/rs0.pid
      timeZoneInfo: /usr/share/zoneinfo
    
    # network interfaces
    net:
      port: 27051
      bindIp: ::,0.0.0.0
    
    #security:
    #  keyFile: /mongodbPath/configSvr/mongodb-keyfile
    #  authorization: enabled
    
    #operationProfiling:
    
    replication:
      replSetName: "config"
    
    sharding:
      clusterRole: configsvr

     

    • mongos .conf 파일
    # where to write logging data.
    systemLog:
      destination: file
      logAppend: true
      path: /mongodb/mongos.log
    
    # how the process runs
    processManagement:
      fork: true
      pidFilePath: /mongodb/router/mongos.pid
      timeZoneInfo: /usr/share/zoneinfo
    
    # network interfaces
    net:
      port: 27050
      bindIp: ::,0.0.0.0
    
    security:
      keyFile: /mongodb/router/mongodb-keyfile
    
    sharding:
      configDB: "config/127.0.0.1:27051,127.0.0.1:27052,127.0.0.1:27053"

     

    3. 실행

    - Mongos 이외 Config Server, Shard01, 02, 03 (각 모든 노드) 실행

    ./mongod –f /mongodb/shard01/config/rs0.conf
    ./mongod –f /mongodb/shard01/config/rs1.conf
    ./mongod –f /mongodb/shard01/config/rs2.conf

     

    4. Replica Set Initiate

    - 각 클러스터의 첫번째 노드에 접속하여 복제 구성을 초기화 한다.

    ex) config: 27050, shard01: 27054, shard02: 27057, shard03: 27060

    ./mongo --port=27050

    - admin DB 를 지정한다.

    use admin

    - (주의) 클러스터 구성이 분리되어 있을 시 모든 Replica Set 설정은 Host 주소로 설정한다!!

    rs.initiate( {
       _id : "shard01",
       members: [
          { _id: 0, host: "127.0.0.1:27054" },
          { _id: 1, host: "127.0.0.1:27055" },
          { _id: 2, host: "127.0.0.1:27056" }
       ]
    })
    
    rs.initiate( {
       _id : "shard02",
       members: [
          { _id: 0, host: "127.0.0.1:27057" },
          { _id: 1, host: "127.0.0.1:27058" },
          { _id: 2, host: "127.0.0.1:27059" }
       ]
    })
    
    
    rs.initiate( {
       _id : "shard03",
       members: [
          { _id: 3, host: "127.0.0.1:27060" },
          { _id: 4, host: "127.0.0.1:27061" },
          { _id: 5, host: "127.0.0.1:27062" }
       ]
    })
    
    
    rs.initiate( {
       _id : "config",
       members: [
          { _id: 0, host: "127.0.0.1:27051" },
          { _id: 1, host: "127.0.0.1:27052" },
          { _id: 2, host: "127.0.0.1:27053" }
       ]
    })

     

    5. 유저 생성 - 몽구스 제외 각 ReplicaSet Primary

    - admin DB 를 지정한다.

    use admin
    config:PRIMARY> admin = db.getSiblingDB("admin")
    
    config:PRIMARY> admin.createUser(
     {
     user: "admin",
     pwd: "admin",
     roles: [ { role: "userAdminAnyDatabase", db: "admin" }]
     }
    )
    db.getSiblingDB("admin").createUser(
      {
        "user" : "clusterAdmin",
        "pwd" : "clusterAdmin",
        roles: [ { "role" : "clusterAdmin", "db" : "admin" } ]
      }
    )

     

    6. Key file 생성

    openssl rand –base64 400 > /폴더경로/mongodb-keyfile

    - (주의) chmod 400 아닐시 실행 되지 않음!

     

    7. Key file 모든 Config 폴더 아래 Copy

     

    8. Key file 관련 주석 해제 후 다시 실행

    - Config Server, Shard01, 02, 03 서버 하위 .conf 파일

    #security:
    #  keyFile: /mongodb/shard01/mongodb-keyfile
    #  authorization: enabled
    
    -----------------------------------------------
    
    security:
      keyFile: /mongodb/shard01/mongodb-keyfile
      authorization: enabled

    - 실행

    ./mongo --port=27050

     

    9. Mongos 실행 후 addShard 및 enableSharding

    - admin DB 를 지정 후 계정 접근

    use admin
    db.auth("user","pw")

    - 3개의 ReplicaSet 을 addShard

    sh.addShard("shard01/127.0.0.1:27054, 127.0.0.1:27055, 127.0.0.1:27056")
    sh.addShard("shard02/127.0.0.1:27057, 127.0.0.1:27058, 127.0.0.1:27059")
    sh.addShard("shard03/127.0.0.1:27060, 127.0.0.1:27061, 127.0.0.1:27062")
    sh.enableSharding(“db이름”)

     

    10. 특정 컬렉션 hashed 설정

    sh.shardCollection("dbName.collection", {"_id": "hashed"})

    - 컬렉션이 가지고 있는 키 중에서 하나를 지정하여 hashed (또는 Range) 방식으로 설정한다.

     

     

    확인

    mongos> use admin
    mongos> db.auth("","")
    mongos> sh.status()
    --- Sharding Status ---
      sharding version: {
            "_id" : 1,
            "minCompatibleVersion" : 5,
            "currentVersion" : 6,
            "clusterId" : ObjectId("60f0e7271cb7762864928188")
      }
      shards:
            {  "_id" : "shard00",  "host" : "shard01/127.0.0.1:27054,127.0.0.1:27055,127.0.0.1:27056",  "state" : 1 }
            {  "_id" : "shard01",  "host" : "shard02/127.0.0.1:27057,127.0.0.1:27058,127.0.0.1:27059",  "state" : 1 }
            {  "_id" : "shard02",  "host" : "shard03/127.0.0.1:27060,127.0.0.1:27061,127.0.0.1:27062",  "state" : 1 }

     

     

     

Designed by Tistory.