I’m creating a kind cluster with kind create cluster --name kind
and I want to access it from another docker container but when I try to apply a Kubernetes file from a container (kubectl apply -f deployment.yml
) I got this error:
The connection to the server 127.0.0.1:6445 was refused - did you specify the right host or port?
Indeed when I try to curl kind control-plane from a container, it’s unreachable.
> docker run --entrypoint curl curlimages/curl:latest 127.0.0.1:6445
curl: (7) Failed to connect to 127.0.0.1 port 6445 after 0 ms: Connection refused
However kind control-plane is publishing to the right port but only to the localhost.
> docker ps --format "table {{.Image}}t{{.Ports}}"
IMAGE PORTS
kindest/node:v1.23.4 127.0.0.1:6445->6443/tcp
Currently the only solution I found is to set the host network mode.
> docker run --network host --entrypoint curl curlimages/curl:latest 127.0.0.1:6445
Client sent an HTTP request to an HTTPS server.
This solution don’t look to be the most secure. Is there another way like connecting the kind network to my container or something like that that I missed ?
2
Answers
I don’t know exactly why you want to do this. but no problem I think this could help you:
first, lets pull your docker image:
In my kind cluster I got 3 control plane nodes and 3 worker nodes. Here are the pod of my kind cluster:
The container that is interesting for us here is the haproxy one (kindest/haproxy:v20220207-ca68f7d4) which have the role of loadbalancing the enterring traffic to the nodes (and, in our example, especially the control plane nodes.) we can see that the port 35625 of our host machine is mapped to the port 6443 of the haproxy container. (127.0.0.1:35625->6443/tcp)
so, our cluster endpoint is https://127.0.0.1:35625, we can confirm this in our kubeconfig file (~/.kube/config):
let’s run the curl container in background:
as expected, we cant HTTP request the endpoint that listen on an HTTPS port:
we can try to use the certificate that is in the field "certificate-authority-data" in our kubeconfig to check if that change something (it should):
Lets create a file named my-ca.crt that contain the stringData of the certificate:
since the working directory of the curl docker image is "/" lets copy our cert to this location in the container and verify that it is actually there:
Let’s try again our curl request but with the certificate:
YOU, can get the same result by adding the "–insecure" flag to your curl request:
However, we can’t access our cluster with anonymous user ! So lets get a token from kubernetes (cf https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/):
Once the token controller has populated the secret with a token:
Now lets execute the curl command directly with the token !
It works !
I still don’t know why you want to do this but I hope that this helped you.
Since It’s not what you wanted because here I use host network, You can use this : How to communicate between Docker containers via "hostname" as proposed @SergioSantiago thanks for your comment !
bguess
Don’t have enough rep to comment on the other answer, but wanted to comment on what ultimately worked for me.
Takeaways
kind
host
network.kind-control-plane:6443
. The port is NOT the exposed port in the example below6443
NOT38669
Kube config for the container
Docker container stuff
If using docker-compose you can add the kind network to the container such as:
If running a new container:
Container already running?