Skip to content

[Feature]: Enable replica mode on an existing cluster #3796

Closed

Description

@gbartolini

Is there an existing issue already for this feature request/idea?

  • I have searched for an existing issue, and could not find anything. I believe this is a new feature request to be evaluated.

What problem is this feature going to solve? Why should it be added?

Right now, if you try and put an existing cluster previously bootstrapped with initdb in replica mode, you are immediately blocked.

The Cluster "pg1" is invalid:
* spec.replicaCluster: Invalid value: v1.ReplicaClusterConfiguration{Source:"pg2", Enabled:true}: replica mode is compatible only with bootstrap using pg_basebackup or recovery
* spec.replicaCluster: Invalid value: v1.ReplicaClusterConfiguration{Source:"pg2", Enabled:true}: Can not enable replication on existing clusters

This behaviour prevents a cluster from being properly shut down and for example become a follower of another cluster sharing the same transactional continuum.

Describe the solution you'd like

We should enable managing the replica stanza on any cluster, no matter how they were initially bootstrapped.

For example, consider the following pg1.yaml file:

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: pg1
spec:
  imageName: ghcr.io/cloudnative-pg/postgresql:16
  instances: 1

  storage:
    size: 1Gi

  postgresql:
    pg_hba:
    - hostssl replication streaming-replica 0.0.0.0/0 cert

  externalClusters:
  - name: pg2
    connectionParameters:
      host: pg2-rw.default.svc
      user: streaming_replica
      sslmode: verify-full
      dbname: postgres
    sslKey:
      name: pg2-replication
      key: tls.key
    sslCert:
      name: pg2-replication
      key: tls.crt
    sslRootCert:
      name: pg2-ca
      key: ca.crt

Run kubectl apply -f pg1.yaml.

Then, look at the pg2.yaml one:

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: pg2
spec:
  imageName: ghcr.io/cloudnative-pg/postgresql:16
  instances: 1

  storage:
    size: 1Gi

  bootstrap:
    pg_basebackup:
      source: pg1

  postgresql:
    pg_hba:
    - hostssl replication streaming-replica 0.0.0.0/0 cert

  replica:
    enabled: true
    source: pg1

  externalClusters:
  - name: pg1
    connectionParameters:
      host: pg1-rw.default.svc
      user: streaming_replica
      dbname: postgres
      sslmode: verify-full
    sslKey:
      name: pg1-replication
      key: tls.key
    sslCert:
      name: pg1-replication
      key: tls.crt
    sslRootCert:
      name: pg1-ca
      key: ca.crt

Run kubectl apply -f pg2.yaml and ensure that the two clusters are up.

Now run kubect edit cluster pg1 and add:

  replica:
    enabled: true
    source: pg2

Instead of blocking the operation, we should:

  • detect the change and acknowledge the request to become a replica cluster
  • properly fence the instance
  • reconfigure replication (using the pg2 external cluster definition)
  • restart the instance

In case this instance has standbys, the instance should work as a standard designated primary.

Describe alternatives you've considered

N/A

Additional context

This is an intermediate step to enhance switchover operations involving replica clusters.

Backport?

No

Are you willing to actively contribute to this feature?

Yes

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added
    triage
    on Feb 7, 2024
  2. self-assigned this
    on Feb 7, 2024
  3. added
    enhancement 🪄
    do not backport
    and removed
    do not backport
    on Feb 7, 2024
  4. added this to the 1.23.0 milestone on Feb 7, 2024
  5. gbartolini commented on Feb 26, 2024

    @gbartolini
    ContributorAuthor
  6. removed
    triage
    on Feb 26, 2024
  7. modified the milestones: 1.23.0, 1.24.0 on Apr 15, 2024
  8. added a commit that references this issue on Apr 15, 2024
    8c506d9
  9. added a commit that references this issue on Apr 24, 2024
    3b4942b
  10. modified the milestones: 1.24.0, 1.23.0 on Apr 24, 2024
  11. added a commit that references this issue on Apr 24, 2024
    ebd476b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement 🪄

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions