Kubernetes 106: Deploy Mongo-DB on Kubernetes
Deploy Mongodb on Kubernetes
1- Create deployment for mongo-db.
2- Create service for mongo-db.
3- Create deployment for mongo-express.
4- Create service for mongo-express.
5- Create secret file for store username & password of mongodb.
6- Create configmap file for store database server.
# Create secret file:
apiVersion: v1
kind: Secret
metadata:
name: mongodb-secret
type: Opaque
data:
mongo-root-username: dXNlcm5hbWU=
mongo-root-password: cGFzc3dvcmQ=
[root@kubemaster ]# echo -n 'username' | base64
[root@kubemaster ]# echo -n 'password' | base64
[root@kubemaster ]# kubectl apply -f mongo-secret.yaml
secret/mongodb-secret created
[root@kubemaster ]# kubectl get secret
NAME TYPE DATA AGE
mongodb-secret Opaque 2 11s
# Create deployment & service for mongodb:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-deployment
labels:
app: mongodb
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: mongo
ports:
- containerPort: 27017
env:
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-username
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-password
---
apiVersion: v1
kind: Service
metadata:
name: mongodb-service
spec:
selector:
app: mongodb
ports:
- protocol: TCP
port: 27017
targetPort: 27017
[root@kubemaster ]# kubectl apply -f mongo.yaml
deployment.apps/mongodb-deployment created
service/mongodb-service created
[root@kubemaster ]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mongodb-service ClusterIP 10.43.155.22 <none> 27017/TCP 10s
# Create ConfigMap file:
apiVersion: v1
kind: ConfigMap
metadata:
name: mongodb-configmap
data:
database_url: mongodb-service
[root@kubemaster ]# kubectl apply -f mongo-configmap.yaml
configmap/mongodb-configmap created
[root@kubemaster ]# kubectl get configmap
NAME DATA AGE
mongodb-configmap 1 27s
# Create deployment & service for Mongodb-Express:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongo-express
labels:
app: mongo-express
spec:
replicas: 1
selector:
matchLabels:
app: mongo-express
template:
metadata:
labels:
app: mongo-express
spec:
containers:
- name: mongo-express
image: mongo-express
ports:
- containerPort: 8081
env:
- name: ME_CONFIG_MONGODB_ADMINUSERNAME
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-username
- name: ME_CONFIG_MONGODB_ADMINPASSWORD
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-password
- name: ME_CONFIG_MONGODB_SERVER
valueFrom:
configMapKeyRef:
name: mongodb-configmap
key: database_url
---
apiVersion: v1
kind: Service
metadata:
name: mongo-express-service
spec:
selector:
app: mongo-express
type: LoadBalancer
ports:
- protocol: TCP
port: 8081
targetPort: 8081
nodePort: 30000
[root@kubemaster ]# kubectl apply -f mongo-express.yaml
deployment.apps/mongo-express created service/mongo-express-service created [root@kubemaster ]# kubectl get pod NAME READY STATUS RESTARTS AGE mongodb-deployment-78444d94d6-vk7vm 1/1 Running 0 10m mongo-express-797845bd97-vb4n2 1/1 Running 0 10s svclb-mongo-express-service-v4cd5 1/1 Running 0 10s svclb-mongo-express-service-z77d9 1/1 Running 0 10s
[root@kubemaster ]# kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 143d mongo-express-service LoadBalancer 10.43.42.96 192.168.100.6 8081:30000/TCP 3m56s [root@kubemaster ]# kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE mongodb-deployment 1/1 1 1 19m mongo-express 1/1 1 1 8m28s
# Can be login into mongodb:
[root@kubemaster ] # kubectl exec -it mongodb-deployment-78444d94d6-vk7vm /bin/bash root@mongodb-deployment-78444d94d6-vk7vm:/# root@mongodb-deployment-78444d94d6-vk7vm:/# df -h Filesystem Size Used Avail Use% Mounted on overlay 58G 2.7G 56G 5% / tmpfs 64M 0 64M 0% /dev tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup /dev/mapper/rootvg-root 58G 2.7G 56G 5% /etc/hosts shm 64M 0 64M 0% /dev/shm tmpfs 7.8G 12K 7.8G 1% /run/secrets/kubernetes.io/serviceaccount tmpfs 7.8G 0 7.8G 0% /proc/acpi tmpfs 7.8G 0 7.8G 0% /proc/scsi tmpfs 7.8G 0 7.8G 0% /sys/firmware root@mongodb-deployment-78444d94d6-vk7vm:/# ls -l /data/ configdb/ db/ root@mongodb-deployment-78444d94d6-vk7vm:/# ls -l /data/db/ total 340 -rw------- 1 mongodb mongodb 47 Apr 18 13:38 WiredTiger -rw------- 1 mongodb mongodb 21 Apr 18 13:38 WiredTiger.lock -rw------- 1 mongodb mongodb 1209 Apr 18 13:41 WiredTiger.turtle -rw------- 1 mongodb mongodb 69632 Apr 18 13:41 WiredTiger.wt -rw------- 1 mongodb mongodb 4096 Apr 18 13:38 WiredTigerLAS.wt -rw------- 1 mongodb mongodb 20480 Apr 18 13:38 _mdb_catalog.wt -rw------- 1 mongodb mongodb 20480 Apr 18 13:38 collection-0-304755173444749226.wt -rw------- 1 mongodb mongodb 36864 Apr 18 13:39 collection-2-304755173444749226.wt -rw------- 1 mongodb mongodb 4096 Apr 18 13:38 collection-4-304755173444749226.wt -rw------- 1 mongodb mongodb 20480 Apr 18 13:38 collection-8-304755173444749226.wt drwx------ 2 mongodb mongodb 71 Apr 18 14:19 diagnostic.data -rw------- 1 mongodb mongodb 20480 Apr 18 13:38 index-1-304755173444749226.wt -rw------- 1 mongodb mongodb 20480 Apr 18 13:38 index-10-304755173444749226.wt -rw------- 1 mongodb mongodb 36864 Apr 18 13:39 index-3-304755173444749226.wt -rw------- 1 mongodb mongodb 4096 Apr 18 13:38 index-5-304755173444749226.wt -rw------- 1 mongodb mongodb 12288 Apr 18 13:40 index-6-304755173444749226.wt -rw------- 1 mongodb mongodb 20480 Apr 18 13:38 index-9-304755173444749226.wt drwx------ 2 mongodb mongodb 110 Apr 18 13:38 journal -rw------- 1 mongodb mongodb 2 Apr 18 13:38 mongod.lock -rw------- 1 mongodb mongodb 36864 Apr 18 13:40 sizeStorer.wt -rw------- 1 mongodb mongodb 114 Apr 18 13:38 storage.bson
Web browser:
# Create db test:
================================================================
Good Luck https://www.linkedin.com/in/ahmedms/
Comments
Post a Comment