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:
- Headless Services
- 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.