'REPLICA'에 해당하는 글 1건

지난번 kubernetes 에서 mongodb replica set 세팅 하는 방법을 정리했다. ( https://blog.1day1.org/598 )

그런데, 이제 mongo db 내부에서 replica set 을 설정한다.

(kube => pod 접속)
$ kubectl exec -ti mongod-0 -c mongod-container bash
   or
$ kubectl exec -ti mongod-0 -- bash

(pod 내에서 mongo console 실행)
# mongo

처음에는 다음처럼 나온다.

rs.status()
{
	"ok" : 0,
	"errmsg" : "no replset config has been received",
	"code" : 94,
	"codeName" : "NotYetInitialized"
}

리플리카 셋을 설정한다.

config = {_id: "MainRepSet", version: 1, members: [
       { _id: 0, host : "mongod-0.mongodb-service:27017" },
       { _id: 1, host : "mongod-1.mongodb-service:27017" },
       { _id: 2, host : "mongod-2.mongodb-service:27017" },
       { _id: 3, host : "mongod-3.mongodb-service:27017" }
 ]}
 rs.initiate(config)

초기 설정 후 관리계정을 만들어 준다.

use admin
db.createUser({user:"admin",pwd:passwordPrompt(),roles:[{role:"root",db:"admin"}]})

(사용은)
db.getSiblingDB('admin').auth("admin", passwordPrompt())

(수정)
db.updateUser("admin", {pwd:passwordPrompt()})

정상 세팅은 다음 처럼 나온다.

더보기

MainRepSet:PRIMARY> rs.status()
{
"set" : "MainRepSet",
"date" : ISODate("2020-02-20T08:39:06.434Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 3,
"writeMajorityCount" : 3,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1582187942, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2020-02-20T08:39:02.691Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1582187942, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2020-02-20T08:39:02.691Z"),
"appliedOpTime" : {
"ts" : Timestamp(1582187942, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1582187942, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2020-02-20T08:39:02.691Z"),
"lastDurableWallTime" : ISODate("2020-02-20T08:39:02.691Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1582187912, 3),
"lastStableCheckpointTimestamp" : Timestamp(1582187912, 3),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2020-02-20T08:38:32.108Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1582187901, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 3,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2020-02-20T08:38:32.690Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2020-02-20T08:38:33.290Z")
},
"members" : [
{
"_id" : 0,
"name" : "mongod-0.mongodb-service:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 805,
"optime" : {
"ts" : Timestamp(1582187942, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-02-20T08:39:02Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1582187912, 1),
"electionDate" : ISODate("2020-02-20T08:38:32Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "mongod-1.mongodb-service:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 44,
"optime" : {
"ts" : Timestamp(1582187942, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1582187942, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-02-20T08:39:02Z"),
"optimeDurableDate" : ISODate("2020-02-20T08:39:02Z"),
"lastHeartbeat" : ISODate("2020-02-20T08:39:06.193Z"),
"lastHeartbeatRecv" : ISODate("2020-02-20T08:39:05.256Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncingTo" : "mongod-0.mongodb-service:27017",
"syncSourceHost" : "mongod-0.mongodb-service:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "mongod-2.mongodb-service:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 44,
"optime" : {
"ts" : Timestamp(1582187942, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1582187942, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-02-20T08:39:02Z"),
"optimeDurableDate" : ISODate("2020-02-20T08:39:02Z"),
"lastHeartbeat" : ISODate("2020-02-20T08:39:06.187Z"),
"lastHeartbeatRecv" : ISODate("2020-02-20T08:39:05.581Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "mongod-0.mongodb-service:27017",
"syncSourceHost" : "mongod-0.mongodb-service:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 3,
"name" : "mongod-3.mongodb-service:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 44,
"optime" : {
"ts" : Timestamp(1582187942, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1582187942, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-02-20T08:39:02Z"),
"optimeDurableDate" : ISODate("2020-02-20T08:39:02Z"),
"lastHeartbeat" : ISODate("2020-02-20T08:39:06.201Z"),
"lastHeartbeatRecv" : ISODate("2020-02-20T08:39:05.256Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncingTo" : "mongod-0.mongodb-service:27017",
"syncSourceHost" : "mongod-0.mongodb-service:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1582187942, 1),
"signature" : {
"hash" : BinData(0,"pHhOTNJaI3y8hvPOpgcOs9zJ7I8="),
"keyId" : NumberLong("6795445338166525955")
}
},
"operationTime" : Timestamp(1582187942, 1)
}


이후에 primary 에서 데이터를 등록하면 secondary 에 반영된다.

kubernetes 에서  delete / apply 등으로 새로 세팅하거나 했을때 기존 설정이 정상 동작 하지 않는 경우.
Primary 가 없거나 하는 경우.

Our replica set config is invalid or we are not a member of it

설정을 재등록(?) 해준다. 

conf = rs.conf()
rs.reconfig(conf, {force:true})

 

[추가] 몇몇 주요 명령

1. replica set 추가

rs.add("mongod-3.mongodb-service:27017")

2. sync 상태 보기

PRIMARY> rs.printSlaveReplicationInfo()

source: mongod-1.mongodb-service:27017
	syncedTo: Fri Feb 28 2020 02:56:26 GMT+0000 (UTC)
	0 secs (0 hrs) behind the primary 
source: mongod-2.mongodb-service:27017
	syncedTo: Fri Feb 28 2020 02:56:26 GMT+0000 (UTC)
	0 secs (0 hrs) behind the primary 
source: mongod-3.mongodb-service:27017
	syncedTo: Fri Feb 28 2020 02:56:26 GMT+0000 (UTC)
	0 secs (0 hrs) behind the primary 

3. 특정노드 에러메시지 (해결책 - 찾는 중)

PRIMARY> rs.status()
..
"stateStr" : "(not reachable/healthy)",
..
"lastHeartbeatMessage" : "Our replica set configuration is invalid or does not include us",
..

PRIMARY> rs.printSlaveReplicationInfo()
...
...
source: mongod-0.mongodb-service:27017
	syncedTo: Thu Jan 01 1970 00:00:00 GMT+0000 (UTC)
	1584736418 secs (440204.56 hrs) behind the primary 

원인이 뭘까? 특별히 비정상 종료 같은 것은 없던 것 같은데. config 서버를 따로 두지 않는 문제일까?

  해결책 1) 해당 pvc / pod 를 삭제한다 => 쿠버네티스 가 알아서 재가동 하면서 정상 처리 된다. (옳은 방법인지는 의문)

kubectl delete pvc mongodb-persistent-storage-claim-mongod-0
kubectl delete pod/mongod-0

 

 

 

반응형

WRITTEN BY
1day1
하루하루 즐거운일 하나씩, 행복한일 하나씩 만들어 가요.

,