0

I'm new with Kubernetes and I try to understand how to connect Postgres database which is outside from Kubernetes (exactly in docker with ip address 172.17.0.2 and port 5432) to my webapp in Kubernetes.

I try connect database through env variable PS_DATABASE_URL in Deployment section.

But it cannot find mentioned url for postgres. How it need to be done correctly?

webapp.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp-deployment
  labels:
    app: webapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webapp
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: dmitriy83/flask_kuber
        ports:
        - containerPort: 5000
        env:
          - name: PS_DATABASE_URL
            value: postgresql://postgres:[email protected]:5432/db
---
apiVersion: v1
kind: Service
metadata:
  name: webapp-service
spec:
  type: NodePort
  selector:
    app: webapp
  ports:
    - protocol: TCP
      port: 5000
      targetPort: 5000
      nodePort: 30100

10
  • Hey Dmitriy, where your k8s infra is hosted? Minikube, cloud? Commented Dec 3, 2021 at 17:49
  • Can you move the database into Kubernetes too? If not, "in Docker" isn't really a meaningful place for the database to be running; the Kubernetes cluster would probably need to connect to a published port on the host running the container, the same as any other out-of-cluster resource. Commented Dec 3, 2021 at 19:25
  • can you check is the k8s master has the connectivity of ip and port of database, there might be some firewall issue Commented Dec 3, 2021 at 21:39
  • @jabbson in Minikube Commented Dec 4, 2021 at 4:16
  • @DavidMaze i understand that Docker not the best place for db. Ii need to know how to connect external db which is not in Kubernetes to app in Kubernetes Commented Dec 4, 2021 at 4:18

1 Answer 1

2

I figured it out. it depends from cloud provider. For this example i use amazon cloud and to connect database on amazon (this is external service). So we must define it in yaml file like an external service.

postgres_external.yaml

kind: Service
apiVersion: v1
metadata:
  name: postgres
spec:
  type: ExternalName
  externalName: db.cdmhjidhpqyu.us-east-2.rds.amazonaws.com

to connect to external service you need to link to it on deployment.

webapp.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp-deployment
  labels:
    app: webapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webapp
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: dmitriy83/flask_kuber
        ports:
        - containerPort: 5000
        env:
          - name: PS_DATABASE_URL
            value: postgresql://<username>:<password>@postgres:5432/db
---
apiVersion: v1
kind: Service
metadata:
  name: webapp-service
spec:
  type: NodePort
  selector:
    app: webapp
  ports:
    - protocol: TCP
      port: 5000
      targetPort: 5000
      nodePort: 30100

Please note in webapp.yaml, env section value value: postgresql://<username>:<password>@postgres:5432/db   contains postgres - this is name of our external service which we define in postgres_external.yaml

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.