问题:
我的flask API无法连接到我的Postgres实例。我已验证数据库和api均能按预期工作,并且部署和服务都在kubernetes中运行。它必须是连接本身。连接是在Flask配置文件中定义的,所以也许我没有正确指定它?我无所适从。
错误
这是我在检查特定于该API的Pod日志时看到的错误,该API试图与Postgres取得联系。
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection timed out
Is the server running on host "postgres" (198.105.244.228) and accepting
TCP/IP connections on port 5432?
堆栈信息
Flask / Config.py
SQLALCHEMY_DATABASE_URI = 'postgres://postgres:postgres@postgres:5432/postgres'
。这与我切换到Kubernetes之前使用Docker Compose的情况相同。
Kubernetes / postgres-cluster-ip-service.yaml
apiVersion: v1
kind: Service
metadata:
name: postgres-cluster-ip-service
spec:
type: ClusterIP
selector:
component: postgres
ports:
- port: 5432
targetPort: 5432
Kubernetes / postgres-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: postres-deployment
spec:
replicas: 1
selector:
matchLabels:
component: postgres
template:
metadata:
labels:
component: postgres
spec:
volumes:
- name: postgres-storage
persistentVolumeClaim:
claimName: database-persistent-volume-claim
imagePullSecrets:
- name: regcred
containers:
- name: postgres
image: my/image-db
ports:
- containerPort: 5432
env:
- name: POSTGRES_PASSWORD
value: postgres
- name: POSTGRES_USER
value: postgres
volumeMounts:
- name: postgres-storage
mountPath: /var/lib/postgresql/data
subPath: postgres
Kuberenetes /数据库持久卷声明
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: database-persistent-volume-claim
spec:
# Access mode gets some instance of storage.
# ReadWriteOncence means that it can be used by a single node.
accessModes:
- ReadWriteOnce
resources:
requests:
# find a storage option that has at least 2 gigs of space
storage: 2Gi
很高兴添加任何其他有帮助的文件!
答案 0 :(得分:2)
服务的名称是主机名,因此您应该连接到import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.BorderLayout;
import java.awt.event.*;
public class AddToTable {
public static void main(String[] args) {
JTable jTable = new JTable(new DefaultTableModel(
new Object[][] {},
new String[] {"Item", "Qty", "Price", "ID"}));
JComboBox<String> jComboBox = new JComboBox<>();
jComboBox.addItem("Item1 _ 2 _ 2.5 _ 101");
jComboBox.addItem("Item2 _ 5 _ 6 _ 201");
jComboBox.addItem("Item3 _ 3 _ 1.5 _ 301");
jComboBox.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
Object items = jComboBox.getSelectedItem();
String name = (String) items;
String[] part = name.split(" _ ");
String item = (part[0]);
Integer qty = Integer.valueOf((part[1]));
Double price = Double.parseDouble((part[2]));
Integer ids = Integer.valueOf(part[3]);
DefaultTableModel df = (DefaultTableModel) jTable.getModel();
int rows = df.getRowCount();
boolean found = false;
for (int i = 0; i < rows; i++) {
if (ids.equals(jTable.getValueAt(i, 3))) {
Integer newQty = qty + (Integer) jTable.getValueAt(i, 1);
Double newPrice = price + (Double) jTable.getValueAt(i, 2);
jTable.setValueAt(newQty, i, 1);
jTable.setValueAt(newPrice , i, 2);
found = true;
break;
}
}
if (!found) {
df.addRow(new Object[]{item, qty, price, ids});
}
}
});
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(jComboBox, BorderLayout.NORTH);
f.getContentPane().add(new JScrollPane(jTable), BorderLayout.CENTER);
f.setBounds(300, 200, 400, 300);
f.setVisible(true);
}
}
(如果要部署到其他Kubernetes命名空间,请更改postgres-cluster-ip-service.default.svc.cluster.local
)。您的错误消息似乎是您正在连接集群环境之外的其他名为default
的系统。
答案 1 :(得分:0)
只需为上述搜索添加到@David的正确答案中即可
SQLALCHEMY_DATABASE_URI的格式为:
schema://username:password@SERVER:PORT/databasename
我的失败原因是因为我认为我的服务器名称是我的Kubernetes postgres-deployment文件定义的Postgres。但是,我有一个单独的“服务”文件,称为postgres-cluster-ip-service。我第二次将其设置为服务器名称,连接开始工作。确保您不是将服务器直接指向服务器,而是将其前面的IP服务指向Kubernetes。
示例:
postgres://username:password@postgres-cluster-ip-service:5432/databasename