github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/docs/user_docs/kubeblocks-for-postgresql/high-availability/high-availability.md (about) 1 --- 2 title: High Availability for PostgreSQL 3 description: High availability for a PostgreSQL cluster 4 keywords: [postgresql, high availability] 5 sidebar_position: 1 6 --- 7 8 # High availability 9 10 KubeBlocks integrates [the open-source Patroni solution](https://patroni.readthedocs.io/en/latest/) to realize high availability and adopts Noop as the switch policy. 11 12 ## Before you start 13 14 * [Install kbcli](./../../installation/install-with-kbcli/install-kbcli.md). 15 * Install KubeBlocks: You can install KubeBlocks by [kbcli](./../../installation/install-with-kbcli/install-kubeblocks-with-kbcli.md) or by [Helm](./../../installation/install-with-helm/install-kubeblocks-with-helm.md). 16 * [Create a PostgreSQL Replication Cluster](./../cluster-management/create-and-connect-a-postgresql-cluster.md#create-a-postgresql-cluster). 17 * Check the Switch Policy and the role probe. 18 * Check whether the switch policy is `Noop`. 19 20 ```bash 21 kubectl get cluster pg-cluster -o yaml 22 > 23 spec: 24 componentSpecs: 25 - name: postgresql 26 componentDefRef: postgresql 27 switchPolicy: 28 type: Noop 29 ``` 30 31 * Check whether the following role probe parameters exist to verify the role probe is enabled. 32 33 ```bash 34 kubectl get cd postgresql -o yaml 35 > 36 probes: 37 roleProbe: 38 failureThreshold: 3 39 periodSeconds: 2 40 timeoutSeconds: 1 41 ``` 42 43 ## Steps 44 45 1. View the initial status of the PostgreSQL cluster. 46 47 ```bash 48 kbcli cluster describe pg-cluster 49 ``` 50 51  52 53 Currently, `pg-cluster-postgresql-0` is the primary pod and `pg-cluster-postgresql-1` is the secondary pod. 54 55 2. Simulate a primary pod exception. 56 57 ```bash 58 # Enter the primary pod 59 kubectl exec -it pg-cluster-postgresql-0 -- bash 60 61 # Delete the data directory of PostgreSQL to simulate an exception 62 root@postgres-postgresql-0:/home/postgres# rm -fr /home/postgres/pgdata/pgroot/data 63 ``` 64 65 3. View logs to observe how the roles of pods switch when an exception occurs. 66 67 ```bash 68 # View the primary pod logs 69 kubectl logs pg-cluster-postgresql-0 70 ``` 71 72 In the logs, the leader lock is released from the primary pod and an HA switch occurs. 73 74 ```bash 75 2023-04-18 08:06:52,338 INFO: Lock owner: pg-cluster-postgresql-0; I am pg-cluster-postgresql-0 76 2023-04-18 08:06:52,460 INFO: Leader key released 77 2023-04-18 08:06:52,552 INFO: released leader key voluntarily as data dir empty and currently leader 78 2023-04-18 08:06:52,553 INFO: Lock owner: pg-cluster-postgresql-1; I am pg-cluster-postgresql-0 79 2023-04-18 08:06:52,553 INFO: trying to bootstrap from leader 'pg-cluster-postgresql-1' 80 ``` 81 82 ```bash 83 # View secondary pod logs 84 kubectl logs pg-cluster-postgresql-1 85 ``` 86 87 In the logs, the original secondary pod has obtained the lock and become the leader. 88 89 ```bash 90 2023-04-18 08:07:14,441 INFO: no action. I am (pg-cluster-postgresql-1), the leader with the lock 91 2023-04-18 08:07:24,354 INFO: no action. I am (pg-cluster-postgresql-1), the leader with the lock 92 ``` 93 94 4. Connect to the PostgreSQL cluster to view the replication information. 95 96 ```bash 97 kbcli cluster connect pg-cluster 98 ``` 99 100 ```bash 101 postgres=# select * from pg_stat_replication; 102 ``` 103 104  105 106 From the output, `pg-cluster-postgresql-0` has been assigned as the secondary's pod. 107 108 5. Describe the cluster and check the instance role. 109 110 ```bash 111 kbcli cluster describe pg-cluster 112 ``` 113 114  115 116 After the failover, `pg-cluster-postgresql-0` becomes the secondary pod and `pg-cluster-postgresql-1` becomes the primary pod.