Home > Mobile >  Cannot reach locahost of host machine in Kubernetes
Cannot reach locahost of host machine in Kubernetes

Time:05-27

I have a kubernetes kind cluster with the following configuration:

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
  - role: control-plane
    kubeadmConfigPatches:
      - |
        kind: InitConfiguration
        imageGCHighThresholdPercent: 70
        evictionHard:
          nodefs.available: "0%"
          nodefs.inodesFree: "0%"
          imagefs.available: "70%"
        nodeRegistration:
          kubeletExtraArgs:
            node-labels: "ingress-ready=true"
    extraPortMappings:
      - containerPort: 80
        hostPort: 80
        protocol: TCP
      - containerPort: 443
        hostPort: 443
        protocol: TCP

Image version: docker.io/kindest/base:v20220305-b67a383f

I am trying to connect to my localhost by using ExternalName:

kind: Endpoints
apiVersion: v1
metadata:
  name: my-external-service
subsets:
  - addresses:
      - ip: 10.0.2.2
    ports:
      - port: 8080
---
kind: Service
apiVersion: v1
metadata:
  name: my-external-service
  labels:
    app: my-external-service
spec:
  ports:
    - port: 8080
      targetPort: 8080
      protocol: TCP

According to this answer: why do we use 10.0.2.2 to connect to local web server instead of using computer ip address in android client, that IP should be the localhost in my local machine.

Then I run a container to debug this setup:

kubectl run -i --tty --rm debug --image=alpine --restart=Never -- sh

8.8.8.8 is pingable, but unfortunately I cannot ping 10.0.2.2.

What am I doing wrong?

CodePudding user response:

Turn out it was not a kubernetes problem, but a docker one. The solution is described in this github comment: https://github.com/kubernetes-sigs/kind/issues/1200#issuecomment-647145134

the address is not 10.0.2.2, but 172.17.0.1 which is your docker bridge default gateway.

Also we need to add this iptables rule in the host machine

iptables -I INPUT -p tcp --dport <your server port> -j ACCEPT
  • Related