【问题标题】:Kubernetes access Service in other namespace via http requestKubernetes 通过 http 请求访问其他命名空间中的服务
【发布时间】:2020-07-06 14:05:06
【问题描述】:

我在 default 命名空间中有一个 InfluxDB 作为数据库服务。 它的服务称为 influxdb,可以与 chronograf 一起很好地可视化数据。

现在我想将另一个部署从 namspace test 连接到该服务。这是一个python应用程序。普通的python Influxdb Lib使用Requests连接db。

架构概述

Istio 也已安装。

命名空间:默认

  • Influxdb 部署
  • Influxdb 服务
  • Chronograf 部署(可视化 influxdb)
  • Chronograf 服务到 Ingress(用于外部 Web 访问)

命名空间:测试

  • 应连接到 influxdb 进行处理等的 Python 应用程序。
  • Influxdb 服务(指向 influxdb.default.svc.cluster.local)

因此我在命名空间 test 中创建了一个 service,它指向默认命名空间中的 influxdb 服务。

apiVersion: v1
kind: Service
metadata:
  name: influxdb
  labels:
    app: pythonapp
  namespace: test
spec:
  type: ExternalName
  externalName: influxdb.default.svc.cluster.local
  ports:
    - port: 8086
      name: http
    - port: 8088
      name: http-flux

现在部署了指向 influxdb 服务的 python 应用程序。一直出现http连接错误。

2020-07-03 13:02:05 - db.meterdb [meterdb.__init__:57] - ERROR - Oops, something wen't wrong during init of db. message: HTTPConnectionPool(host='influxdb', port=8086): Max retries exceeded with url: /query?q=SHOW+DATABASES (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f6863ed6310>: Failed to establish a new connection: [Errno 111] Connection refused'))
2020-07-03 13:02:05 - db.meterdb [meterdb.check_connection:113] - ERROR - can't reach db server... message: HTTPConnectionPool(host='influxdb', port=8086): Max retries exceeded with url: /ping (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f6863e57650>: Failed to establish a new connection: [Errno 111] Connection refused'))

当我使用 kiali 可视化流量时,我看到 Python 应用程序尝试连接到 influxdb 服务,但对于 http 流量是未知的。

我不知道如何使用创建的 influxdb 服务。

python influxdb 客户端库的连接设置。 Link to python influxdb lib

  • host=influxdb
  • 端口=8086

Traffic from Kiali

如何将流量路由到正确的服务? 在我看来,它将流量路由到未知服务,因为它是 http 而不是 tcp。

【问题讨论】:

    标签: kubernetes


    【解决方案1】:

    你不需要

    kind: Service
    metadata:
      name: influxdb
      labels:
        app: pythonapp
      namespace: test
    

    只需在您的 python 请求中直接访问该服务:

    requests.get('influxdb.default.svc.cluster.local:8086')
    

    而且这可以更可配置。

    # Kubernetes deployment
          containers:
          - name: pythonapp
            env:
            - name: DB_URL
              value: influxdb.default.svc.cluster.local:8086
    
    # python
    DB = os.environ['DB_URL']
    requests.get(DB)
    

    【讨论】:

    • 感谢您的回答。这解决了我的问题。离开Service,直接指向默认命名空间中的influxdb。
    猜你喜欢
    • 2021-01-21
    • 1970-01-01
    • 2022-01-25
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多