[mongoDB] Clustering / Shading

Use_Silver·2022년 1월 19일
0

mongoDB

목록 보기
6/6

Clustering

  • Clustering : 어떤 목적을 위한 집합 LIKE 조편성
    장애가 날 것을 대비해서 클러스터링 하는 것을 의미
  • System / Application Clustering:
    장애 조치 -> HA(고가용성)

    1. 여러대(3대 이상 권장)
    2. Data 전체 복제
    3. 1대가 Primary(Active Server), 나머지 Seconda(Passive)
    4. 설정 서버 구성 필요 (config)
    5. 요청 전송 서버 필요 (Routing 개념)

1) 하나의 시스템이 아니라, 다수의 시스템으로 유지해야 한다.
2) 하나가 사라질 때를 대비해서 똑같은 데이터를 보유하고 있어야 한다.
3) 서비스를 운영중인 애를 선택해두자

  • 누가 일단 서비스를 하고 있을래? Primary를 누가?
  • --> 다 Second..

4) if primary에 장애가 생겼으면,

    1. mash형에서 heartbit으로 장애가 생겼는지 확인함
    1. config라는 설정에 관련된 서버를 하나 둠
      => 구성 정보, db가 어떤게 들어갈지 알고 있음, primary에 장애가 생겼을 경우 config가 확인
      => 대장 뽑아! primary로 다른애 선정
      => 홀수 계산법
      => 여러대, 최소 3대 권장

MongoDB

  1. 그대로 복제되는지 알려면 접속을 해서 collection과 document를 구성해둬야 함 -> 구성한 상태에서 복제가 되는지 확인

  2. 장애를 일으켜봄(시스템 다운시킴) -> 전이가 되는지 확인


실습

db 1, 2, 3 생성

db로 쓸 창 3개 띄우기

  • C:\data\repdb
    mongod --replSet repSet --dbpath "C:\data\repdb\db1" --port 40001
    mongod --replSet repSet --dbpath "C:\data\repdb\db2" --port 40002
    mongod --replSet repSet --dbpath "C:\data\repdb\db3" --port 40003
  • config = {_id : repSet, members : [ {_id:0 , host:"localhost:40001" } ,
    {_id:1 , host:"localhost:40002" } ,
    {_id:3 , host:"localhost:40003" } ]
  • rs.initiate(config)

  • mongo --port 40001

  • mongo --port 40002

  • primary test

  • secondary test
    db조회가 안됨
    db.test.find() 불가

  • primary 종료
    - secondary가 primary 되는지 확인
    - test db collection에 alice가 있는지 확인

  • secondary -> primary로 변경됨

  • Primary port 다시 살렸을 때, Primary로 바뀌는지 확인하기 => 안바뀜, 시스템은 다시 권한 돌려주지 않음

Shading

Shading

[Business Data -> OLTP -> 수정] 원활한 비즈니스 목적

Data 저장, 처리, 관리에는 기준이 있음 -> ACID -> RDB (index, transaction, commit, log ,trigger, lock ...)

  • 원자성(Atomicity) : 예약
  • 일관성(Consistency) : 규칙 유지 -> Data Type, Constraints
  • 고립성(Isolation) : 작업 방어 -> Transaction
  • 지속성(Durability) : (Redo) Log (기록을 남겨 데이터가 들어가지 않은 상태에서 재부팅 가능)

[Analysys Data -> OLAP -> 읽기]

  • BigData -> NoSQL Data -> ACID [X] <= 대량, 구조?
    Volume, Variety, Velocity => BigData 3V
    --> BASE
    - Basically Available : *Replica Set
    - Soft state : 상태 유지
    - Eventually Consistent : 이벤트성 일관성(장애 정도만 잡아주는 조건) -->[O] 구조 [X] --> DataType, 범위

Shading : 나눔(Distribution), 속도(Speed)

  • RDB : 수평적 Table 분할/ 파티션
  • BigData -> NoSQL Data : Shading(여러개 있는지..)

MongoDB Shading Architecture

이미지 출처 : https://www.cloudduggu.com/mongodb/sharding/

  • Router : round robin(LIKE 고스톱, 포커... 나눠주는 개념)
  • Sharding : 나눠주기 (하나의 Shad에서 replica set 나눠줌)

Shading 구성요소: MongoDB 편 (Round Robin)

  • Data 하위 집합 : Shard
  • Data Routing : Mongos
  • 구성설정 : Config Server

실습

[ directory 5개 ]

  mkdir shadb
  			/config1 
  			/config2 
  			/config3 
  				/shard1 
  						/db1
  						/db2
  						/db3
  				/shard2
  						/db1
  						/db2
  						/db3
  
  				/shard3 
    						/db1
  						/db2
  						/db3
  


Config 3대 환경 구성 : --port 31001, --port 31002, --port 31003

mongod --configsvr --replSet configRepl --dbpath "C:\data\shadb\config1" -port 31001
mongod --configsvr --replSet configRepl --dbpath "C:\data\shadb\config2" -port 31002
mongod --configsvr --replSet configRepl --dbpath "C:\data\shadb\config3" -port 31003


mongo localhost:31001 (config01 server 접속)

var config = {

    _id : "configRepl", members : [ 
        {_id : 0, host : 'localhost:31001'},
        {_id : 1, host : 'localhost:31002'},
        {_id : 2, host : 'localhost:31003'}  
    ]
}
var config = {_id : "configRepl", members : [  {_id : 0, host : 'localhost:31001'},{_id : 1, host : 'localhost:31002'},{_id : 2, host : 'localhost:31003'}  ]}
rs.initiate(config)
rs.status()

Shard 3대 구성 :

[ Port ]

1) shard1 --port 41001 / 41002 / 41003
2) shard2 --port 42001 / 42002 / 42003
3) shard3 --port 43001 / 43002 / 43003

[shard1]

mongod --shardsvr --replSet shardRep1 --dbpath "C:\data\shadb\shard1\db1" -port 41001
mongod --shardsvr --replSet shardRep1 --dbpath "C:\data\shadb\shard1\db2" -port 41002
mongod --shardsvr --replSet shardRep1 --dbpath "C:\data\shadb\shard1\db3" -port 41003

mongo localhost:41001 (shardRep1 접속)

var config = {

    _id : "shardRep1", members : [ 
        {_id : 0, host : 'localhost:41001'},
        {_id : 1, host : 'localhost:41002'},
        {_id : 2, host : 'localhost:41003'}  
    ]
}

var config = { _id : "shardRep1", members : [ {_id : 0, host : 'localhost:41001'},{_id : 1, host : 'localhost:41002'}, {_id : 2, host : 'localhost:41003'} ]}

rs.initiate(config)

[shard2]

mongod --shardsvr --replSet shardRep2 --dbpath "C:\data\shadb\shard2\db1" -port 42001
mongod --shardsvr --replSet shardRep2 --dbpath "C:\data\shadb\shard2\db2" -port 42002
mongod --shardsvr --replSet shardRep2 --dbpath "C:\data\shadb\shard2\db3" -port 42003

mongo localhost:42001 (shardRep2 접속)

var config = {

    _id : "shardRep2", members : [ 
        {_id : 0, host : 'localhost:42001'},
        {_id : 1, host : 'localhost:42002'},
        {_id : 2, host : 'localhost:42003'}  
    ]
}

var config = { _id : "shardRep2", members : [ {_id : 0, host : 'localhost:42001'},{_id : 1, host : 'localhost:42002'}, {_id : 2, host : 'localhost:42003'} ]}

rs.initiate(config)

[shard3]

mongod --shardsvr --replSet shardRep3 --dbpath "C:\data\shadb\shard3\db1" -port 43001
mongod --shardsvr --replSet shardRep3 --dbpath "C:\data\shadb\shard3\db2" -port 43002
mongod --shardsvr --replSet shardRep3 --dbpath "C:\data\shadb\shard3\db3" -port 43003

mongo localhost:43001 (shardRep3 접속)

var config = {

    _id : "shardRep3", members : [ 
        {_id : 0, host : 'localhost:43001'},
        {_id : 1, host : 'localhost:43002'},
        {_id : 2, host : 'localhost:43003'}  
    ]
}

var config = { _id : "shardRep3", members : [ {_id : 0, host : 'localhost:43001'},{_id : 1, host : 'localhost:43002'}, {_id : 2, host : 'localhost:43003'} ]}

rs.initiate(config)

routing 구성 : mongos 3대 구성

mongos --configdb configRepl/localhost:31001,localhost:31002,localhost:31003

** 만약 오류 발생할 경우
서비스 -> mongo 켜져있을 경우 -> 죽이고 다시 실행

<cmd 별도>

mongo --> mongos 확인

sh.addShard("shardRep1/localhost:41001")

sh.addShard("shardRep2/localhost:42001")

sh.addShard("shardRep3/localhost:43001")

sh.shardCollection("msTest.person",{_id:"hashed"});

> use msTest
	> show collections
	> for(var i=0; i < 100000 ; i++) { db.person.insert({ number: i, name: "kor"+i }); };

	> db.person.find().count();



```

#. mongodb shard 구성 실습

[ directory 5개 구성 ]

mkdir 	shadb
		/config1
		/config2
		/config3
		/shard1
			/db1
			/db2
			/db3
		/shard2
			/db1
			/db2
			/db3
		/shard3
			/db1
			/db2
			/db3

[cmd 창] 5개 띄움


#. Config 3대 환경 구성

<cmd:빨간색>

mongod --configsvr --replSet configRepl --dbpath "C:\data\shadb\config1" -port 31001
mongod --configsvr --replSet configRepl --dbpath "C:\data\shadb\config2" -port 31002
mongod --configsvr --replSet configRepl --dbpath "C:\data\shadb\config3" -port 31003

mongo localhost:31001 (config01 server 접속)

var config = {

_id : "configRepl", members : [ 
    {_id : 0, host : 'localhost:31001'},
    {_id : 1, host : 'localhost:31002'},
    {_id : 2, host : 'localhost:31003'}  
]

}

var config = { _id : "configRepl", members : [ {_id : 0, host : 'localhost:31001'},{_id : 1, host : 'localhost:31002'}, {_id : 2, host : 'localhost:31003'} ]}

rs.initiate(config)

rs.status()

#. Shard 3대 구성 :
[ Port ]
1) shard1 --port 41001 / 41002 / 41003
2) shard2 --port 42001 / 42002 / 42003
3) shard3 --port 43001 / 43002 / 43003

<cmd [shard1] : 파란색 >
mongod --shardsvr --replSet shardRep1 --dbpath "C:\data\shadb\shard1\db1" -port 41001
mongod --shardsvr --replSet shardRep1 --dbpath "C:\data\shadb\shard1\db2" -port 41002
mongod --shardsvr --replSet shardRep1 --dbpath "C:\data\shadb\shard1\db3" -port 41003

mongo localhost:41001

var config = {

_id : "shardRep1", members : [ 
    {_id : 0, host : 'localhost:41001'},
    {_id : 1, host : 'localhost:41002'},
    {_id : 2, host : 'localhost:41003'}  
]

}

var config = { _id : "shardRep1", members : [ {_id : 0, host : 'localhost:41001'},{_id : 1, host : 'localhost:41002'}, {_id : 2, host : 'localhost:41003'} ]}

rs.initiate(config)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

<cmd [shard2] : 초록색>
mongod --shardsvr --replSet shardRep2 --dbpath "C:\data\shadb\shard2\db1" -port 42001
mongod --shardsvr --replSet shardRep2 --dbpath "C:\data\shadb\shard2\db2" -port 42002
mongod --shardsvr --replSet shardRep2 --dbpath "C:\data\shadb\shard2\db3" -port 42003

mongo localhost:42001

var config = {

_id : "shardRep1", members : [ 
    {_id : 0, host : 'localhost:42001'},
    {_id : 1, host : 'localhost:42002'},
    {_id : 2, host : 'localhost:42003'}  
]

}

var config = { _id : "shardRep2", members : [ {_id : 0, host : 'localhost:42001'},{_id : 1, host : 'localhost:42002'}, {_id : 2, host : 'localhost:42003'} ]}

rs.initiate(config)

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<cmd [shard3] : 보라색>

mongod --shardsvr --replSet shardRep3 --dbpath "C:\data\shadb\shard3\db1" -port 43001
mongod --shardsvr --replSet shardRep3 --dbpath "C:\data\shadb\shard3\db2" -port 43002
mongod --shardsvr --replSet shardRep3 --dbpath "C:\data\shadb\shard3\db3" -port 43003

mongo localhost:43001

var config = {

_id : "shardRep1", members : [ 
    {_id : 0, host : 'localhost:43001'},
    {_id : 1, host : 'localhost:43002'},
    {_id : 2, host : 'localhost:43003'}  
]

}

var config = { _id : "shardRep3", members : [ {_id : 0, host : 'localhost:43001'},{_id : 1, host : 'localhost:43002'}, {_id : 2, host : 'localhost:43003'} ]}

rs.initiate(config)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#. routing 구성 : mongos 3대 구성

mongos --configdb configRepl/localhost:31001,localhost:31002,localhost:31003

#. mongos 에서 sharding 구성
<cmd 별도>

mongo --> mongos 확인

sh.addShard("shardRep1/localhost:41001")

sh.addShard("shardRep2/localhost:42001")

sh.addShard("shardRep3/localhost:43001")

sh.enableSharding("msTest")
sh.shardCollection("msTest.person",{_id:"hashed"});

use msTest
show collections
for(var i=0; i < 100000 ; i++) { db.person.insert({ number: i, name: "kor"+i }); };

+++++++++++++++++++ [ Sharding 분배 확인 ] ++++++++++++++

> db.person.find().count();

<cmd 별도>

mongo --port 41001

> show dbs
> use msTest
> show collections
> db.person.find().count()
> db.person.find().skip(1000).limit(10);

<shard2 >
mongo --port 42001

> show dbs
> use msTest
> show collections
> db.person.find().count()
> db.person.find().skip(1000).limit(10);

<shard3 >
mongo --port 41001

> show dbs
> use msTest
> show collections
> db.person.find().count()
> db.person.find().skip(1000).limit(10);
      
 참고     https://kslee7746.tistory.com/category/MongoDB
profile
과정은 힘들지만😨 성장은 즐겁습니다🎵

0개의 댓글