Access external database from Kubernetes

14,191

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

Share:
14,191
Francisco Gonzalez
Author by

Francisco Gonzalez

Try to search my destiny

Updated on July 10, 2022

Comments

  • Francisco Gonzalez
    Francisco Gonzalez almost 2 years

    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.

  • Popopame
    Popopame over 3 years
    Can you post your updated configuration ? Also , when you run "kubectl get services" does everything run smoothly ?
  • hesmoh
    hesmoh over 2 years
    Is it possible to use this endpoint in the .env file of the application that we deployed on the kubernetes cluster? something like this: DB_ADDRESS=backend-mobile-db-service