Mongo-Express and MongoDB project:
> Create "mongo-express-deployment.yaml" config file.
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: 30000In the above configuration, we are creating a deployment for the web application and an External service.
> Create "mongodb-deployment.yaml" config file.
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: 27017In the above configuration, we are creating a deployment for the database and an Internal service.
> Create "mongo-configmap.yaml" config file.
apiVersion: v1
kind: ConfigMap
metadata:
  name: mongodb-configmap
data:
  database_url: mongodb-serviceIn the above configuration, we are creating a ConfigMap to save the external configuration of the web application.
> Create "mongodb-secrets.yaml" config file.
apiVersion: v1
kind: Secret
metadata:
  name: mongodb-secret
type: Opaque
data:
  mongo-root-username: dXNlcm5hbWU=
  mongo-root-password: cGFzc3dvcmQ=In the above configuration, we are creating a Secret to save the external configuration of the web application.
> Run the below commands to create Mongo-Express web application and the MongoDB.
$ kubectl apply -f mongo-configmap.yaml$ kubectl apply -f mongodb-secrets.yaml$ kubectl apply -f mongodb-deployment.yaml$ kubectl apply -f mongo-express-deployment.yamlVerify all the components created using the below command.
$ kubectl get allTo access the Mongo-express application, go to a browser and enter "localhost:8081" you should reach the Mongo-express dashboard.
> Run the below commands to delete the Mongo-Express web application and the MongoDB.
$ kubectl delete -f mongo-configmap.yaml$ kubectl delete -f mongodb-secrets.yaml$ kubectl delete -f mongodb-deployment.yaml$ kubectl delete -f mongo-express-deployment.yaml

Comments
Post a Comment