Search code examples
kubernetesk3s

k3s/kubernetes, can not curl pod ip from different vendor vps node


I create 4 nginx service, show:

kubectl get pods -o wide (hide some unimportant)
Test-nginx 1/1 Running 10.42.1.3 k8s-worker1
Test-nginx1 1/1 Running 10.42.1.6 k8s-worker1
Test-nginx3 1/1 Running 10.42.1.7 k8s-worker1
Test-nginx2 1/1 Running 10.42.0.9 k8s-master

Then i curl 10.42.1.3 on k8s-master, no response. But i can curl 10.42.1.9 on k8s-master. Also,i can't curl 10.42.1.9 on k8s-worker1.

k8s-master is Ubuntu 20.04.4 LTS with iptables v1.8.4 (legacy). k8s-worker1 is Ubuntu 22.04.1 LTS with iptables v1.8.7 (nf_tables)

Maybe this reason: https://docs.k3s.io/installation/network-options#embedded-k3s-multicloud-solution


Solution

  • The pod ip is internal ip, can only access by other pod.

    Here two simple way to access pod

    • Use Port Forwarding to Access Applications in a Cluster

      kubectl port-forward pods/Test-nginx 8080:80
      

      Then open another terminal and run curl http://k8s-master-ip:8080/

    • Use a Service to Access an Application in a Cluster

      # create node port service
      kubectl expose pod test-nginx --type=NodePort --port=80 --name=test-nginx-service
      # get nodeport
      NODEPORT=$(kubectl get service test-nginx-service -o jsonpath='{.spec.ports[0].nodePort}')
      # access
      curl http://k8s-master-ip:$NODEPORT/
      

    other advanced way please see doc https://kubernetes.io/docs/tasks/access-application-cluster/.