I would like to configure a cluster of MongoDB nodes in my Minikube cluster.
The manual which I found assume to execute a following script in mongosh against first pod:
rs.initiate(
{
_id: "rs0",
version: 1,
members: [
{ _id: 0, host : "mongodb-deployment-0.mongo-service.default.svc.cluster.local:27017" },
{ _id: 1, host : "mongodb-deployment-1.mongo-service.default.svc.cluster.local:27017" },
{ _id: 2, host : "mongodb-deployment-2.mongo-service.default.svc.cluster.local:27017" }
]
}
)
Unfortunately, I am getting an error:
MongoServerError: No host described in new configuration with {version: 1, term: 0} for replica set rs0 maps to this node
It looks that the full hostnames do not exist in my Minikube cluster, because if I provide IP address of the nodes:
rs.initiate(
{
_id: "rs0",
version: 1,
members: [
{ _id: 0, host : "10.244.0.3" },
{ _id: 1, host : "10.244.0.4" },
{ _id: 2, host : "10.244.0.6" }
]
}
)
it works fine.
I found that the hostnames are build in a following way: pod-name.service-name.namespace.svc.cluster.local:port, so my first command should work.
tom@macbook-air-tomasz-2 k8s-stateful % kubectl get pods
NAME READY STATUS RESTARTS AGE
mongodb-deployment-0 1/1 Running 0 24m
mongodb-deployment-1 1/1 Running 0 22m
mongodb-deployment-2 1/1 Running 0 22m
tomaszbadon@macbook-air-tomasz-2 k8s-stateful % kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 26m
mongo-service ClusterIP None <none> 27017/TCP 26m
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mongodb-deployment
labels:
app: mongodb
spec:
replicas: 3
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: mongo
ports:
- containerPort: 27017
volumeMounts:
- mountPath: /data/db
name: mongo-volume
env:
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-username
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-password
command:
- mongod
- "--bind_ip_all"
- "--replSet"
- rs0
volumeClaimTemplates:
- metadata:
name: mongo-volume
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: demo-storage
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: Service
metadata:
name: mongo-service
spec:
ports:
- name: mongodb
port: 27017
targetPort: 27017
clusterIP: None
selector:
app: mongodb
What are my full hostnames registered in Minikube dns, which could allow me to provide them in cluster configuration?
Thank you.