Helm으로 MongoDB 설치하고, 접속하기

지니박박구리·2022년 4월 26일
1
post-thumbnail

쿠버네티스 환경에서 Helm으로 mongoDB를 손쉽게 배포하는 방법을 알아보겠습니다!

MongoDB Helm Repo :
https://github.com/bitnami/charts/tree/master/bitnami/mongodb

실습 환경 :

  • AWS CentOS 인스턴스
  • minikube 클러스터

⭐️ MongoDB 설치

1. Namespace 생성

kubectl create ns mongo

2. mongo Repo 추가

helm repo add bitnami https://charts.bitnami.com/bitnami

3. value 파일 작성

values-mongo.yaml

mage:
  registry: docker.io
  repository: bitnami/mongodb
  tag: 4.4.4-debian-10-r41
  pullPolicy: IfNotPresent

architecture: standalone

useStatefulSet: true

## MongoDB(R) Authentication parameters
auth:
  enabled: true
  rootPassword: "yourpasswd"
  username: admin
  password: yourpasswd
  database: sample

##
persistence:
  enabled: true
  storageClass: "nfs-standard"
  accessModes:
    - ReadWriteOnce
  size: 2Gi

service:
  type: NodePort
  port: 27017
  portName: mongodb

저는 인증 정보, 볼륨, 서비스(노드포트)에 대해 정의해주었습니다.
자세한 파라미터 정보는 파라미터 정리 표, values.yaml을 참고해주세요.


4. Helm install

helm install mongodb-tset bitnami/mongodb-sharded --namespace mongo --values values-mongo.yaml

NAME: mongodb-tset
LAST DEPLOYED: Tue Apr 26 15:58:06 2022
NAMESPACE: mongo
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: mongodb-sharded
CHART VERSION: 4.0.21
APP VERSION: 4.4.13

** 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:

    mongodb-tset-mongodb-sharded.mongo.svc.cluster.local

To get the root password run:

    export MONGODB_ROOT_PASSWORD=$(kubectl get secret --namespace mongo mongodb-tset-mongodb-sharded -o jsonpath="{.data.mongodb-root-password}" | base64 --decode)

To connect to your database run the following command:

    kubectl run --namespace mongo mongodb-tset-mongodb-sharded-client --rm --tty -i --restart='Never' --image docker.io/bitnami/mongodb-sharded:4.4.13-debian-10-r50 --command -- mongo admin --host mongodb-tset-mongodb-sharded

To connect to your database from outside the cluster execute the following commands:

    export NODE_IP=$(kubectl get nodes --namespace mongo -o jsonpath="{.items[0].status.addresses[0].address}")
    export NODE_PORT=$(kubectl get --namespace mongo -o jsonpath="{.spec.ports[0].nodePort}" services mongodb-tset-mongodb-sharded)
    mongo --host $NODE_IP --port $NODE_PORT --authenticationDatabase admin -p $MONGODB_ROOT_PASSWORD

kubernetes에 배포가 완료되면
mongoDB 접속과 관련된 몇가지 명령어를 알려준다.

  • To get the root password run:
    secret에 저장되어 있는 root password를 복호한 값을 가져온다

  • To connect to your database run the following command:
    kubectl로 데이터베이스에 직접 접속한다.

  • To connect to your database from outside the cluster execute the following commands:
    쿠버네티스 클러스터 외부에서 접속할 수 있는 IP주소와 포트 번호를 알려준다.


5. 배포된 리소스 확인

kubectl get all -n mongo
  
NAME                                                       READY   STATUS    RESTARTS      AGE
pod/mongodb-tset-mongodb-sharded-configsvr-0               1/1     Running   0             95m
pod/mongodb-tset-mongodb-sharded-mongos-6cccfd844b-7dd7l   1/1     Running   0             95m
pod/mongodb-tset-mongodb-sharded-shard0-data-0             1/1     Running   1 (92m ago)   95m
pod/mongodb-tset-mongodb-sharded-shard1-data-0             1/1     Running   0             95m

NAME                                            TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)           AGE
service/mongodb-tset-mongodb-sharded            NodePort    10.96.8.157   <none>        27017:30631/TCP   95m
service/mongodb-tset-mongodb-sharded-headless   ClusterIP   None          <none>        27017/TCP         95m

NAME                                                  READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/mongodb-tset-mongodb-sharded-mongos   1/1     1            1           95m

NAME                                                             DESIRED   CURRENT   READY   AGE
replicaset.apps/mongodb-tset-mongodb-sharded-mongos-6cccfd844b   1         1         1       95m

NAME                                                        READY   AGE
statefulset.apps/mongodb-tset-mongodb-sharded-configsvr     1/1     95m
statefulset.apps/mongodb-tset-mongodb-sharded-shard0-data   1/1     95m
statefulset.apps/mongodb-tset-mongodb-sharded-shard1-data   1/1     95m

파드도 정상적으로 배포되었고, 서비스도 NodePort로 배포되었다~!



⭐️ Mongo DB 접속

이제 DB에 접속해보자!

[방법1] kubectl로 MongoDB 접속

kubectl run --namespace mongo mongodb-tset-mongodb-sharded-client --rm --tty -i --restart='Never' --image docker.io/bitnami/mongodb-sharded:4.4.13-debian-10-r50 --command -- mongo admin --host mongodb-tset-mongodb-sharded
  
If you don't see a command prompt, try pressing enter.
mongos>

[방법2] 클러스터 외부에서 MongoDB 접속

다음 명령을 사용하여 Node application의 URL을 가져온다.

    export NODE_IP=$(kubectl get nodes --namespace mongo -o jsonpath="{.items[0].status.addresses[0].address}")
    export NODE_PORT=$(kubectl get --namespace mongo -o jsonpath="{.spec.ports[0].nodePort}" services mongodb-tset-mongodb-sharded)
    mongo --host $NODE_IP --port $NODE_PORT --authenticationDatabase admin -p $MONGODB_ROOT_PASSWORD

따라서 minikube가 설치된 호스트에서 mongoDB에 접속할 수 있다.

mongo --host $NODE_IP --port $NODE_PORT --authenticationDatabase admin -p $MONGODB_ROOT_PASSWORD

MongoDB shell version v5.0.8
connecting to: mongodb://192.168.49.2:30631/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a808d91d-18f1-472f-8717-824c3f190b40") }
MongoDB server version: 4.4.13
WARNING: shell and server versions do not match
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
mongos>

[방법3] Public IP로 접속하기

현재 실습환경이 minikube이기 때문에 서비스에 로드밸런서를 붙일 수 없다.
따라서 Public IP로 접속이 불가하다💦

하지만 EKS, GKE와 같은 환경에서 서비스 타입을 로드밸런서로 지정하면 외부에서 접속할 수 있는 퍼블릭한(External) IP를 얻을 수 있다.

export SERVICE_IP=$(kubectl get svc --namespace default my-todo-app-mean -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo http://$SERVICE_IP/
profile
킹왕짱 Cloud Engineer 지니박박구리 블로그

0개의 댓글