Not able to expose pod for outside using Service and ingress

11/23/2019

I created a pod for mongodb and deployment files looks like:

The persistent volume claim is defined as:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mongodb-data-claim
  annotations:
    volume.beta.kubernetes.io/storage-class: vmfs1
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

The stateful deployment yaml :

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongodb
spec:
  serviceName: "mongodb"
  selector:
    matchLabels:
      app: mongodb
  replicas: 1
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      volumes:
       - name: mongodb-data
         persistentVolumeClaim:
           claimName: mongodb-data-claim
      containers:
        - name: mongodb       
          image: imagefromInternalRepo/mongo:4.0.12
          command:
            - mongod
            - --bind_ip
            - 0.0.0.0
          volumeMounts:
             - name: mongodb-data
               mountPath: /data/db
          ports:
            - containerPort: 27017

the service yaml is:

apiVersion: v1
kind: Service
metadata:
  name: mongodb
  labels:
    app: mongodb
spec:
  type: NodePort
  ports:
    - port: 27017
      targetPort: 27017
      nodePort: 32017
      protocol: TCP
      name: http
  selector:
    app: mongodb

And the ingress yaml is:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-mongodb
spec:
  rules:
    - host: "bb-mongo-db.namespace.com"
      http:
        paths:
          - path: /
            backend:
              serviceName: mongodb
              servicePort: 32017
  tls:
    - hosts:
      - "bb-mongo-db.namespace.com"

The pod is up and running. But when I am trying the hit the url bb-mongo-db.namespace.com, I am getting 503: service unavailable.

I am new to this.Please help me with this.

-- Abhishek Mishra
kubernetes
kubernetes-ingress
kubernetes-pod
kubernetes-service
mongodb

1 Answer

11/23/2019

If you're using Nginx Ingress it doesn't support TCP/UDP services out of the box, you're trying to reach MongoDB via Http which doesn't work, to configure TCP/UDP for nginx ingress you can use this work around:

https://kubernetes.github.io/ingress-nginx/user-uide/exposing-tcp-udp-services/

https://github.com/nginxinc/kubernetes-ingress/tree/master/examples/tcp-udp

-- B.Mouad
Source: StackOverflow