Home > Software engineering >  Ingress not creating address for mongo, but external service is creating
Ingress not creating address for mongo, but external service is creating

Time:09-04

I am a newbie to this k8s ingress, please help. Current problem is I am trying to use k8s to create mongo-express service which inturn will connect to MongoDB service. Now when I tried to expose mongo-express to an external service by setting type: LoadBalancer it quickly creates up an IP and I am able to access my db via it. But same when I am trying to do expose it to a domain name via ingress I am not getting assigned an address

Mongo DB Deployment :

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo-deployments
  labels:
    app: mongodb
spec:
  replicas: 3
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      containers:
      - name:  mongodb
        image: mongo
        ports:
        - containerPort: 27017
        env:
          - name: MONGO_INITDB_ROOT_USERNAME
            valueFrom: 
              secretKeyRef:
                name: mongo-secrets
                key: mongo-root-username
          - name: MONGO_INITDB_ROOT_PASSWORD
            valueFrom: 
              configMapKeyRef:
                name: mongo-coi
                key: mongo-root-password

Mongo DB Service

apiVersion: v1
kind: Service
metadata:
  name: mongodb-service
spec:
    selector:
      app: mongodb
    ports:
      - protocol: TCP
        port : 27017
        targetPort: 27017

Mongo express deployment :

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo-express-deployments
  labels:
    app: mongo-express
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongo-express
  template:
    metadata:
      labels:
        app: mongo-express
    spec:
      containers:
      - name:  mongo-express
        image: mongo-express
        ports:
        - containerPort: 8081
        env:
          - name: ME_CONFIG_MONGODB_ADMINUSERNAME
            valueFrom: 
              secretKeyRef:
                name: mongo-secrets
                key: mongo-root-username
          - name: ME_CONFIG_MONGODB_ADMINPASSWORD
            valueFrom: 
              secretKeyRef:
                name: mongo-secrets
                key: mongo-root-password
          - name: ME_CONFIG_MONGODB_SERVER 
            valueFrom: 
              configMapKeyRef:
                name: mongo-config-map
                key:  database-url

Mongo express service file

apiVersion: v1
kind: Service
metadata:
  name: mongo-express-service
spec:
    selector:
      app: mongo-express
    # type: LoadBalancer
    ports:
      - protocol: TCP
        port : 8081
        targetPort: 8081
        # nodePort : 31313

mongo ingress file :

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: mongo-express-dashboard
  namespace: default
  annotations:
    kubernetes.io/ingress.class: public
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: mongo-jainva.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: mongo-express-service
            port:
              number: 8081

I am attaching how my current k8s structure looks like :

NAME                                            READY   STATUS    RESTARTS   AGE
pod/mongo-deployments-66764645fc-jpjf2          1/1     Running   0          3d2h
pod/mongo-express-deployments-976669559-l4s7h   1/1     Running   0          3d

NAME                            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)     AGE
service/kubernetes              ClusterIP   10.96.0.1        <none>        443/TCP     40d
service/mongo-express-service   ClusterIP   10.109.215.229   <none>        8081/TCP    3d
service/mongodb-service         ClusterIP   10.103.145.203   <none>        27017/TCP   3d2h

NAME                                        READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/mongo-deployments           1/1     1            1           3d2h
deployment.apps/mongo-express-deployments   1/1     1            1           3d1h

NAME                                                   DESIRED   CURRENT   READY   AGE
replicaset.apps/mongo-deployments-66764645fc           1         1         1       3d2h
replicaset.apps/mongo-express-deployments-59f9544cf7   0         0         0       3d1h
replicaset.apps/mongo-express-deployments-854b7f8b86   0         0         0       3d
replicaset.apps/mongo-express-deployments-976669559    1         1         1       3d

Attaching current ingress status

NAME                      CLASS    HOSTS              ADDRESS   PORTS   AGE
mongo-express-dashboard   <none>   mongo-jainva.com             80      33m

And also sharing description of ingres file

Name:             mongo-express-dashboard
Labels:           <none>
Namespace:        default
Address:
Ingress Class:    <none>
Default backend:  <default>
Rules:
  Host              Path  Backends
  ----              ----  --------
  mongo-jainva.com
                    /   mongo-express-service:8081 (172.17.0.4:8081)
Annotations:        kubernetes.io/ingress.class: public
                    nginx.ingress.kubernetes.io/rewrite-target: /
Events:             <none>

NOTE - I have already tried mentioned IP 172.17.0.4:8081 its not working and also I am not able to understand what IP address it is specifying

CodePudding user response:

Which ingress-controller did you install? I see ingress-class as None. Make sure you have installed ingress-controller and set proper ingress class in ingress resource.

CodePudding user response:

Thank you guys whosoever responded, I have figured out the problem Problem is that in ingress file I have mentioned 2 ingress classes somehow the first one kubernetes.io/ingress.class: public is creating a problem after removing it I was able to see the address

For now, I am closing this question but still I have one question if anyone can kindly answer When I use below showned ingress-controller to route my mongo-express in ingress file I see all JS and other styling files are not coming with mongo-express due to which I could see mongo-express buttons but completely ruined UI

nginx.ingress.kubernetes.io/rewrite-target: /$1

But if I change the above code line to

nginx.ingress.kubernetes.io/rewrite-target: /

It gives me a clean and stable UI what does this $1 specifies due to which I am seeing this behaviour

  • Related