Using Mongo-Express and mongo DB to create the simple web application set with the help of the K8s components.
- 2 Deployments / Pods → To create the application and data base.
- 2 Services → To connect the application through the URL.
- ConfigMap → To build the deployment.yaml file and contain the database url.
- Secret → For the credentials It contain credentials.
We create the application with no external service
This the basic setup of the application.
- It start from the browser send the request to the mongo express external service.
- Then forwarded it to the mongo express pod.
- It convert the request into the internal service.
- It means the mongo db pod.
- And the component take the credential with the request and check and allow to access it.
create the mongo db configuration yaml file.
## this file name is mongo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongo
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: mongo-secret
key: mongo-root-username
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mongo-secret
key: mongo-root-password
create the secret configuration file for the security.
apiVersion: v1
kind: Secret
metadata:
name: mongo-secret
type: Opaque
data:
mongo-root-username: dXNlcm5hbWU=
mongo-root-password: cGFzc3dvcmQ=
Note: In a mongo.yaml file we’re referenced to the secret.yaml file for the credentials so It search for the credential inside the cluster.
So apply the secret.yaml file first and then apply the mongo.yaml file
🧀 `$ kubectl apply -f mongo-secret.yaml`It create the secret to see that $ kubectl get secret
.
To check the it running status $ kubectl get pod
Create Mongo DB internal Service config file to connect the application to the database.
apiVersion: v1
kind: Service #This is the service type configuration
metadata:
name: mongo-service #random name for the file
spec:
selector:
app: mongodb #It help to connect to pod through the label
ports:
- protocol: TCP
port: 27017 #Service port that from the server
targetPort: 27017 #container port of thdeployment
To check the service is configure $ kubectl get service
use this.
It shows the IP address the internal mongo-db IP address.
check the mongo-db IP address ,
🧀 `$ kubectl get pod -o wide`Create the express deployment file.
apiVersion: apps/v1
kind: Deployment
metadata:
name: 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: mongo-secret
key: mongo-root-username
- name: ME_CONFIG_MONGODB_ADMINPASSWORD
valueFrom:
secretKeyRef:
name: mongo-secret
key: mongo-root-password
- name: ME_CONFIG_MONGODB_SERVER
valueFrom:
configMapKeyRef:
name: mongo-configmap
key: database_url
Crete the configMap file for the application.
apiVersion: v1
kind: ConfigMap #type of the configuration
metadata:
name: mongo-configmap #name of the file
data:
database_url: mongo-service #reference to the link
Before we did the db configuration same as we will do for this.
- First apply the configMap file because it is referenced to the application file.
Check that is deployed or not using this $ kubectl get configmap
- Then deploy the Mango-express file
to check $ kubectl get deploment
Config the external service to connect with the application.
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
To check service $ kubectl get service
.
Here is the type: LoadBalancer gives the internal IP address and the External IP address also.
That’s all now use the application using this command.
🧀 `$ minikube service mongo-express-service`This is the final output of the application that running on the port number 37239.