[root@k8s-master ~]# helm search repo postgresql
NAME CHART VERSION APP VERSION DESCRIPTION
aliyun/postgresql 0.9.1 Object-relational database management system (O...
bitnami/postgresql 10.13.9 11.14.0 Chart for PostgreSQL, an object-relational data...
bitnami/postgresql-ha 8.0.3 11.14.0 Chart for PostgreSQL with HA architecture (usin...
aliyun/gcloud-sqlproxy 0.2.3 Google Cloud SQL Proxy
# 部署集群# 1.下载源码# First, clone the repository and change to the directory
git clone https://github.com/zalando/postgres-operator.git
cd postgres-operator
# 2.安装Operator# apply the manifests in the following order
kubectl create -f manifests/configmap.yaml # configuration
kubectl create -f manifests/operator-service-account-rbac.yaml # identity and permissions
kubectl create -f manifests/postgres-operator.yaml # deployment
kubectl create -f manifests/api-service.yaml # operator API to be used by UI# 3.部署operator UI# manual deployment
kubectl apply -f ui/manifests/
# or kustomization
kubectl apply -k github.com/zalando/postgres-operator/ui/manifests
# or helm chart
helm install postgres-operator-ui ./charts/postgres-operator-ui
# 4.创建PostgreSQL集群# create a Postgres cluster
kubectl create -f manifests/minimal-postgres-manifest.yaml
[root@k8s-master ~]# helm search repo postgresql
NAME CHART VERSION APP VERSION DESCRIPTION
bitnami/postgresql 10.13.9 11.14.0 Chart for PostgreSQL, an object-relational data...
bitnami/postgresql-ha 8.0.3 11.14.0 Chart for PostgreSQL with HA architecture (usin...
https://github.com/bitnami/charts/tree/master/bitnami
https://github.com/bitnami/charts/tree/master/bitnami/postgresql
https://bitnami.com/stack/postgresql/helm
https://github.com/bitnami/charts/tree/master/bitnami/postgresql-ha
https://bitnami.com/stack/postgresql-ha/helm
# 部署集群# 1.下载源码# First, clone the repository and change to the directory
git clone https://github.com/zalando/postgres-operator.git
cd postgres-operator
# 2.安装Operator# apply the manifests in the following order
kubectl create -f manifests/configmap.yaml # configuration
kubectl create -f manifests/operator-service-account-rbac.yaml # identity and permissions
kubectl create -f manifests/postgres-operator.yaml # deployment
kubectl create -f manifests/api-service.yaml # operator API to be used by UI# 3.部署operator UI# manual deployment
kubectl apply -f ui/manifests/
# or kustomization
kubectl apply -k github.com/zalando/postgres-operator/ui/manifests
# or helm chart
helm install postgres-operator-ui ./charts/postgres-operator-ui
# 4.创建PostgreSQL集群# create a Postgres cluster
kubectl create -f manifests/minimal-postgres-manifest.yaml
# git clone https://github.com/zalando/postgres-operator.git[root@k8s-master operator]# git clone https://github.com/zalando/postgres-operator.git
Cloning into 'postgres-operator'...
remote: Enumerating objects: 22785, done.
remote: Counting objects: 100% (342/342), done.
remote: Compressing objects: 100% (182/182), done.
remote: Total 22785 (delta 198), reused 259 (delta 153), pack-reused 22443
Receiving objects: 100% (22785/22785), 8.71 MiB | 1.68 MiB/s, done.
Resolving deltas: 100% (16287/16287), done.[root@k8s-master operator]# ll
total 4
drwxr-xr-x 15 root root 4096 Dec 6 12:51 postgres-operator
[root@k8s-master operator]# cd postgres-operator/
[root@k8s-master postgres-operator]# ll
total 140
-rwxr-xr-x 1 root root 207 Dec 6 12:51 build-ci.sh
drwxr-xr-x 4 root root 59 Dec 6 12:51 charts
drwxr-xr-x 2 root root 21 Dec 6 12:51 cmd
-rw-r--r-- 1 root root 93 Dec 6 12:51 CODEOWNERS
-rw-r--r-- 1 root root 841 Dec 6 12:51 CONTRIBUTING.md
-rw-r--r-- 1 root root 3103 Dec 6 12:51 delivery.yaml
drwxr-xr-x 3 root root 69 Dec 6 12:51 docker
drwxr-xr-x 4 root root 157 Dec 6 12:51 docs
drwxr-xr-x 4 root root 215 Dec 6 12:51 e2e
-rw-r--r-- 1 root root 551 Dec 6 12:51 go.mod
-rw-r--r-- 1 root root 76016 Dec 6 12:51 go.sum
drwxr-xr-x 2 root root 105 Dec 6 12:51 hack
drwxr-xr-x 3 root root 93 Dec 6 12:51 kubectl-pg
-rw-r--r-- 1 root root 1077 Dec 6 12:51 LICENSE
-rw-r--r-- 1 root root 200 Dec 6 12:51 MAINTAINERS
-rw-r--r-- 1 root root 2805 Dec 6 12:51 Makefile
drwxr-xr-x 2 root root 4096 Dec 6 12:51 manifests
-rw-r--r-- 1 root root 520 Dec 6 12:51 mkdocs.yml
drwxr-xr-x 2 root root 22 Dec 6 12:51 mocks
drwxr-xr-x 10 root root 122 Dec 6 12:51 pkg
-rw-r--r-- 1 root root 4824 Dec 6 12:51 README.md
-rwxr-xr-x 1 root root 8325 Dec 6 12:51 run_operator_locally.sh
-rw-r--r-- 1 root root 100 Dec 6 12:51 SECURITY.md
drwxr-xr-x 5 root root 225 Dec 6 12:51 ui
[root@k8s-master postgres-operator]# cd manifests/
[root@k8s-master manifests]# ll
total 148
-rw-r--r-- 1 root root 192 Dec 6 12:51 api-service.yaml
-rw-r--r-- 1 root root 5856 Dec 6 12:51 complete-postgres-manifest.yaml
-rw-r--r-- 1 root root 5322 Dec 6 12:51 configmap.yaml
-rw-r--r-- 1 root root 243 Dec 6 12:51 custom-team-membership.yaml
-rw-r--r-- 1 root root 209 Dec 6 12:51 e2e-storage-class.yaml
-rw-r--r-- 1 root root 670 Dec 6 12:51 fake-teams-api.yaml
-rw-r--r-- 1 root root 299 Dec 6 12:51 infrastructure-roles-configmap.yaml
-rw-r--r-- 1 root root 303 Dec 6 12:51 infrastructure-roles-new.yaml
-rw-r--r-- 1 root root 723 Dec 6 12:51 infrastructure-roles.yaml
-rw-r--r-- 1 root root 173 Dec 6 12:51 kustomization.yaml
-rw-r--r-- 1 root root 977 Dec 6 12:51 minimal-fake-pooler-deployment.yaml
-rw-r--r-- 1 root root 403 Dec 6 12:51 minimal-postgres-manifest-12.yaml
-rw-r--r-- 1 root root 406 Dec 6 12:51 minimal-postgres-manifest.yaml
-rw-r--r-- 1 root root 20622 Dec 6 12:51 operatorconfiguration.crd.yaml
-rw-r--r-- 1 root root 4456 Dec 6 12:51 operator-service-account-rbac.yaml
-rw-r--r-- 1 root root 275 Dec 6 12:51 platform-credentials.yaml
-rw-r--r-- 1 root root 1368 Dec 6 12:51 postgres-operator.yaml
-rw-r--r-- 1 root root 318 Dec 6 12:51 postgres-pod-priority-class.yaml
-rw-r--r-- 1 root root 21426 Dec 6 12:51 postgresql.crd.yaml
-rw-r--r-- 1 root root 5941 Dec 6 12:51 postgresql-operator-default-configuration.yaml
-rw-r--r-- 1 root root 2017 Dec 6 12:51 postgresteam.crd.yaml
-rw-r--r-- 1 root root 399 Dec 6 12:51 standby-manifest.yaml
-rw-r--r-- 1 root root 987 Dec 6 12:51 user-facing-clusterroles.yaml
1.1.部署PostgreSQL集群
1.1.1.安装Operator
1
2
3
4
5
6
7
8
# 安装步骤# apply the manifests in the following order
kubectl create -f manifests/configmap.yaml # configuration
kubectl create -f manifests/operator-service-account-rbac.yaml # identity and permissions
kubectl create -f manifests/postgres-operator.yaml # deployment
kubectl create -f manifests/api-service.yaml # operator API to be used by UI
# 创建Operator[root@k8s-master postgres-operator]# kubectl create -f manifests/configmap.yaml
configmap/postgres-operator created
[root@k8s-master postgres-operator]# kubectl create -f manifests/operator-service-account-rbac.yaml
serviceaccount/postgres-operator created
clusterrole.rbac.authorization.k8s.io/postgres-operator created
clusterrolebinding.rbac.authorization.k8s.io/postgres-operator created
clusterrole.rbac.authorization.k8s.io/postgres-pod created
[root@k8s-master postgres-operator]# kubectl create -f manifests/postgres-operator.yaml
deployment.apps/postgres-operator created
[root@k8s-master postgres-operator]# kubectl create -f manifests/api-service.yaml
service/postgres-operator created
[root@k8s-master ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/postgres-operator-569b58b8c6-dqfj2 0/1 ImagePullBackOff 0 11m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 114d
service/postgres-operator ClusterIP 10.99.200.1 <none> 8080/TCP 11m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/postgres-operator 0/1 1 0 11m
NAME DESIRED CURRENT READY AGE
replicaset.apps/postgres-operator-569b58b8c6 1 1 0 11m
# 安装失败# 无法下载镜像 docker pull registry.opensource.zalan.do/acid/postgres-operator:v1.7.1# 通过阿里云镜像服务拉取镜像
# 无法下载镜像 registry.opensource.zalan.do/acid/postgres-operator:v1.7.1# 通过阿里云镜像服务拉取镜像# 详细信息参考拉取墙外镜像文档# 拉取镜像# docker pull registry.cn-beijing.aliyuncs.com/k8s-middleware/zalando-postgres-operator:[镜像版本号]
docker pull registry.cn-beijing.aliyuncs.com/k8s-middleware/zalando-postgres-operator:v1.7.1
# 打标签
docker tag registry.cn-beijing.aliyuncs.com/k8s-middleware/zalando-postgres-operator:v1.7.1 registry.opensource.zalan.do/acid/postgres-operator:v1.7.1
# postgres-operator正常运行[root@k8s-master ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/postgres-operator-569b58b8c6-dqfj2 1/1 Running 0 124m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 114d
service/postgres-operator ClusterIP 10.99.200.1 <none> 8080/TCP 124m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/postgres-operator 1/1 1 1 124m
NAME DESIRED CURRENT READY AGE
replicaset.apps/postgres-operator-569b58b8c6 1 1 1 124m
# 创建[root@k8s-master postgres-operator-1.6.1]# kubectl apply -f ui/manifests/
deployment.apps/postgres-operator-ui created
Warning: networking.k8s.io/v1beta1 Ingress is deprecated in v1.19+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
ingress.networking.k8s.io/postgres-operator-ui created
service/postgres-operator-ui created
serviceaccount/postgres-operator-ui created
clusterrole.rbac.authorization.k8s.io/postgres-operator-ui created
clusterrolebinding.rbac.authorization.k8s.io/postgres-operator-ui created
error: unable to recognize "ui/manifests/kustomization.yaml": no matches for kind "Kustomization"in version "kustomize.config.k8s.io/v1beta1"[root@k8s-master postgres-operator-1.6.1]# kubectl apply -k github.com/zalando/postgres-operator/ui/manifests
serviceaccount/postgres-operator-ui unchanged
clusterrole.rbac.authorization.k8s.io/postgres-operator-ui unchanged
clusterrolebinding.rbac.authorization.k8s.io/postgres-operator-ui unchanged
service/postgres-operator-ui unchanged
deployment.apps/postgres-operator-ui configured
ingress.networking.k8s.io/postgres-operator-ui configured
# 下载不下来[root@k8s-master postgres-operator-1.6.1]# kubectl get pod -lname=postgres-operator-ui
NAME READY STATUS RESTARTS AGE
postgres-operator-ui-6577ddc48c-2wzdc 0/1 ImagePullBackOff 0 4m48s
postgres-operator-ui-6879669844-2j8tz 0/1 ImagePullBackOff 0 4m6s
# 无法下载镜像 registry.opensource.zalan.do/acid/postgres-operator:v1.7.1# 通过阿里云镜像服务拉取镜像
1.1.3.安装Postgres集群
1
2
3
4
5
6
7
8
9
10
11
# create a Postgres cluster
kubectl create -f manifests/minimal-postgres-manifest.yaml
# check the deployed cluster
kubectl get postgresql
# check created database pods
kubectl get pods -lapplication=spilo -L spilo-role
# check created service resources
kubectl get svc -lapplication=spilo -L spilo-role
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@k8s-master postgres-operator]# kubectl create -f manifests/minimal-postgres-manifest-12.yaml
postgresql.acid.zalan.do/acid-upgrade-test created
# 查看[root@k8s-master ~]# kubectl get postgresql
NAME TEAM VERSION PODS VOLUME CPU-REQUEST MEMORY-REQUEST AGE STATUS
acid-upgrade-test acid 12 2 1Gi 36s Creating
[root@k8s-master ~]# kubectl get svc -lapplication=spilo -L spilo-role
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SPILO-ROLE
acid-upgrade-test ClusterIP 10.108.89.38 <none> 5432/TCP 92s master
acid-upgrade-test-repl ClusterIP 10.107.53.203 <none> 5432/TCP 92s replica
[root@k8s-master ~]# kubectl get pods -lapplication=spilo -L spilo-role
NAME READY STATUS RESTARTS AGE SPILO-ROLE
acid-upgrade-test-0 0/1 Pending 0 106s
# 查看[root@k8s-master operator]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/acid-minimal-cluster-0 1/1 Running 0 16h
pod/acid-minimal-cluster-1 1/1 Running 0 15h
pod/postgres-operator-569b58b8c6-dqfj2 1/1 Running 0 18h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/acid-minimal-cluster ClusterIP 10.100.29.145 <none> 5432/TCP 16h
service/acid-minimal-cluster-config ClusterIP None <none> <none> 15h
service/acid-minimal-cluster-repl ClusterIP 10.103.33.77 <none> 5432/TCP 16h
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 114d
service/postgres-operator ClusterIP 10.99.200.1 <none> 8080/TCP 18h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/postgres-operator 1/1 1 1 18h
NAME DESIRED CURRENT READY AGE
replicaset.apps/postgres-operator-569b58b8c6 1 1 1 18h
NAME READY AGE
statefulset.apps/acid-minimal-cluster 2/2 16h
NAME TEAM VERSION PODS VOLUME CPU-REQUEST MEMORY-REQUEST AGE STATUS
postgresql.acid.zalan.do/acid-minimal-cluster acid 14 2 1Gi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@k8s-master operator]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
acid-minimal-cluster-0 1/1 Running 0 16h 10.244.36.105 k8s-node1 <none> <none>
acid-minimal-cluster-1 1/1 Running 0 15h 10.244.36.96 k8s-node1 <none> <none>
[root@k8s-master operator]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
# 主
acid-minimal-cluster-0 1/1 Running 0 16h application=spilo,cluster-name=acid-minimal-cluster,controller-revision-hash=acid-minimal-cluster-77b87b69f9,spilo-role=master,statefulset.kubernetes.io/pod-name=acid-minimal-cluster-0,team=acid
# 从
acid-minimal-cluster-1 1/1 Running 0 15h application=spilo,cluster-name=acid-minimal-cluster,controller-revision-hash=acid-minimal-cluster-77b87b69f9,spilo-role=replica,statefulset.kubernetes.io/pod-name=acid-minimal-cluster-1,team=acid
# 安装步骤# apply the manifests in the following order
kubectl create -f manifests/configmap.yaml # configuration
kubectl create -f manifests/operator-service-account-rbac.yaml # identity and permissions
kubectl create -f manifests/postgres-operator.yaml # deployment
kubectl create -f manifests/api-service.yaml # operator API to be used by UI
# 创建Operator[root@k8s-master postgres-operator]# kubectl create -f manifests/configmap.yaml
configmap/postgres-operator created
[root@k8s-master postgres-operator]# kubectl create -f manifests/operator-service-account-rbac.yaml
serviceaccount/postgres-operator created
clusterrole.rbac.authorization.k8s.io/postgres-operator created
clusterrolebinding.rbac.authorization.k8s.io/postgres-operator created
clusterrole.rbac.authorization.k8s.io/postgres-pod created
[root@k8s-master postgres-operator]# kubectl create -f manifests/postgres-operator.yaml
deployment.apps/postgres-operator created
[root@k8s-master postgres-operator]# kubectl create -f manifests/api-service.yaml
service/postgres-operator created
[root@k8s-master postgres-operator]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/postgres-operator-569b58b8c6-6qdlw 1/1 Running 0 53s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 114d
service/pg-svc-master NodePort 10.104.70.22 <none> 5432:31669/TCP 39m
service/pg-svc-replica NodePort 10.105.246.108 <none> 5432:30871/TCP 36m
service/postgres-operator ClusterIP 10.98.87.50 <none> 8080/TCP 42s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/postgres-operator 1/1 1 1 53s
NAME DESIRED CURRENT READY AGE
replicaset.apps/postgres-operator-569b58b8c6 1 1 1 53s
1.6.2.修改配置
1
2
3
4
5
# vim complete-postgres-manifest.yaml
volume:
size: 20Gi
storageClass: "rook-ceph-block"
1.6.3.安装Postgres集群
1
2
3
4
5
6
7
8
9
10
11
# create a Postgres cluster
kubectl create -f manifests/complete-postgres-manifest.yaml
# check the deployed cluster
kubectl get postgresql
# check created database pods
kubectl get pods -lapplication=spilo -L spilo-role
# check created service resources
kubectl get svc -lapplication=spilo -L spilo-role
# 部署单实例[root@k8s-master ~]# helm search repo postgresql
NAME CHART VERSION APP VERSION DESCRIPTION
bitnami/postgresql 10.13.9 11.14.0 Chart for PostgreSQL, an object-relational data...
https://github.com/bitnami/charts/tree/master/bitnami
https://github.com/bitnami/charts/tree/master/bitnami/postgresql
# values.yaml
persistence:
## @param persistence.enabled Enable persistence using PVC##
enabled: true## @param persistence.existingClaim Provide an existing `PersistentVolumeClaim`, the value is evaluated as a template.## If defined, PVC must be created manually before volume will be bound## The value is evaluated as a template, so, for example, the name can depend on .Release or .Chart##
existingClaim: ""## @param persistence.mountPath The path the volume will be mounted at, useful when using different## PostgreSQL images.##
mountPath: /bitnami/postgresql
## @param persistence.subPath The subdirectory of the volume to mount to## Useful in dev environments and one PV for multiple services##
subPath: ""## @param persistence.storageClass PVC Storage Class for PostgreSQL volume## If defined, storageClassName: <storageClass>## If set to "-", storageClassName: "", which disables dynamic provisioning## If undefined (the default) or set to null, no storageClassName spec is## set, choosing the default provisioner. (gp2 on AWS, standard on## GKE, AWS & OpenStack)##
storageClass: "rook-ceph-block"## @param persistence.accessModes PVC Access Mode for PostgreSQL volume##
accessModes:
- ReadWriteOnce
## @param persistence.size PVC Storage Request for PostgreSQL volume##
size: 20Gi
## @param persistence.annotations Annotations for the PVC##
annotations: {}## @param persistence.selector Selector to match an existing Persistent Volume (this value is evaluated as a template)## selector:## matchLabels:## app: my-app
selector: {}## @param updateStrategy.type updateStrategy for PostgreSQL StatefulSet and its reads StatefulSets## ref: https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#update-strategies### 修改存储# 修改内容
storageClass: "rook-ceph-block"
size: 20Gi
[root@k8s-master helm]# helm install my-pg postgresql
NAME: my-pg
LAST DEPLOYED: Mon Dec 6 20:58:19 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: postgresql
CHART VERSION: 10.13.9
APP VERSION: 11.14.0
** Please be patient while the chart is being deployed **
PostgreSQL can be accessed via port 5432 on the following DNS names from within your cluster:
my-pg-postgresql.default.svc.cluster.local - Read/Write connection
To get the password for"postgres" run:
export POSTGRES_PASSWORD=$(kubectl get secret --namespace default my-pg-postgresql -ojsonpath="{.data.postgresql-password}" | base64--decode)
To connect to your database run the following command:
kubectl run my-pg-postgresql-client --rm--tty-i--restart='Never'--namespace default --image docker.io/bitnami/postgresql:11.14.0-debian-10-r0 --env="PGPASSWORD=$POSTGRES_PASSWORD"--command-- psql --host my-pg-postgresql -U postgres -d postgres -p 5432
To connect to your database from outside the cluster execute the following commands:
kubectl port-forward --namespace default svc/my-pg-postgresql 5432:5432 &
PGPASSWORD="$POSTGRES_PASSWORD" psql --host 127.0.0.1 -U postgres -d postgres -p 5432
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 查看[root@k8s-master helm]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/my-pg-postgresql-0 1/1 Running 0 53s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 111d
service/my-pg-postgresql ClusterIP 10.110.139.14 <none> 5432/TCP 54s
service/my-pg-postgresql-headless ClusterIP None <none> 5432/TCP 54s
NAME READY AGE
statefulset.apps/my-pg-postgresql 1/1 54s
[root@k8s-master helm]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
data-my-pg-postgresql-0 Bound pvc-633f7d51-c04f-40aa-9070-fd5c6381e998 20Gi RWO rook-ceph-block 62s
# 查看配置[root@k8s-master helm]# kubectl exec-it my-pg-postgresql-0 -- bash
I have no name!@my-pg-postgresql-0:/$ ls
bin boot docker-entrypoint-initdb.d etc lib media opt root sbin sys usr
bitnami dev docker-entrypoint-preinitdb.d home lib64 mnt proc run srv tmp var
I have no name!@my-pg-postgresql-0:/$ cd bitnami/
I have no name!@my-pg-postgresql-0:/bitnami$ ls
postgresql
I have no name!@my-pg-postgresql-0:/bitnami$ cd postgresql/
I have no name!@my-pg-postgresql-0:/bitnami/postgresql$ ls
data lost+found
I have no name!@my-pg-postgresql-0:/bitnami/postgresql$ cd data/
I have no name!@my-pg-postgresql-0:/bitnami/postgresql/data$ ls-l
total 88
drwx------ 5 1001 root 4096 Dec 6 08:53 base
drwx------ 2 1001 root 4096 Dec 6 08:54 global
drwx------ 2 1001 root 4096 Dec 6 08:53 pg_commit_ts
drwx------ 2 1001 root 4096 Dec 6 08:53 pg_dynshmem
-rw------- 1 1001 root 1636 Dec 6 08:53 pg_ident.conf
drwx------ 4 1001 root 4096 Dec 6 09:23 pg_logical
drwx------ 4 1001 root 4096 Dec 6 08:53 pg_multixact
drwx------ 2 1001 root 4096 Dec 6 08:53 pg_notify
drwx------ 2 1001 root 4096 Dec 6 08:53 pg_replslot
drwx------ 2 1001 root 4096 Dec 6 08:53 pg_serial
drwx------ 2 1001 root 4096 Dec 6 08:53 pg_snapshots
drwx------ 2 1001 root 4096 Dec 6 08:53 pg_stat
drwx------ 2 1001 root 4096 Dec 6 12:42 pg_stat_tmp
drwx------ 2 1001 root 4096 Dec 6 08:53 pg_subtrans
drwx------ 2 1001 root 4096 Dec 6 08:53 pg_tblspc
drwx------ 2 1001 root 4096 Dec 6 08:53 pg_twophase
-rw------- 1 1001 root 3 Dec 6 08:53 PG_VERSION
drwx------ 3 1001 root 4096 Dec 6 08:53 pg_wal
drwx------ 2 1001 root 4096 Dec 6 08:53 pg_xact
-rw------- 1 1001 root 88 Dec 6 08:53 postgresql.auto.conf
-rw------- 1 1001 root 249 Dec 6 08:53 postmaster.opts
-rw------- 1 1001 root 79 Dec 6 08:53 postmaster.pid
I have no name!@my-pg-postgresql-0:/bitnami/postgresql/data$ exit
exit[root@k8s-master helm]#
# 查看[root@k8s-master ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/my-pg-postgresql-0 1/1 Running 0 14h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/my-pg-postgresql ClusterIP 10.110.139.14 <none> 5432/TCP 14h
service/my-pg-postgresql-headless ClusterIP None <none> 5432/TCP 14h
NAME READY AGE
statefulset.apps/my-pg-postgresql 1/1 14h
# 修改service类型[root@k8s-master ~]# kubectl edit svc my-pg-postgresql
service/my-pg-postgresql edited
spec:
......
type: NodePort
[root@k8s-master ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-pg-postgresql NodePort 10.110.139.14 <none> 5432:30867/TCP 14h
my-pg-postgresql-headless ClusterIP None <none> 5432/TCP 14h
# 外部访问信息
172.51.216.81
30867
postgres
Lpm1dkJUab
2.1.6.删除PostgreSQL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 查看[root@k8s-master ~]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
my-pg default 1 2021-12-06 20:58:19.693528941 +0800 CST deployed postgresql-10.13.9 11.14.0
[root@k8s-master ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/my-pg-postgresql-0 1/1 Running 0 14h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 111d
service/my-pg-postgresql NodePort 10.110.139.14 <none> 5432:30867/TCP 14h
service/my-pg-postgresql-headless ClusterIP None <none> 5432/TCP 14h
NAME READY AGE
statefulset.apps/my-pg-postgresql 1/1 14h
[root@k8s-master ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
data-my-pg-postgresql-0 Bound pvc-633f7d51-c04f-40aa-9070-fd5c6381e998 20Gi RWO rook-ceph-block 14h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 删除[root@k8s-master ~]# helm uninstall my-pg
release "my-pg" uninstalled
[root@k8s-master ~]# kubectl delete pvc data-my-pg-postgresql-0
persistentvolumeclaim "data-my-pg-postgresql-0" deleted
# 查看[root@k8s-master ~]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
[root@k8s-master ~]# kubectl get all
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 111d
2.2.部署PostgreSQL高可用
1
2
3
4
5
6
7
8
9
[root@k8s-master ~]# helm search repo postgresql
NAME CHART VERSION APP VERSION DESCRIPTION
bitnami/postgresql-ha 8.0.3 11.14.0 Chart for PostgreSQL with HA architecture (usin...
https://github.com/bitnami/charts/tree/master/bitnami
https://github.com/bitnami/charts/tree/master/bitnami/postgresql-ha
2.2.1.下载安装包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@k8s-master helm]# helm fetch bitnami/postgresql-ha
[root@k8s-master helm]# ll
total 112
-rw-r--r-- 1 root root 54909 Dec 6 21:15 postgresql-ha-8.0.3.tgz
[root@k8s-master helm]# tar-zxf postgresql-ha-8.0.3.tgz
[root@k8s-master helm]# ll
total 112
drwxr-xr-x 5 root root 138 Dec 6 21:15 postgresql-ha
-rw-r--r-- 1 root root 54909 Dec 6 21:15 postgresql-ha-8.0.3.tgz
[root@k8s-master helm]# cd postgresql-ha
[root@k8s-master postgresql-ha]# ll
total 164
-rw-r--r-- 1 root root 220 Nov 30 08:49 Chart.lock
drwxr-xr-x 3 root root 20 Dec 6 21:15 charts
-rw-r--r-- 1 root root 731 Nov 30 08:49 Chart.yaml
drwxr-xr-x 2 root root 67 Dec 6 21:15 ci
-rw-r--r-- 1 root root 97854 Nov 30 08:49 README.md
drwxr-xr-x 4 root root 301 Dec 6 21:15 templates
-rw-r--r-- 1 root root 58220 Nov 30 08:49 values.yaml
# values.yaml
persistence:
## @param persistence.enabled Enable data persistence##
enabled: true## @param persistence.existingClaim A manually managed Persistent Volume and Claim## If defined, PVC must be created manually before volume will be bound.## All replicas will share this PVC, using existingClaim with replicas > 1 is only useful in very special use cases.## The value is evaluated as a template.##
existingClaim: ""## @param persistence.storageClass Persistent Volume Storage Class## If defined, storageClassName: <storageClass>## If set to "-", storageClassName: "", which disables dynamic provisioning## If undefined (the default) or set to null, no storageClassName spec is## set, choosing the default provisioner.##
storageClass: ""##
existingClaim: ""## @param persistence.storageClass Persistent Volume Storage Class## If defined, storageClassName: <storageClass>## If set to "-", storageClassName: "", which disables dynamic provisioning## If undefined (the default) or set to null, no storageClassName spec is## set, choosing the default provisioner.##
storageClass: "rook-ceph-block"## @param persistence.mountPath The path the volume will be mounted at, useful when using different PostgreSQL images.##
mountPath: /bitnami/postgresql
## @param persistence.accessModes List of access modes of data volume##
accessModes:
- ReadWriteOnce
## @param persistence.size Persistent Volume Claim size##
size: 30Gi
## @param persistence.annotations Persistent Volume Claim annotations##
annotations: {}## @param persistence.selector Selector to match an existing Persistent Volume (this value is evaluated as a template)## selector:## matchLabels:## app: my-app##
selector: {}## @section Traffic Exposure parameters#### PgPool service parameters### 修改存储# 修改内容
storageClass: "rook-ceph-block"
size: 30Gi
[root@k8s-master helm]# helm install pg-ha postgresql-ha
NAME: pg-ha
LAST DEPLOYED: Tue Dec 7 11:52:39 2021
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: postgresql-ha
CHART VERSION: 8.0.3
APP VERSION: 11.14.0
** Please be patient while the chart is being deployed **
PostgreSQL can be accessed through Pgpool via port 5432 on the following DNS name from within your cluster:
pg-ha-postgresql-ha-pgpool.default.svc.cluster.local
Pgpool acts as a load balancer for PostgreSQL and forward read/write connections to the primary node while read-only connections are forwarded to standby nodes.
To get the password for"postgres" run:
export POSTGRES_PASSWORD=$(kubectl get secret --namespace default pg-ha-postgresql-ha-postgresql -ojsonpath="{.data.postgresql-password}" | base64--decode)
To get the password for"repmgr" run:
export REPMGR_PASSWORD=$(kubectl get secret --namespace default pg-ha-postgresql-ha-postgresql -ojsonpath="{.data.repmgr-password}" | base64--decode)
To connect to your database run the following command:
kubectl run pg-ha-postgresql-ha-client --rm--tty-i--restart='Never'--namespace default --image docker.io/bitnami/postgresql-repmgr:11.14.0-debian-10-r9 --env="PGPASSWORD=$POSTGRES_PASSWORD"\--command-- psql -h pg-ha-postgresql-ha-pgpool -p 5432 -U postgres -d postgres
To connect to your database from outside the cluster execute the following commands:
kubectl port-forward --namespace default svc/pg-ha-postgresql-ha-pgpool 5432:5432 &
psql -h 127.0.0.1 -p 5432 -U postgres -d postgres
# 查看[root@k8s-master helm]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
pg-ha default 1 2021-12-07 11:52:39.764011376 +0800 CST deployed postgresql-ha-8.0.3 11.14.0
[root@k8s-master helm]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/pg-ha-postgresql-ha-pgpool-58d6b8ffd-krm2p 1/1 Running 7 17m
pod/pg-ha-postgresql-ha-postgresql-0 1/1 Running 0 17m
pod/pg-ha-postgresql-ha-postgresql-1 1/1 Running 0 17m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 111d
service/pg-ha-postgresql-ha-pgpool ClusterIP 10.100.19.211 <none> 5432/TCP 17m
service/pg-ha-postgresql-ha-postgresql ClusterIP 10.99.166.15 <none> 5432/TCP 17m
service/pg-ha-postgresql-ha-postgresql-headless ClusterIP None <none> 5432/TCP 17m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/pg-ha-postgresql-ha-pgpool 1/1 1 1 17m
NAME DESIRED CURRENT READY AGE
replicaset.apps/pg-ha-postgresql-ha-pgpool-58d6b8ffd 1 1 1 17m
NAME READY AGE
statefulset.apps/pg-ha-postgresql-ha-postgresql 2/2 17m
[root@k8s-master helm]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
data-pg-ha-postgresql-ha-postgresql-0 Bound pvc-d4bfd3a8-40f9-4363-ba53-04fa2d22ba9c 30Gi RWO rook-ceph-block 9m47s
data-pg-ha-postgresql-ha-postgresql-1 Bound pvc-ffaf16b4-d0c7-45aa-a230-c672b28430c8 30Gi RWO rook-ceph-block 9m47s