快速上手Zalando Postgres Operator,你會(huì)了嗎?
前提條件
由于 Postgres Operator 是為 Kubernetes (K8s) 框架設(shè)計(jì)的,因此首先設(shè)置它。對(duì)于本地測(cè)試,我們建議使用以下解決方案之一:
- minikube, 在 VM 內(nèi)創(chuàng)建單節(jié)點(diǎn) K8s 集群(需要 KVM 或 VirtualBox)
https://github.com/kubernetes/minikube/releases
- kind 和 k3d, 允許創(chuàng)建在 Docker 上運(yùn)行的多節(jié)點(diǎn) K8s 集群(需要 Docker)
https://kind.sigs.k8s.io/
https://k3d.io
要與 K8s 基礎(chǔ)設(shè)施交互,請(qǐng)安裝其 CLI 運(yùn)行時(shí) kubectl。
https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-kubectl-binary-via-curl
本快速入門(mén)假設(shè)您已經(jīng)啟動(dòng)了 minikube 或創(chuàng)建了本地 kind 集群。請(qǐng)注意,您還可以使用 Docker Desktop for Mac 中內(nèi)置的 K8s 支持來(lái)遵循本教程的步驟。您必須將 minikube start 和 minikube delete 替換為您的啟動(dòng)操作,以獲得 Docker 內(nèi)置的 K8s 支持。
配置選項(xiàng)
只能在部署新的 Postgres 集群之前配置 Postgres Operator。這可以通過(guò)兩種方式工作:通過(guò) ConfigMap 或自定義 OperatorConfiguration 對(duì)象。有關(guān)配置的更多詳細(xì)信息,請(qǐng)參見(jiàn)此處。
- https://postgres-operator.readthedocs.io/en/latest/reference/operator_parameters/
部署選項(xiàng)
Postgres Operator 可以通過(guò)以下方式部署:
- Manual deployment
- Kustomization
- Helm chart
Kubernetes 上的手動(dòng)部署設(shè)置
只需應(yīng)用 yaml 清單即可安裝 Postgres Operator。注意,我們提供的 /manifests 目錄僅作為示例;您應(yīng)該考慮將清單調(diào)整為您的 K8s 環(huán)境(例如:命名空間)。
# 首先,克隆存儲(chǔ)庫(kù)并切換到目錄
git clone https://github.com/zalando/postgres-operator.git
cd postgres-operator
# 按以下順序應(yīng)用清單
kubectl create -f manifests/configmap.yaml # 配置
kubectl create -f manifests/operator-service-account-rbac.yaml # 身份和權(quán)限
kubectl create -f manifests/postgres-operator.yaml # 部署
kubectl create -f manifests/api-service.yaml # 通過(guò) UI 使用 operator API
有一個(gè) Kustomization manifest 結(jié)合了上述資源(CRD 除外)— 它可以與 kubectl 1.14 或更高版本一起使用,如下所示:
- https://github.com/kubernetes-sigs/kustomize
- https://github.com/zalando/postgres-operator/blob/master/manifests/kustomization.yaml
kubectl apply -k github.com/zalando/postgres-operator/manifests
為方便起見(jiàn),我們使用 run_operator_locally 腳本通過(guò) minikube 自動(dòng)啟動(dòng) operator。它應(yīng)用 acid-minimal-cluster。清單。
- https://github.com/zalando/postgres-operator/blob/master/manifests/minimal-postgres-manifest.yaml
./run_operator_locally.sh
OpenShift 上的手動(dòng)部署設(shè)置
要在 OpenShift 中安裝 Postgres Operator,您必須將配置參數(shù) kubernetes_use_configmaps 更改為 "true"。否則,operator 和 Patroni 將在 OpenShift 不支持的 Endpoints 中存儲(chǔ) leader 和配置 keys。對(duì)于 postgres-operator 和 postgres-pod 集群角色,這還需要一組稍微不同的規(guī)則。
oc create -f manifests/operator-service-account-rbac-openshift.yaml
Helm chart
或者,可以使用提供的 Helm chart 安裝 operator,從而為您節(jié)省手動(dòng)步驟??寺〈?repo 并將目錄更改為 repo 根目錄。安裝 Helm v3 后,您應(yīng)該能夠運(yùn)行:
- https://helm.sh/
helm install postgres-operator ./charts/postgres-operator
該 chart 適用于 Helm 2 和 Helm 3。使用 v3 時(shí),將跳過(guò) v2 中的 crd-install hook 并發(fā)出警告。使用 Helm 2 安裝應(yīng)用程序的文檔可以在 v2 文檔中找到。
- https://v2.helm.sh/docs/
該 chart 還托管在:
- https://opensource.zalando.com/postgres-operator/charts/postgres-operator/
檢查 Postgres Operator 是否正在運(yùn)行
啟動(dòng) operator 可能需要幾秒鐘。在應(yīng)用 Postgres 集群清單之前檢查 operator pod 是否正在運(yùn)行。
# 如果您使用 yaml 清單創(chuàng)建了 operator
kubectl get pod -l name=postgres-operator
# 如果您使用 helm chart 創(chuàng)建了 operator
kubectl get pod -l app.kubernetes.io/name=postgres-operator
如果 operator 沒(méi)有進(jìn)入 Running 狀態(tài),請(qǐng)使用 kubectl describe 檢查 deployment 或 pod 的最新 K8s 事件或檢查 operator 日志:
kubectl logs "$(kubectl get pod -l name=postgres-operator
--output='name')"
部署 operator UI
在以下段落中,我們將描述如何使用 kubectl 從命令行訪問(wèn)和管理 PostgreSQL 集群。但也可以通過(guò)基于瀏覽器的 Postgres Operator UI 來(lái)完成。在部署 UI 之前,請(qǐng)確保 operator 正在運(yùn)行,并且可以通過(guò) K8s service 訪問(wèn)其 REST API。此 API 的 URL 必須在 UI 的部署清單中配置。
- https://postgres-operator.readthedocs.io/en/latest/operator-ui/
- https://github.com/zalando/postgres-operator/blob/master/manifests/api-service.yaml
- https://github.com/zalando/postgres-operator/blob/master/ui/manifests/deployment.yaml#L43
要部署 UI,只需應(yīng)用其所有清單文件或使用 UI helm chart:
# 手動(dòng)部署
kubectl apply -f ui/manifests/
# 或 kustomization
kubectl apply -k github.com/zalando/postgres-operator/ui/manifests
# 或 helm chart
helm install postgres-operator-ui ./charts/postgres-operator-ui
與 operator 一樣,檢查 UI pod 是否進(jìn)入 Running 狀態(tài):
# 如果您使用 yaml 清單創(chuàng)建了 operator
kubectl get pod -l name=postgres-operator-ui
# 如果您使用 helm chart 創(chuàng)建了 operator
kubectl get pod -l app.kubernetes.io/name=postgres-operator-ui
您現(xiàn)在可以通過(guò)端口轉(zhuǎn)發(fā) UI pod(注意 label selector)訪問(wèn) Web 界面,并在瀏覽器中輸入 localhost:8081:
kubectl port-forward svc/postgres-operator-ui 8081:80
UI 文檔中詳細(xì)解釋了可用選項(xiàng)。
https://postgres-operator.readthedocs.io/en/latest/operator-ui/
創(chuàng)建 Postgres cluster
如果 operator pod 正在運(yùn)行,它會(huì)偵聽(tīng)有關(guān) postgresql 資源的新事件?,F(xiàn)在,是時(shí)候提交您的第一個(gè) Postgres 集群清單了。
# 創(chuàng)建一個(gè) Postgres cluster
kubectl create -f manifests/minimal-postgres-manifest.yaml
在 cluster 清單提交并通過(guò)驗(yàn)證后,operator 將創(chuàng)建 Service 和 Endpoint 資源以及一個(gè) StatefulSet,它根據(jù)清單中指定的實(shí)例數(shù)量啟動(dòng)新的 Pod。所有資源都像 cluster 一樣命名。數(shù)據(jù)庫(kù) pod 可以通過(guò)它們的數(shù)字后綴來(lái)識(shí)別,從 -0 開(kāi)始。他們運(yùn)行 Zalando 的 Spilo 容器鏡像。至于 services 和 endpoints,將有一個(gè)用于 master pod,另一個(gè)用于所有副本(-repl 后綴)。檢查是否所有組件都出現(xiàn)了。使用標(biāo)簽 application=spilo 過(guò)濾并列出標(biāo)簽 spilo-role 以查看當(dāng)前誰(shuí)是 master。
# 檢查部署的 cluster
kubectl get postgresql
# 檢查創(chuàng)建的 database pod
kubectl get pods -l application=spilo -L spilo-role
# 檢查創(chuàng)建的 service 資源
kubectl get svc -l application=spilo -L spilo-role
通過(guò) psql 連接到 Postgres cluster
您可以在 database pod 上創(chuàng)建端口轉(zhuǎn)發(fā)以連接到 Postgres。有關(guān)說(shuō)明,請(qǐng)參閱用戶(hù)指南。使用 minikube 也很容易從指向 master pod 的 K8s service 中檢索連接字符串:
export HOST_PORT=$(minikube service acid-minimal-cluster --url | sed 's,.*/,,')
export PGHOST=$(echo $HOST_PORT | cut -d: -f 1)
export PGPORT=$(echo $HOST_PORT | cut -d: -f 2)
從 cluster 中創(chuàng)建的 K8s Secret 中檢索密碼。默認(rèn)情況下拒絕非加密連接,因此將 SSL 模式設(shè)置為 require:
export PGPASSWORD=$(kubectl get secret postgres.acid-minimal-cluster.credentials -o 'jsonpath={.data.password}' | base64 -d)
export PGSSLMODE=require
psql -U postgres
刪除 Postgres cluster
要?jiǎng)h除 Postgres 集群,只需刪除 postgresql 自定義資源。
kubectl delete postgresql acid-minimal-cluster
這應(yīng)該刪除關(guān)聯(lián)的 StatefulSet、database Pods、Services 和 Endpoints。PersistentVolume 被釋放,PodDisruptionBudget 被刪除。但是,Secrets 不會(huì)被刪除,并且備份將保留在原位。
在集群仍在啟動(dòng)或在該階段卡住時(shí)刪除集群時(shí),可能會(huì)刪除 postgresql 資源,留下孤立的組件。在創(chuàng)建新的 Postgres 集群時(shí),這可能會(huì)導(dǎo)致麻煩。對(duì)于全新的設(shè)置,您可以刪除本地 minikube 或 kind 集群并重新開(kāi)始。
https://github.com/zalando/postgres-operator/issues/551