「Kubernetes」- 部署 PostgreSQL 数据库



该笔记将记录:在 Kubernetes Cluser 中,如何部署 PostgreSQL 数据库,以及相关问题的解决办法;


# helm repo add bitnami https://charts.bitnami.com/bitnami
# helm repo update

# helm pull bitnami/postgresql                                                  # CHART 11.6.18, APP 14.4.0
# helm show values ./postgresql-11.6.18.tgz > postgresql-11.6.18.helm-values.yaml
...(2)修改 postgres 密码:global.postgresql.auth.postgresPassword

# helm --namespace infra-database                                              \
    install postgresql ./postgresql-11.6.18.tgz                                \
    -f postgresql-11.6.18.helm-values.yaml                                     \

# helm --namespace infra-database                                              \
    upgrade postgresql ./postgresql-11.6.18.tgz                                \
    -f postgresql-11.6.18.helm-values.yaml

// ---------------------------------------------------------------------------- // 当执行成功后,产生如下输出:
PostgreSQL can be accessed via port 5432 on the following DNS names from within your cluster:

    postgresql.infra-database.svc.cluster.local - Read/Write connection

To get the password for "postgres" run:

    export POSTGRES_PASSWORD=$(kubectl get secret --namespace infra-database postgresql -o jsonpath="{.data.postgres-password}" | base64 -d)

To connect to your database run the following command:

    kubectl run postgresql-client --rm --tty -i --restart='Never' --namespace infra-database --image docker.io/bitnami/postgresql:14.4.0-debian-11-r12 --env="PGPASSWORD=$POSTGRES_PASSWORD" \
      --command -- psql --host postgresql -U postgres -d postgres -p 5432

    > NOTE: If you access the container using bash, make sure that you execute "/opt/bitnami/scripts/postgresql/entrypoint.sh /bin/bash" in order to avoid the error "psql: local user with ID 1001} does not exist"

To connect to your database from outside the cluster execute the following commands:

  NOTE: It may take a few minutes for the LoadBalancer IP to be available.
        Watch the status with: 'kubectl get svc --namespace infra-database -w postgresql'

    export SERVICE_IP=$(kubectl get svc --namespace infra-database postgresql --template "{{ range (index .status.loadBalancer.ingress 0) }}{{ . }}{{ end }}")
    PGPASSWORD="$POSTGRES_PASSWORD" psql --host $SERVICE_IP --port 5432 -U postgres -d postgres