본문 바로가기
Kubernetes/NoSQL

MongoDB Sharded by Bitnami

by 여행을 떠나자! 2021. 11. 3.

1. 개요

- MongDB 

   ✓ MongoDB는 크로스 플랫폼 도큐먼트 지향 데이터베이스 시스템이다.

   ✓ NoSQL 데이터베이스로 분류되는 MongoDB는 JSON과 같은 동적 스키마형 도큐먼트들을 선호한다.

 

- MongoDB Replica sets vs Sharded cluster

    https://severalnines.com/database-blog/turning-mongodb-replica-set-sharded-cluster

   a. Replica sets

       Replica Sets are a great way to replicate MongoDB data across multiple servers and have the database automatically failover in case of server failure.

   b. Sharded

       Sharding is a way to split data across multiple servers. In a MongoDB Sharded Cluster, the database will handle distribution of the data and dynamically load-balance queries.

          MongoDB stores documents in collections. Collections are analogous to tables in relational databases.

       Config servers store the meta data for the sharded cluster.

       Routers(mongos) are the processes that clients connect to, and queries are then routed to the appropriate shard.

 

- MongoDB Sharded helm chart by Bitnami

   This chart bootstraps a  deployment on a  cluster using the  package manager.

 

 

2. 환경

- bitnami/mongodb-sharded 3.9.14

- MongoDB 4.4.10

- Helm 3.3.1

- Kubernetes 1.20.11, 1.16.15

 

 

3. 구성

- Helm chart repository 추가 및 MongoDB sharded 검색

$ helm repo add bitnami https://charts.bitnami.com/bitnami
"bitnami" has been added to your repositories
$ helm repo list
NAME                	URL
bitnami             	https://charts.bitnami.com/bitnami
…
$ helm repo update
…
...Successfully got an update from the "bitnami" chart repository
...Successfully got an update from the "nvidia" chart repository
Update Complete. ⎈Happy Helming!⎈
$ helm search repo mongodb
NAME                                            	CHART VERSION	APP VERSION	DESCRIPTION
bitnami/mongodb                                 	10.29.0      	4.4.10     	NoSQL document-oriented database that stores JS...
bitnami/mongodb-sharded                         	3.9.14       	4.4.10     	NoSQL document-oriented database that stores JS...
prometheus-community/prometheus-mongodb-exporter	2.8.1        	v0.10.0    	A Prometheus exporter for MongoDB metrics
stable/mongodb                                  	7.8.10       	4.2.4      	DEPRECATED NoSQL document-oriented database tha...
stable/mongodb-replicaset                       	3.17.2       	3.6        	DEPRECATED - NoSQL document-oriented database t...
stable/prometheus-mongodb-exporter              	2.8.1        	v0.10.0    	DEPRECATED A Prometheus exporter for MongoDB me...
bitnami/mean                                    	6.1.2        	4.6.2      	DEPRECATED MEAN is a free and open-source JavaS...
stable/unifi                                    	0.10.2       	5.12.35    	DEPRECATED - Ubiquiti Network's Unifi Controller$

 

- helm chart용 parametes yaml 파일 생성 및 변경

   https://artifacthub.io/packages/helm/bitnami/mongodb-sharded#parameters

   mongodbRootPassword와 replicaSetKey 값 설정을 권고한다. 미 설정시 랜덤하게 생성되며, 향후 'helm upgrade' 명령어로 설정 변경 시 자동으로 변경되는 문제가 발생된다.

$ helm inspect values bitnami/mongodb-sharded --version 3.9.14 > mongodb-sharded_3.9.14_yoosung-jeon.values
$ vi mongodb-sharded_3.9.14_yoosung-jeon.valuesmongodb-sharded_3.9.14_yoosung-jeon.values 
      ...
079:  ## @param mongodbRootPassword MongoDB® root password
080:  ## If set to null it will be randomly generated
085:  mongodbRootPassword: "root1234"
086:  ## @param replicaSetKey Replica Set key (shared for shards and config servers)
090:  replicaSetKey: "repl1234"
      ...
099:  ## @param shards Number of shards to be created
102:  shards: 3
      ...
327:  configsvr:
328:    ## @param configsvr.replicas Number of nodes in the replica set (the first node will be primary)
330:    replicas: 2
      ...
534:  mongos:                                             
535:    ## @param mongos.replicas Number of replicas    
537:    replicas: 2
      ...
747:  shardsvr:
748:    ## Properties for data nodes (primary and secondary)
750:    dataNode:
751:      ## @param shardsvr.dataNode.replicas Number of nodes in each shard replica set (the first node will be primary)
753:      replicas: 2
      ...      
$

 

- MongoDB sharded 설치

$ helm install ysjeon-mongodb bitnami/mongodb-sharded --namespace yoosung-jeon --values mongodb-sharded_3.9.14_yoosung-jeon.values
NAME: ysjeon-mongodb
LAST DEPLOYED: Wed Nov  3 11:21:50 2021
NAMESPACE: yoosung-jeon
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: mongodb-sharded
CHART VERSION: 3.9.14
APP VERSION: 4.4.10

** Please be patient while the chart is being deployed **

The MongoDB® Sharded cluster can be accessed via the Mongos instances in port 27017 on the following DNS name from within your cluster:
    ysjeon-mongodb-mongodb-sharded.yoosung-jeon.svc.cluster.local

To get the root password run:
    export MONGODB_ROOT_PASSWORD=$(kubectl get secret --namespace yoosung-jeon ysjeon-mongodb-mongodb-sharded -o jsonpath="{.data.mongodb-root-password}" | base64 --decode)

To connect to your database run the following command:
    kubectl run --namespace yoosung-jeon ysjeon-mongodb-mongodb-sharded-client --rm --tty -i --restart='Never' --image docker.io/bitnami/mongodb-sharded:4.4.10-debian-10-r15 --command -- mongo admin --host ysjeon-mongodb-mongodb-sharded

To connect to your database from outside the cluster execute the following commands:
    kubectl port-forward --namespace yoosung-jeon svc/ysjeon-mongodb-mongodb-sharded 27017:27017 &
    mongo --host 127.0.0.1 --authenticationDatabase admin -p $MONGODB_ROOT_PASSWORD$
$ helm list -n yoosung-jeon | egrep 'NAME|mongodb'
NAME          	NAMESPACE   	REVISION	UPDATED                             	STATUS  	CHART                 	APP VERSION
ysjeon-mongodb	yoosung-jeon	1       	2021-11-03 11:23:45.247177 +0900 KST	deployed	mongodb-sharded-3.9.14	4.4.10
$

 

- MongoDB service type 변경

   Kubernetes 외부에서 접속할 수 있도록 MongoDB Service type을 NodePort로 변경한다. 

   할당된 NodePort는 30997이며, IP주소는 Kubernetes Master/Worker node IP를 사용하면 된다.

$ kubectl patch service ysjeon-mongodb-mongodb-sharded -n yoosung-jeon -p '{ "spec": { "type": "NodePort" } }'
service/ysjeon-mongodb-mongodb-sharded patched
$ kubectl get svc -n yoosung-jeon | egrep 'NAME|mongodb'
NAME                                      TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)           AGE
ysjeon-mongodb-mongodb-sharded            NodePort    10.106.64.229   <none>        27017:30997/TCP   123m
ysjeon-mongodb-mongodb-sharded-headless   ClusterIP   None            <none>        27017/TCP         123m
$

 

- MongoDB 설치 결과 확인

$ k get pod -l app.kubernetes.io/name=mongodb-sharded -n yoosung-jeon
NAME                                                    READY   STATUS    RESTARTS   AGE
ysjeon-mongodb-mongodb-sharded-configsvr-0              1/1     Running   0          42m
ysjeon-mongodb-mongodb-sharded-configsvr-1              1/1     Running   0          41m
ysjeon-mongodb-mongodb-sharded-mongos-54f4d86d4-6vwcx   1/1     Running   0          42m
ysjeon-mongodb-mongodb-sharded-mongos-54f4d86d4-xbsbg   1/1     Running   0          42m
ysjeon-mongodb-mongodb-sharded-shard0-data-0            1/1     Running   0          42m
ysjeon-mongodb-mongodb-sharded-shard0-data-1            1/1     Running   0          41m
ysjeon-mongodb-mongodb-sharded-shard1-data-0            1/1     Running   0          42m
ysjeon-mongodb-mongodb-sharded-shard1-data-1            1/1     Running   0          41m
ysjeon-mongodb-mongodb-sharded-shard2-data-0            1/1     Running   0          42m
ysjeon-mongodb-mongodb-sharded-shard2-data-1            1/1     Running   0          40m
$ k get pvc -n yoosung-jeon | egrep 'NAME|mongodb'
NAME                                                   STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
datadir-ysjeon-mongodb-mongodb-sharded-configsvr-0     Bound    pvc-1c251d54-11f5-4b6a-adeb-7e7cbfc54cef   8Gi        RWO            nfs-sc-iap     43m
datadir-ysjeon-mongodb-mongodb-sharded-configsvr-1     Bound    pvc-2f3adeab-b7aa-4864-a46e-0eff88930644   8Gi        RWO            nfs-sc-iap     42m
datadir-ysjeon-mongodb-mongodb-sharded-shard0-data-0   Bound    pvc-366395bb-e303-4b3c-9588-92252075a38b   8Gi        RWO            nfs-sc-iap     43m
datadir-ysjeon-mongodb-mongodb-sharded-shard0-data-1   Bound    pvc-983a146d-9708-4300-97c0-f2804dc52063   8Gi        RWO            nfs-sc-iap     41m
datadir-ysjeon-mongodb-mongodb-sharded-shard1-data-0   Bound    pvc-dced0577-697e-4310-a9e6-3e2ab8a61149   8Gi        RWO            nfs-sc-iap     43m
datadir-ysjeon-mongodb-mongodb-sharded-shard1-data-1   Bound    pvc-5d886770-a64e-4199-a9b4-39b525e4a720   8Gi        RWO            nfs-sc-iap     41m
datadir-ysjeon-mongodb-mongodb-sharded-shard2-data-0   Bound    pvc-c5ae1516-3e48-4550-9405-28a6503592fd   8Gi        RWO            nfs-sc-iap     43m
datadir-ysjeon-mongodb-mongodb-sharded-shard2-data-1   Bound    pvc-66b6f243-2e1d-4989-a0e4-88355b71587a   8Gi        RWO            nfs-sc-iap     41m
$

 

- MongoDB 접속 및 테스트

   ✓ Databases(db) hold one or more collections of documents.

   MongoDB stores documents in collections. Collections are analogous to tables in relational databases.

https://docs.mongodb.com/manual/images/crud-annotated-collection.bakedsvg.svg

$ kubectl get secret -n yoosung-jeon ysjeon-mongodb-mongodb-sharded -o jsonpath="{.data.mongodb-root-password}" | base64 --decode
wLF4Ko6cvG
$ kubectl run -n yoosung-jeon ysjeon-mongodb-mongodb-sharded-client --rm --tty -i --restart='Never' --image docker.io/bitnami/mongodb-sharded:4.4.10-debian-10-r15 --command -- bash
I have no name!@ysjeon-mongodb-mongodb-sharded-client:/$ mongo admin --host ysjeon-mongodb-mongodb-sharded -u root -p
MongoDB shell version v4.4.10
Enter password:
connecting to: mongodb://ysjeon-mongodb-mongodb-sharded:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b44ce100-668f-4265-bbde-57431087406e") }
MongoDB server version: 4.4.10
mongos> db
admin
mongos> db.user.insertOne({username:"ysjeon", password:"111111"})
{
	"acknowledged" : true,
	"insertedId" : ObjectId("61820cbe6745c016e88ba770")
}
mongos> show collections
system.users
system.version
user
mongos> exit
bye
I have no name!@ysjeon-mongodb-mongodb-sharded-client:/$

 

- MongoDB sharding 내용 조회

mongos> sh.status();
--- Sharding Status ---
  sharding version: {
  	"_id" : 1,
  	"minCompatibleVersion" : 5,
  	"currentVersion" : 6,
  	"clusterId" : ObjectId("618226d716b26ae4e23a42f6")
  }
  shards:
        {  "_id" : "ysjeon-mongodb-mongodb-sharded-shard-0",  "host" : "ysjeon-mongodb-mongodb-sharded-shard-0/ysjeon-mongodb-mongodb-sharded-shard0-data-0.ysjeon-mongodb-mongodb-sharded-headless.yoosung-jeon.svc.cluster.local:27017,ysjeon-mongodb-mongodb-sharded-shard0-data-1.ysjeon-mongodb-mongodb-sharded-headless.yoosung-jeon.svc.cluster.local:27017",  "state" : 1 }
        {  "_id" : "ysjeon-mongodb-mongodb-sharded-shard-1",  "host" : "ysjeon-mongodb-mongodb-sharded-shard-1/ysjeon-mongodb-mongodb-sharded-shard1-data-0.ysjeon-mongodb-mongodb-sharded-headless.yoosung-jeon.svc.cluster.local:27017,ysjeon-mongodb-mongodb-sharded-shard1-data-1.ysjeon-mongodb-mongodb-sharded-headless.yoosung-jeon.svc.cluster.local:27017",  "state" : 1 }
        {  "_id" : "ysjeon-mongodb-mongodb-sharded-shard-2",  "host" : "ysjeon-mongodb-mongodb-sharded-shard-2/ysjeon-mongodb-mongodb-sharded-shard2-data-0.ysjeon-mongodb-mongodb-sharded-headless.yoosung-jeon.svc.cluster.local:27017,ysjeon-mongodb-mongodb-sharded-shard2-data-1.ysjeon-mongodb-mongodb-sharded-headless.yoosung-jeon.svc.cluster.local:27017",  "state" : 1 }
  active mongoses:
        "4.4.10" : 2
  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:
                682 : Success
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                ysjeon-mongodb-mongodb-sharded-shard-0	342
                                ysjeon-mongodb-mongodb-sharded-shard-1	341
                                ysjeon-mongodb-mongodb-sharded-shard-2	341
                        too many chunks to print, use verbose if you want to force print
 mongos>

 

 

4. MongoDB Compass

a. Compass ?

- MongoDB Compass is a powerful GUI for querying, aggregating, and analyzing your MongoDB data in a visual environment.

- supported operation system: Windows, macOS, Linux

- https://docs.mongodb.com/compass/current/

 

b. MacOS용 설치

   다운로드: https://downloads.mongodb.com/compass/mongodb-compass-1.28.4-darwin-x64.dmg

   설치: mongodb-compass-1.28.4-darwin-x64

 

c. MongDB 접속

- New Connection 

   ✓ 형식: mongodb://username:password@{kubernetes IP}:{MongoDB NodePort}/admin

   ✓ ysjeon MongoDB : mongodb://root:wLF4Ko6cvG@api.acp.kt.co.kr:30997/admin

       Kubernetes IP: api.acp.kt.co.kr (ACP API Server의 Load balancer IP)

 

- Collection 조회

댓글