設定:
我有一個 minikube 設定和一個 docker-compose 設定并排運行。這樣我就可以輕松地在 docker/docker-compose 中開發我的應用程式并在 minikube 中運行其他服務。我在 Linux (Ubuntu) 上作業。
問題:
我想使用 cURL 訪問在 docker-compose 內運行的 docker 容器中的 minikube API。
我試過的:
- 使用代理設定訪問 minikube:(
curl http://localhost:8080/api當使用 kubectl proxy 時kubectl proxy --port=8080)但這當然不起作用,因為 localhost 是容器的本地主機。這將導致curl: (7) Failed to connect to localhost port 8080: Connection refused - 通過泊塢窗內部主機訪問minikube:
curl http://host.docker.internal:8080/api。這也導致curl: (7) Failed to connect to host.docker.internal port 8080: Connection refused. - 通過從以下腳本中找到的 api/credentials 訪問 minikube:
APISERVER=$(kubectl config view --minify | grep server | cut -f 2- -d ":" | tr -d " ")
SECRET_NAME=$(kubectl get secrets | grep ^default | cut -f1 -d ' ')
TOKEN=$(kubectl describe secret $SECRET_NAME | grep -E '^token' | cut -f2 -d':' | tr -d " ")
curl $APISERVER/api --header "Authorization: Bearer $TOKEN" --insecure
但這一切都失敗了。
我還將它添加到我的 docker-compose.yml 檔案中:
extra_hosts:
- "host.docker.internal:host-gateway"
當我在 docker 容器外嘗試上述命令時,一切正常。如何從 docker 容器內訪問 minikube?
提前致謝!
uj5u.com熱心網友回復:
我強烈建議您也使用 kubernetes 清單來包裝您的開發并將您的服務部署在集群中。只需一次設定,然后您就可以進行多次測驗,而不是使用 docker-compose 方式進行開發設定。
話雖如此,我嘗試了您的上述設定(但我在 mac、minikube 和 docker for mac 中嘗試過)。有效的解決方案正是這樣做:
- 添加
extra_hosts到 docker-compose:
version: "3.9" services: busy1: image: progrium/busybox command: sleep 3600 extra_hosts: - "host.docker.internal:host-gateway"
- 運行一個 hello-world 示例并使用 minikube 中的服務公開它(這類似于創建部署和服務清單)
minikube kubectl -- create deployment node-hello --image=gcr.io/google-samples/node-hello:1.0 --port=8080 minikube kubectl -- expose deployment node-hello --port=8080
- 運行帶有標志的 minikube作為回應
--disable-filter=true來克服forbidden。
minikube kubectl -- proxy --disable-filter=true --port=8080
host.docker.internal在 docker 中運行的容器內使用以訪問主機
curl http://host.docker.internal:8080/api/ { "kind": "APIVersions", "versions": [ "v1" ], "serverAddressByClientCIDRs": [ { "clientCIDR": "0.0.0.0/0", "serverAddress": "192.168.49.2:8443" } ] }
所以基本上流程是,容器 -> host.docker.internal -> kubectl 代理 -> kubernetes 服務 -> kubectl 部署
這完全取決于您的 kubernetes 服務配置,以便代理可以正常作業。但是就像我之前說的,使用 kubernetes 優先模式進行開發,這樣您就可以專注于邏輯和業務功能,而不是擔心網路復雜性。所有這些都因 docker 版本、平臺(linux、mac、windows 等)、服務配置(或入口)等、橋接或覆寫或使用的主機網路而異。祝你好運。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/367015.html
標籤:码头工人 Kubernetes 卷曲 迷你酷
