准备

1
2
git clone --depth 1 "https://github.com/CrunchyData/postgres-operator-examples.git"
cd postgres-operator-examples

安装PGO

官方地址:https://access.crunchydata.com/documentation/postgres-operator/latest/quickstart

1
2
kubectl apply -k kustomize/install/namespace
kubectl apply --server-side -k kustomize/install/default

配置集群

参考地址:https://www.crunchydata.com/blog/postgis-with-postgresql-operator

1
vim kustomize/postgres/postgres.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
name: hippo
spec:
#image: registry.developers.crunchydata.com/crunchydata/crunchy-postgres:ubi8-16.3-0
image : registry.developers.crunchydata.com/crunchydata/crunchy-postgres-gis:ubi8-16.3-3.4-0
#image: RELATED_IMAGE_POSTGRES_16_GIS_3.4
postgresVersion: 16
postGISVersion: "3.4"
instances:
  - name: instance1
    dataVolumeClaimSpec:
      accessModes:
      - "ReadWriteOnce"
      resources:
        requests:
          storage: 100Gi
users:
- name: "hippo"
  options: 'SUPERUSER'
backups:
  pgbackrest:
    image: registry.developers.crunchydata.com/crunchydata/crunchy-pgbackrest:ubi8-2.51-0
    repos:
    - name: repo1
      volume:
        volumeClaimSpec:
          accessModes:
          - "ReadWriteOnce"
          resources:
            requests:
              storage: 100Gi

安装集群

官方地址:Create a Postgres Cluster (crunchydata.com)

1
kubectl apply -k kustomize/postgres

已知问题

问题1:

storageclass调用nfs失败

原因: 没有应用nfs_provisioner

解决: yaml文件放在/data/nfsyml目录下。


问题2:

Output: mount: /var/lib/kubelet/pods/bd8b92ad-3311-4743-8154-0e75e5298fbe/volumes/kubernetes.io~nfs/pvc-db9f2bae-9eae-4f5a-91d0-831cf832389c: bad option; for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount.<type> helper program.

原因: node节点没有客户端,ubuntu高版本必须安装utils和common centos系统安装utils即可

解决: 节点安装nfs客户端 apt install nfs-common -y


问题3:

Output: mount.nfs: access denied by server while mounting 192.168.0.170:/postgres-data

原因:nfs server配置错误

解决:确认nfs的ip和端口,重启nfs-server

配置

pgo使用PostgresCluster资源说明,官方地址:PostgresCluster (crunchydata.com)

高可用

1
kubectl edit postgresclusters.postgres-operator.crunchydata.com -n postgres-operator hippo # 修改replicas = 2

PostGIS

如果安装时制定了gis的地址就不需要这一步

修改镜像
1
kubectl edit postgresclusters -n postgres-operator hippo
查看镜像
1
cat kustomize/install/manager/manager.yaml
替换spec.image镜像地址

这里使用postgre v16和gis v3.4

1
registry.developers.crunchydata.com/crunchydata/crunchy-postgres-gis:ubi8-16.3-3.4-0

连接

直接连接

1
psql $(kubectl -n postgres-operator get secrets hippo-pguser-hippo -o go-template='{{.data.uri | base64decode}}')

端口转发连接

1
2
3
4
5
6
PG_CLUSTER_PRIMARY_POD=$(kubectl get pod \
-n postgres-operator \
-o name \
-l postgres-operator.crunchydata.com/cluster=hippo,postgres-operator.crunchydata.com/role=master)

kubectl -n postgres-operator port-forward "${PG_CLUSTER_PRIMARY_POD}" --address 0.0.0.0 5432:5432
获取密码
1
kubectl get secrets -n postgres-operator "hippo-pguser-hippo" -o go-template='{{.data.password | base64decode}}'
获取用户
1
kubectl get secrets -n postgres-operator "hippo-pguser-hippo" -o go-template='{{.data.user | base64decode}}'
获取数据库
1
kubectl get secrets -n postgres-operator "hippo-pguser-hippo" -o go-template='{{.data.dbname | base64decode}}'
连接数据库
1
psql -h localhost -U hippo 

输入密码

删除集群

卸载集群

1
kubectl delete -k kustomize/postgres

卸载pgo

1
2
kubectl delete -k kustomize/install/default
kubectl delete -k kustomize/install/namespace

已知问题

namespace terminal

强制删除ns

1
kubectl proxy --port=8081
1
2
3
4
kubectl get ns postgres-operator -o json > 1.json


curl -k -H "Content-Type: application/json" -X PUT --data-binary @1.json http://127.0.0.1:8081/api/v1/namespaces/postgres-operator/finalize