kubernetes - Kubernetes 通过 http 请求访问其他命名空间中的服务
问题描述
我在默认命名空间中有一个 InfluxDB 作为数据库服务。它的服务称为 influxdb 并与 chronograf 一起工作以可视化数据。
现在我想连接从 namspace测试到该服务的其他部署。这是一个python应用程序。普通的 python Influxdb Lib 使用Requests连接到数据库。
架构概述
Istio 也已安装。
命名空间:默认
- Influxdb 部署
- 涌入数据库服务
- Chronograf 部署(可视化 influxdb)
- Chronograf Service to Ingress(用于外部网络访问)
命名空间:测试
- Python 应用程序应该连接到 influxdb 进行处理等。
- Influxdb 服务(指向 influxdb.default.svc.cluster.local)
因此,我在命名空间测试中创建了一个服务,它指向默认命名空间中的 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 库
- 主机=涌入数据库
- 端口=8086
如何将流量路由到正确的服务?在我看来,它将流量路由到未知服务,因为它是 http 而不是 tcp。
解决方案
你不需要
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)
推荐阅读
- html - HTML File image input is blank screen on iPhone using Chrome
- javascript - 如何从 JSON 字符串中提取值列表并遍历它们?
- python - I wanted to open a file with the module of python "open", and trying to make it in fragments, then, it appears this phrase "list index out of range"
- javascript - Entering javascript into test boxes
- javascript - SyntaxError: missing ) after argument list in google script while i was creating event from GoogleAppsScript to Google Calendar
- javascript - Javascript. Why won't my string replace update show in my browser
- typescript - RXJS bufferedAmount / accumulate value / reduce with reset
- c - Unable to concatenate tokens in CPP
- c# - BackgroundWorker not passing FileNotFoundException to RunWorkerCompleted
- macos - VS Code Will Not Save Changes - Mac