Kubernetes Pod cannot communicate with external services like Oracle, Mongo etc

158 views Asked by At

My database is running in different host which is not deployed in kubernetes( for eg: hostName is testDevDb)

When i run my Micro Service(MS) as K8 Pod, MS cannot able to open connection with external database. i am getting unknown host exception. Looks like DNS is not resolved.

Caused by: java.net.UnknownHostException: testDevDb
    at java.net.InetAddress$CachedAddresses.get(InetAddress.java:797) ~[?:?]
    at java.net.InetAddress.getAllByName0(InetAddress.java:1505) ~[?:?]
    at java.net.InetAddress.getAllByName(InetAddress.java:1364) ~[?:?]
    at java.net.InetAddress.getAllByName(InetAddress.java:1298) ~[?:?]

I don't find right article to resolve this.

Is there any way to use of host's /etc/resolv.conf inside K8's POD.

Please help me the right approach to resolve this

1

There are 1 answers

0
Furkan On

If you want to add host and IP information to /etc/hosts file inside a pod, you can use hostAliases. There is an example for a pod in doc.

sample deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: yourdeploymentname
  labels:
    app: yourapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: yourapp
  template:
    metadata:
      labels:
        app: yourapp
    spec:
      containers:
      - name: yourapp
        image: yourappimage:latest
    hostAliases:
      - ip: "10.10.10.10"
        hostnames:
        - "testDevDb"