Kubernetes通过http请求访问其他命名空间中的Service

时间:2020-07-03 13:37:21

标签: kubernetes

我在默认名称空间中有一个InfluxDB作为数据库服务。 它的服务称为influxdb,可与chronograf完美配合以可视化数据。

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

架构概述

还安装了Istio。

域名空间:默认

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

命名空间:测试

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

因此,我在命名空间 test 中创建了一个服务,该服务指向默认命名空间中的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
  • port = 8086

Traffic from Kiali

我该如何将交通运输引导至正确的服务? 在我看来,它会将流量路由发送到未知服务,因为它是http而不是tcp。

1 个答案:

答案 0 :(得分:2)

您不需要

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)