Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expose MongoDB on Kubernetes with StatefulSets outside cluster

I followed the guide in the following link: http://blog.kubernetes.io/2017/01/running-mongodb-on-kubernetes-with-statefulsets.html

and set up a mongo DB replica set on Kubernetes with StatefulSets. So far so good, but how do I expose that static hostnames outside the cluster so that I can access them from a Google instance for example?

If I use the IPs of the nodes it will work fine but those can change anytime (upon pod failure and restart with a different IP etc.)...

Thanks in advance!

like image 454
tzik Avatar asked Jan 31 '17 23:01

tzik


People also ask

How do I access the database outside Kubernetes cluster?

Here is what worked for me: Define a service , but set clusterIP: None , so no endpooint is created. And then create an endpoint yourself with the SAME NAME as your service and set the IP and port of your db. In your example , you have a type in your endpoint: the name of your endpoint is postgresql not postgresSql.

How do you expose Kubernetes pod outside the Kubernetes cluster?

You have several options for connecting to nodes, pods and services from outside the cluster: Access services through public IPs. Use a service with type NodePort or LoadBalancer to make the service reachable outside the cluster. See the services and kubectl expose documentation.

How deploy MongoDB on Kubernetes?

Deploy MongoDB on Kubernetes Using Helm It uses a deployment with persistent volume as it is a single pod setup. First, add the bitnami helm repo. Once installed, you will see all the details to connect to the MongoDb deployment in the output as shown below.


1 Answers

It looks like the answer is present in the StatefulSet Basics documentation section Using Stable Network Identities:

The Pods’ ordinals, hostnames, SRV records, and A record names have not changed, but the IP addresses associated with the Pods may have changed. In the cluster used for this tutorial, they have. This is why it is important not to configure other applications to connect to Pods in a StatefulSet by IP address.

If you need to find and connect to the active members of a StatefulSet, you should query the CNAME of the Headless Service (nginx.default.svc.cluster.local). The SRV records associated with the CNAME will contain only the Pods in the StatefulSet that are Running and Ready.

If your application already implements connection logic that tests for liveness and readiness, you can use the SRV records of the Pods ( web-> 0.nginx.default.svc.cluster.local, web-1.nginx.default.svc.cluster.local), as they are stable, and your application will be able to discover the Pods’ addresses when they transition to Running and Ready.

like image 101
Adam Avatar answered Sep 30 '22 20:09

Adam