从 Kubernetes 访问外部数据库

Fra*_*lez 10 postgresql digital-ocean kubernetes

我有一个 Kubernetes (v1.18.6),其中有 1 个服务(负载均衡器)、2 个 pod:

apiVersion: v1
kind: Service
metadata:
  name: app-service
spec:
  selector:
    app: app
  ports:
  - protocol: "TCP"
    port: 6000
    targetPort: 5000
  type: LoadBalancer
Run Code Online (Sandbox Code Playgroud)

访问互联网的网络策略(这对我来说是必要的):

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: internet-access
spec:
  podSelector:
    matchLabels:
      networking/allow-internet-access: "true"
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - {}
Run Code Online (Sandbox Code Playgroud)

部署配置文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-deployment
spec:
  progressDeadlineSeconds: 120
  selector:
    matchLabels:
      app: app
  replicas: 2
  template:
    metadata:
      labels:
        app: app
    spec:
      imagePullSecrets:
        - name: myregistrykey
      containers:
      - name: app
        image: app
        imagePullPolicy: Always
        ports:
        - containerPort: 5000
Run Code Online (Sandbox Code Playgroud)

它工作正常。但现在,我想将此图像连接到外部数据库(在另一个网络中只能通过互联网访问)。对于这个建议,我使用这项服务:

apiVersion: v1
kind: Service
metadata:
  name: postgresql
spec:
  clusterIP: None
  ports:
  - port: 25060


---
apiVersion: v1
kind: Endpoints
metadata:
  name: postgresql
subsets:
  - addresses:
        - ip: 206............
    ports:
      - port: 25060
        name: postgresql
Run Code Online (Sandbox Code Playgroud)

这是所有的服务:

NAME                TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)          AGE
app-service         LoadBalancer   10.245.134.137   206...........   6000:31726/TCP   2d4h
kubernetes          ClusterIP      10.245.0.1       <none>           443/TCP          3d7h
postgresql          ClusterIP      None             <none>           25060/TCP        19h
Run Code Online (Sandbox Code Playgroud)

但是当我尝试连接时,我收到数据库超时错误,例如无法连接到数据库。

图片中我有互联网连接。

我找到了解决方案,问题出在数据库的入站规则上。我必须添加 Kubernetes 的 IP。

谢谢。

P E*_*ram 0

应更正服务定义。默认服务类型是 clusterIP,不适用于外部数据库。您需要更新服务类型,如下所示

类型:外部名称

还要确保服务名称和端点名称应该匹配。你的 yaml 中是不同的。请检查