【问题标题】:Kubernetes service not working as expectedKubernetes 服务未按预期工作
【发布时间】: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


【解决方案1】:

如果不访问您的集群,很难判断。这在我的 AWS 集群上运行良好。需要注意的一些事项:

  • kube-proxy 是否在所有节点上运行?
  • 您的网络覆盖/CNI 是否在所有节点上运行?
  • 这种情况是否仅发生在 pg pod 中?其他 pod 呢?
  • DNS 似乎没问题,因为 pg 正在解析为 10.245.102.15
  • 您的节点是否允许来自 Linux 端的IP forwarding
  • 您的 Digital Ocean 防火墙规则是否允许来自端口 5432 上任何来源的流量?请注意,PodCidr 和 K8s 服务 IP 范围与 hostCidr(您的 Droplet)不同。

【讨论】:

  • 我还使用您的 gist 文件在 GCP 上进行了测试,工作正常
  • 目前出于调试原因,单节点集群 - 所有节点上的 kube-proxy:是的 - 所有节点上的 cni:是的,我假设是这样。 - 所有 pod 上的问题:不。我可以 curl 另一个正在运行网络服务器的 pod,甚至跨越命名空间 - DNS:是的,工作得很好 - IP 转发:该节点由 DigitalOcean 管理。由于其他一切工作正常(例如,具有集群 IP 和入口的 Web 服务器)我认为这应该没问题。 - 防火墙:由于这一切都发生在集群中唯一的主机上,我非常有信心这不会导致问题。
  • 如有必要,我将在 DO 上的另一个新集群上复制,以提高票证
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-21
  • 1970-01-01
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
  • 2022-08-17
相关资源
最近更新 更多