无法从 Kubernetes 连接到本地主机上托管的 SQL 服务器数据库,我该如何调试?
Can not connect to SQL Server database hosted on localhost from Kubernetes, how can I debug this?
我正在尝试在 Kubernetes 中部署 asp.net 核心 2.2 应用程序。此应用程序是一个简单的网页,需要访问 SQL 服务器数据库才能显示一些信息。该数据库托管在我的本地开发计算机 (localhost) 上,Web 应用程序部署在 minikube 集群中以模拟生产环境,在该环境中我的 Web 应用程序可以部署在云中并访问远程数据库。
我设法通过公开端口 80 来显示我的 Web 应用程序。但是,我无法弄清楚如何使我的 Web 应用程序从集群内部连接到本地计算机上托管的 SQL 服务器数据库.
我假设我的连接字符串是正确的,因为当我在 IIS 本地服务器上部署我的 Web 应用程序时,它可以连接到 SQL 服务器数据库,在 docker 容器中(docker 运行) 或 docker 服务(docker 创建服务),但当它部署在 Kubernetes 集群中时则不会。我知道集群在不同的网络中,所以我尝试创建一个没有选择器的服务,如 this question 中所述,但没有运气......我什至尝试更改连接字符串 IP 地址以匹配其中一个创建了服务,但也失败了。
我的防火墙设置为接受到 1433 端口的入站连接。
我的 SQL 服务器数据库配置为允许远程访问。
这是我使用的连接字符串:
"Server=172.24.144.1\MyServer,1433;Database=TestWebapp;User Id=user_name;Password=********;"
这是我用来部署 Web 应用程序的文件:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: webapp
spec:
replicas: 1
template:
metadata:
labels:
app: webapp
spec:
containers:
- name: webapp
image: <private_repo_url>/webapp:db
imagePullPolicy: Always
ports:
- containerPort: 80
- containerPort: 443
- containerPort: 1433
imagePullSecrets:
- name: gitlab-auth
volumes:
- name: secrets
secret:
secretName: auth-secrets
---
apiVersion: v1
kind: Service
metadata:
name: webapp
labels:
app: webapp
spec:
type: NodePort
selector:
app: webapp
ports:
- name: port-80
port: 80
targetPort: 80
nodePort: 30080
- name: port-443
port: 443
targetPort: 443
nodePort: 30443
---
apiVersion: v1
kind: Service
metadata:
name: sql-server
labels:
app: webapp
spec:
ports:
- name: port-1433
port: 1433
targetPort: 1433
---
apiVersion: v1
kind: Endpoints
metadata:
name: sql-server
labels:
app: webapp
subsets:
- addresses:
- ip: 172.24.144.1 <-- IP of my local computer where SQL Server is running
ports:
- port: 1433
所以我得到了一个名为 'webapp' 的部署,只有一个 pod、两个名为 'webapp' 和 'sql-server' 的服务以及两个名为 'webapp' 和 [=40= 的端点].以下是他们的详细信息:
> kubectl describe svc webapp
Name: webapp
Namespace: default
Labels: app=webapp
Annotations: <none>
Selector: app=webapp
Type: NodePort
IP: 10.108.225.112
Port: port-80 80/TCP
TargetPort: 80/TCP
NodePort: port-80 30080/TCP
Endpoints: 172.17.0.4:80
Port: port-443 443/TCP
TargetPort: 443/TCP
NodePort: port-443 30443/TCP
Endpoints: 172.17.0.4:443
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
> kubectl describe svc sql-server
Name: sql-server
Namespace: default
Labels: app=webapp
Annotations: <none>
Selector: <none>
Type: ClusterIP
IP: 10.107.142.32
Port: port-1433 1433/TCP
TargetPort: 1433/TCP
Endpoints:
Session Affinity: None
Events: <none>
> kubectl describe endpoints webapp
Name: webapp
Namespace: default
Labels: app=webapp
Annotations: <none>
Subsets:
Addresses: 172.17.0.4
NotReadyAddresses: <none>
Ports:
Name Port Protocol
---- ---- --------
port-443 443 TCP
port-80 80 TCP
Events: <none>
> kubectl describe endpoints sql-server
Name: sql-server
Namespace: default
Labels: app=webapp
Annotations: <none>
Subsets:
Addresses: 172.24.144.1
NotReadyAddresses: <none>
Ports:
Name Port Protocol
---- ---- --------
<unset> 1433 TCP
Events: <none>
我希望连接到 SQL 服务器数据库,但是当我的应用程序尝试打开连接时,我收到此错误:
SqlException: 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)
我是 Kubernetes 的新手,我对网络不太熟悉,所以欢迎任何帮助。
最好的帮助是给我一些 advices/tools 来调试它,因为我什至不知道连接尝试在何处或何时被阻止...
谢谢!
您认为主机的 IP 地址是内部网络的私有 IP。该 IP 地址可能是您的机器在您正在使用的 "real" 网络上使用的 IP 地址。 kubernetes 虚拟网络位于不同的子网上,因此 - 您在内部使用的 IP 不可访问。
subsets:
- addresses:
- ip: 172.24.144.1 <-- IP of my local computer where SQL Server is running
ports:
- port: 1433
您可以通过 DNS 条目连接 host.docker.internal
阅读更多 here and here for windows
我不确定这是否适用于 minicube - 主机的 linux/windows 实现曾经有不同的 DNS 名称。
如果您想使用 IP(请记住它最终会改变),您可以追踪它并确保它是来自虚拟子网的 "visible"。
PS :我现在正在使用与 docker 一起使用的 kubernetes,似乎更容易使用。
我正在尝试在 Kubernetes 中部署 asp.net 核心 2.2 应用程序。此应用程序是一个简单的网页,需要访问 SQL 服务器数据库才能显示一些信息。该数据库托管在我的本地开发计算机 (localhost) 上,Web 应用程序部署在 minikube 集群中以模拟生产环境,在该环境中我的 Web 应用程序可以部署在云中并访问远程数据库。
我设法通过公开端口 80 来显示我的 Web 应用程序。但是,我无法弄清楚如何使我的 Web 应用程序从集群内部连接到本地计算机上托管的 SQL 服务器数据库.
我假设我的连接字符串是正确的,因为当我在 IIS 本地服务器上部署我的 Web 应用程序时,它可以连接到 SQL 服务器数据库,在 docker 容器中(docker 运行) 或 docker 服务(docker 创建服务),但当它部署在 Kubernetes 集群中时则不会。我知道集群在不同的网络中,所以我尝试创建一个没有选择器的服务,如 this question 中所述,但没有运气......我什至尝试更改连接字符串 IP 地址以匹配其中一个创建了服务,但也失败了。
我的防火墙设置为接受到 1433 端口的入站连接。
我的 SQL 服务器数据库配置为允许远程访问。
这是我使用的连接字符串:
"Server=172.24.144.1\MyServer,1433;Database=TestWebapp;User Id=user_name;Password=********;"
这是我用来部署 Web 应用程序的文件:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: webapp
spec:
replicas: 1
template:
metadata:
labels:
app: webapp
spec:
containers:
- name: webapp
image: <private_repo_url>/webapp:db
imagePullPolicy: Always
ports:
- containerPort: 80
- containerPort: 443
- containerPort: 1433
imagePullSecrets:
- name: gitlab-auth
volumes:
- name: secrets
secret:
secretName: auth-secrets
---
apiVersion: v1
kind: Service
metadata:
name: webapp
labels:
app: webapp
spec:
type: NodePort
selector:
app: webapp
ports:
- name: port-80
port: 80
targetPort: 80
nodePort: 30080
- name: port-443
port: 443
targetPort: 443
nodePort: 30443
---
apiVersion: v1
kind: Service
metadata:
name: sql-server
labels:
app: webapp
spec:
ports:
- name: port-1433
port: 1433
targetPort: 1433
---
apiVersion: v1
kind: Endpoints
metadata:
name: sql-server
labels:
app: webapp
subsets:
- addresses:
- ip: 172.24.144.1 <-- IP of my local computer where SQL Server is running
ports:
- port: 1433
所以我得到了一个名为 'webapp' 的部署,只有一个 pod、两个名为 'webapp' 和 'sql-server' 的服务以及两个名为 'webapp' 和 [=40= 的端点].以下是他们的详细信息:
> kubectl describe svc webapp
Name: webapp
Namespace: default
Labels: app=webapp
Annotations: <none>
Selector: app=webapp
Type: NodePort
IP: 10.108.225.112
Port: port-80 80/TCP
TargetPort: 80/TCP
NodePort: port-80 30080/TCP
Endpoints: 172.17.0.4:80
Port: port-443 443/TCP
TargetPort: 443/TCP
NodePort: port-443 30443/TCP
Endpoints: 172.17.0.4:443
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
> kubectl describe svc sql-server
Name: sql-server
Namespace: default
Labels: app=webapp
Annotations: <none>
Selector: <none>
Type: ClusterIP
IP: 10.107.142.32
Port: port-1433 1433/TCP
TargetPort: 1433/TCP
Endpoints:
Session Affinity: None
Events: <none>
> kubectl describe endpoints webapp
Name: webapp
Namespace: default
Labels: app=webapp
Annotations: <none>
Subsets:
Addresses: 172.17.0.4
NotReadyAddresses: <none>
Ports:
Name Port Protocol
---- ---- --------
port-443 443 TCP
port-80 80 TCP
Events: <none>
> kubectl describe endpoints sql-server
Name: sql-server
Namespace: default
Labels: app=webapp
Annotations: <none>
Subsets:
Addresses: 172.24.144.1
NotReadyAddresses: <none>
Ports:
Name Port Protocol
---- ---- --------
<unset> 1433 TCP
Events: <none>
我希望连接到 SQL 服务器数据库,但是当我的应用程序尝试打开连接时,我收到此错误:
SqlException: 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)
我是 Kubernetes 的新手,我对网络不太熟悉,所以欢迎任何帮助。 最好的帮助是给我一些 advices/tools 来调试它,因为我什至不知道连接尝试在何处或何时被阻止...
谢谢!
您认为主机的 IP 地址是内部网络的私有 IP。该 IP 地址可能是您的机器在您正在使用的 "real" 网络上使用的 IP 地址。 kubernetes 虚拟网络位于不同的子网上,因此 - 您在内部使用的 IP 不可访问。
subsets:
- addresses:
- ip: 172.24.144.1 <-- IP of my local computer where SQL Server is running
ports:
- port: 1433
您可以通过 DNS 条目连接 host.docker.internal 阅读更多 here and here for windows
我不确定这是否适用于 minicube - 主机的 linux/windows 实现曾经有不同的 DNS 名称。
如果您想使用 IP(请记住它最终会改变),您可以追踪它并确保它是来自虚拟子网的 "visible"。
PS :我现在正在使用与 docker 一起使用的 kubernetes,似乎更容易使用。