Accessing Externally Hosted Mongo-DB in the
Kubernetes/Minikube PODS
We can directly access the externally
hosted mongo-db inside the pod, by using it public IP.
But, suppose in future,
if the externally hosted mongo dB IP changes, then we would require to update
all the pods, that are accessing the database.
The better option is creating a service without selector. So, for it no
endpoints will be created. After that we will manually create the endpoints and provide the
externally hosted mongodb address there.
In this way, when we access the service, it
will automatically route to the end points created for it.
And, if later on if public IP changes for the DB, we will not require to update the pods, just we will require to update the end points.
Below are the json files for it:
- Mongodb Service without Selector:
{
"kind":
"Service",
"apiVersion": "v1",
"metadata":{
name:
"mongodb"
},
"spec":{
"ports":[{
"protocol": "TCP",
"port": 27017,
"targetPort": 27017
}]
},
"selector": {}
}
As you can see, we have
not provided any selector for it. So, there will be no end point created for
it.
By default the mongo-db is accessible to the default port (27017).
2. End-Point for the above service:
{
"kind":
"Endpoints",
"apiVersion":
"v1",
"metadata":{
"name":
"mongodb"
},
"subsets":[{
"addresses":[{
"ip":
"30.188.60.252", // This is the external end point.
}],
"ports":[{
"port": 27017,
"protocol": "TCP"
}]
}]
}
Note : The name property value must match with the newly created service name. That's how both the endpoints and service will associate.
And to access the external mongo-db now, We can just use the service name directly.
Ex : let constr = "mongodb://abcd:abcd@mongodb";
Or, we can use the service IP also that will proxy the traffic to the endpoint.
No comments:
Post a Comment