在 Linux x86_64 上创建一个 Postgres 数据存储
安装 Postgres 操作程序并设置数据存储器。
准备工作
确保已准备好联机和脱机主机以从外部存储库中拉取映像。 此外,请确保添加了正确的 Helm 存储库。
如需更多信息,请参阅 “准备安装数据存储 ”。
Postgres 用于部署的运营商版本和镜像标签
置顶的 Helm Chart 或操作程序版本需要以下映像。
| 平台 | 操作程序版本 | Helm Chart 版本 | 带有标记的图像 |
|---|---|---|---|
| Linux® x86_64 | 1.29.1 | 0.28.2 | artifact-public.instana.io/self-hosted-images/3rd-party/operator/cloudnative-pg: v1.29.1_v0.34.0artifact-public.instana.io/self-hosted-images/3rd-party/datastore/cnpg-containers: 15_v0.39.0 |
使用 CloudNativePG 操作符在线安装 Postgres
请按照以下步骤在在线环境中安装 Postgres 数据存储。
创建
instana-postgres名称空间。kubectl create namespace instana-postgres为
instana-postgres名称空间创建映像拉取私钥。kubectl create secret docker-registry instana-registry --namespace instana-postgres \ --docker-username=_ \ --docker-password=<download_key> \ --docker-server=artifact-public.instana.io如果您要在 Red Hat® OpenShift® 集群上安装 Postgres ,请从命名空间中确定文件系统组 ID
instana-postgres。 Red Hat OpenShift 要求文件系统组必须位于该命名空间特有的数值范围内。kubectl get namespace instana-postgres -o yaml对于该命令,将显示类似于以下示例的输出:
apiVersion: v1 kind: Namespace metadata: annotations: ....... openshift.io/sa.scc.uid-range: 1000750000/10000 creationTimestamp: "2024-01-14T07:04:59Z" labels: kubernetes.io/metadata.name: instana-postgres ....... name: instana-postgresopenshift.io/sa.scc.supplemental-groups注释包含允许的标识范围。 范围1000750000/10000指示以标识1000750000开头的 10,000 个值,因此它指定从1000750000到1000760000的标识范围。 在此示例中,值1000750000可用作文件系统组标识 (UID)。安装 Postgres 操作程序。 使用上一步中的 UID 作为以下命令中的
<UID from namespace>:helm install cnpg instana/cloudnative-pg --set image.repository=artifact-public.instana.io/self-hosted-images/3rd-party/operator/cloudnative-pg --set image.tag=v1.29.1_v0.34.0 --version=0.28.2 --set imagePullSecrets[0].name=instana-registry --set containerSecurityContext.runAsUser=<UID from namespace> --set containerSecurityContext.runAsGroup=<UID from namespace> -n instana-postgres在 base64中生成密码。 记下密码。 稍后需要将此文件存储在
config.yaml文件中。openssl rand -base64 24 | tr -cd 'a-zA-Z0-9' | head -c32; echo使用先前命令中获取的密码创建类型为
Secret的资源。kind: Secret apiVersion: v1 metadata: name: instanaadmin type: Opaque stringData: username: instanaadmin password: <user-generate-password>创建 Postgres 私钥。
kubectl apply -f postgres-secret.yaml -n instana-postgres创建一个名为 YAML 的文件,例如
postgres.yaml,其中包含 Postgres 集群的配置。apiVersion: postgresql.cnpg.io/v1 kind: Cluster metadata: name: postgres spec: # Add the following Affinity snippet to schedule each pod on a different node. # affinity: # podAntiAffinityType: required instances: 3 imageName: artifact-public.instana.io/self-hosted-images/3rd-party/datastore/cnpg-containers:15_v0.39.0 imagePullPolicy: IfNotPresent imagePullSecrets: - name: instana-registry postgresql: parameters: shared_buffers: 32MB pg_stat_statements.track: all auto_explain.log_min_duration: '10s' pg_hba: - local all all trust - host all all 0.0.0.0/0 md5 - local replication standby trust - hostssl replication standby all md5 - hostnossl all all all reject - hostssl all all all md5 managed: roles: - name: instanaadmin login: true superuser: true createdb: true createrole: true passwordSecret: name: instanaadmin bootstrap: initdb: database: instanaadmin owner: instanaadmin secret: name: instanaadmin superuserSecret: name: instanaadmin storage: size: 1Gi # storageClass: "Optional"部署 Postgres 集群。
kubectl apply -f postgres.yaml -n instana-postgres完成 部署和验证 Postgres (联机和脱机)中的步骤。
使用 CloudNative PG 操作员离线安装 Postgres
在脱机环境中安装 Postgres 操作程序。
如果在 准备安装时尚未从外部注册表中拉取 Postgres 映像,那么可以立即拉取这些映像。 在防御主机上运行以下命令。 然后,将这些图像复制到位于物理隔离环境中的 Instana 主机上。
docker pull artifact-public.instana.io/self-hosted-images/3rd-party/operator/cloudnative-pg:v1.29.1_v0.34.0
docker pull artifact-public.instana.io/self-hosted-images/3rd-party/datastore/cnpg-containers:15_v0.39.0
请在您的 Instana 主机上完成以下步骤。
将映像重新标记到内部映像注册表。
docker tag artifact-public.instana.io/self-hosted-images/3rd-party/operator/cloudnative-pg:v1.29.1_v0.34.0 <internal-image-registry>/operator/cloudnative-pg:v1.29.1_v0.34.0 docker tag artifact-public.instana.io/self-hosted-images/3rd-party/datastore/cnpg-containers:15_v0.39.0 <internal-image-registry>/datastore/cnpg-containers:15_v0.39.0将映像推送到防御主机上的内部映像注册表。
docker push <internal-image-registry>/operator/cloudnative-pg:v1.29.1_v0.34.0 docker push <internal-image-registry>/datastore/cnpg-containers:15_v0.39.0创建
instana-postgres名称空间。kubectl create namespace instana-postgres可选:如果内部图像注册表需要身份验证,请创建图像拉取密钥。
kubectl create secret docker-registry <secret_name> --namespace instana-postgres \ --docker-username=<registry_username> \ --docker-password=<registry_password> \ --docker-server=<internal-image-registry>:<internal-image-registry-port> \ --docker-email=<registry_email>如果您要在 Red Hat® OpenShift® 集群上安装 Postgres ,请从命名空间中确定文件系统组 ID
instana-postgres。 Red Hat OpenShift 要求文件系统组必须位于该命名空间特有的数值范围内。kubectl get namespace instana-postgres -o yaml对于该命令,将显示类似于以下示例的输出:
apiVersion: v1 kind: Namespace metadata: annotations: ....... openshift.io/sa.scc.uid-range: 1000750000/10000 creationTimestamp: "2024-01-14T07:04:59Z" labels: kubernetes.io/metadata.name: instana-postgres ....... name: instana-postgresopenshift.io/sa.scc.supplemental-groups注释包含允许的标识范围。 范围1000750000/10000指示以标识1000750000开头的 10,000 个值,因此它指定从1000750000到1000760000的标识范围。 在此示例中,值1000750000可用作文件系统组标识 (UID)。安装 Postgres 操作程序。 在以下命令中,请将 <download_key> 替换为您自己的代理密钥。 如果您为内部注册表创建了图像拉取密钥,请在以下命令中添加
--set image.imagePullSecrets[0].name="<internal-image-registry-pull-secret>。Red Hat OpenShift 集群
将上一步中的 UID 用作以下命令中的
<UID from namespace>:helm install cnpg cloudnative-pg-0.28.2.tgz --set image.repository=image-registry.openshift-image-registry.svc:5000/instana-postgres/cloudnative-pg-operator --set image.tag=v1.29.1_v0.34.0 --version=0.28.2 --set containerSecurityContext.runAsUser=<UID from namespace> --set containerSecurityContext.runAsGroup=<UID from namespace> -n instana-postgresKubernetes 集群
helm install cnpg cloudnative-pg-0.28.2.tgz --set image.repository=<internal-image-registry>/operator/cloudnative-pg --set image.tag=v1.29.1_v0.34.0 --version=0.28.2 -n instana-postgres
在 base64中生成密码。 记下密码。 稍后需要将此文件存储在
config.yaml文件中。openssl rand -base64 24 | tr -cd 'a-zA-Z0-9' | head -c32; echo使用先前命令中获取的密码创建类型为
Secret的资源。kind: Secret apiVersion: v1 metadata: name: instanaadmin type: Opaque stringData: username: instanaadmin password: <user-generate-password>创建 Postgres 私钥。
kubectl apply -f postgres-secret.yaml -n instana-postgres创建一个名为 YAML 的文件,例如
postgres.yaml,其中包含 Postgres 集群的配置。apiVersion: postgresql.cnpg.io/v1 kind: Cluster metadata: name: postgres spec: # Add the following Affinity snippet to schedule each pod on a different node. # affinity: # podAntiAffinityType: required instances: 3 imageName: <internal-image-registry>/datastore/cnpg-containers:15_v0.39.0 imagePullPolicy: IfNotPresent # Optional: if you created an image pull secret for your internal registry, uncomment the following lines and update the image pull secret information. # imagePullSecrets: # - name: <internal-image-registry-pull-secret> postgresql: parameters: shared_buffers: 32MB pg_stat_statements.track: all auto_explain.log_min_duration: '10s' pg_hba: - local all all trust - host all all 0.0.0.0/0 md5 - local replication standby trust - hostssl replication standby all md5 - hostnossl all all all reject - hostssl all all all md5 managed: roles: - name: instanaadmin login: true superuser: true createdb: true createrole: true passwordSecret: name: instanaadmin bootstrap: initdb: database: instanaadmin owner: instanaadmin secret: name: instanaadmin superuserSecret: name: instanaadmin storage: size: 1Gi # storageClass: "Optional"- 部署 Postgres 集群。
kubectl apply -f postgres.yaml -n instana-postgres - 完成 部署和验证 Postgres (联机和脱机)中的步骤。
部署和验证 Postgres (在线和离线)
完成以下步骤以部署 Postgres 实例并创建数据存储器。
将先前生成的密码存储在
config.yaml文件中。datastoreConfigs: ... postgresConfigs: - user: instanaadmin password: <USER_GENERATED_PASSWORD> adminUser: instanaadmin adminPassword: <USER_GENERATED_PASSWORD> ...创建
postgresql资源。kubectl apply -f postgres.yaml --namespace=instana-postgres验证PostgresOperator 部署。
kubectl get all -n instana-postgres如果成功部署了 Postgres 操作程序,那么该命令的结果如下所示:
NAME READY STATUS RESTARTS AGE pod/postgres-1 1/1 Running 0 100s pod/postgres-2 1/1 Running 0 69s pod/postgres-3 1/1 Running 0 41s pod/cnpg-cloudnative-pg-64bbc87958-fqnrl 1/1 Running 0 11m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT service/cnpg-webhook-service ClusterIP 172.30.66.183 <none> 443/TCP service/postgres-r ClusterIP 172.30.163.146 <none> 5432/TCP service/postgres-ro ClusterIP 172.30.226.75 <none> 5432/TCP service/postgres-rw ClusterIP 172.30.235.178 <none> 5432/TCP NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/cnpg-cloudnative-p 1/1 1 1 11m NAME DESIRED CURRENT READY AGE replicaset.apps/cnpg-cloudnative-pg-64bbc87958 1 1 1 11m
将数据从 Zalando 迁移至 CloudNativePG
通过使用 pg_basebackup 引导程序方式,您可以创建新的 PostgreSQL 集群 (目标) ,以复制现有 PostgreSQL 实例 (源) 的准确物理状态。
您可以通过有效的流式复制连接并通过将源 PostgreSQL 实例用作主服务器或备用 PostgreSQL 服务器,从实时集群进行引导。
若要通过引导 pg_basebackup 模式将数据从 Zalando PostgreSQL 集群迁移到 CloudNativePG 副本集群,请参阅 《将数据从 Zalando 迁移到 CloudNativePG 》。