I am trying to run a sql script upon a postgreSQL container in k8S
If i run my commands in the command line one by one , it works.
However if i run the script file it does not trying to connect to port 8080
.
There is nothing running on port 8080
and my postgres pod uses the default port of 5432
I can connect to my pod if i run the commands one by one, i can connect to the sql pod with the PostgreAdmin, but somehow i get this error , when i run the script:
Error
sanzor@DESKTOP-GOMS8S8:~/NovaWebsocketServer$ ./start.sh got here The connection to the server localhost:8080 was refused - did you specify the right host or port?
script.sh
#!/bin/bash
# Define the PostgreSQL username and password
PGUSER="admin"
PGPASSWORD="test123"
# Define the SQL script path
SQL_SCRIPT_PATH="/home/sanzor/NovaWebsocketServer/resources/tables.pgsql"
# Get the name of the PostgreSQL pod
POSTGRES_POD=$(kubectl get pods -l app=postgres -o jsonpath='{.items[0].metadata.name}')
echo "got here"
# Use cat to insert the SQL script content into psql in the PostgreSQL pod
cat "$SQL_SCRIPT_PATH" | sudo -S kubectl exec -it "$POSTGRES_POD" -- psql -U "$PGUSER" -d postgresdb
# Exit the script
exit
Pod data
sanzor@DESKTOP-GOMS8S8:~/NovaWebsocketServer$ kubectl get pods
NAME READY STATUS RESTARTS AGE
postgres-7b9fb8d6c5-5s85s 1/1 Running 69 111d
Pod description
sanzor@DESKTOP-GOMS8S8:~/NovaWebsocketServer$ kubectl describe pod postgres-7b9fb8d6c5-5s85s
Name: postgres-7b9fb8d6c5-5s85s
Namespace: default
Priority: 0
Node: docker-desktop/192.168.65.4
Start Time: Sat, 15 Jul 2023 16:39:59 +0300
Labels: app=postgres
pod-template-hash=7b9fb8d6c5
Annotations: <none>
Status: Running
IP: 10.1.2.108
IPs:
IP: 10.1.2.108
Controlled By: ReplicaSet/postgres-7b9fb8d6c5
Containers:
postgres:
Container ID: docker://661876a876e9e2e7be10c8c36f42804a9cb5de0a573d3352c7bd38b1b1043934
Image: postgres:10.1
Image ID: docker-pullable://postgres@sha256:3f4441460029e12905a5d447a3549ae2ac13323d045391b0cb0cf8b48ea17463
Port: 5432/TCP
Host Port: 0/TCP
State: Running
Started: Sat, 04 Nov 2023 07:45:17 +0200
Last State: Terminated
Reason: Error
Exit Code: 255
Started: Fri, 03 Nov 2023 15:38:42 +0200
Finished: Sat, 04 Nov 2023 07:45:03 +0200
Ready: True
Restart Count: 69
Environment Variables from:
postgres-config ConfigMap Optional: false
Environment: <none>
Mounts:
/var/lib/postgresql/data from postgredb (rw)
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-wdg6m (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
postgredb:
Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
ClaimName: postgres-pv-claim
ReadOnly: false
kube-api-access-wdg6m:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SandboxChanged 49m kubelet Pod sandbox changed, it will be killed and re-created.
Normal Pulled 49m kubelet Container image "postgres:10.1" already present on machine
Normal Created 49m kubelet Created container postgres
Normal Started 49m kubelet Started container postgres
2
Answers
The
The connection to the server localhost:8080 was refused - did you specify the right host or port?
seems to be related tokubectl
trying to read in the contents ofcat "$SQL_SCRIPT_PATH"
— thetables.pgsql
is getting passed tokubectl
and ends there, and doesn’t make it all the way topsql
. I believe if you callkubectl cluster-info
you should get something like:So when you
cat "$SQL_SCRIPT_PATH" | sudo -S kubectl exec -it ...
, it’skubectl
erroring out because it doesn’t know what to do with the contents oftables.pgsql
Rather than
cat
the file intosudo kubectl ...
I would recommend copying the file into the pod withkubectl cp $SQL_SCRIPT_PATH <namespace>/<pod>:/path/to/tables.pgsql, then tell the pod to call
psql -f /path/to/tables.pgsql -U "$PGUSER" -d postgresdb`.If you run
sudo kubectl config get-contexts
and see no cluster is listed, you need to copy the current user’s ~/.kube/config to the root user’s home directory first.