<p>I followed the <a href="https://www.mongodb.com/blog/post/running-mongodb-as-a-microservice-with-docker-and-kubernetes" rel="nofollow noreferrer">https://www.mongodb.com/blog/post/running-mongodb-as-a-microservice-with-docker-and-kubernetes</a> and tried to run a single mongodb instance with a LoadBalancer Service and Replication Controller on a Google Cloud account.</p>

<p>Following is the yaml file:</p>

<pre><code>---
apiVersion: v1
kind: Service
metadata:
  name: mongo-svc-a
  labels:
    name: mongo-svc-a
spec:
  type: LoadBalancer
  ports:
  - port: 27017
    targetPort: 27017
    protocol: TCP
    name: mongo-svc-a
  selector:
    name: mongo-node1
    instance: rod
---
apiVersion: v1
kind: ReplicationController
metadata:
  name: mongo-rc1
  labels:
    name: mongo-rc
spec:
  replicas: 1
  selector:
    name: mongo-node1
  template:
    metadata:
      labels:
        name: mongo-node1
        instance: rod
    spec:
      containers:
      - name: mongo-node1
        image: mongo
        ports:
        - containerPort: 27017
        volumeMounts:
        - name: mongo-persistent-storage1
          mountPath: /data/db
      volumes:
      - name: mongo-persistent-storage1
        gcePersistentDisk:
          pdName: mongodb-disk1-in-cluster1
          fsType: ext4
</code></pre>

<p>Following are the details of the Service, Replication Controller and Pods that were created. All seems to be fine.</p>

<pre><code>$ kubectl describe service mongo-svc-a 
Name:                   mongo-svc-a
Namespace:              default
Labels:                 name=mongo-svc-a
Selector:               instance=rod,name=mongo-node1
Type:                   LoadBalancer
IP:                     10.3.241.11
LoadBalancer Ingress:   104.198.236.2
Port:                   mongo-svc-a     27017/TCP
NodePort:               mongo-svc-a     31808/TCP
Endpoints:              10.0.0.3:27017
Session Affinity:       None
No events.


$ kubectl describe rc mongo-rc1
Name:           mongo-rc1
Namespace:      default
Image(s):       mongo
Selector:       name=mongo-node1
Labels:         name=mongo-rc
Replicas:       1 current / 1 desired
Pods Status:    1 Running / 0 Waiting / 0 Succeeded / 0 Failed
Volumes:
  mongo-persistent-storage1:
    Type:       GCEPersistentDisk (a Persistent Disk resource in Google Compute Engine)
    PDName:     mongodb-disk1-in-cluster1
    FSType:     ext4
    Partition:  0
    ReadOnly:   false
No events.


$ kubectl describe pod mongo-rc1-h0j8r
Name:           mongo-rc1-h0j8r
Namespace:      default
Node:           gke-cluster1-default-pool-d58b6c05-74fq/10.128.0.6
Start Time:     Sat, 11 Feb 2017 18:43:00 +0530
Labels:         instance=rod
                name=mongo-node1
Status:         Running
IP:             10.0.0.3
Controllers:    ReplicationController/mongo-rc1
Containers:
  mongo-node1:
    Container ID:       docker://9f28e482d3806b74f7f595c47e6c7940c2313e95860db13d137ad6eaa88bb341
    Image:              mongo
    Image ID:           docker://sha256:ad974e767ec4f06945b1e7ffdfc57bd10e06baf66cdaf5a003e0e6a36924e30b
    Port:               27017/TCP
    Requests:
      cpu:              100m
    State:              Running
      Started:          Sat, 11 Feb 2017 18:45:37 +0530
    Ready:              True
    Restart Count:      0
    Volume Mounts:
      /data/db from mongo-persistent-storage1 (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-3wfv3 (ro)
    Environment Variables:      &lt;none&gt;
Conditions:
  Type          Status
  Initialized   True 
  Ready         True 
  PodScheduled  True 
Volumes:
  mongo-persistent-storage1:
    Type:       GCEPersistentDisk (a Persistent Disk resource in Google Compute Engine)
    PDName:     mongodb-disk1-in-cluster1
    FSType:     ext4
    Partition:  0
    ReadOnly:   false
  default-token-3wfv3:
    Type:       Secret (a volume populated by a Secret)
    SecretName: default-token-3wfv3
QoS Class:      Burstable
Tolerations:    &lt;none&gt;
No events.
</code></pre>

<p>But when i try to remote connect, <code>mongo --hostname &lt;Extenal_IP_of_the_service&gt;</code> to the newly created monogdb instance, i am unable to connect. I believe the configuration seems alright. Any help would be appreciated. Thanks,</p>

<ul>
<li>Pavan</li>
</ul>

<p>Tried a different approach and nailed it. </p>

<p>Followed the following tutorial: <a href="https://kubernetes.io/docs/tutorials/stateful-application/run-stateful-application/" rel="nofollow noreferrer">https://kubernetes.io/docs/tutorials/stateful-application/run-stateful-application/</a></p>

<p>Exposed the headless service: </p>

<pre><code>kubectl expose service your_service_name --port=27017 --target-port=27017 --name=mongo-lb-service --type=LoadBalancer
</code></pre>
