mongodb Deployment - q-uest/notes-doc-k8s-docker-jenkins-all-else GitHub Wiki
Kubernetes MongoDB Deployment
mongo-secret.yaml
apiVersion: v1
data:
password: cGFzc3dvcmQxMjM=
username: YWRtaW51c2Vy
kind: Secret
metadata:
name: mongo-creds
type: Opaque
===
mongo.yaml (with security enabled)
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: mongo
name: mongo
spec:
replicas: 1
selector:
matchLabels:
app: mongo
template:
metadata:
labels:
app: mongo
spec:
containers:
- image: mongo
name: mongo
resources:
limits:
memory: "500Mi"
env:
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef:
name: mongo-creds
key: username
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mongo-creds
key: password
mongo.yaml (with out security)
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: mongo
name: mongo
spec:
replicas: 1
selector:
matchLabels:
app: mongo
template:
metadata:
labels:
app: mongo
spec:
containers:
- image: mongo
name: mongo
resources:
limits:
memory: "500Mi"
mongo.yaml (with PV/PVC)
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: mongo
name: mongo
spec:
replicas: 1
selector:
matchLabels:
app: mongo
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: mongo
spec:
containers:
- image: mongo
name: mongo
args: ["--dbpath","/data/db"]
env:
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef:
name: mongo-creds
key: username
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mongo-creds
key: password
volumeMounts:
- name: "mongo-data-dir"
mountPath: "/data/db"
volumes:
- name: "mongo-data-dir"
persistentVolumeClaim:
claimName: "pvc"
===
mongo-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mongo-data
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
hostPath:
path: /data/mongo
===
mongo-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc
spec:
storageClassName: ""
accessModes:
- ReadWriteOnce
volumeName: mongo-data
resources:
requests:
storage: 1Gi
=== mongodb-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: mongo
name: mongo-client
resources:
limits:
memory: "1000Mi"
env:
- name: mongo-client_INITDB_ROOT_USERNAME
value: 'adminuser'
- name: mongo-client_INITDB_ROOT_PASSWORD
value: 'password123'
mongodb-svc.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: mongo
name: mongo-nodeport-svc
spec:
ports:
- port: 27017
protocol: TCP
targetPort: 27017
nodePort: 31055
selector:
app: mongo
type: NodePort
status:
loadBalancer: {}
===
Connect to Mongo (without password) & Execute commands:
- Connecting to Mongodb:
root@mongo-66889b9cb6-j9qqh:/# mongo
MongoDB shell version v5.0.8
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("6664712d-1ead-4644-9a19-a5f97748b1c1") }
MongoDB server version: 5.0.8
================
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/
================
---
The server generated these startup warnings when booting:
2022-06-01T17:28:33.802+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2022-06-01T17:28:34.509+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
- List Databases
> show dbs
admin 0.000GB
appoint-me 0.000GB
config 0.000GB
local 0.000GB
- Connect to a database & Listing Collections
> use appoint-me
switched to db appoint-me
> db.createCollection('appointments');
{ "ok" : 1 }
> show collections
appointments
- List Records of a collection
> db.appointments.find()
{ "_id" : ObjectId("6297a39349fac9b940b2b324"), "name" : "ram", "fulfilled" : false }
{ "_id" : ObjectId("6297a3a049fac9b940b2b325"), "name" : "mat", "fulfilled" : false }
- Fetch Records based on criteria
> db.appointments.find({ name: "ram" });
{ "_id" : ObjectId("6297a39349fac9b940b2b324"), "name" : "ram", "fulfilled" : false }
> db.appointments.find({ fulfilled: false });
{ "_id" : ObjectId("6297a39349fac9b940b2b324"), "name" : "ram", "fulfilled" : false }
{ "_id" : ObjectId("6297a3a049fac9b940b2b325"), "name" : "mat", "fulfilled" : false }
- Insert Records
> db.appointments.save({name:'new chap',fulfilled: false});
WriteResult({ "nInserted" : 1 })
> db.appointments.find()
{ "_id" : ObjectId("6297a39349fac9b940b2b324"), "name" : "ram", "fulfilled" : false }
{ "_id" : ObjectId("6297a3a049fac9b940b2b325"), "name" : "mat", "fulfilled" : false }
{ "_id" : ObjectId("6297b19c01cd024afe55c6a4"), "name" : "new chap", "fulfilled" : false }
- Update Records
Note: Include all the fields whether they need to be updated or not.
> db.appointments.update({ "name" : "Mat"},{"name" : "Mat", "fulfilled" : true });
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.appointments.find()
{ "_id" : ObjectId("6297a39349fac9b940b2b324"), "name" : "ram", "fulfilled" : false }
{ "_id" : ObjectId("6297a3a049fac9b940b2b325"), "name" : "Mat", "fulfilled" : true }
{ "_id" : ObjectId("6297b19c01cd024afe55c6a4"), "name" : "new chap", "fulfilled" : false }
- Update without setting all the fields
> db.appointments.update({ "name" : "new chap"},{"name" : "Mat"});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.appointments.find()
{ "_id" : ObjectId("6297a39349fac9b940b2b324"), "name" : "ram", "fulfilled" : false }
{ "_id" : ObjectId("6297b19c01cd024afe55c6a4"), "name" : "Mat" }
Note: The field, "fulfilled" is missing in the second record, as it was not included while setting values in the above update.
- Remove/Delete Records
> db.appointments.remove({"name" : "Mat"});
WriteResult({ "nRemoved" : 1 })
> db.appointments.find()
{ "_id" : ObjectId("6297a39349fac9b940b2b324"), "name" : "ram", "fulfilled" : false }
{ "_id" : ObjectId("6297b19c01cd024afe55c6a4"), "name" : "new chap", "fulfilled" : false }
===