title: How can a Kubernetes pod connect to database which is running in the same local network (outside the cluster) as the host?
body: <p>I have a Kubernetes cluster (K8s) running in a physical server A (internal network IP 192.168.200.10) and a PostgreSQL database running in another physical server B (internal network IP 192.168.200.20). How can my Java app container (pod) running in the K8s be able to connect to the PostgreSQL DB in server B? </p>
  
  <p>OS: Ubuntu v16.04
  Docker 18.09.7
  Kubernetes v1.15.4
  Calico v3.8.2
  Pod base image: openjdk:8-jre-alpine</p>
  
  <p>I have tried following <a href="https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-mapping-external-services" rel="nofollow noreferrer">this example</a> to create a service and endpoint</p>
  
  <pre><code>kind: Service
  apiVersion: v1
  metadata:
   name: external-postgres
  spec:
   ports:
   - port: 5432
     targetPort: 5432
  ---
  kind: Endpoints
  apiVersion: v1
  metadata:
   name: external-postgres
  subsets:
   - addresses:
       - ip: 192.168.200.20
     ports:
       - port: 5432
  </code></pre>
  
  <p>And had my JDBC connection string as: jdbc:postgresql://external-postgres/MY_APPDB , but it doesn't work. The pod cannot ping server B or telnet the DB using the said internal IP or ping external-postgres service name. I do not wish to use "hostNetwork: true" or connect server B via a public IP.</p>
  
  <p>Any advice is much appreciated. Thanks.</p>