【问题标题】:Application inside container of a Kubernetes Deployment fails with error localhost:port can not be accessed?Kubernetes 部署容器内的应用程序失败并出现错误 localhost:port 无法访问?
【发布时间】:2019-12-13 20:05:08
【问题描述】:

我有一个应用程序 Docker 映像,它在随机端口上启动一个 mongodb 实例。当我使用应用程序映像创建 Kubernetes Pod 时;应用程序已成功初始化,并且 mongodb 实例在随机端口上以 localhost:port 的身份启动,没有任何错误。

但是,当我创建 Kubernetes 部署时;容器内相同的应用程序初始化失败,并出现错误“无法启动 mongodb,因为无法访问 localhost:port”。

如果有人能解释一下,为什么 K8-Deployment 的应用程序初始化失败,而 K-8Pod 却没有?还有,我该如何解决这个问题?

apiVersion: apps/v1
kind: Pod
metadata:
  name: my-app
spec:
  containers:
  - name: my-app
    image: my-app:v1
    ports:
    - containerPort: 8888 # Apps exposed port

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  labels:
    app: my-dep
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-app:v1
        ports:
        - containerPort: 8888 # Apps exposed port

谢谢

【问题讨论】:

  • 您能否提供实际的源代码(如 Dockerfile 和 YAML 工件)和实际的错误消息? “随机端口”和“本地主机”在 Kubernetes 环境中听起来都有些不寻常。
  • @David,应用程序代码具有随机识别主机上的可用端口并在该端口上启动 mongodb 服务器的逻辑,稍后应用程序将数据加载到该 mongodb 服务器。在 K8-Deployment 的情况下,应用程序会在 localhost:[random-port] 处启动 mongodb 服务器,但失败并出现错误 localhost:random Port can't be access.但是,在 K8-Pod 的情况下也可以正常工作。
  • 我希望通常在 StatefulSet 中运行类似 MongoDB 的东西,并通过服务访问它。 localhost 通常表示“这个容器”或“这个 pod”,而不是主机系统上的其他东西。
  • 为 Pod 和 Deployment 添加了 yaml 工件。

标签: mongodb docker kubernetes kubernetes-pod kubernetes-deployment


【解决方案1】:

您应该尝试将 MongoDB 配置为监听 0.0.0.0:port,而不是监听 localhost:port。这对我在其他应用遇到类似问题时有所帮助。

配置 MongoDB 监听所有接口

你的mongod.conf

# /etc/mongod.conf

# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip = 127.0.0.1

改成这个

# /etc/mongod.conf

# Listen to local interface only. Comment out to listen on all interfaces.
# bind_ip = 127.0.0.1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    • 2020-08-18
    相关资源
    最近更新 更多