dask-kubernetes icon indicating copy to clipboard operation
dask-kubernetes copied to clipboard

Can not connect to k8s websocket deployed in Rancher

Open dbalabka opened this issue 2 years ago • 5 comments

Describe the issue: Deploy k8s cluster in a rancher. Try to connect with default settings. You will see:

2024-01-02 21:19:05,453 - asyncio - ERROR - Task exception was never retrieved
future: <Task finished name='Task-26' coro=<PortForward._sync_sockets() done, defined at /home/torinaki/src/demand-forecasting/.venv/lib/python3.10/site-packages/kr8s/_portforward.py:168> exception=AssertionError('Only absolute URLs without path part are supported')>
Traceback (most recent call last):
  File ".../.venv/lib/python3.10/site-packages/kr8s/_portforward.py", line 171, in _sync_sockets
    async with self._connect_websocket() as ws:
  File "/usr/lib/python3.10/contextlib.py", line 199, in __aenter__
    return await anext(self.gen)
  File ".../.venv/lib/python3.10/site-packages/kr8s/_portforward.py", line 150, in _connect_websocket
    async with self.pod.api.open_websocket(
  File "/usr/lib/python3.10/contextlib.py", line 199, in __aenter__
    return await anext(self.gen)
  File ".../.venv/lib/python3.10/site-packages/kr8s/_api.py", line 184, in open_websocket
    async with aiohttp.ClientSession(
  File ".../.venv/lib/python3.10/site-packages/aiohttp/client.py", line 261, in __init__
    self._base_url.origin() == self._base_url
AssertionError: Only absolute URLs without path part are supported

because Rancher provides the following URL to connect to websockets: https://rancher.example.com/k8s/clusters/c-m-4wx4gzpx

Minimal Complete Verifiable Example:

TBD

Anything else we need to know?:

Environment:

  • Dask version: 2023.11.0
  • Python version: 3.10
  • Operating System: Linux
  • Install method (conda, pip, source): poetry

dbalabka avatar Jan 02 '24 19:01 dbalabka

To fix the issue, aiohttp needs to support the path in base_url: https://github.com/aio-libs/aiohttp/issues/6647

It works for me in my patched version.

dbalabka avatar Jan 02 '24 20:01 dbalabka

Would you mind opening an issue upstream in kr8s for this? Preferably with a small example we can use to reproduce the issue (I know we would need Rancher but the snippet that reproduces on your end would still be useful).

jacobtomlinson avatar Jan 05 '24 16:01 jacobtomlinson

@jacobtomlinson , I think the simplest way to reproduce the problem is to put k8s API behind the proxy and add an extra path (proxy http://127.0.0.1/somepath to http://127.0.0.1) and adjust kubectl settings. The Nginx reverse proxy_pass rule can help:

location /somepath {
  proxy_pass http://127.0.0.1
}

dbalabka avatar Jan 10 '24 12:01 dbalabka

It looks like kubectl can handle this for you.

$ kubectl proxy --api-prefix=/somepath/
Starting to serve on 127.0.0.1:8001
$ curl localhost:8001/somepath/version
{
  "major": "1",
  "minor": "29",
  "gitVersion": "v1.29.0",
  "gitCommit": "3f7a50f38688eb332e2a1b013678c6435d539ae6",
  "gitTreeState": "clean",
  "buildDate": "2023-12-14T19:18:17Z",
  "goVersion": "go1.21.5",
  "compiler": "gc",
  "platform": "linux/amd64"
}

jacobtomlinson avatar Jan 11 '24 17:01 jacobtomlinson

While digging into this I've noticed some other strange behaviour when the Kubernetes API is proxied with an extra path. I opened https://github.com/kr8s-org/kr8s/issues/284 to track.

jacobtomlinson avatar Jan 11 '24 17:01 jacobtomlinson