首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Zalando postgres operator在k8s中从外部访问集群

Zalando postgres operator在k8s中从外部访问集群
EN

Stack Overflow用户
提问于 2020-11-16 00:39:05
回答 1查看 463关注 0票数 1

我已经安装了postgres集群Zalando postgres operator https://github.com/zalando/postgres-operator,如何从外部访问postgres数据库?我尝试将集群服务类型从ClusterIP更改为NodePort,但它被自动覆盖。

EN

回答 1

Stack Overflow用户

发布于 2020-11-19 05:45:39

这里解释了这个过程:https://postgres-operator.readthedocs.io/en/latest/user/

你需要两个步骤。

打开端口并将其转发到本地计算机的

  1. 脚本。

我创建了一个名为set_dbforwarding.sh的脚本。您需要将脚本中的名称更改为您的集群名称和设置!所以cdf-cluster应该成为yourclustername

代码语言:javascript
复制
   #!/usr/bin/env bash
     
    set -u   # crash on missing env variables
    set -e   # stop on any error
    set -x   # print what we are doing
     
    export NAMESPACE=$1
     
    export PGMASTER=$(kubectl -n cdf-acc get pods -o jsonpath={.items..metadata.name} -l application=spilo,cluster-name=cdf-cluster,spilo-role=m
    
    # PGMASTER should be now the master node. There are cases under failover
    # that you should connect to a different node in your cluster.
    # If you want to change something you should always connect to the master.
    # otherwise you get 
     
    # set up port forward
    kubectl -n $NAMESPACE port-forward $PGMASTER 6432:5432
     
    # get the password..it is printend in your terminal
    # so you can use it in your db tool of choice.
    export PGPASSWORD=$(kubectl -n $NAMESPACE get secret cdf.cdf-cluster.credentials.postgresql.acid.zalan.do -o 'jsonpath={.data.password}' | b
    export PGSSLMODE=require

执行方式如下:

代码语言:javascript
复制
     ./set_dbforwarding.sh yourclusternamespace

  1. 使用正确的凭据连接到群集。restore_db.sh脚本。

#!/usr/bin/env bash set -u #在缺少环境变量时崩溃设置-e #在任何错误时停止设置-x #打印我们正在做的导出NAMESPACE=$1导出DATABASE=$2导出DATABASEDUMP=$3导出群集(kubectl -n $NAMESPACE get pods -o jsonpath={.items..metadata.name} -l application=spilo,cluster-name=cdf-PGMASTER=$,spilo-rol export PGPASSWORD=$(kubectl -n $NAMESPACE get secret PGPASSWORD=$ 'jsonpath={.data.password} export PGSSLMODE=require # examples你现在可以运行上面的ENV变量集了。# psql -h 127.0.0.1 -U postgres -d cdf -p 6432 #cat ~/dump/cbs_schema_wfs.sql| psql -h 127.0.0.1 -U postgres -d cdf -p 6432 # pg_restore -h 127.0.0.1 -U postgres -p 6432 -d $2 -c $3 #仅数据# pg_restore -h 127.0.0.1 -U postgres -p 6432 -d $2 $3 #一切pg_restore -h 127.0.0.1 -U postgres -p 6432 -d $2 $3

使用方式如下

代码语言:javascript
复制
     ./restore_db.sh namespace databasename backup.gz

  1. 提示如果您正在使用像DBbeaver这样的数据库工具,请确保每隔5秒左右选中一次keep-alive框。否则连接将被断开。keep alive会让它保持打开。但这些设置在DBBeaver上是相当隐藏的。

编辑连接->连接设置-> initialization -> Keep-Alive。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64847000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档