问题描述
有人可以帮助我完成此方案,因为我已成功运行了所有程序,但未达到预期的最终结果。我有一个Postgresql数据库和Redis(后端)Pod,以及一个我将用于发送API请求的前端API应用程序。不幸的是,在完成所有设置并确保它们都在Kubernetes仪表板上运行之后,发送API请求时,没有任何事情发生,好像我的服务之间没有连接。在极力地张贴在这里之前,我进行了一些研究以找到解决此问题的方法,然后尝试了使用ingress-service连接服务的本教程,但没有成功。我还遇到了这个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 : 要从容器公开的端口列表。 在此处公开端口可以 有关网络连接的系统附加信息 容器使用,但主要是提供信息。没有指定端口 此处并不能防止该端口暴露在外。任何端口是 侦听容器内默认的“ 0.0.0.0”地址将是 可从网络访问。无法更新。
您会看到containerPort
是信息,不会使应用程序在该端口上进行侦听。要真正使应用程序在端口400
上进行侦听,您需要在代码中更改端口。