mongodb / mongodb-kubernetes-operator

MongoDB Community Kubernetes Operator
Other
1.24k stars 506 forks source link

MongoDBCommunity not using the `replicaSetHorizons` values to generate `Mongo Uri` resulting in `MongoNetworkError` #1614

Open MichaelKora opened 2 months ago

MichaelKora commented 2 months ago

What did you do to encounter the bug? I followed this guide to deploy a MongoDBCommunity instance with external access But i am struggling to connect to the instance as described in the documentation.

Steps to reproduce the behavior:

  1. Apply the following config using kubectl:

    apiVersion: mongodbcommunity.mongodb.com/v1
    kind: MongoDBCommunity
    metadata:
    name: my-mongo-db
    namespace: mongodb
    spec:
    members: 2
    arbiters: 1
    type: ReplicaSet
    version: "4.4.13"
    replicaSetHorizons:
    - horizon: my-mongodb-0.example.com:31181
    - horizon: my-mongodb-1.example.com:31182
    - horizon: my-mongo-db-arb-0.example.com:31183
    
    security:
    tls:
      enabled: true
      certificateKeySecretRef:
        name: mongodb-tls
      caConfigMapRef:
        name: ca-config-map
    authentication:
      modes: ["SCRAM"]
    users:
    - name: my-db-admin
      db: admin
      passwordSecretRef:
        name: mongodb-secret
        key: mongo-root-password
      roles:
        - name: clusterAdmin
          db: admin
        - name: userAdminAnyDatabase
          db: admin
        - name: root
          db: admin
        - name: readWrite
          db: test_db
        - name: root
          db: test_db
      scramCredentialsSecretName: my-scram
    additionalMongodConfig:
    storage.wiredTiger.engineConfig.journalCompressor: zlib
    prometheus:
    username: my-prom-user
    passwordSecretRef:
      name: prom-pwd
    statefulSet:
    spec:
      dnsPolicy: None
      dnsConfig:
         nameservers:
           - xx.xx.xx.xxx
           - yy.yy.yy.yyy

    My Certificate:

apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: cert-manager-certificate
  namespace: mongodb
spec:
  secretName: mongodb-tls
  issuerRef:
    name: ca-issuer
    kind: Issuer
  commonName: "*.my-mongo-db-svc.mongodb.svc.cluster.local"
  dnsNames:
    - "*.my-mongo-db-svc.mongodb.svc.cluster.local"
    - my-mongo-db-0.example.com
    - my-mongo-db-1.example.com
    - my-mongo-db-arb-0.example.com

External services definitions:

---
kind: Service
apiVersion: v1
metadata:
  name: external-mongo-service-0
  namespace: mongodb
  annotations:
    kube-linter.io/ignore-all: "used for sample"
spec:
  type: NodePort
  selector:
    app: my-mongo-db-svc
    statefulset.kubernetes.io/pod-name: my-mongo-db-0
  ports:
    - protocol: TCP
      nodePort: 31181
      port: 31181
      targetPort: 27017

---
kind: Service
apiVersion: v1
metadata:
  name: external-mongo-service-1
  namespace: mongodb
  annotations:
    kube-linter.io/ignore-all: "used for sample"
spec:
  type: NodePort
  selector:
    app: my-mongo-db-svc
    statefulset.kubernetes.io/pod-name: my-mongo-db-1
  ports:
    - nodePort: 31182
      port: 31182
      targetPort: 27017

---
kind: Service
apiVersion: v1
metadata:
  name: external-mongo-arbiter-svc
  namespace: mongodb
  annotations:
    kube-linter.io/ignore-all: "used for sample"
spec:
  type: NodePort
  selector:
    app: my-mongo-db-svc
    statefulset.kubernetes.io/pod-name: my-mongo-db-arb-0
  ports:
    - nodePort: 31183
      port: 31183
      targetPort: 27017
  1. Wait until the MongoDBCommunity instance in Running state in Kubernetes and describe:
    k des mdbc my-mongo-db -n mongodb
    ---
    Name:         my-mongo-db
    Namespace:    mongodb
    Labels:       <none>
    Annotations:  mongodb.com/v1.lastAppliedMongoDBVersion: 4.4.13
              mongodb.com/v1.lastSuccessfulConfiguration:
                {"members":2,"type":"ReplicaSet","version":"4.4.13","arbiters":1,"replicaSetHorizons":[{"horizon":"my-mongodb-0.example.com:311...
    API Version:  mongodbcommunity.mongodb.com/v1
    . . . 
    Status:
    Current Mongo DB Arbiters:               1
    Current Mongo DB Members:                2
    Current Stateful Set Arbiters Replicas:  1
    Current Stateful Set Replicas:           2
    Mongo Uri:                               mongodb://my-mongo-db-0.mongo-db-svc.my-mongodb.svc.cluster.local:27017,my-mongo-db-1.mongo-db-svc.my-mongodb.svc.cluster.local:27017/?replicaSet=my-mongo-db
    Phase:                                   Running
    Version:                                 4.4.13
    Events:                                    <none>

What did you expect?

I expected:

  1. the FQDNs defined in the replicaSetHorizons section to be seen under the Mongo Uri when i describe the MongoDBCommunity instance in K8s, Instead of Mongo Uri: mongodb://my-mongo-db-0.mongo-db-svc.mongodb.svc.cluster.local:27017,my-mongo-db-1.mongo-db-svc.mongodb.svc.cluster.local:27017/?replicaSet=my-mongo-db .

  2. to use mongosh and the external URI to connect to the DB:

mongosh --tls --tlsCAFile mkcert/rootCA.pem --tlsCertificateKeyFile mongo-server-key.pem --username $MONGODB_USER --password $MONGODB_ROOT_PASSWORD mongodb://my-mongo-db-0.example.com:31181,my-mongo-db-1.example.com:31182 --authenticationDatabase=admin

What happened instead? i keep getting the error

Current Mongosh Log ID: xxxxxxxxxxxxxxxxxx

Connecting to:          mongodb://<credentials>@my-mongo-db-0.example.com:31181,my-mongo-db-1.example.com:31182/?tls=true&tlsCAFile=mkcert%2FrootCA.pem&tlsCertificateKeyFile=mongo-server-key.pem&authSource=admin&appName=mongosh+2.2.10

MongoNetworkError: getaddrinfo ENOTFOUND my-mongo-db-0.my-mongo-db-svc.mongodb.svc.cluster.local

❯ k get pods -n mongodb

NAME                                           READY   STATUS    RESTARTS   AGE
my-mongo-db-0                                 2/2     Running   0          9d
my-mongo-db-1                                 2/2     Running   0          9d
my-mongo-db-arb-0                             2/2     Running   0          9d
mongodb-kubernetes-operator-5c9948c9b6-vhxrc   1/1     Running   0          9d
mongodb-kubernetes-operator-5c9948c9b6-vvptl   1/1     Running   0          9d                     

❯ kg svc -n mongodb

NAME                         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)              AGE
external-mongo-arbiter-svc   NodePort    10.106.17.137    <none>        31183:31183/TCP      14d
external-mongo-service-0     NodePort    10.99.234.6      <none>        31181:31181/TCP      14d
external-mongo-service-1     NodePort    10.111.103.194   <none>        31182:31182/TCP      14d
my-mongo-db-svc             ClusterIP   None             <none>        27017/TCP,9216/TCP   14d

❯ k get mdbc -n mongodb

NAME           PHASE     VERSION
my-mongo-db   Running   4.4.13
KarooolisZi commented 2 months ago

I also have strange issue which could be related: https://github.com/mongodb/mongodb-kubernetes-operator/issues/1613#issuecomment-2333758420

MichaelKora commented 2 months ago

any update on your issue @KarooolisZi ?

github-actions[bot] commented 4 days ago

This issue is being marked stale because it has been open for 60 days with no activity. Please comment if this issue is still affecting you. If there is no change, this issue will be closed in 30 days.

MichaelKora commented 4 days ago

issue still not fixed