我正在使用Spring Boot创建REST API,并且在加载时访问MongoDB。
我正在为REST API进行一项部署和服务,为mongodb进行一项部署和服务。
但是我的REST API容器坏了,而且没有加载时间,因为它正在寻找mongodb服务,但无法ping通该主机。
我已经将mongodb作为服务公开,还将REST API作为服务公开了。
REST API公开为NodePort,mongodb公开为ClusreIP。
我尝试的一切,但没有解决方案。
=================================== MongoDB部署=============== ===========
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: tech-hunt-mongodb
spec:
replicas: 1
template:
metadata:
name: tech-hunt-mongodb
labels:
app: tech-hunt
module: mongodb
spec:
containers:
- image: <image>
name: tech-hunt-mongodb
ports:
- containerPort: 27017
=================================== MongoDB服务=============== ===========
apiVersion: v1
kind: Service
metadata:
name: tech-hunt-mongodb
spec:
#type: ClusterIP
selector:
app: tech-hunt
module: mongodb
ports:
- port: 27017
targetPort: 27017
protocol: TCP
clusterIP: None
#nodePort: 30000
#protocol: TCP
=========================== REST API部署===================== ============
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: tech-hunt-api
spec:
template:
metadata:
name: tech-hunt-api
labels:
app: tech-hunt
module: rest-api
spec:
containers:
- image: <image>
name: tech-hunt-api
ports:
- containerPort: 4000
=============================== REST API服务================= =============
apiVersion: v1
kind: Service
metadata:
name: tech-hunt-api-client
spec:
type: NodePort
selector:
app: tech-hunt
module: rest-api-client
ports:
- port: 5000
targetPort: 5000
nodePort: 30010
最佳答案
clusterIP: None
几乎肯定不是您想要发生的事情,因为这将全部填充
Endpoints
的负担全部放在了您-或外部 Controller 上(StatefulSet
Controller 就是这样的示例)。您必须先删除
tech-hunt-mongodb
Service
,然后再重新创建,才能将其clusterIP
从None
更改为自动填充的值,但是您必须首先确定要执行此操作。But my REST API pod is breaking and not coming up as on load time it looks for mongodb service but it is not able to ping that host.
作为FYI,您将永远无法“ping”一个
Service
IP,因为这些地址是“伪造的”。只有Service
IP元组及其流量端口(在您的两个示例中为27017
或5000
)将响应任何数据包。您应该使用curl
或nc
来测试连接性,而不是ping
。
关于kubernetes - Pod到Pod的通信在kubernetes中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54600917/