[mongodb] shard cluster 구성

DB 2019. 7. 18. 20:41

 

 

- 첨부파일

shard.tar.gz
다운로드
Program.cs
다운로드

 

 

 

* shard cluster 구성

 

replica set 2세트 (PSS 방식으로 총 6개 mongod 실행)

config server 3대

router(mongos) 3대

 

* 장비를 아끼기 위해 1대의 머신에 router, config, arbiter를 같이 설치할 수 있음

 

 

- replica set 2개 설치

PSA 혹은 PSS 방식으로 설치 (PSS 방식으로 설치함)

 

- conf 파일에 shard 임을 설정

 sharding:
    clusterRole: shardsvr

 

- 모두 구동

shard/rs/run_rs.sh

 

- rs1 설정 및 확인

> mongo localhost:27020/admin
rs.initiate(
   {
      _id: "rs1",
      version: 1,
      members: [
         { _id: 0, host : "192.168.56.101:27020" },
         { _id: 1, host : "192.168.56.101:27030" },
         { _id: 2, host : "192.168.56.101:27040" }
      ]
   }
);

// 상태 확인
rs.status();

// root 계정 생성
use admin;
db.createUser({user: "user1", pwd: "user1", roles: ["root" ]});

 

 

- rs2 설정 및 확인

> mongo localhost:27120/admin
rs.initiate(
   {
      _id: "rs2",
      version: 1,
      members: [
         { _id: 0, host : "192.168.56.101:27120" },
         { _id: 1, host : "192.168.56.101:27130" },
         { _id: 2, host : "192.168.56.101:27140" }
      ]
   }
);

// 상태 확인
rs.status();

// root 계정 생성
use admin;
db.createUser({user: "user1", pwd: "user1", roles: ["root" ]});

 

 

 

 

- config server 설치

 

하나의 샤드 클러스터 내에 하나의 그룹만 설치되며 여러대 설치한다면 서로 복제됨.

PSS 방식으로 설치 필요.

 

- conf 파일 작성 (첨부파일)

 

- 설정 및 확인

> mongo localhost:27011/admin
rs.initiate(
 {
    _id: "cstest",
    version: 1,
    members: [
       { _id: 0, host : "192.168.56.101:27011" },
       { _id: 1, host : "192.168.56.101:27012" },
       { _id: 2, host : "192.168.56.101:27013" }
    ]
 }
);

rs.status();

 

 

 

 

 

- router(mongos) 설치

- mongos.conf 설정파일 작성 (첨부파일)

- 구동

> mongos -f 설정파일

 

- 종료 : mongo shell 접속 후 shutdown 명령 실행

> mongo localhost:27016/admin

db.shutdownServer();

 

- 설정 (in mongo shell)

primary db 로 사용할 곳 한군데에서만 설정하면 됨.

> mongo localhost:27016/admin

 

- replica set

sh.addShard("rs1/192.168.56.101:27020,192.168.56.101:27030,192.168.56.101:27040")
sh.addShard("rs2/192.168.56.101:27120,192.168.56.101:27130,192.168.56.101:27140")


// 상태 확인
sh.status();

// root 계정 생성
use admin;
db.createUser({user: "user1", pwd: "user1", roles: ["root" ]});

 

 

 

 

- 테스트 1

- mongo shell

- 1번 router 접속하여 데어터 입력

router1> mongo localhost:27016

use user2;
db.col1.insert({"test" : "ok"});

 

- 1,2,3번 router에서 조회

router1> mongo localhost:27016
router2> mongo localhost:27017
router3> mongo localhost:27018

use user2;
db.col1.find();

 

 

- replica set 1,2의 primary 에서 조회 (둘 중 한곳에 저장됨)

RS1 P> mongo localhost:27020
RS2 P> mongo localhost:27120

 

 

 

 

 

- shard key 설정

 

특정 db의 collection에 대해 샤딩을 활성화 시켜야 한다.

안그러면 한쪽 replica set에만 저장된다.

 

기본으로 샤딩에 object id(_id) 로 샤딩하는데 특정시간에 부하가 몰리면 하나의 replica set으로 부하가 몰릴 수 있다.

그래서 hashed shard key를 사용할 것이다.

아래와 같이 test db의 col1 컬렉션에 대해 hashed shard key를 지정한다.

mongos> 

use test;

// test 디비 샤딩 활성화
sh.enableSharding("test");

// collection 생성
db.createCollection('col1')

// index 생성
db.col1.ensureIndex({_id: "hashed"})

// object id(_id) 를 hashed shard key 로 지정
sh.shardCollection("test.col1", {"_id": "hashed"})

 

 

 

- 테스트 2

hashed shard key 지정 후 테스트

 

- 콘솔 앱(.Net core) 프로젝트

https://www.nuget.org/packages/mongodb.driver 설치

 

엔터 칠때마다 insert 수행

using System;

namespace DotNetConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            // mongos 주소
            var client = new MongoDB.Driver.MongoClient(
                "mongodb://192.168.56.101:27016,192.168.56.101:27017,192.168.56.101:27018");

            //var settings = new MongoDB.Driver.MongoClientSettings
            //{
            //    Servers = new[]
            //    {
            //            new MongoDB.Driver.MongoServerAddress("192.168.56.101", 27016),
            //            new MongoDB.Driver.MongoServerAddress("192.168.56.101", 27017),
            //            new MongoDB.Driver.MongoServerAddress("192.168.56.101", 27018),
            //        },
            //    MinConnectionPoolSize = 10
            //};
            //var client = new MongoDB.Driver.MongoClient(settings);

            var db = client.GetDatabase("test");
                var col = db.GetCollection<MongoDB.Bson.BsonDocument>("col1");

                while (true)
                {
                    try
                    {

                        Console.WriteLine("ENTER");
                        Console.ReadLine();

                        var doc = new MongoDB.Bson.BsonDocument
                        {
                            { "name", "dot net core" },
                        };

                        col.InsertOne(doc);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }

            }

        }
    }
}

 

 

- replica set 1,2의 primary 에서 조회

양쪽에 골고루 저장 되어있는지 확인한다.

 

RS1 P> mongo localhost:27020

RS2 P> mongo localhost:27120