Kubernetes上PostgreSQL集群的管理

基于Kubernetes部署PostgreSQL能够得到快速伸缩、故障转移、在线修复等优势。在《Kubernetes快速部署高可用PostgreSQL》中介绍了使用Stolon项目进行部署的方法和步骤。集群安装完毕后,能够经过pgsql命令行工具进行操做。咱们更但愿有一个WebUI的图形化工具,这里介绍pgAdmin4的安装和使用(以Ubuntu 18.04LTS为例)。python

一、安装pgadm4

得到 repository key,在 https://www.postgresql.org/media/keys/ACCC4CF8.asc,以下:git

sudo apt-get install curl ca-certificates
curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

建立安装源描述文件 /etc/apt/sources.list.d/pgdg.list。操做系统分发版名称为 codename-pgdg。在下面的例子中,须要将 stretch 替换为你所用的deb/ubuntu版本(能够使用命令 lsb_release -c来肯定版本号),以下:github

deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main

自动获取版本号并建立安装源的元文件,以下:web

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

而后,更新软件包的列表,并安装:sql

sudo apt-get update 

# 安装postgresql服务器,这里我使用K8s的集群,不须要安装。
#sudo apt-get install postgresql-11

# 安装pgadmin4,用于管理postgresql服务器。
sudo apt-get install pgadmin4

二、使用pgadmin4

输入命令pgadmin4,启动管理服务:docker

supermap@podc01:~/openthings/stolon-chart$ pgadmin4
Python path:  "/usr/lib/python3/dist-packages" 
Python Home:  ""
Webapp path:  "/usr/share/pgadmin4/web/pgAdmin4.py"
NOTE: Configuring authentication for DESKTOP mode.
pgAdmin 4 - Application Initialisation
======================================

将会自动打开浏览器,并启动管理页面。数据库

在管理页面添加以前在《Kubernetes快速部署高可用PostgreSQL》设置的服务器10.1.1.201,端口30900,数据库postgres。json

  • 用户名stolon,登陆口令经过下面方法获取。
PGPASSWORD=$(kubectl get secret --namespace stolon waxen-seal-stolon -o jsonpath="{.data.pg_su_password}" | base64 --decode; echo)

echo $PGPASSWORD

而后登陆到该PostgreSQL服务,打开页面:ubuntu

选中Databases/postgres,而后在菜单tool选择Query tool,打开查询页面,输入:浏览器

select * from test

三、在容器中运行pgadmin4

容器镜像位于:

拉取容器镜像:

# https://hub.docker.com/r/dpage/pgadmin4

docker pull dpage/pgadmin4

运行容器实例,注意将管理端口映射出来。

docker pull dpage/pgadmin4 docker run -p 980:80 \ -e "PGADMIN_DEFAULT_EMAIL=user@domain.com" \ -e "PGADMIN_DEFAULT_PASSWORD=SuperSecret" \ -d dpage/pgadmin4

运行加密的TLS容器,使用配置 config/storage 目录在 /private/var/lib/pgadmin(宿主机),以下:

docker pull dpage/pgadmin4 docker run -p 9443:443 \ -v "/private/var/lib/pgadmin:/var/lib/pgadmin" \ -v "/path/to/certificate.cert:/certs/server.cert" \ -v "/path/to/certificate.key:/certs/server.key" \ -e "PGADMIN_DEFAULT_EMAIL=user@domain.com" \ -e "PGADMIN_DEFAULT_PASSWORD=SuperSecret" \ -e "PGADMIN_ENABLE_TLS=True" \ -d dpage/pgadmin4

注意:

  • 为了减小端口冲突,我将端口80和443分别映射到了980和9443,由于这两个端口常常被其余的服务所占用。

参考:

四、在kubernetes中运行pgadmin4

4.1 准备helm chart

经过 helm chart来进行安装,得到模版:

git clone https://github.com/jjcollinge/pgadmin-chart

缺省配置文件values.yaml 将使pgAdmin部署能够经过IP地址和plaintext HTTP进行访问。

http访问

为了使pgAdmin 实例使用域名进行plaintext HTTP访问,须要:

  1. 设置 service.typeNodePort
  2. 设置 ingress.enabledtrue
  3. 保留静态 IP 在 Kubernetes cluster (使用 e.g. gcloud compute addresses create my-pgadmin-static-ip --global for GCP)
  4. 设置 ingress.staticIPReservation 为静态IP address,为上面第三步建立。
  5. 在域名注册服务中,建立一条记录指向该静态IP地址,为上面第三步建立。

https访问

为了访问 pgAdmin 实例,经过域名和HTTPS,,则须要以下的设置:

  1. Set ingress.tls.enabled to true
  2. Set ingress.tls.clusterIssuer to the name of a cert-manager ClusterIssuer deployed in your Kubernetes cluster
  3. Set ingress.tls.externalDNSName to the (fully-qualified) domain name you registered in step 5

4.2 安装helm chart

使用helm的命令进行安装,这里安装到命名空间stolon里,与以前的postgreSQL安装到了一个命名空间。

helm install --name pgadmin4 --namespace stolon

缺省的登陆帐号:

  • username: pgadmin4@pgadmin.org
  • password: admin

这个能够在value.yaml中修改,或者经过以下方式在安装时进行设置:

helm install --set pgadmin.username=myuser,pgadmin.password=mypassword  --name pgadmin4 --namespace stolon

 

更多参考:

相关文章
相关标签/搜索