[英]How to make kubernetes pod have access to PostgreSQL Pod
我正在嘗試本地 Kubernetes(Docker-on-mac),並嘗試提交 spark 作業。 火花作業與 PostgreSQL 數據庫連接並進行一些計算。
PostgreSQL 在我的 Kube 上運行,因為我已經發布了它,我可以通過 localhost:5432 從主機訪問它。 但是,當 spark 應用程序嘗試連接到 PostgreSQL 時,它會拋出
Exception in thread "main" org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
kubectl 集群信息
Kubernetes master is running at https://kubernetes.docker.internal:6443
KubeDNS is running at https://kubernetes.docker.internal:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
kubectl 獲取服務 postgresql-發布
kubectl 描述服務 spark-store-1588217023181-driver-svc
Name: spark-store-1588217023181-driver-svc
Namespace: default
Labels: <none>
Annotations: <none>
Selector: spark-app-selector=spark-533ecb8556b6439eb938d487cc77c330,spark-role=driver
Type: ClusterIP
IP: None
Port: driver-rpc-port 7078/TCP
TargetPort: 7078/TCP
Endpoints: <none>
Port: blockmanager 7079/TCP
TargetPort: 7079/TCP
Endpoints: <none>
Session Affinity: None
我怎樣才能讓我的火花工作,可以訪問 PostgreSQL 服務?
localhost
在EXTERNAL_IP
中,但 Kubernetes 集群 DNS 系統(CoreDNS)不知道如何將其解析為 IP 地址。 EXTERNAL_IP
is supposed to be resolved by an external DNS server and it's generally meant to be used to connect to Postgres from outside the Kubernetes cluster(ie from another system or from Kubernetes nodes as well) and not from the inside the cluster(ie from another莢)
應該可以通過10.106.15.112:5432
或postgresql-published:5432
從 spark pod 訪問 Postgres,因為 kubernetes 集群 DNS 系統知道如何解決它。
測試 Postgres 連接性
kubectl run postgresql-postgresql-client --rm --tty -i --restart='Never' --namespace default --image bitnami/postgresql --env="PGPASSWORD=<HERE_YOUR_PASSWORD>" --command -- psql --host <HERE_HOSTNAME=SVC_OR_IP> -U <HERE_USERNAME>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORTS
postgresql-published LoadBalancer 10.106.15.112 localhost 5432:31277
意味着該服務應可在集群內訪問10.106.15.112:5432
, postgresql-published:5432
和外部localhost:31277
。
請注意,對於 Pod, localhost
就是 Pod 本身。 在這種情況下, localhost
看起來很模糊。 然而,這就是expose
的工作方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.