AKS - Web api 无法使用同一群集上的 ClusterIP 连接到数据库

AKS - web api cannot connect to database using ClusterIP on the same cluster

我的网站 api 无法连接到 AKS 中同一群集上的 PostgreSQL 数据库。我为 postgres 创建了一个 ClusterIP,然后使用该 ClusterIP 的 IP 地址作为我的数据库连接中的主机。我使用了默认端口 5432。

请注意,我之前使用 NodePort 时它是有效的。

这是 PostgreSQL ClusterIP 服务 yaml:

apiVersion: v1
kind: Service
metadata:
  name: postgres-service
  labels:
    app: portal
spec:  
  type: ClusterIP
  ports:  
  - port: 5432
  selector:
    app: portal
    tier: postgres

以下是 运行 的服务。我使用 postgres-service 的 Cluster-IP 作为我 web ap 中数据库连接字符串的主机。

这是我的连接字符串的一部分: "portal": "host=10.0.....;port=5432;

不确定这有什么问题,但是网络 api 无法连接到数据库。下面是错误。

Cannot connect to database. Error: Npgsql.NpgsqlException (0x80004005): Exception while connecting\n ---> System.TimeoutException: The operation has timed out.\n at Npgsql.TaskExtensions.WithTimeout(Task task, NpgsqlTimeout timeout)\n at Npgsql.NpgsqlConnector.ConnectAsync(NpgsqlTimeout timeout, CancellationToken cancellationToken)\n at Npgsql.NpgsqlConnector.ConnectAsync(NpgsqlTimeout timeout, CancellationToken cancellationToken)\n at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)\n at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)\n at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<<Open>g__OpenLong|0>d.MoveNext()\n--- End of stack trace from previous location where exception was thrown ---\n at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.ExistsAsync(CancellationToken cancellationToken)\n at

这是对 postgres 服务的描述

Name:              postgres-service
Namespace:         default
Labels:            app=portal
Annotations:       <none>
Selector:          app=portal,tier=postgres
Type:              ClusterIP
IP:                10.0.xxx.xxx
Port:              <unset>  5432/TCP
TargetPort:        5432/TCP
Endpoints:         10.xxx.xxx.xxx:5432
Session Affinity:  None
Events:            <none>

我现在可以让它工作了。问题不是 ClusterIP,而是我对 AKS 的 API 部署无法正常工作。

修复我的发布管道后,一切又恢复正常了。