【发布时间】:2019-06-02 13:32:02
【问题描述】:
我未能在 kubernetes 上部署 postgres(单节点,官方镜像)并允许服务通过 ClusterIP 服务访问 postgres。
配置相当简单——命名空间、部署、服务:
---
apiVersion: v1
kind: Namespace
metadata:
name: database
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
namespace: database
name: postgres
spec:
replicas: 1
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:11.1
imagePullPolicy: "IfNotPresent"
ports:
- containerPort: 5432
---
apiVersion: v1
kind: Service
metadata:
name: pg
namespace: database
labels:
app: postgres
spec:
selector:
app: postgres
ports:
- protocol: TCP
name: postgres
port: 5432
targetPort: 5432
测试是在 pod 中执行“/bin/bash”并运行一个简单的 psql 命令来测试连接。在本地一切正常:
kubectl --kubeconfig $k8sconf -n database exec -it $(kubectl --kubeconfig $k8sconf -n database get pods -o jsonpath='{.items[*].metadata.name}') -- psql -U admin postgresdb -c "\t"
Tuples only is on.
但是一旦我尝试通过服务访问 postgres,命令就会失败:
kubectl --kubeconfig $k8sconf -n database exec -it $(kubectl --kubeconfig $k8sconf -n database get pods -o jsonpath='{.items[*].metadata.name}') -- psql -h pg -U admin postgresdb -c "\t"
psql: could not connect to server: Connection timed out
Is the server running on host "pg" (10.245.102.15) and accepting
TCP/IP connections on port 5432?
这是在 DigitalOcean 单节点集群 (1.12.3) 上测试的。
Postgres 在正确的端口上侦听 *,pg_hba.conf 默认看起来像这样:
...
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
host all all all md5
要重现见this gist
执行方式(请使用新集群并通读):
export k8sconf=/path/to/your/k8s/confic/file
kubectl --kubeconfig $k8sconf apply -f https://gist.githubusercontent.com/sontags/c364751e7f0d8ba1a02a9805efc68db6/raw/01b1808348541d743d6a861402cfba224bee8971/database.yaml
kubectl --kubeconfig $k8sconf -n database exec -it $(kubectl --kubeconfig $k8sconf -n database get pods -o jsonpath='{.items[*].metadata.name}') -- /bin/bash /reproducer/runtest.sh
任何提示为什么该服务不允许连接或执行其他测试?
【问题讨论】:
-
以防万一有人遇到同样的问题:我在 DigitalOcean 上打开了一个问题。它被派给负责 k8s 的团队,考虑到他们的发夹防火墙导致了问题。到目前为止还没有确定。我会及时通知你。
标签: postgresql kubernetes