如何在 Google Cloud 上的 Kubernetes 中备份 Postgres 数据库?

2024-04-01

备份运行于其上的 Postgres 数据库的最佳实践是什么谷歌云容器引擎 https://cloud.google.com/container-engine/?

我的想法是努力将备份存储在谷歌云存储 https://cloud.google.com/storage/,但我不确定如何将磁盘/Pod 连接到存储桶。

我使用以下配置在 Kubernetes 集群中运行 Postgres:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: postgres-deployment
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - image: postgres:9.6.2-alpine
          imagePullPolicy: IfNotPresent
          env:
            - name: PGDATA
              value: /var/lib/postgresql/data
            - name: POSTGRES_DB
              value: my-database-name
            - name: POSTGRES_PASSWORD
              value: my-password
            - name: POSTGRES_USER
              value: my-database-user
          name: postgres-container
          ports:
            - containerPort: 5432
          volumeMounts:
            - mountPath: /var/lib/postgresql
              name: my-postgres-volume
      volumes:
        - gcePersistentDisk:
            fsType: ext4
            pdName: my-postgres-disk
          name: my-postgres-volume

我试图创建一个Job https://kubernetes.io/docs/user-guide/jobs/运行备份:

apiVersion: batch/v1
kind: Job
metadata:
  name: postgres-dump-job
spec:
  template:
    metadata:
      labels:
        app: postgres-dump
    spec:
      containers:
        - command:
            - pg_dump
            - my-database-name
          # `env` value matches `env` from previous configuration.
          image: postgres:9.6.2-alpine
          imagePullPolicy: IfNotPresent
          name: my-postgres-dump-container
          volumeMounts:
            - mountPath: /var/lib/postgresql
              name: my-postgres-volume
              readOnly: true
      restartPolicy: Never
      volumes:
        - gcePersistentDisk:
            fsType: ext4
            pdName: my-postgres-disk
          name: my-postgres-volume

(据我了解)这应该运行pg_dump https://www.postgresql.org/docs/current/static/backup-dump.html命令并将备份数据输出到标准输出(它应该出现在kubectl logs).

顺便说一句,当我检查 Pod 时(使用kubectl get pods),它显示 Pod 永远不会脱离“待处理”状态,我认为这是因为没有足够的资源来启动作业。

将这个过程作为作业运行是否正确? 如何将作业连接到 Google Cloud Storage? 或者我应该做一些完全不同的事情?

我猜逃跑是不明智的pg_dump在数据库容器中(带有kubectl exec)由于性能受到影响,但也许这在开发/登台服务器中没问题?


正如 @Marco Lamina 所说,你可以在 postgres pod 上运行 pg_dump

DUMP
// pod-name         name of the postgres pod
// postgres-user    database user that is able to access the database
// database-name    name of the database
kubectl exec [pod-name] -- bash -c "pg_dump -U [postgres-user] [database-name]" > database.sql


RESTORE
// pod-name         name of the postgres pod
// postgres-user    database user that is able to access the database
// database-name    name of the database
cat database.sql | kubectl exec -i [pod-name] -- psql -U [postgres-user] -d [database-name]

您可以拥有一个运行此命令并将其导出到文件存储系统(例如 AWS s3)的作业 Pod。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Google Cloud 上的 Kubernetes 中备份 Postgres 数据库? 的相关文章

随机推荐