kubernetes 无法从bash脚本在Postgres容器上运行run命令

nr9pn0ug  于 11个月前  发布在  Kubernetes
关注(0)|答案(4)|浏览(123)

我试图在k8S中的PostgreSQL容器上运行SQL脚本如果我在命令行中逐个运行命令,它可以工作。但是如果我运行脚本文件,它不会尝试连接到端口8080。端口8080上没有运行任何东西,我的postgres pod使用默认端口5432,如果我逐个运行命令,我可以连接到我的pod,我可以用PostgreSQL管理员连接到sql pod,但不知何故,当我运行脚本时,我得到了这个错误:

错误

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数据

sanzor@DESKTOP-GOMS8S8:~/NovaWebsocketServer$ kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
postgres-7b9fb8d6c5-5s85s   1/1     Running   69         111d

Pod描述

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

gxwragnw

gxwragnw1#

The connection to the server localhost:8080 was refused - did you specify the right host or port?似乎与kubectl试图读取cat "$SQL_SCRIPT_PATH"的内容有关--tables.pgsql被传递到kubectl并在那里结束,并没有一直传递到psql。我相信如果你调用kubectl cluster-info,你应该得到这样的结果:

root@sqa03:~# kubectl cluster-info
Kubernetes master is running at http://localhost:8080

字符串
所以当你cat "$SQL_SCRIPT_PATH" | sudo -S kubectl exec -it ...,它是kubectl出错,因为它不知道如何处理tables.pgsql的内容
而不是cat文件到sudo kubectl ...我建议复制文件到pod与kubectl 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`。

fwzugrvs

fwzugrvs2#

如果你运行sudo kubectl config get-contexts并看到没有集群被列出,你需要先把当前用户的~/.kube/config复制到root用户的主目录。

bprjcwpo

bprjcwpo3#

作者:Nijo Luca
通常,当Kubernetes无法访问您的集群时,会发生此错误,因为它缺乏必要的权限。
要解决此问题,请将配置文件移动到kube文件夹中。

尝试以下故障排除步骤解决问题

如果您是普通用户,请使用以下命令设置正确的所有权并将配置文件移动到kube文件夹中。

mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config

字符串
如果您是root用户,请导出KUBECONFIG环境变量

export KUBECONFIG=/etc/kubernetes/admin.conf


注意:在大多数情况下,配置文件通常称为admin.conf。如果名称不同,请根据名称修改命令。

d7v8vwbk

d7v8vwbk4#

问题是双重的。
1.正如@ gohm 'c所指出的,即使我使用默认的k8s上下文(docker-desktop)运行,运行kubectl config get-contexts也不会呈现任何上下文
所以我做的是

cd ~
ls -a .kube
sudo cp -R ~/.kube /root

字符串
1.然后,我的命令即使是在工作目录中的visual studio代码的集成终端中运行,当运行脚本(位于完全相同的文件夹中)时,它似乎不是从该目录运行的。
我更新的脚本看起来像这样:

#!/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"

# Define the Kubernetes context you want to use
KUBE_CONTEXT="docker-desktop"

# Change the working directory to the location of the SQL script
cd "$(dirname "$SQL_SCRIPT_PATH")"

# Get the name of the PostgreSQL pod
POSTGRES_POD=$(kubectl get pods -l app=postgres -o jsonpath='{.items[0].metadata.name}' --context="$KUBE_CONTEXT")

# Use kubectl cp to copy the SQL script to the PostgreSQL pod
kubectl cp "$(basename "$SQL_SCRIPT_PATH")" "$POSTGRES_POD:$(basename "$SQL_SCRIPT_PATH")" --context="$KUBE_CONTEXT"

# Change back to the original working directory
cd -

# Connect to the PostgreSQL pod and run the SQL script using sudo -S to provide the password
echo "$PGPASSWORD" | sudo -S kubectl exec -i "$POSTGRES_POD" -- psql -U "$PGUSER" -d postgresdb -f "$(basename "$SQL_SCRIPT_PATH")"

# Exit the script
exit

相关问题