英文:
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部署中,你的selector
的match labels
和template
的label
需要保持一致。
其次,你需要创建一个类型为ClusterIP
的Service
。
以下是示例代码:
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 # <- *****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: "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
答案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 > service-definition.yaml
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论