.NET Core pod 无法连接到 Kubernetes 中的 SQL Server pod
Posted
技术标签:
【中文标题】.NET Core pod 无法连接到 Kubernetes 中的 SQL Server pod【英文标题】:.NET Core pod can't connect to SQL Server pod in Kubernetes 【发布时间】:2021-09-10 01:24:33 【问题描述】:我有一个 .NET Core pod,需要访问 Kubernetes(docker-desktop)中的 SQL Server pod。 使用端口转发,我可以从 SQL Server Management Studio 连接到该 SQL Server。但是当我尝试从 .NET Core pod 连接时,它会说
服务器未找到或无法访问
这是来自日志的错误
[04:28:38 Error] Microsoft.EntityFrameworkCore.Database.Connection
An error occurred using the connection to database 'MyTestDatabase' on server 'tcp:sqlserver-service,1433'.
[04:28:38 Error] Microsoft.EntityFrameworkCore.Query
An exception occurred while iterating over the results of a query for context type 'Web.Data.ApplicationDbContext'.
Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)
我在容器中的连接字符串
Server=tcp:sqlserver-service,1433;User ID=sa;Password=myPass12;Initial Catalog=MyTestDatabase;MultipleActiveResultSets=true;Connection Timeout=30;
SQL Server 部署 yml 文件
apiVersion: v1
kind: PersistentVolume
metadata:
name: sqldata
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteMany
hostPath:
path: "/sqldata"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: dbclaim
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 2Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: sqlserver
spec:
replicas: 1
selector:
matchLabels:
app: sqlserver
template:
metadata:
labels:
app: sqlserver
spec:
volumes:
- name: sqldata-storage
persistentVolumeClaim:
claimName: dbclaim
terminationGracePeriodSeconds: 10
initContainers:
- name: volume-permissions
image: busybox
command: ["sh", "-c", "chown -R 10001:0 /var/opt/mssql"]
volumeMounts:
- mountPath: "/var/opt/mssql"
name: sqldata-storage
containers:
- name: sqlserver1
image: mcr.microsoft.com/mssql/server
ports:
- containerPort: 1433
env:
- name: MSSQL_PID
value: "Developer"
- name: SA_PASSWORD
value: "myPass12"
- name: ACCEPT_EULA
value: "Y"
volumeMounts:
- mountPath: "/var/opt/mssql/data"
name: sqldata-storage
---
apiVersion: v1
kind: Service
metadata:
name: sqlserver-service
spec:
ports:
- name: sqlserver
port: 1433
targetPort: 1433
protocol: TCP
selector:
name: sqlserver
type: LoadBalancer
从 SQL Server Management Studio 连接
我肯定错过了什么。
提前致谢
【问题讨论】:
数据库存在于 ms sql 中,您尝试连接核心服务? 连接字符串应该与SSMS的登录窗口匹配。登录窗口显示服务器名称/实例。还会用 Window Credential 或 SQL Credential 来指示。如果 Window Credential 比连接字符串不应该有用户名/密码。而是在连接字符串中使用 Integrated Security = true。 @HarshManvar yes 数据库存在。 @jdweng SQL 凭据已启用,我可以使用来自 SSMS 的 SA 帐户登录 您可以尝试使用 Linux 映像确保更改 PVC 或 PV,因为它将存储配置,更改容器配置不会重写因此创建新的 PVC 或 PV 以测试 Linux . 【参考方案1】:这是我的错。实际上服务选择器是错误的。
selector:
name: sqlserver
应该是
selector:
app: sqlserver
谢谢大家
【讨论】:
应该是正确的答案,所以我补充一下,网络服务的类型必须是 ClusterIP 而不是 LoadBlancer以上是关于.NET Core pod 无法连接到 Kubernetes 中的 SQL Server pod的主要内容,如果未能解决你的问题,请参考以下文章
Blazor 无法连接到 ASP.NET Core WebApi (CORS)
NativeScript:我无法连接到我的本地 .NET Core API
NativeScript:我无法连接到我的本地 .NET Core API