将前端 API 应用程序连接到后端数据库

Connecting Frontend API application to Backend database

谁能帮我完成这个场景,因为我已经成功 运行 但没有达到预期的最终结果。我有一个 PostgreSQL 数据库和 Redis(后端)Pods 以及一个前端 API 应用程序,我将使用它来发送 API 请求。不幸的是,在设置完所有内容并确保它们都在 Kubernetes 仪表板上 运行 之后,发送 API 请求时没有任何反应,就好像我的服务之间没有连接一样。在拼命张贴在这里之前,我做了一些研究以找到解决这个问题的方法,我尝试了这个使用 ingress-service to connect my services but did not work. I also came across this tutorial 的教程,它将使用某种上游配置文件将我的后端服务连接到我的前端服务但是它没有成功。

如果有人感兴趣,这是我的 YAML 配置文件:

apiVersion: apps/v1
kind: Deployment
metadata:
   name: dictionary-project
   labels:
     app: net
spec:
  replicas: 1
  selector:
    matchLabels:
      app: dictionary
      tier: frontend
  template:
    metadata:
      labels:
        app: dictionary
        tier: frontend
    spec:
       hostNetwork: true
       containers:
       - name: dictionaryapi
         image: amin/dictionary_server_api:latest
         ports:
         - containerPort: 400

apiVersion: v1
kind: Service
metadata:
   name: dictionary-service
   labels:
     app: net
spec:
  selector:
      app: dictionary
      tier: frontend
  type: NodePort 
  ports:
  - nodePort: 31003
    port: 67
    targetPort: 400
    protocol: TCP

apiVersion: v1
kind: Service
metadata:
    name: postgrebackendservice
    labels:
       run: backend
spec:
 selector:
      app: postgres
      tier: backend
 type: ClusterIP
 ports:
    - port: 5432
      targetPort: 5432
      name: postgresdb

apiVersion: v1
kind: Service
metadata:
    name: reddisbackendservice
    labels:
        run: backend
spec:
  selector:
      app: reddis
      tier: backend
  type: ClusterIP
  ports:
  - port: 6379
    targetPort: 6379
    name: client
  - port: 16379
    targetPort: 16379
    name: gossip

所有服务的输出:

Name:                     dictionary-service
Namespace:                default
Labels:                   app=net
Annotations:              <none>
Selector:                 app=dictionary,tier=frontend
Type:                     NodePort
IP:                       10.126.146.18
Port:                     <unset>  67/TCP
TargetPort:               400/TCP
NodePort:                 <unset>  31003/TCP
Endpoints:                192.168.x.x:400
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

Name:              postgrebackendservice
Namespace:         default
Labels:            run=backend
Annotations:       Selector:  app=postgres,tier=backend
Type:              ClusterIP
IP:                10.182.374.13
Port:              postgresdb  5432/TCP
TargetPort:        5432/TCP
Endpoints:         192.168.x.x:5432
Session Affinity:  None
Events:            <none>

Name:              reddisbackendservice
Namespace:         default
Labels:            run=backend
Annotations:       Selector:  app=reddis,tier=backend
Type:              ClusterIP
IP:                10.182.0.60
Port:              client  6379/TCP
TargetPort:        6379/TCP
Endpoints:         192.168.x.x:6379
Port:              gossip  16379/TCP
TargetPort:        16379/TCP
Endpoints:         192.168.x.x:16379
Session Affinity:  None
Events:            <none>

我正在通过发送 http://{workernodeIP}:31003/swagger 在 Web 浏览器上测试我的前端 API 应用程序,但由于未连接到服务器,页面未加载。

集群信息:

Kubernetes version: v1.18.6
Environment being used: bare-metal, 1 VM Master Node and 1 VM Worker Node
Installation method: kubeadm
Host OS: Ubuntu 18.04
CNI and version: calico
CRI and version: Docker 19.03.6

来自文档 here

containerPort: List of ports to expose from the container. Exposing a port here gives the system additional information about the network connections a container uses, but is primarily informational. Not specifying a port here DOES NOT prevent that port from being exposed. Any port which is listening on the default "0.0.0.0" address inside a container will be accessible from the network. Cannot be updated.

如您所见,containerPort 是信息,不会让应用程序侦听该端口。要真正让应用程序侦听端口 400,您需要在代码中更改端口。