首页 > 解决方案 > 用于从外部 AKS 访问 postgres 的 Port-foward 命令在 kubectl 中由于 & 符号而无法工作

问题描述

我正在尝试从以下 Bitnami 图表在 AKS 上部署 PostgreSQL:https ://github.com/bitnami/charts/tree/master/upstreamed/postgresql/#installing-the-chart

在部署期间,我被邀请使用以下命令(一旦安装)以确保我能够访问集群外部的 postgres(例如一些本地 DBMS)

kubectl port-forward --namespace default svc/dozing-coral-postgresql 5432:5432 & PGPASSWORD="$POSTGRES_PASSWORD" psql --host 127.0.0.1 -U postgres -p 5432

这会导致语法错误:

At line:1 char:80
+ ... d --namespace default svc/dozing-coral-postgresql 5432:5432 & PGPASSW 
...
+                                                                 ~
The ampersand (&) character is not allowed. The & operator is reserved for 
future use; wrap an ampersand in double quotation marks ("&") to pass it as 
part of a string.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : AmpersandNotAllowed

解锁端口的正确命令是什么?

标签: postgresqlkuberneteskubectlbitnamiazure-aks

解决方案


您应该看到的输出类似于

To connect to your database from outside the cluster execute the following commands:

    kubectl port-forward --namespace default svc/jolly-raccoon-postgresql 5432:5432 &
    PGPASSWORD="$POSTGRES_PASSWORD" psql --host 127.0.0.1 -U postgres -p 5432

有两种不同的命令。第一个是转发端口,最后&是将此命令发送到后台,以便您能够继续使用shell

$ kubectl port-forward --namespace default svc/jolly-raccoon-postgresql 5432:5432 &
[1] 62447
Forwarding from 127.0.0.1:5432 -> 5432

第二个命令允许您使用已安装psql客户端的另一台主机的转发端口连接到数据库

$ PGPASSWORD="$POSTGRES_PASSWORD" psql --host 127.0.0.1 -U postgres -p 5432
psql (11.3)
Type "help" for help.

postgres=#

推荐阅读