How to connect MongoDB , golang in Kubernetes

huangapple go评论118阅读模式
英文:

How to connect MongoDB , golang in Kubernetes

问题

数据库和服务器未连接。
尝试在Kubernetes环境中部署。

这是关于MongoDB和Golang HTTP服务器的部署和服务的代码:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: backend
  5. labels:
  6. app: backend
  7. spec:
  8. selector:
  9. matchLabels:
  10. app: backend
  11. template:
  12. metadata:
  13. labels:
  14. app: backend
  15. spec:
  16. containers:
  17. - image: royroyee/backend:0.8
  18. name: backend
  19. ports:
  20. - containerPort: 9001
  21. ---
  22. apiVersion: v1
  23. kind: Service
  24. metadata:
  25. name: backend-service
  26. labels:
  27. run: backend-service
  28. spec:
  29. ports:
  30. - port: 9001
  31. targetPort: 9001
  32. protocol: TCP
  33. selector:
  34. app: backend
  35. type: NodePort
  36. ---
  37. apiVersion: apps/v1
  38. kind: Deployment
  39. metadata:
  40. name: mongo
  41. labels:
  42. app: mongo
  43. spec:
  44. selector:
  45. matchLabels:
  46. app: mongo
  47. template:
  48. metadata:
  49. labels:
  50. app: mongo
  51. spec:
  52. containers:
  53. - image: mongo
  54. name: mongo-db
  55. ports:
  56. - containerPort: 27017
  57. ---
  58. apiVersion: v1
  59. kind: Service
  60. metadata:
  61. name: mongo-service
  62. labels:
  63. run: mongo-service
  64. spec:
  65. ports:
  66. - port: 27017
  67. targetPort: 27017
  68. protocol: TCP
  69. selector:
  70. app: mongo

这是我的Golang代码中关于MongoDB会话的部分:

  1. func getSession() *mgo.Session {
  2. s, err := mgo.Dial("mongodb://mongo-service:27017/mongo-db")

请告诉我你的问题是什么。

另外,我尝试了以下几种连接方式:

  • mongodb://mongo-service:27017/backend
  • mongodb://mongo-service:27017/mongo-db
  • mongodb://mongo-service:27017
英文:

The database and the server are not connected.
Attempting to deploy in Kubernetes environment.

this is deployment, sevice of mongodb , golang http server

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: backend
  5. labels:
  6. app: backend
  7. spec:
  8. selector:
  9. matchLabels:
  10. app: backend
  11. template:
  12. metadata:
  13. labels:
  14. app: backend
  15. spec:
  16. containers:
  17. - image: royroyee/backend:0.8
  18. name: backend
  19. ports:
  20. - containerPort: 9001
  21. ---
  22. apiVersion: v1
  23. kind: Service
  24. metadata:
  25. name: backend-service
  26. labels:
  27. run: backend-service
  28. spec:
  29. ports:
  30. - port: 9001
  31. targetPort: 9001
  32. protocol: TCP
  33. selector:
  34. app: backend
  35. type: NodePort
  36. ---
  37. apiVersion: apps/v1
  38. kind: Deployment
  39. metadata:
  40. name: mongo
  41. labels:
  42. app: mongo
  43. spec:
  44. selector:
  45. matchLabels:
  46. app: mongo
  47. template:
  48. metadata:
  49. labels:
  50. app: mongo
  51. spec:
  52. containers:
  53. - image: mongo
  54. name: mongo-db
  55. ports:
  56. - containerPort: 27017
  57. ---
  58. apiVersion: v1
  59. kind: Service
  60. metadata:
  61. name: mongo-service
  62. labels:
  63. run: mongo-service
  64. spec:
  65. ports:
  66. - port: 27017
  67. targetPort: 27017
  68. protocol: TCP
  69. selector:
  70. app: mongo

and my golang code ...
mongodb session

  1. func getSession() *mgo.Session {
  2. s, err := mgo.Dial("mongodb://mongo-service:27017/mongo-db")

pls let me know ..

also I tried something like this.

  1. // mongodb://mongo-service:27017/backend
  2. // mongodb://mongo-service:27017/mongo-db
  3. // mongodb://mongo-service:27017

答案1

得分: 1

要在Kubernetes环境中使用Golang连接MongoDB,您需要按照以下步骤进行操作:

  1. 在Kubernetes集群中部署MongoDB,可以选择使用statefulset或deployment。

  2. 为MongoDB创建一个Service,以便从您的Golang应用程序访问已部署的pods。

  3. 在您的Golang应用程序中,使用官方的MongoDB Go驱动程序通过指定服务名称和端口来建立与MongoDB服务的连接。

  4. 运行一个简单的测试来验证连接,测试可以插入和检索MongoDB数据库中的数据。

  5. 最后,将Golang应用程序打包为Docker镜像,并在同一Kubernetes集群中部署为deployment。

以下是一个连接MongoDB的示例Go代码:

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "go.mongodb.org/mongo-driver/mongo"
  7. "go.mongodb.org/mongo-driver/mongo/options"
  8. )
  9. func main() {
  10. // 设置客户端选项
  11. clientOptions := options.Client().ApplyURI("mongodb://mongodb-service:27017")
  12. // 连接MongoDB
  13. client, err := mongo.Connect(context.TODO(), clientOptions)
  14. if err != nil {
  15. log.Fatal(err)
  16. }
  17. // 检查连接
  18. err = client.Ping(context.TODO(), nil)
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. fmt.Println("Connected to MongoDB!")
  23. }

以下是一个将MongoDB部署为StatefulSet和将Go应用程序部署为Deployment的示例YAML文件:

  1. apiVersion: apps/v1
  2. kind: StatefulSet
  3. metadata:
  4. name: mongodb
  5. spec:
  6. serviceName: mongodb-service
  7. replicas: 1
  8. selector:
  9. matchLabels:
  10. app: mongodb
  11. template:
  12. metadata:
  13. labels:
  14. app: mongodb
  15. spec:
  16. containers:
  17. - name: mongodb
  18. image: mongo:4.4
  19. ports:
  20. - containerPort: 27017
  21. volumeMounts:
  22. - name: mongodb-data
  23. mountPath: /data/db
  24. volumeClaimTemplates:
  25. - metadata:
  26. name: mongodb-data
  27. annotations:
  28. volume.beta.kubernetes.io/storage-class: standard
  29. spec:
  30. accessModes: [ "ReadWriteOnce" ]
  31. resources:
  32. requests:
  33. storage: 1Gi
  34. ---
  35. apiVersion: v1
  36. kind: Service
  37. metadata:
  38. name: mongodb-service
  39. spec:
  40. selector:
  41. app: mongodb
  42. ports:
  43. - name: mongodb
  44. port: 27017
  45. targetPort: 27017
  46. clusterIP: None
  47. ---
  48. apiVersion: apps/v1
  49. kind: Deployment
  50. metadata:
  51. name: go-app
  52. spec:
  53. replicas: 1
  54. selector:
  55. matchLabels:
  56. app: go-app
  57. template:
  58. metadata:
  59. labels:
  60. app: go-app
  61. spec:
  62. containers:
  63. - name: go-app
  64. image: <your-go-app-image>
  65. ports:
  66. - containerPort: 8080

注意:您需要将your-go-app-image替换为实际的Go应用程序的Docker镜像。

英文:

To connect MongoDB with Golang in a Kubernetes environment, you need to follow these steps:

Deploy MongoDB as a statefulset or a deployment in your Kubernetes cluster.

Create a Service for MongoDB to access the deployed pods from your Golang application.

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.

Verify the connection by running a simple test that inserts and retrieves data from the MongoDB database.

Finally, package the Golang application as a Docker image and deploy it as a deployment in the same Kubernetes cluster.

Here is a sample Go code to connect to MongoDB:

  1. package main
  2. import (
  3. &quot;context&quot;
  4. &quot;fmt&quot;
  5. &quot;log&quot;
  6. &quot;go.mongodb.org/mongo-driver/mongo&quot;
  7. &quot;go.mongodb.org/mongo-driver/mongo/options&quot;
  8. )
  9. func main() {
  10. // Set client options
  11. clientOptions := options.Client().ApplyURI(&quot;mongodb://mongodb-service:27017&quot;)
  12. // Connect to MongoDB
  13. client, err := mongo.Connect(context.TODO(), clientOptions)
  14. if err != nil {
  15. log.Fatal(err)
  16. }
  17. // Check the connection
  18. err = client.Ping(context.TODO(), nil)
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. fmt.Println(&quot;Connected to MongoDB!&quot;)
  23. }

Here's a sample YAML file for deploying MongoDB as a StatefulSet and a Go application as a Deployment:

  1. apiVersion: apps/v1
  2. kind: StatefulSet
  3. metadata:
  4. name: mongodb
  5. spec:
  6. serviceName: mongodb-service
  7. replicas: 1
  8. selector:
  9. matchLabels:
  10. app: mongodb
  11. template:
  12. metadata:
  13. labels:
  14. app: mongodb
  15. spec:
  16. containers:
  17. - name: mongodb
  18. image: mongo:4.4
  19. ports:
  20. - containerPort: 27017
  21. volumeMounts:
  22. - name: mongodb-data
  23. mountPath: /data/db
  24. volumeClaimTemplates:
  25. - metadata:
  26. name: mongodb-data
  27. annotations:
  28. volume.beta.kubernetes.io/storage-class: standard
  29. spec:
  30. accessModes: [ &quot;ReadWriteOnce&quot; ]
  31. resources:
  32. requests:
  33. storage: 1Gi
  34. ---
  35. apiVersion: v1
  36. kind: Service
  37. metadata:
  38. name: mongodb-service
  39. spec:
  40. selector:
  41. app: mongodb
  42. ports:
  43. - name: mongodb
  44. port: 27017
  45. targetPort: 27017
  46. clusterIP: None
  47. ---
  48. apiVersion: apps/v1
  49. kind: Deployment
  50. metadata:
  51. name: go-app
  52. spec:
  53. replicas: 1
  54. selector:
  55. matchLabels:
  56. app: go-app
  57. template:
  58. metadata:
  59. labels:
  60. app: go-app
  61. spec:
  62. containers:
  63. - name: go-app
  64. image: &lt;your-go-app-image&gt;
  65. ports:
  66. - containerPort: 8080

Note: You will need to replace your-go-app-image with the actual Docker image of your Go application.

huangapple
  • 本文由 发表于 2023年1月31日 16:25:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75294389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定