2

My webprotege StatefulSet has the following configuration.

apiVersion: apps/v1
kind: StatefulSet
metadata:
   name: webprotege-test
   labels:
      app: webprotege-test
spec:
   serviceName: "webprotege-service-test"
   volumeClaimTemplates:
   - metadata:
       name: data
     spec:
       accessModes: [ "ReadWriteOnce" ]
       resources:
         requests:
           storage: 20Gi
   replicas: 1
   selector:
      matchLabels:
         app: webprotege-test
   template:
      metadata:
         labels:
            app: webprotege-test
      spec:
         containers:
         #
         # webprotege app
         - name: webprotege-test
           #image: alexmilowski/allegrograph-kubernetes:latest
           image: testwebprotege:4.0.2
           # Wait for database. Checks that the db is listening on port 27017.
           command: ["/bin/sh"]
           args: ["-c", "for i in {1..20}; do if nc -z localhost 27017; then catalina.sh run; fi; echo 'waiting for database...'; sleep 2; done; exit 1"]
           volumeMounts:
           - name: data
             mountPath: /srv/webprotege
           ports:
           - containerPort: 8080
             name: webprotege-http
           env:
           - name: webprotege.mongodb.host
             value: localhost
         #
         # mongodb
         - name: wpmongo-test
           image: mongo:4.1-bionic
           volumeMounts:
           - name: data
             mountPath: /data/db

I would like to access the mongo db container outside kubernetes using a mongodb client. How do I expose the mongodb to clients outside the kubernetes cluster. I have tried adding a containerPort but this does not seem to work.

  • you can expose your mongoDb deployment with a service using type nodeport. this will open a high-number port on all your nodes on which you can access your your container. as this will by default only make your pod accessible via ip, another way would be to have an ingress controller deployed on your cluster, exposing the ingress controller with a nodeport service and setup a single loadbalancer pointing to your ingress controller and then creating ingress resources exposing your mongodb deployment – meaningqo Jul 19 '21 at 07:12
  • Do you run your `StatefulSet` on **GKE** as a tag added to your question may suggest ? If so, take a closer look at [Running MongoDB on Kubernetes with StatefulSets](https://kubernetes.io/blog/2017/01/running-mongodb-on-kubernetes-with-statefulsets/). It should help you to understand it better, including how to expose it externally. – mario Jul 19 '21 at 23:33

2 Answers2

0

There are multiple ways of exposing your service. Which one to use depends on your use case.

According to the documentation, these are 2 possible ways to expose your Mongo DB pods outside your cluster:

  • NodePort - Exposes the Service on the same port of each selected Node in the cluster using NAT. Makes a Service accessible from outside the cluster using :. Superset of ClusterIP.
  • LoadBalancer - Creates an external load balancer in the current cloud (if supported) and assigns a fixed, external IP to the Service. Superset of NodePort.

Please note that you need to be running your cluster in an environment that supports LoadBalancers in order to use them. This typically means running in a supported cloud provider (AWS, Azure, Google Cloud, Digital Ocean, and many others) or on an on prem cluster specifically configured to support Load Balancers.

whites11
  • 12,008
  • 3
  • 36
  • 53
0

"How do I expose the mongodb to clients outside the kubernetes cluster": you need a "normal" service (in addition to the headless service that must go with the statefulset). There is an excellant tutorial on statefulsets at https://redhat-scholars.github.io/kubernetes-tutorial/kubernetes-tutorial/statefulset.html which gives an example for that service (follow the tutorial to change the details according to your situation):

apiVersion: v1
kind: Service
metadata:
  name: quarkus-statefulset-2
spec:
  type: LoadBalancer 
  externalTrafficPolicy: Local 
  selector:
    statefulset.kubernetes.io/pod-name: quarkus-statefulset-2 
  ports:
  - port: 8080
  name: web