Introduction
In the Serverspace you can create a server with already installed app "MongoDB".
MongoDB is one of most popular NoSQL database management solution. In combination with Kubernetes orchestrator it could be easy-to-scale, multipurpose solution.
Requirements
To work with MongoDB in Kubernetes you need one server under any operation system (Linux with root access or sudo membership preferred) to manage and Kubernetes cluster (see next step).
Kubernetes instance creation
Before deploy MongoDB you need to have Kubernetes. To create it in ServerSpace infrastructure, just login into your client area, then click to Kubernetes link and create an instance:
Process may take a time, please be patient. When finished, you will see cluster parameters and should download access credentions file:
Instance setup
To install database service on your Kubernetes cluster please do follow:
- Login to your management server as privileged user and install necessary tools:
apt-get update && apt install curl apt-transport-https -y && curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - && echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | tee -a /etc/apt/sources.list.d/kubernetes.list && apt-get update && apt install kubectl -y
- Make a file which stores cluster access data and set this as system variable:
cat << EOF > testcluster.conf
<PASTE CONFIGURATION DATA HERE>
EOF
echo "export KUBECONFIG=testcluster.conf" >> ~/.bashrc
- To check connection just run:
If output looks like picture below - connection is successful
- MongoDB needs storage to save it's data. Storage creating process describes in special configuration files. You can customize it by your needs:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mongodatapv # Should be the same with name in previous file
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi # Should be the same with capacity in previous file
EOF
Next step is creating credentials file, which stores access to MongoDB:
apiVersion: v1
data:
username: <BASE64_ENCODED_LOGIN>
password: <BASE64_ENCODED_PASSWORD>
kind: Secret
metadata:
creationTimestamp: null
name: creds
EOF
Tip: To encode and decode data you can use simple commands:
echo <BASE64_ENCRYPTED_DATA> | base64 -d # to decrypt it
- Then create an instance deployment file:
cat << EOF > Deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: mongo
name: mongo
spec:
replicas: 1
selector:
matchLabels:
app: mongo
strategy: {}
template:
metadata:
labels:
app: mongo
spec:
containers:
- image: mongo
name: mongo
args: ["--dbpath","/data/db"]
livenessProbe:
exec:
command:
- mongo
- --disableImplicitSessions
- --eval
readinessProbe:
exec:
command:
- mongo
- --disableImplicitSessions
- --eval
env:
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef:
name: creds
key: username
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: creds
key: password
volumeMounts:
- name: "datadir"
mountPath: "/data/db"
volumes:
- name: "datadir"
persistentVolumeClaim:
claimName: "mongopvc"
EOF
- To launch MongoDB please run command:
Successful output looks like at picture below:
Connection check
- Now instances is deployed, so you should check connection. Just run:
mongo
In case you see MongoDB prompt, connect is succesful:
- To create new database just "switch" to the new database. NOTE: Data will not be saved until you add something into the database:
db.createCollection("newdata") # example to add data
show dbs # check is database exist
Conclusion
After this article reading you knew how to create Kubernetes via ServerSpace client area, deploy MongoDB into it, create new database and insert new data to this base.