0

我试图在一个简单的 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 失败了。

我该如何调试问题?

4

1 回答 1

0

进入吊舱并尝试以下操作:

nslookup mongodb-service.default.svc.cluster.local

它将为您提供服务的 clusterIP,然后尝试wget使用此 IP 而不是服务的 DNS。

wget http://10.100.142.166 -O-

使用 IP over DNS 的原因:https ://stackoverflow.com/questions/53921281/kubernetes-1-13-coredns-cluster-curl-service

于 2021-06-23T12:08:23.187 回答