How can I communicate with a DB External to my Kubernetes Cluster

10/15/2020

Good afternoon, I have a question, I am new to Kubernetes and I need to connect to a DB that is outside of my cluster, I could only connect to the DB using the hostNetwork = true, however, this is not recommended, in this case there is a method to communicate with External DB?

I leave you the yaml that I am currently using, my pod contains one container that work with spring boot

apiVersion: apps/v1
kind: Deployment
metadata:
  name: find-complementary-account-info
  labels:
    app: find-complementary-account-info
spec:
  replicas: 2
  selector:
    matchLabels:
      app: find-complementary-account-info
  template:
    metadata:
      labels:
        app: find-complementary-account-info
    spec:
      hostNetwork: true
      dnsPolicy: Default
      containers:
      - name: find-complementary-account-info
        image:find-complementary-account-info:latest
        imagePullPolicy: IfNotPresent
        resources:
          limits:
            memory: "350Mi"
          requests:
            memory: "300Mi"
        ports:
        - containerPort: 8081
        env:
        - name: URL_CONNECTION_BD
          value: jdbc:oracle:thin:@11.160.9.18:1558/DEFAULTSRV.WORLD
        - name: USERNAME_CONNECTION_BD
          valueFrom:
            secretKeyRef:
              name: credentials-bd-pers
              key: user_pers
        - name: PASSWORD_CONNECTION_BD
          valueFrom:
            secretKeyRef:
              name: credentials-bd-pers
              key: password_pers
              key: password_pers
---
apiVersion: v1
kind: Service
metadata:
  name: find-complementary-account-info
spec:
  type: NodePort
  selector:
    app: find-complementary-account-info
  ports:
    - protocol: TCP
      port: 8081
      targetPort: 8081
      nodePort: 30020

Anyone have an idea how to communicate with external DB? This is not a cloud cluster, it is OnPremise

-- Cesar Justo
docker
kubernetes

1 Answer

10/15/2020

hostNetwork parameter is used for accessing pods from outside of the Cluster, you don't need that.

Pods from inside the Cluster can communicate externally because they are NATted. If not, something external prevent it, like a firewall or a missing routing.

The quicker way to find that is to ssh to one of your Kubernetes cluster nodes and try

telnet 11.160.9.18 1558

Anyway that IP address seems a Public one, so you have to check your company firewall imho

-- oldgiova
Source: StackOverflow