github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/docs/user_docs/kubeblocks-for-postgresql/cluster-management/switchover.md (about)

     1  ---
     2  title: Switch over a PostgreSQL cluster
     3  description: How to switch over a PostgreSQL cluster
     4  keywords: [postgresql, switch over a cluster, switchover]
     5  sidebar_position: 6
     6  sidebar_label: Switchover
     7  ---
     8  
     9  import Tabs from '@theme/Tabs';
    10  import TabItem from '@theme/TabItem';
    11  
    12  # Switch over a PostgreSQL cluster
    13  
    14  You can initiate a switchover for a PostgreSQL Replication Cluster by executing the kbcli or kubectl command. Then KubeBlocks modifies the instance roles.
    15  
    16  ## Before you start
    17  
    18  * Make sure the cluster is running normally.
    19  * Check whether the following role probe parameters exist to verify whether the role probe is enabled.
    20  
    21     ```bash
    22     kubectl get cd postgresql -o yaml
    23     >
    24     probes:
    25       roleProbe:
    26         failureThreshold: 3
    27         periodSeconds: 2
    28         timeoutSeconds: 1
    29     ```
    30  
    31  ## Initiate the switchover
    32  
    33  You can switch over a secondary of a PostgreSQL PrimaeySecondary database to the primary role, and the former primary instance to a secondary.
    34  
    35  <Tabs>
    36  
    37  <TabItem value="kbcli" label="kbcli" default>
    38  
    39  * Switchover with no primary instance specified
    40  
    41      ```bash
    42      kbcli cluster promote mycluster
    43      ```
    44  
    45  * Switchover with a specified new primary instance
    46  
    47      ```bash
    48      kbcli cluster promote mycluster --instance='mycluster-postgresql-2'
    49      ```
    50  
    51  * If there are multiple components, you can use `--component` to specify a component.
    52  
    53      ```bash
    54      kbcli cluster promote mycluster --instance='mycluster-postgresql-2' --component='postgresql'
    55      ```
    56  
    57  </TabItem>
    58  
    59  <TabItem value="kubectl" label="kubectl">
    60  
    61  The value of `instanceName` decides whether a new primary instance is specified for the switchover.
    62  
    63  * Switchover with no specified primary instance
    64  
    65    ```yaml
    66    kubectl apply -f -<<EOF
    67    apiVersion: apps.kubeblocks.io/v1alpha1
    68    kind: OpsRequest
    69    metadata:
    70      name: mycluster-switchover-jhkgl
    71    spec:
    72      clusterRef: mycluster
    73      type: Switchover
    74      switchover:
    75      - componentName: postgresql
    76        instanceName: '*'
    77    >>
    78    ```
    79  
    80  * Switchover with a specified new primary instance
    81  
    82    ```yaml
    83    kubectl apply -f -<<EOF
    84    apiVersion: apps.kubeblocks.io/v1alpha1
    85    kind: OpsRequest
    86    metadata:
    87      name: mycluster-switchover-jhkgl
    88    spec:
    89      clusterRef: mycluster
    90      type: Switchover
    91      switchover:
    92      - componentName: postgresql
    93        instanceName: 'mycluster-postgresql-2'
    94    >>
    95    ```
    96  
    97  </TabItem>
    98  
    99  </Tabs>
   100  
   101  ## Verify the switchover
   102  
   103  Check the instance status to verify whether the switchover is performed successfully.
   104  
   105  ```bash
   106  kbcli cluster list-instances
   107  ```
   108  
   109  ## Handle an exception
   110  
   111  If an error occurs, refer to [Handle an exception](./../../handle-an-exception/handle-a-cluster-exception.md) to troubleshoot the operation.