无法连接到Kubernetes集群中的mongodb服务 [英] Cannot connect to a mongodb service in a Kubernetes cluster

查看:663
本文介绍了无法连接到Kubernetes集群中的mongodb服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google Cloud上有一个Kubernetes集群,我有一个数据库服务,该服务在mongodb部署之前运行.我也有一系列微服务,它们正在尝试连接到该数据存储.

I have a Kubernetes cluster on Google Cloud, I have a database service, which is running in front of a mongodb deployment. I also have a series of microservices, which are attempting to connect to that datastore.

但是,他们似乎找不到主机.

However, they can't seem to find the host.

apiVersion: v1
kind: Service
metadata:
 labels:
   name: mongo
 name: mongo
spec:
 ports:
 - port: 27017
   targetPort: 27017
 selector:
   name: mongo

这是我的mongo部署...

Here's my mongo deployment...

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mongo-deployment
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: mongo
    spec:
      containers:
      - image: mongo:latest
        name: mongo
        ports:
        - name: mongo
          containerPort: 27017
          hostPort: 27017
        volumeMounts:
          - name: mongo-persistent-storage
            mountPath: /data/db
      volumes:
        - name: mongo-persistent-storage
          gcePersistentDisk:
            pdName: mongo-disk
            fsType: ext4

还有我的一项服务的示例...

And an example of one of my services...

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: bandzest-artists
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: bandzest-artists
    spec:
      containers:
      - name: artists-container
        image: gcr.io/<omitted>/artists:41040e8
        ports: 
        - containerPort: 7000
        imagePullPolicy: Always
        env:
        - name: DB_HOST
          value: mongo
        - name: AWS_BUCKET_NAME
          value: <omitted>
        - name: AWS_ACCESS_KEY_ID
          value: <omitted>
        - name: AWS_SECRET_KEY
          value: <omitted> 

推荐答案

首先,检查服务是否已创建

First, check that the service is created

kubectl describe svc mongo

您应该看到它表明它已经创建并且正在路由到您的Pod的IP.如果您想知道您的Pod的IP是什么,可以通过

You should see it show that it is both created and routing to your pod's IP. If you're wondering what your pod's IP is you can check it out via

kubectl get po | grep mongo

应该返回以下内容:mongo-deployment-<guid>-<guid>,然后执行

Which should return something like: mongo-deployment-<guid>-<guid>, then do

kubectl describe po mongo-deployment-<guid>-<guid>

您应该确保Pod已正确启动,并说出Running而不是ImagePullBackoff之类的字样.似乎您正在从gcePersistentDisk装入卷.如果您看到Pod只是以ContainerCreating状态挂出,则很可能是您未正确安装磁盘.在尝试执行创建磁盘之前,请确保href ="http://kubernetes.io/docs/user-guide/volumes/#example-pod-2" rel ="noreferrer">将其安装为卷.

You should make sure the pod is started correctly and says Running not something like ImagePullBackoff. It looks like you're mounting a volume from a gcePersistentDisk. If you're seeing your pod just hanging out in the ContainerCreating state it's very likely you're not mounting the disk correctly. Make sure you create the disk before you try and mount it as a volume.

如果您的服务似乎路由正确,则可以检查pod的日志以确保其正确启动了mongo:

If it looks like your service is routing correctly, then you can check the logs of your pod to make sure it started mongo correctly:

kubectl logs mongo-deployment-<guid>-<guid>

如果pod和日志看起来正确,则可以执行到pod中并确保mongo实际上正在启动并工作: kubectl exec -it mongo-deployment-<guid>-<guid> sh

If it looks like the pod and logs are correct, you can exec into the pod and make sure mongo is actually starting and working: kubectl exec -it mongo-deployment-<guid>-<guid> sh

应该将您带入容器(Pod),然后您可以尝试类似的操作,看看您的数据库是否正在运行.

Which should get you into the container (Pod) and then you can try something like this to see if your DB is running.

这篇关于无法连接到Kubernetes集群中的mongodb服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆