I am trying to expose a stateful mongo replica set running on my cluster for outside access.
I have three replicas and I have created a LoadBalancer
service for each replica with the same LoadBalancerIP
while incrementing the port sequentially from 10255
to 10257
.
apiVersion: v1
kind: Service
metadata:
name: mongo-service-0
namespace: datastore
labels:
app: mongodb-replicaset
spec:
loadBalancerIP: staticip
type: LoadBalancer
externalTrafficPolicy: Local
selector:
statefulset.kubernetes.io/pod-name: mongo-mongodb-replicaset-0
ports:
- protocol: TCP
port: 10255
targetPort: 27017
The issue is that only one service mongo-service-0
is deployed successfully with static IP, the other timeout after a while.
What I am trying to figure out is if I can use a single static IP address as LoadBalancerIP across multiple services with different ports.
Since you are using different ports for each of your Mongo replicas you had to create different Kubernetes Services for each one of the replicas. You can only tie a Kubernetes service with a single load balancer and each load balancer will have its own unique IP (or IPs) address, so you won't be able to share that IP address across a LoadBalancer type of Service.
The workaround is to use a NodePort service and basically manage your load balancer independently and point it to the NodePort for each one of your replicas.
Another workaround is to use the same port on your Mongo replicas and use the same Kubernetes LoadBalancer service. Is there any reason why you are not using that?