在kubernetes集群中运行的ECONNREFUSED MongoDB

时间:2020-09-15 07:24:30

标签: mongodb docker kubernetes

我是docker和kubernetes的初学者。我在kubernetes集群上的pod内运行了docker容器,我正在尝试连接到mongo db,但是它一直失败

当我检查豆荚和服务时,一切运行良好:

NAME                               READY   STATUS    RESTARTS   AGE

auth-depl-65565b6884-rxcvj         1/1     Running   0          58s

auth-mongo-depl-6c87496969-9sxj4   1/1     Running   0          56s


NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)     AGE

auth-mongo-srv   ClusterIP   10.106.91.10    <none>        27017/TCP   2m

auth-srv         ClusterIP   10.96.108.116   <none>        3000/TCP    2m3s

kubernetes       ClusterIP   10.96.0.1       <none>        443/TCP     2m20s




我的Mongo部署文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth-mongo-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongo-depl
  template:
    metadata:
      labels:
        app: mongo-depl
    spec:
      containers:
        - name: mongo-depl
          image: mongo
---
apiVersion: v1
kind: Service
metadata:
  name: auth-mongo-srv
spec:
  selector:
    type: ClusterIP
    app: mongo-depl
  ports:
    - name: db
      protocol: TCP
      port: 27017
      targetPort: 27017

与数据库的连接:

const start = async (): Promise<void> => {
  try {
    await mongoose.connect("mongodb://auth-mongo-srv:27017/auth", {
      useCreateIndex: true,
      useFindAndModify: false,
      useNewUrlParser: true,
      useUnifiedTopology: true
    });
    console.log("connected to db");
  } catch (error) {
    console.log(error);
  }
};

start();

当我检查日志时,出现此错误:

[auth] MongooseServerSelectionError: connect ECONNREFUSED 10.106.91.10:27017
 reason: TopologyDescription {                                            
   type: 'Single',                                                        
   setName: null,                                                         
   maxSetVersion: null,                                                   
   maxElectionId: null,                                                   
   servers: Map(1) { 'auth-mongo-srv:27017' => [ServerDescription] },     
   stale: false,                                                          
   compatible: true,                                                      
   compatibilityError: null,                                              
   logicalSessionTimeoutMinutes: null,                                    
   heartbeatFrequencyMS: 10000,                                           
   localThresholdMS: 15,                                                  
   commonWireVersion: null                                                
 }                                                                        

任何线索可能导致问题的原因?

3 个答案:

答案 0 :(得分:2)

您似乎未正确配置服务的选择器。您已添加标签type: ClusterIP,因此此服务未选择您的mongo pod,因为它们没有这样的标签。您可能想在type: ClusterIP下添加spec,而不是selector。因此,请从选择器中删除标签type: ClusterIP并将其放置在spec下(ClusterIP类型是服务的默认设置)。

答案 1 :(得分:0)

您必须在pod容器上配置端口,例如:

    spec:
      containers:
        - name: mongo-depl
          image: mongo
          ports:
          - containerPort: 27017

请参见Connecting Applications with Services ,说明

创建[a] Pod,并注意它具有容器端口规范

答案 2 :(得分:0)

感谢我现在看到了我的错误– Kevin M在9月15日11:06插入了

这是因为Kubernetes(k8s)服务只是“一种将运行在一组Pod上的应用程序公开为网络服务的抽象方式” (link)

“豆荚”是Endpoint。您可以使用Endpoints命令检查kubectl get ep。这就是为什么第一步就是检查auth-mongo-srv服务是否存在端点的原因。

您创建的服务规格产生于:

$ kubectl get svc auth-mongo-srv -o wide 
NAME             TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)     AGE   SELECTOR
auth-mongo-srv   ClusterIP   10.0.9.213   <none>        27017/TCP   30s   app=mongo-depl,type=ClusterIP

$ kubectl get ep auth-mongo-srv 
NAME             ENDPOINTS   AGE
auth-mongo-srv   <none>      8m47s

如您所见,没有创建该服务的终结点,因为k8无法将Service选择器与列出同一选择器集的任何Pod进行匹配。

在这种情况下,需要调整auth-mongo-srv

有时您可能想手动创建Endpoint"Headless Services"services without selectors)。