Connecting to MongoDB from Outside

Let’s try and access the database without collection. To do so, we need to build another Kubernetes Service. The services at Kubernetes are things that pods use to communicate. ClusterIP type services are often used for inter-pod communication. For starters, it is important to know that there are two types of ClusterIP services:

  1. Headless Services
  2. Services

Kubernetes standard services serve as load balances and follow the round-robin mind to distribute cargo. Headless services do not work as load balances. Also, standard services are provided for IPs by Kubernetes while Headless services are not available. Headless services are most commonly used when using statefulset applications.

In the case of our MongoDB feed example, we will use the standard service with Nodeport 32000 as we use the shipping type. Let’s create a NodePort-type service. Download the Kubernetes MongoDB service below YAML as mongodb-nodeport-svc.yaml.

apiVersion: v1

kind: Service

metadata:

  labels:

    app: mongo

  name: mongo-nodeport-svc

spec:

  ports:

  – port: 27017

    protocol: TCP

    targetPort: 27017

    nodePort: 32000

  selector:

    app: mongo

  type: NodePort

status:

  loadBalancer:{}

Create an svc file.

kubectl create -f mongodb-nodeport-svc.yaml

To connect outside of the Kubernetes collection, you must use the IP address of the Kubernetes collection staff member or the load balance address. In case you follow Minikube, you can use minikube IP to connect. To identify minikube IP or service URLs, use the following instructions.

minikube ip

minikube service –url mongo-nodeport-svc

Connect command:

mongo –host <ip> –port <port of nodeport svc> -u adminuser -p password123



How to Install and Run MongoDB on Kubernetes?

MongoDB is known as a document-oriented database server. A New York-based organization called 10 gen was building a platform as a service similar to window azure, and then developed MongoDB as a PAAS (Platform As A Service) in 2007 and later released it as an open source database. server in 2009 and then the company gained popularity as MongoDB Inc where the word Mongo is derived from Humongous. In simpler terms, we can define it as an open-source database server product that is used to store documents. Here, each component needed to use MongoDB in Kubernetes is described, also how to make the collection accessible outside of Kubernetes. Also how to perform basic tasks within MongoDB. As a beginner, creating individual sections while understanding the steps involved is a great way to learn about Kubernetes and MongoDB.

Similar Reads

MongoDB Cluster Kubernetes Manifests

All Kubernetes Mongodb YAML used in this guide is hosted on GitHub. Clone the repository for reference and use....

Create MongoDB Secrets

Secrets in Kubernetes are objects used to provide vessels with sensitive information. They are similar to ConfigMaps with the difference that data is stored in a coded format. For the security of our MongoDB model, it is wise to limit access to a website with a password. We will use secrets to mount the desired passwords into containers. Save the following manifest:...

Create MongoDB Persistent Volume

We need volumes to keep track of data. This way, even if our pod goes down – the data is not lost. For Kubernetes, there are two essentials for creating volumes....

Deploying the MongoDB Deployment

Let’s create a post now. I added a description of the MongoDB post file again at the end of this section. Save the following manifest as mongodb-deployment.yaml. Here we use the official mongo image from the docker hub....

Connecting to MongoDB from Outside

Let’s try and access the database without collection. To do so, we need to build another Kubernetes Service. The services at Kubernetes are things that pods use to communicate. ClusterIP type services are often used for inter-pod communication. For starters, it is important to know that there are two types of ClusterIP services:...