-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible accessing host machine ports from Kind pods? #1200
Comments
I came with a working experimental approach, but requires some hacks and modifications in Kind. Steps:
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
image: kindest/node:v1.16.3
extraDockerOptions:
- --network
- integration-tests
- role: worker
image: kindest/node:v1.16.3
extraDockerOptions:
- --network
- integration-tests ("integration-tests" is a pre-existing docker network:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: socat-dns
namespace: kube-system
labels:
k8s-app: socat-dns
spec:
selector:
matchLabels:
name: socat-dns
template:
metadata:
labels:
name: socat-dns
spec:
hostNetwork: true
containers:
- name: socat
image: alpine/socat
args:
- tcp-listen:5353,reuseaddr,fork
- tcp:127.0.0.11:53
env:
- name: HOST_IP
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: status.hostIP
# old config:
# forward . /etc/resolv.conf
forward . {$HOST_IP}:5353 {
force_tcp
} And profit! With this setup, it allows you to resolve names of containers attached to the docker network from inside Kind pods! :D :
For steps 3. 4. and 5. I made some custom manifests with those modifications to apply easily with I'm not super happy with the approach..., it's kinda hacky, but it's an alternative 🤷♂ , maybe it can bring some ideas for the conversation at #148 🤔 . Do you think PS: Sorry for the long text. |
Removing the forward breaks upstream DNS..? I am working on docker networks support, please see other issues in the tracker for the problems with doing this properly. I'm currently developing a prototype. We are not accepting docker flags anywhere in the API surface, we do not wish to be coupled to docker's CLI externally in any way and have discussed this in the past. |
This should be possible without user defined networks IIRC, I will have to dig out the details. host.docker.internal should be possible to mimic on linux |
It doesn't remove it, it replaces the
👍
I will explore a bit more 🤔 |
Finally I found a way to achive this with qoomon/docker-host: ---
apiVersion: apps/v1
kind: Deployment
metadata:
name: dockerhost
labels:
k8s-app: dockerhost
spec:
replicas: 1
selector:
matchLabels:
k8s-app: dockerhost
template:
metadata:
labels:
k8s-app: dockerhost
spec:
containers:
- name: dockerhost
image: qoomon/docker-host
securityContext:
capabilities:
add: ["NET_ADMIN", "NET_RAW"]
env:
# Not needed in MacOs:
- name: DOCKER_HOST
value: 172.17.0.1 # <-- docker bridge network default gateway
---
apiVersion: v1
kind: Service
metadata:
name: dockerhost
spec:
clusterIP: None # <-- Headless service
selector:
k8s-app: dockerhost From inside a pod running in Kind:
Thanks @BenTheElder for pointing me into another direction! :) Do you think this trick is worthy to be added to the user guide documentation ("Accessing host machine ports")? In any case, I think we can close this question, thx for the assistance!. |
@fllaca how exactly did you get the final solution to work? I did a edit: think adding this into the docs would be great. |
hi @s12chung ! |
I think it is possible to get the same result, by only allowing the ports in the firewall, without the need to use a container to NAT connections to the docker default gateway.
now from inside container |
I'm having trouble with this sadly. I can connect from my host with
|
For those interested I think I came with a simpler solution to route network to the host machine, using headless or ExternalName services. The solution is different for Linux and Mac/Window (the latter using Docker desktop). Linux solution: headless service + endpoint: ---
apiVersion: v1
kind: Endpoints
metadata:
name: dockerhost
subsets:
- addresses:
- ip: 172.17.0.1 # this is the gateway IP in the "bridge" docker network
---
apiVersion: v1
kind: Service
metadata:
name: dockerhost
spec:
clusterIP: None Mac/Windows (Docker Desktop) solution: ExternalName service: ---
apiVersion: v1
kind: Service
metadata:
name: dockerhost
spec:
type: ExternalName
externalName: host.docker.internal |
One use-case for us for doing this is being able to locally develop an extensions server that we can get aggregated into the kind cluster using
|
Working config for extension servers pointed to a locally running process on Linux: ---
apiVersion: v1
kind: Endpoints
metadata:
name: api
subsets:
- addresses:
- ip: 172.17.0.1 # docker0 bridge ip
ports:
- appProtocol: https
port: 8443
protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
name: api
spec:
ports:
- protocol: TCP
appProtocol: https
port: 8443
targetPort: 8443
|
@fllaca @charandas I wanted to access the interfaces of the host machine from the kind pods. Does your solution work for accessing interfaces as well? |
No, that's not going to work. You should probably investigate running the host as a single-node kubeadm cluster directly instead if you're going to directly interact with host devices. An alternative would be https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/ |
Hi!
we are planning to use Kind as part of our development environment in our laptops, the idea is to use it in combination with Skaffold. We are finding some difficulties to make connections from the application running in pods inside Kind to the database and queues running in the host machine (usually also in a another container).
To give you some context, ee are basically using 2/3 approaches:
Approach 3. is fine, but it gets a bit more complicated, specially if data is wanted to be persisted (it can be done with persistent volumes and kind node mounts), as opposed to have these dependencies running in simple docker-compose files. Of course we have tried running our app also in docker-compose (since Kind doesn't aim to be an alternative to docker-compose), but using Kind allows us to make our local environment look closer to our pre-production and production deployments, and to test some features that are K8s-related (in our case: an embedded distributed cache (Hazelcast) that uses K8s API for peers discovery).
Do you have any recommendation for this use case?
Thanks for this amazing project
The text was updated successfully, but these errors were encountered: