Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access external database from Kubernetes

I have a kubernetes (v1.18.6) with 1 service (loadbalancer), 2 pods in a develoment:

apiVersion: v1
kind: Service
metadata:
  name: app-service
spec:
  selector:
    app: app
  ports:
  - protocol: "TCP"
    port: 6000
    targetPort: 5000
  type: LoadBalancer

A network policy to access Intenert (it is necesary for me):

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: internet-access
spec:
  podSelector:
    matchLabels:
      networking/allow-internet-access: "true"
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - {}

Deployment config file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-deployment
spec:
  progressDeadlineSeconds: 120
  selector:
    matchLabels:
      app: app
  replicas: 2
  template:
    metadata:
      labels:
        app: app
    spec:
      imagePullSecrets:
        - name: myregistrykey
      containers:
      - name: app
        image: app
        imagePullPolicy: Always
        ports:
        - containerPort: 5000

It is working correctly. But now, I want to connect this imagen to an external database (in another network only access by internet). For this proposition I use this service:

apiVersion: v1
kind: Service
metadata:
  name: postgresql
spec:
  clusterIP: None
  ports:
  - port: 25060


---
apiVersion: v1
kind: Endpoints
metadata:
  name: postgresql
subsets:
  - addresses:
        - ip: 206............
    ports:
      - port: 25060
        name: postgresql

It is all the services:

NAME                TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)          AGE
app-service         LoadBalancer   10.245.134.137   206...........   6000:31726/TCP   2d4h
kubernetes          ClusterIP      10.245.0.1       <none>           443/TCP          3d7h
postgresql          ClusterIP      None             <none>           25060/TCP        19h

But when I try to connect I receive a timeout error of the database, like can't connect to the database.

I have an internet connection in the image.

I find the solution, the problem was the rules of inbound of the database. I must add the IP of Kubernetes.

Thx.

like image 520
Francisco Gonzalez Avatar asked Nov 07 '22 05:11

Francisco Gonzalez


1 Answers

Here is what worked for me:

Define a service , but set clusterIP: None , so no endpooint is created.

And then create an endpoint yourself with the SAME NAME as your service and set the IP and port of your db.

In your example , you have a type in your endpoint: the name of your endpoint is postgresql not postgresSql.

My example:

---
service.yaml
kind: Service
apiVersion: v1
metadata:
  name: backend-mobile-db-service
spec:
  clusterIP: None
  ports:
  - port: 5984
---
kind: Endpoints
apiVersion: v1
metadata:
  name: backend-mobile-db-service
subsets:
  - addresses:
        - ip: 192.168.1.50
    ports:
      - port: 5984
        name: backend-mobile-db-service

like image 58
Popopame Avatar answered Nov 11 '22 08:11

Popopame