本篇來說明如何在Kubernetes cluster內部署一個MongoDB,相信這種DB解決方案的部署應該都快變成管理員的日常工作之一了,今天透過這個操作演示,讓大家了解在實務上如何把MongoDB成功建立起來,並且還要可以進行基本操作。
本篇將分成以下重點進行說明:
#-----------------------------------------------
# S1-1. 取得所有manifests
#-----------------------------------------------
[master]# git clone https://github.com/scriptcamp/kubernetes-mongodb.git
[master]# tree .
[master]# kubectl create ns mongodb
#-----------------------------------------------
# S1-2. 建立Secrets
# 使用password來保護DB
#-----------------------------------------------
[master]# vim mongodb-secrets.yaml
apiVersion: v1
data:
password: cGFzc3dvcmQxMjM= #password123
username: YWRtaW51c2Vy #adminuser
kind: Secret
metadata:
creationTimestamp: null
name: mongo-creds
[master]# kubectl create -f mongodb-secrets.yaml
#-----------------------------------------------
# S1-3. 解開加密物件
# k8s會將所有的secret以base64進行加密,使用以下方式進行解密
#-----------------------------------------------
[master]# echo "cGFzc3dvcmQxMjM=" | base64 --decode
password123
建立PVC,從storageclass自動綁定PV
#-----------------------------------------------
# S2-1. 建立PVC
#-----------------------------------------------
[master]# vim mongodb-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc
spec:
storageClassName: "managed-nfs-storage"
accessModes:
- ReadWriteOnce
volumeName: pv
resources:
requests:
storage: 1Gi
[master]# kubectl create -f mongodb-pvc.yaml
[master]# kubectl get pvc
本LAB建立的MongoDB為Standalone類型,只有一個Pod。
#-----------------------------------------------
# S3-1. 建立Deployment
#-----------------------------------------------
[master]# vim mongodb-deployment.yaml
[master]# kubectl create -f mongodb-deployment.yaml
[master]# kubectl get pod -o wide
#-----------------------------------------------
# S3-2. 建立svc (此處使用nodeport)
#-----------------------------------------------
[master]# vim mongodb-nodeport-svc.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: mongo
name: mongo-nodeport-svc
spec:
ports:
- port: 27017
protocol: TCP
targetPort: 27017
nodePort: 32001
selector:
app: mongo
type: NodePort
[master]# kubectl create -f mongodb-nodeport-svc.yaml
[master]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/mongo-nodeport-svc NodePort 10.103.143.51 <none> 27017:32001/TCP 100m
#-----------------------------------------------
# S3-3. 確認
#-----------------------------------------------
[master]# kubectl exec -it mongo-5d8b9d77f6-vd4tz /bin/bash
root@mongo-5d8b9d77f6-vd4tz:/# mongosh --host 10.107.88.15 --port 32001 -u adminuser -p password123
=> 出現以下畫面即完成連線
#---------------------------------------------
# S4-1. Create mongodb client pod
#---------------------------------------------
[master]# vim mongodb-new-client.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: mongo-client
name: mongo-client
spec:
replicas: 1
selector:
matchLabels:
app: mongo-client
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: mongo-client
spec:
containers:
- image: localhost/mongodb:latest
name: mongo-client
env:
- name: mongo-client_INITDB_ROOT_USERNAME
value: 'dummy'
- name: mongo-client_INITDB_ROOT_PASSWORD
value: 'dummy'
[master]# kubectl create -f mongodb-new-client.yaml
#---------------------------------------------
# S4-2. 進入client pod,執行基本操作
#---------------------------------------------
[master]# kubectl exec -it mongo-client-6cb867d578-8pdw6 /bin/bash
root@mongo-client-6cb867d578-8pdw6:/# mongosh --host 10.107.88.15 --port 32001 -u adminuser -p password123
(Display list of DBs)
# show dbs
(Get into DB)
# use db1
(Display a list of collections inside the ‘db1’ database)
# show collections
(Insert data into the db1 database)
# db.blogs.insert({name: "devopscube" })
(Display data from db1 database)
# db.blogs.find()
本篇簡單說明如何部署MongoDB並且執行基本測試,要注意的是mongo這個指令已經被mongosh取代,執行時要注意不要用錯。
部署單一的MongoDB pod算是不會太困難,希望大家有空也可以自已部署來試試看。
本篇到此為止,下期再見~~
Reference: