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!
It looks like the answer is present in the StatefulSet Basics documentation section Using Stable Network Identities:
I would strongly suggest taking a glance at the service docs to make sure you're familiar with what is happening:
https://kubernetes.io/docs/concepts/services-networking/service/
With that in mind and the guide you're using, note the following:
So what you've created is a headless service(no load balancer or exposed IPs)
So instead of the configuration given for a headless service:
What you actually want is:
Very subtle but you'll notice that the
clusterIP
property no longer exists.I also prefer to specify the protocol, for completeness, even though TCP is the default.
You need to expose the service (svc). The pod by definition would as you said have different IP's.
In the example mentioned at https://kubernetes.io/docs/user-guide/petset/ , you will notice the service definition.
This is what you need to concentrate on.The service once tied to DNS would give you a stable lookup. By the way , StatefulSets is the maturing of earlier Pet Sets.