在Kubernetes上使用Golang部署PostgreSQL

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

Deploying Postgresql with Golang on Kubernetes

问题

我正在处理Kubernetes。无法连接服务器和数据库。告诉我我做错了什么,还需要做什么。

要获取对数据库的访问权限,你需要在Deployment的环境变量中指定数据库的主机名、端口、用户名、密码等信息。在你的配置文件中,你已经指定了数据库的环境变量,但是缺少了一些必要的信息。

server的Deployment中,你需要将以下环境变量添加到containers部分的env字段中:

  • DB_HOST:指定数据库的主机名,这里应该是testdb
  • DB_PORT:指定数据库的端口号,这里应该是5432
  • DB_USER:指定数据库的用户名,这里应该是postgres
  • DB_DBNAME:指定要连接的数据库名称,这里应该是postgres
  • DB_PASSWORD:指定数据库的密码,这里应该是root
  • DB_SSL:指定数据库连接是否启用SSL,这里应该是disable

添加这些环境变量后,你的应用程序应该能够连接到数据库了。

关于如何使用数据库和API的示例,你可以参考以下代码:

  1. import psycopg2
  2. # 连接到数据库
  3. conn = psycopg2.connect(
  4. host="testdb",
  5. port="5432",
  6. user="postgres",
  7. password="root",
  8. dbname="postgres"
  9. )
  10. # 创建游标
  11. cur = conn.cursor()
  12. # 执行SQL查询
  13. cur.execute("SELECT * FROM your_table")
  14. # 获取查询结果
  15. result = cur.fetchall()
  16. # 处理查询结果
  17. for row in result:
  18. print(row)
  19. # 关闭游标和数据库连接
  20. cur.close()
  21. conn.close()

这是一个使用Python的psycopg2库连接到PostgreSQL数据库并执行查询的示例。你可以根据自己的需求进行修改和扩展。

希望这能帮到你!如果还有其他问题,请随时提问。

英文:

I'm dealing with Kubernetes. It is not possible to connect the server and the database. Tell me what I'm doing wrong and what else I need to do.

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: testdb
  5. spec:
  6. replicas: 1
  7. selector:
  8. matchLabels:
  9. tier: testdb
  10. app: testapp
  11. template:
  12. metadata:
  13. labels:
  14. app: testdb
  15. spec:
  16. containers:
  17. - name: testdb
  18. image: pryby/testdbcon
  19. env:
  20. - name: POSTGRES_USER
  21. value: postgres
  22. - name: POSTGRES_PASSWORD
  23. value: root
  24. - name: POSTGRES_DB
  25. value: postgres
  26. ports:
  27. - containerPort: 5432
  28. ---
  29. apiVersion: apps/v1
  30. kind: Deployment
  31. metadata:
  32. name: server
  33. spec:
  34. replicas: 1
  35. selector:
  36. matchLabels:
  37. app: server
  38. template:
  39. metadata:
  40. labels:
  41. app: server
  42. spec:
  43. containers:
  44. - name: server
  45. image: pryby/testdocker
  46. env:
  47. - name: LISTEN
  48. value: 0.0.0.0:8080
  49. - name: DB_HOST
  50. value: testdb
  51. - name: DB_PORT
  52. value: "5432"
  53. - name: DB_USER
  54. value: postgres
  55. - name: DB_DBNAME
  56. value: postgres
  57. - name: DB_PASSWORD
  58. value: root
  59. - name: DB_SSL
  60. value: disable
  61. ports:
  62. - containerPort: 8080

What and where do I need to specify in order to get access to the database?
If possible, give an example for working with the database and api, I climbed everywhere where I can, I didn't find anything like that. Maybe I didn't search well.

答案1

得分: 1

首先,在你的testDB部署中,你的selectormatch labelstemplatelabel需要保持一致。

其次,你需要创建一个类型为ClusterIPService

以下是示例代码:

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: testdb
  5. labels:
  6. app: testdb
  7. spec:
  8. ports:
  9. - port: 5432
  10. name: web
  11. clusterIP: None
  12. selector:
  13. app: testdb

最终的yaml文件应该如下所示:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: testdb
  5. spec:
  6. replicas: 1
  7. selector:
  8. matchLabels:
  9. # tier: testdb
  10. app: testdb # <- *****在这里进行了更改******
  11. template:
  12. metadata:
  13. labels:
  14. app: testdb
  15. spec:
  16. containers:
  17. - name: testdb
  18. image: pryby/testdbcon
  19. env:
  20. - name: POSTGRES_USER
  21. value: postgres
  22. - name: POSTGRES_PASSWORD
  23. value: root
  24. - name: POSTGRES_DB
  25. value: postgres
  26. ports:
  27. - containerPort: 5432
  28. ---
  29. apiVersion: apps/v1
  30. kind: Deployment
  31. metadata:
  32. name: server
  33. spec:
  34. replicas: 1
  35. selector:
  36. matchLabels:
  37. app: server
  38. template:
  39. metadata:
  40. labels:
  41. app: server
  42. spec:
  43. containers:
  44. - name: server
  45. image: pryby/testdocker
  46. env:
  47. - name: LISTEN
  48. value: 0.0.0.0:8080
  49. - name: DB_HOST
  50. value: testdb
  51. - name: DB_PORT
  52. value: "5432"
  53. - name: DB_USER
  54. value: postgres
  55. - name: DB_DBNAME
  56. value: postgres
  57. - name: DB_PASSWORD
  58. value: root
  59. - name: DB_SSL
  60. value: disable
  61. ports:
  62. - containerPort: 8080
  63. ---
  64. apiVersion: v1
  65. kind: Service
  66. metadata:
  67. name: testdb
  68. labels:
  69. app: testdb
  70. spec:
  71. ports:
  72. - port: 5432
  73. name: web
  74. clusterIP: None
  75. selector:
  76. app: testdb
英文:

Firstly, Here in your testDB deployment your selector's match labels and template's label need to be the same.

Secondly, You need to create a Service of Type ClusterIP.

Here is the example:

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: testdb
  5. labels:
  6. app: testdb
  7. spec:
  8. ports:
  9. - port: 5432
  10. name: web
  11. clusterIP: None
  12. selector:
  13. app: testdb

The final yaml should look like this:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: testdb
  5. spec:
  6. replicas: 1
  7. selector:
  8. matchLabels:
  9. # tier: testdb
  10. app: testdb # &lt;- *****changed here******
  11. template:
  12. metadata:
  13. labels:
  14. app: testdb
  15. spec:
  16. containers:
  17. - name: testdb
  18. image: pryby/testdbcon
  19. env:
  20. - name: POSTGRES_USER
  21. value: postgres
  22. - name: POSTGRES_PASSWORD
  23. value: root
  24. - name: POSTGRES_DB
  25. value: postgres
  26. ports:
  27. - containerPort: 5432
  28. ---
  29. apiVersion: apps/v1
  30. kind: Deployment
  31. metadata:
  32. name: server
  33. spec:
  34. replicas: 1
  35. selector:
  36. matchLabels:
  37. app: server
  38. template:
  39. metadata:
  40. labels:
  41. app: server
  42. spec:
  43. containers:
  44. - name: server
  45. image: pryby/testdocker
  46. env:
  47. - name: LISTEN
  48. value: 0.0.0.0:8080
  49. - name: DB_HOST
  50. value: testdb
  51. - name: DB_PORT
  52. value: &quot;5432&quot;
  53. - name: DB_USER
  54. value: postgres
  55. - name: DB_DBNAME
  56. value: postgres
  57. - name: DB_PASSWORD
  58. value: root
  59. - name: DB_SSL
  60. value: disable
  61. ports:
  62. - containerPort: 8080
  63. ---
  64. apiVersion: v1
  65. kind: Service
  66. metadata:
  67. name: testdb
  68. labels:
  69. app: testdb
  70. spec:
  71. ports:
  72. - port: 5432
  73. name: web
  74. clusterIP: None
  75. selector:
  76. app: testdb

答案2

得分: 0

将您的testdb部署暴露为类型为ClusterIP且端口为5432的服务。

您可以手动编写yaml文件,或者运行kubectl expose deployment testdb --port 5432 --targetPort 5432 --name testdb命令。

您还可以使用以下命令生成yaml定义文件:

kubectl expose deployment testdb --port 5432 --targetPort 5432 --name testdb -dry-run=client -o yaml > service-definition.yaml

英文:

expose your testdb deployment with a service of type ClusterIP and port 5432.

either craft the yaml yourself, or run kubectl expose deployment testdb --port 5432 --targetPort 5432 --name testdb

you can also use the following command to generate the yaml definition file:

  1. kubectl expose deployment testdb --port 5432 --targetPort 5432 --name testdb -dry-run=client -o yaml &gt; service-definition.yaml

huangapple
  • 本文由 发表于 2021年7月7日 17:56:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/68283816.html
匿名

发表评论

匿名网友

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

确定