简体   繁体   English

无法连接到Kubernetes群集中的mongodb服务

[英]Cannot connect to a mongodb service in a Kubernetes cluster

I have a Kubernetes cluster on Google Cloud, I have a database service, which is running in front of a mongodb deployment. 我在Google Cloud上有一个Kubernetes集群,我有一个数据库服务,它在mongodb部署之前运行。 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

Here's my mongo deployment... 这是我的mongo部署......

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

You should see it show that it is both created and routing to your pod's IP. 您应该看到它显示它已创建并路由到您的pod的IP。 If you're wondering what your pod's IP is you can check it out via 如果你想知道你的pod的IP是什么,你可以通过它来检查它

kubectl get po | grep mongo

Which should return something like: mongo-deployment-<guid>-<guid> , then do 哪个应该返回类似: mongo-deployment-<guid>-<guid> ,然后执行

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

You should make sure the pod is started correctly and says Running not something like ImagePullBackoff . 你应该确保pod正确启动,并说Running不像ImagePullBackoff It looks like you're mounting a volume from a gcePersistentDisk . 看起来你正在从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. 如果你看到你的pod刚刚挂在ContainerCreating状态,你很可能没有正确安装磁盘。 Make sure you create the disk before you try and mount it as a volume . 在尝试将其作为卷安装之前,请确保创建磁盘

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: 如果您的服务看起来路由正确,那么您可以检查您的pod的日志,以确保它正确启动mongo:

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

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和日志是正确的,你可以执行进入pod并确保mongo实际上正在启动和工作: kubectl exec -it mongo-deployment-<guid>-<guid> sh

Which should get you into the container (Pod) and then you can try something like this to see if your DB is running. 哪个应该让你进入容器(Pod),然后你可以尝试这样的东西 ,看看你的数据库是否正在运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM