How to connect to a private IP from Kubernetes Pod

4/1/2019

I am trying to connect to a private IP from within a pod. Ping to that IP from the pod returns unreachable. However, I am able to ping that IP from the host system. What is the best way to route the traffic from the pod to the destination private IP?

-- Forkmohit
kubernetes
networking

1 Answer

4/2/2019

Pods are not allowed to connect directly outside of kubernetes network. You can find more details here. To connect external IP you have to define Endpoints and kubernets will redirect request from inside pod to that IP. If you private IP need any extra task like DNS configure or anything else will is out of kubernetes. For kubernetes you will need to define Endpoints. Create you Endpoints

kind: Endpoints
apiVersion: v1
metadata:
  name: local-ip
subsets:
 - addresses:
     - ip: 10.240.0.4  # IP of your desire end point
   ports:
     - port: 27017     # Port that you want to access

Now you can connect from inside you pods using Endpoints name. But better to access Endpoints through Service. You can find more details here. You can find similar answer and flow diagram here.

-- Shalauddin Ahamad Shuza
Source: StackOverflow