如何将 Kind 中的 pod 连接到本地数据库
How to connect a pod in Kind with a local database
我正在尝试将 运行 in Kind 中的 pod 与在 Docker 容器中运行的本地 Postgres 数据库连接起来。我尝试添加以下服务,但使用 DNS 名称 postgres.dev.svc
.
时 pod 仍然无法连接
kind: Service
apiVersion: v1
metadata:
name: postgres
namespace: dev
spec:
type: ExternalName
externalName: 10.0.2.2
还有其他方法可以连接这两个组件吗?
我可以给出一些建议,我将如何尝试调试此类问题。
确保您可以从您的工作站或其他主机登录数据库,这样我们就可以排除此问题与 docker/database-host 相关。
检查您是否可以从您的集群访问您的数据库,或者流量是否被防火墙等阻止。您可以为此用例生成一个 tmp 容器 kubectl run -i --tty --rm debug --image=busybox --restart=Never -- sh
,然后尝试 ping、curl、wget ... ip(busybox 仅包含 wget,但请随意使用其他图像)
在连接字符串中对 IP 进行硬编码,而不是在您的命名空间中为 dns 使用外部服务。
如果这不能解决问题,您可能需要 post 更详细的描述。
首先,ExternalName
服务类型的用法不正确。尽管将 IP 地址放在 externalName
字段中是完全可行的,即资源将被创建并且您不会从 kubernetes API 服务器收到任何投诉。 ❗但是这个值被视为域名,由数字组成,而不是IP地址。您可以在 the official kubernetes docs:
中阅读相关信息
Note: ExternalName accepts an IPv4 address string, but as a DNS names comprised of digits, not as an IP address. ExternalNames that
resemble IPv4 addresses are not resolved by CoreDNS or ingress-nginx
because ExternalName is intended to specify a canonical DNS name. To
hardcode an IP address, consider using headless
Services.
所以你真正需要的是 Service without a selector:
Services most commonly abstract access to Kubernetes Pods, but they
can also abstract other kinds of backends. For example:
- You want to have an external database cluster in production, but in your test environment you use your own databases.
- You want to point your Service to a Service in a different Namespace
or on another cluster.
- You are migrating a workload to Kubernetes. While evaluating the approach, you run only a portion of your backends in Kubernetes.
In any of these scenarios you can define a Service without a Pod
selector. For example:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9376
Because this Service has no selector, the corresponding Endpoints
object is not created automatically. You can manually map the Service
to the network address and port where it's running, by adding an
Endpoints object manually:
apiVersion: v1
kind: Endpoints
metadata:
name: my-service
subsets:
- addresses:
- ip: 192.0.2.42
ports:
- port: 9376
在您的特定情况下,您的 Service
定义可能如下所示:
apiVersion: v1
kind: Service
metadata:
name: postgres
spec:
ports:
- protocol: TCP
port: 5432
targetPort: 5432
和相应的 Endpoints
对象可能如下所示:
apiVersion: v1
kind: Endpoints
metadata:
name: postgres
subsets:
- addresses:
- ip: 10.0.2.2
ports:
- port: 5432
当然,IP 地址 10.0.2.2
必须可以从您的 kubernetes 集群中访问到。
我正在尝试将 运行 in Kind 中的 pod 与在 Docker 容器中运行的本地 Postgres 数据库连接起来。我尝试添加以下服务,但使用 DNS 名称 postgres.dev.svc
.
kind: Service
apiVersion: v1
metadata:
name: postgres
namespace: dev
spec:
type: ExternalName
externalName: 10.0.2.2
还有其他方法可以连接这两个组件吗?
我可以给出一些建议,我将如何尝试调试此类问题。
确保您可以从您的工作站或其他主机登录数据库,这样我们就可以排除此问题与 docker/database-host 相关。
检查您是否可以从您的集群访问您的数据库,或者流量是否被防火墙等阻止。您可以为此用例生成一个 tmp 容器
kubectl run -i --tty --rm debug --image=busybox --restart=Never -- sh
,然后尝试 ping、curl、wget ... ip(busybox 仅包含 wget,但请随意使用其他图像)在连接字符串中对 IP 进行硬编码,而不是在您的命名空间中为 dns 使用外部服务。
如果这不能解决问题,您可能需要 post 更详细的描述。
首先,ExternalName
服务类型的用法不正确。尽管将 IP 地址放在 externalName
字段中是完全可行的,即资源将被创建并且您不会从 kubernetes API 服务器收到任何投诉。 ❗但是这个值被视为域名,由数字组成,而不是IP地址。您可以在 the official kubernetes docs:
Note: ExternalName accepts an IPv4 address string, but as a DNS names comprised of digits, not as an IP address. ExternalNames that resemble IPv4 addresses are not resolved by CoreDNS or ingress-nginx because ExternalName is intended to specify a canonical DNS name. To hardcode an IP address, consider using headless Services.
所以你真正需要的是 Service without a selector:
Services most commonly abstract access to Kubernetes Pods, but they can also abstract other kinds of backends. For example:
- You want to have an external database cluster in production, but in your test environment you use your own databases.
- You want to point your Service to a Service in a different Namespace or on another cluster.
- You are migrating a workload to Kubernetes. While evaluating the approach, you run only a portion of your backends in Kubernetes.
In any of these scenarios you can define a Service without a Pod selector. For example:
apiVersion: v1 kind: Service metadata: name: my-service spec: ports: - protocol: TCP port: 80 targetPort: 9376
Because this Service has no selector, the corresponding Endpoints object is not created automatically. You can manually map the Service to the network address and port where it's running, by adding an Endpoints object manually:
apiVersion: v1 kind: Endpoints metadata: name: my-service subsets: - addresses: - ip: 192.0.2.42 ports: - port: 9376
在您的特定情况下,您的 Service
定义可能如下所示:
apiVersion: v1
kind: Service
metadata:
name: postgres
spec:
ports:
- protocol: TCP
port: 5432
targetPort: 5432
和相应的 Endpoints
对象可能如下所示:
apiVersion: v1
kind: Endpoints
metadata:
name: postgres
subsets:
- addresses:
- ip: 10.0.2.2
ports:
- port: 5432
当然,IP 地址 10.0.2.2
必须可以从您的 kubernetes 集群中访问到。