首页 > 解决方案 > Kubernetes 通过 http 请求访问其他命名空间中的服务

问题描述

我在默认命名空间中有一个 InfluxDB 作为数据库服务。它的服务称为 influxdb 并与 chronograf 一起工作以可视化数据。

现在我想连接从 namspace测试到该服务的其他部署。这是一个python应用程序。普通的 python Influxdb Lib 使用Requests连接到数据库。

架构概述

Istio 也已安装。

命名空间:默认

命名空间:测试

因此,我在命名空间测试中创建了一个服务,它指向默认命名空间中的 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 客户端库的连接设置。链接到 python influxdb 库

来自 Kiali 的交通

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

标签: kubernetes

解决方案


你不需要

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)

推荐阅读