last day (15 days later) » 

09:32
0
Q: Spring Boot App can't access Postgres using credentials from Kubernetes secret

Urr4I'm not an expert when it comes to Kubernetes so, for learning, I started building a simple Spring Boot Web-App (called meal-planner) which reads and writes to a PostgreSQL DB. Postgres and Spring Boot app are deployed in Kubernetes which is running on (currently) 2 Raspberry Pis (3 and 3B+). I w...

Could you try setting the variables' value directly in the deployment template? For example - name: spring.datasource.password valueFrom: secretKeyRef: name: dev-db-secret key: password
Thank, I will try that
Didn't work, I still get the same error. I'm now assuming that I somehow cant reach the postgres service from the meal-planner
I can see in your project that your dev profile is hitting localhost and the prod one is hitting postgres. Are you using prod profile in your deployment? Your application property is also using dev. Also to use kubernetes dns resolver you must specify the namespace as well, try with postgres.default instead of just postgres in the database connection string
I build the application in prod profile, the dev profile is juste checked into git. Since postgres and meal-planner are in the same namespace, should "postgres" suffice instead of postgres.default? I still tried "postgres", "postgres.default" and "postgres.home" as dns names. Still nothing.
Could you try removing type: NodePort from your service? From what I understand you're trying to reach your service from within the cluster so you should use ClusterIP (default value)
09:35
Hi, your right and I already did that aufer San's feedback. Failed to update the question though
Aha, I see
Could you also share here the full value of your jdbc url string? It should look something like this jdbc:postgresql://postgres.home/home
I think would be better if you first try to establish connection to the database from the pod, can you ssh into it and run a telnet postgres.home 5432?
Its jdbc:postgresql://postgres:5432/home. I left out the namespace, since meal-planner and the postgres are in the same ns
I don't know how to ssh into the pod. Do you mean ssh into the node?
I tried to exec into the pod and ping the service using kubectl exec <<pod-name>> -- ping postgres:5432
I don't think you have to specify the port again, try only with postgres
yes, exec into the pod should work
to ssh there just do kubectl exec -it <<pod-name>> -- bash
or sh, depending your runtime
sadly, I have to use windows, so neither bash nor sh work, however exec <<pod>> -- ping postgres yields Destination Net Unreachable
is your postgresql runtime windows?
09:42
no, the whole cluster is running on 2 raspberry pis in arm32v7
So, your docker image is postgres:13.2
yes
that image is ubuntu based AFAIK
so you should be able to bash or sh in there
I try again, maybe i screwed up somehow
kubectl exec -it <<pod-name>> -- bash

What's after the -- is the command to execute inside the pod, if bash doesn't work try with bin/bash, sh, or bin/sh
09:46
I've tried exec -it <<pod>> -n home -- bash bin/bash, sh and bin/sh. Always get Unable to use a TTY - input is not a terminal or the right kind of file. Thats why i assumed it somehow binds to my systems terminal, which is windows and fails
Ah, I see. Do you have git bash or something? If not you can try adding a winpty at the beginning of the command
winpty kubectl exec -it <<pod-name>> -n home -- bash
Oh cool,. never head of winpty, but this worked using bin/sh
ok so now you're inside try running telnet postgres.home 5432
or just ping postgres.home
telnet yields Connected to postgres.home
ping seems to send 56 bytes of data with nothing happening afterwards
100% packet loss
so you can establish connection
telnet is fine
can you connect to the database from there now? try installing psql cli inside your pod
PGPASSWORD=$DB_PASSWORD psql -h postgres.home -p 5432 -U $DB_USER -d home
I think this should work for you
10:02
I'm failing to install psql since the pods keeps restarting because of the db-connection error. I tried updating so it won't restart but it doesn't allow it. I tried using a busybox, but it behaves differently, pinging the db successfully. Since I have a meeting in about 15minutes, i'll stop for now
I will remove the db connection from the meal-planner so it doesnt restart and try again later this day. I'll update the question accordingly. Still don't know what the problem is, but you helped me a great deal in debugging it and maybe this helps me to find the problem later one. Thank you very much
I think the issue is with your database url string :) try setting the environment variables pointing to override your spring settings, e.g:

- name: spring.datasource.url
value: jdbc:postgresql://postgres.home:5432/home

And same for username and password.
Also try printing the values from application startup to make sure they're being read properly
Sadly I've already tried that without success :-D they are read correctly, no matter what approach in injecting them i tried. even hardcoded into the application.yaml it doesn't work.
Need to leave now. Have a good day, thank again for all the help and see you
Bye man, good luck!

last day (15 days later) »