如何从Kubernetes集群外部连接到CockroachDB? [英] How can I connect to CockroachDB from outside the Kubernetes cluster?
问题描述
我已经设置并部署了一个Kubernetes状态集,其中包含三个CockroachDB pod,
I've set up and deployed a Kubernetes stateful set containing three CockroachDB pods, as per docs. My ultimate objective is to query the database without requiring use of kubectl. My intermediate objective is to query the database without actually shelling into the database pod.
我从Pod转发了一个端口到我的本地计算机,并尝试连接:
I forwarded a port from a pod to my local machine, and attempted to connect:
$ kubectl port-forward cockroachdb-0 26257
Forwarding from 127.0.0.1:26257 -> 26257
Forwarding from [::1]:26257 -> 26257
# later, after attempting to connect:
Handling connection for 26257
E0607 16:32:20.047098 80112 portforward.go:329] an error occurred forwarding 26257 -> 26257: error forwarding port 26257 to pod cockroachdb-0_mc-red, uid : exit status 1: 2017/06/07 04:32:19 socat[40115] E connect(5, AF=2 127.0.0.1:26257, 16): Connection refused
$ cockroach node ls --insecure --host localhost --port 26257
Error: unable to connect or connection lost.
Please check the address and credentials such as certificates (if attempting to
communicate with a secure cluster).
rpc error: code = Internal desc = transport is closing
Failed running "node"
有人设法做到这一点吗?
Anyone manage to accomplish this?
推荐答案
在Kubernetes集群内部,您可以通过连接cockroachdb-public
DNS名称与数据库进行通信.在文档,它与示例命令相对应:
From inside the Kubernetes cluster, you can talk to the database by connecting the cockroachdb-public
DNS name. In the docs, that corresponds to the example command:
kubectl run cockroachdb -it --image=cockroachdb/cockroach --rm --restart=Never -- sql --insecure --host=cockroachdb-public
尽管该命令使用的是CockroachDB映像,但是在Kubernetes集群上运行时,您使用的任何Postgres客户端驱动程序都应该能够连接到cockroachdb-public
.
While that command is using the CockroachDB image, any Postgres client driver you use should be able to connect to cockroachdb-public
when running with the Kubernetes cluster.
从Kubernetes集群外部连接到数据库将需要公开cockroachdb-public
服务.详细信息将在某种程度上取决于您的Kubernetes集群的部署方式,因此,我建议您查看有关它们的文档:
https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/#expose-the-service
Connecting to the database from outside of the Kubernetes cluster will require exposing the cockroachdb-public
service. The details will depend somewhat on how your Kubernetes cluster was deployed, so I'd recommend checking out their docs on that:
https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/#exposing-the-service
如果您好奇的话,转发端口26257对您不起作用的原因是,从Pod进行端口转发仅在Pod中的进程正在侦听localhost时才起作用,而在Statefulset配置中的CockroachDB进程才起作用设置为侦听Pod的主机名(通过--host
标志配置).
And in case you're curious, the reason forwarding port 26257 isn't working for you is because port forwarding from a pod only works if the process in the pod is listening on localhost, but the CockroachDB process in the statefulset configuration is set up to listen on the pod's hostname (as configured via the --host
flag).
这篇关于如何从Kubernetes集群外部连接到CockroachDB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!