The setup:
I have a minikube setup and a docker-compose setup side by side running. This way I can easily develop my application within docker/docker-compose and run other services within minikube. I'm working on Linux (Ubuntu).
The problem:
I'd like to access the minikube API within a docker container running inside docker-compose with cURL.
What I've tried:
- Accessing minikube with the proxy setup:
curl http://localhost:8080/api
(when using the kubectl proxykubectl proxy --port=8080
) but that ofcourse won't work because localhost is the container's localhost. This leads tocurl: (7) Failed to connect to localhost port 8080: Connection refused
- Accessing minikube via the docker internal host:
curl http://host.docker.internal:8080/api
. This also leads tocurl: (7) Failed to connect to host.docker.internal port 8080: Connection refused
. - Accessing minikube through the api/credentials found from the script below:
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
But that all fails.
I've also added this to my docker-compose.yml file:
extra_hosts:
- "host.docker.internal:host-gateway"
It all works fine when I try the above commands outside the docker container. How do I access minikube from inside a docker container?
Thanks in advance!
CodePudding user response:
I strongly suggest that you wrap you development also using kubernetes manifests and deploy your service inside cluster. Just one time setup and then you can test multiple times instead of docker-compose way for dev setup.
Having said that, I tried your above setup (But I tried in mac, minikube and docker for mac). The solution that worked was doing exactly these:
- Add
extra_hosts
to docker-compose:
version: "3.9" services: busy1: image: progrium/busybox command: sleep 3600 extra_hosts: - "host.docker.internal:host-gateway"
- Run a hello-world sample and expose it using service in minikube (this is similar to creating deployment and service manifests)
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
- Run minikube with flag
--disable-filter=true
to overcomeforbidden
as response.
minikube kubectl -- proxy --disable-filter=true --port=8080
- Use
host.docker.internal
from within the container running in docker to access host
curl http://host.docker.internal:8080/api/ { "kind": "APIVersions", "versions": [ "v1" ], "serverAddressByClientCIDRs": [ { "clientCIDR": "0.0.0.0/0", "serverAddress": "192.168.49.2:8443" } ] }
So basically flow is, container -> host.docker.internal -> kubectl proxy -> kubernetes service -> kubectl deployment
It totally depends on your kubernetes service configuration so that proxy will work fine. But like I said before, develop with kubernetes first mode and so that you can focus in logic and business functionality than worrying about network complexity. And all these varies with docker version, platform (linux, mac, windows etc), service configuration (or ingress) etc, bridge or overlay or host network used. Good luck.