在Kubernetes上使用Golang部署PostgreSQL

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

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的示例,你可以参考以下代码:

import psycopg2

# 连接到数据库
conn = psycopg2.connect(
    host="testdb",
    port="5432",
    user="postgres",
    password="root",
    dbname="postgres"
)

# 创建游标
cur = conn.cursor()

# 执行SQL查询
cur.execute("SELECT * FROM your_table")

# 获取查询结果
result = cur.fetchall()

# 处理查询结果
for row in result:
    print(row)

# 关闭游标和数据库连接
cur.close()
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.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: testdb
spec:
  replicas: 1
  selector:
    matchLabels:
      tier: testdb
      app: testapp
  template:
    metadata:
      labels:
        app: testdb
    spec:
      containers:
        - name: testdb
          image: pryby/testdbcon
          env:
            - name: POSTGRES_USER
              value: postgres
            - name: POSTGRES_PASSWORD
              value: root
            - name: POSTGRES_DB
              value: postgres
          ports:
            - containerPort: 5432
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: server
  template:
    metadata:
      labels:
        app: server
    spec:
      containers:
        - name: server
          image: pryby/testdocker
          env:
            - name: LISTEN
              value: 0.0.0.0:8080
            - name: DB_HOST
              value: testdb
            - name: DB_PORT
              value: "5432"
            - name: DB_USER
              value: postgres
            - name: DB_DBNAME
              value: postgres
            - name: DB_PASSWORD
              value: root
            - name: DB_SSL
              value: disable
          ports:
            - 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

以下是示例代码:

apiVersion: v1
kind: Service
metadata:
  name: testdb
  labels:
    app: testdb
spec:
  ports:
  - port: 5432
    name: web
  clusterIP: None
  selector:
    app: testdb

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

apiVersion: apps/v1
kind: Deployment
metadata:
  name: testdb
spec:
  replicas: 1
  selector:
    matchLabels:
      # tier: testdb
      app: testdb # <- *****在这里进行了更改******
  template:
    metadata:
      labels:
        app: testdb
    spec:
      containers:
        - name: testdb
          image: pryby/testdbcon
          env:
            - name: POSTGRES_USER
              value: postgres
            - name: POSTGRES_PASSWORD
              value: root
            - name: POSTGRES_DB
              value: postgres
          ports:
            - containerPort: 5432
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: server
  template:
    metadata:
      labels:
        app: server
    spec:
      containers:
        - name: server
          image: pryby/testdocker
          env:
            - name: LISTEN
              value: 0.0.0.0:8080
            - name: DB_HOST
              value: testdb
            - name: DB_PORT
              value: "5432"
            - name: DB_USER
              value: postgres
            - name: DB_DBNAME
              value: postgres
            - name: DB_PASSWORD
              value: root
            - name: DB_SSL
              value: disable
          ports:
            - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: testdb
  labels:
    app: testdb
spec:
  ports:
  - port: 5432
    name: web
  clusterIP: None
  selector:
    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:

apiVersion: v1
kind: Service
metadata:
  name: testdb
  labels:
    app: testdb
spec:
  ports:
  - port: 5432
    name: web
  clusterIP: None
  selector:
    app: testdb

The final yaml should look like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: testdb
spec:
  replicas: 1
  selector:
    matchLabels:
      # tier: testdb
      app: testdb # &lt;- *****changed here******
  template:
    metadata:
      labels:
        app: testdb
    spec:
      containers:
        - name: testdb
          image: pryby/testdbcon
          env:
            - name: POSTGRES_USER
              value: postgres
            - name: POSTGRES_PASSWORD
              value: root
            - name: POSTGRES_DB
              value: postgres
          ports:
            - containerPort: 5432
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: server
  template:
    metadata:
      labels:
        app: server
    spec:
      containers:
        - name: server
          image: pryby/testdocker
          env:
            - name: LISTEN
              value: 0.0.0.0:8080
            - name: DB_HOST
              value: testdb
            - name: DB_PORT
              value: &quot;5432&quot;
            - name: DB_USER
              value: postgres
            - name: DB_DBNAME
              value: postgres
            - name: DB_PASSWORD
              value: root
            - name: DB_SSL
              value: disable
          ports:
            - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: testdb
  labels:
    app: testdb
spec:
  ports:
  - port: 5432
    name: web
  clusterIP: None
  selector:
    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:

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:

确定