我试图在一个简单的 kubernetes 集群中公开一个服务,该集群由一个工人和一个主人组成。特别是,我使用下面的描述符:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-deployment
labels:
app: mongodb
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: mongo
ports:
- containerPort: 27017
---
apiVersion: v1
kind: Service
metadata:
name: mongodb-service
spec:
selector:
app: mongodb
ports:
- protocol: TCP
port: 27017
targetPort: 27017
然后我尝试从另一个 pod 使用此服务:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongo-express
labels:
app: mongo-express
spec:
replicas: 1
selector:
matchLabels:
app: mongo-express
template:
metadata:
labels:
app: mongo-express
spec:
containers:
- name: mongo-express
image: mongo-express
ports:
- containerPort: 8081
env:
- name: ME_CONFIG_MONGODB_SERVER
value: mongodb-service
但是,我在另一个 mongo-express pod 中得到的是 mongodb-service 无法解析。事实上,如果我启动一个 pod 并尝试一个简单的 wget,这就是输出:
$ wget http://mongodb-service/ -O-
--2021-06-23 13:31:08-- http://mongodb-service/
Resolving mongodb-service (mongodb-service)... failed: Name or service not known.
wget: unable to resolve host address 'mongodb-service'
相反 nslookup mongodb-service 工作正常:
$ nslookup mongodb-service
Server: 10.96.0.10
Address: 10.96.0.10#53
但是,如果我尝试使用 netcat,我会得到:
$nc mongodb-service 27017
nc: getaddrinfo for host "mongodb-service" port 27017: Name or service not known
所以似乎是 getaddrinfo 失败了。
我该如何调试问题?