<p>The database and the server are not connected.
Attempting to deploy in Kubernetes environment.</p>
<p>this is deployment, sevice of mongodb , golang http server</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend
  labels:
    app: backend
spec:
  selector:
    matchLabels:
      app: backend
  template:
    metadata:
      labels:
        app: backend
    spec:
      containers:
      - image: royroyee/backend:0.8
        name: backend
        ports:
        - containerPort: 9001

---
apiVersion: v1
kind: Service
metadata:
  name: backend-service
  labels:
    run: backend-service
spec:
  ports:
    - port: 9001
      targetPort: 9001
      protocol: TCP
  selector:
    app: backend
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo
  labels:
    app: mongo
spec:
  selector:
    matchLabels:
      app: mongo
  template:
    metadata:
      labels:
        app: mongo
    spec:
      containers:
      - image: mongo
        name: mongo-db
        ports:
        - containerPort: 27017
---
apiVersion: v1
kind: Service
metadata:
  name: mongo-service
  labels:
    run: mongo-service
spec:
  ports:
    - port: 27017
      targetPort: 27017
      protocol: TCP
  selector:
    app: mongo
</code></pre>
<p>and my golang code ...
mongodb session</p>
<pre><code>func getSession() *mgo.Session {
    s, err := mgo.Dial(&quot;mongodb://mongo-service:27017/mongo-db&quot;)

</code></pre>
<p>pls let me know ..</p>
<p>also I tried something like this.</p>
<pre><code>// mongodb://mongo-service:27017/backend
// mongodb://mongo-service:27017/mongo-db
// mongodb://mongo-service:27017
</code></pre>

Younghwan Kim
<p>To connect MongoDB with Golang in a Kubernetes environment, you need to follow these steps:</p>
<p>Deploy MongoDB as a statefulset or a deployment in your Kubernetes cluster.</p>
<p>Create a Service for MongoDB to access the deployed pods from your Golang application.</p>
<p>In your Golang application, use the official MongoDB Go driver to establish a connection to the MongoDB service by specifying the service name and port.</p>
<p>Verify the connection by running a simple test that inserts and retrieves data from the MongoDB database.</p>
<p>Finally, package the Golang application as a Docker image and deploy it as a deployment in the same Kubernetes cluster.</p>
<p>Here is a sample Go code to connect to MongoDB:</p>
<pre class="lang-golang prettyprint-override"><code>package main

import (
    &quot;context&quot;
    &quot;fmt&quot;
    &quot;log&quot;

    &quot;go.mongodb.org/mongo-driver/mongo&quot;
    &quot;go.mongodb.org/mongo-driver/mongo/options&quot;
)

func main() {
    // Set client options
    clientOptions := options.Client().ApplyURI(&quot;mongodb://mongodb-service:27017&quot;)

    // Connect to MongoDB
    client, err := mongo.Connect(context.TODO(), clientOptions)
    if err != nil {
        log.Fatal(err)
    }

    // Check the connection
    err = client.Ping(context.TODO(), nil)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(&quot;Connected to MongoDB!&quot;)
}

</code></pre>
<p>Here's a sample YAML file for deploying MongoDB as a StatefulSet and a Go application as a Deployment:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongodb
spec:
  serviceName: mongodb-service
  replicas: 1
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      containers:
        - name: mongodb
          image: mongo:4.4
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: mongodb-data
              mountPath: /data/db
  volumeClaimTemplates:
    - metadata:
      name: mongodb-data
      annotations:
        volume.beta.kubernetes.io/storage-class: standard
      spec:
        accessModes: [ &quot;ReadWriteOnce&quot; ]
        resources:
          requests:
            storage: 1Gi

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

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: go-app
  template:
    metadata:
      labels:
        app: go-app
    spec:
      containers:
        - name: go-app
          image: &lt;your-go-app-image&gt;
          ports:
            - containerPort: 8080

</code></pre>
<p>Note: You will need to replace <code>your-go-app-image</code> with the actual Docker image of your Go application.</p>
