Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
postgresql_patroni [2024/02/21 08:52] – [Setup Patroni] andonovj | postgresql_patroni [2025/05/13 05:14] (current) – andonovj | ||
---|---|---|---|
Line 128: | Line 128: | ||
So we have our key-value propagated among the cluster, nice.... | So we have our key-value propagated among the cluster, nice.... | ||
+ | |||
+ | |||
+ | We can also check the health of the cluster endpoints as follow: | ||
+ | |||
+ | < | ||
+ | [root@etcd02 ~]# etcdctl endpoint status --write-out=table --endpoints=http:// | ||
+ | +--------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ | ||
+ | | ENDPOINT | ||
+ | +--------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ | ||
+ | | http:// | ||
+ | | http:// | ||
+ | | http:// | ||
+ | +--------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+ | ||
+ | [root@etcd02 ~]# | ||
+ | |||
+ | </ | ||
+ | |||
=====Setup Patroni===== | =====Setup Patroni===== | ||
Line 157: | Line 174: | ||
cat > / | cat > / | ||
- | scope: stampede | + | scope: stampede |
- | name: node02 | + | name: ${host} |
- | + | ||
- | restapi: | + | restapi: |
- | listen: | + | listen: |
- | connect_address: | + | connect_address: |
- | + | ||
- | etcd: | + | etcd: |
- | host: node02:2379 | + | host: ${host}:2379 |
- | + | ||
- | bootstrap: | + | bootstrap: |
- | dcs: | + | dcs: |
- | ttl: 30 | + | ttl: 30 |
- | loop_wait: 10 | + | loop_wait: 10 |
- | retry_timeout: | + | retry_timeout: |
- | maximum_lag_on_failover: | + | maximum_lag_on_failover: |
- | postgresql: | + | postgresql: |
- | use_pg_rewind: | + | use_pg_rewind: |
- | use_slots: true | + | use_slots: true |
- | parameters: | + | parameters: |
- | wal_level: logical | + | wal_level: logical |
- | wal_log_hints: | + | wal_log_hints: |
- | + | ||
- | initdb: | + | initdb: |
- | - encoding: UTF8 | + | - encoding: UTF8 |
- | - data-checksums | + | - data-checksums |
- | + | ||
- | pg_hba: | + | pg_hba: |
- | - host replication rep_user | + | - host replication rep_user |
- | - host all all 10.100.10.0/24 md5 | + | - host all all ${subnet}.0/24 md5 |
- | + | ||
- | users: | + | users: |
- | admin: | + | admin: |
- | password: adminpass | + | password: adminpass |
- | options: | + | options: |
- | - createrole | + | - createrole |
- | - createdb | + | - createdb |
- | + | ||
- | postgresql: | + | postgresql: |
- | listen: | + | listen: |
- | connect_address: | + | connect_address: |
- | data_dir: / | + | data_dir: /db/pgdata |
- | bin_dir: | + | bin_dir: |
- | pgpass: / | + | pgpass: / |
- | authentication: | + | authentication: |
- | replication: | + | replication: |
- | username: rep_user | + | username: rep_user |
- | password: newpass | + | password: newpass |
- | superuser: | + | superuser: |
- | username: postgres | + | username: postgres |
- | password: newpass | + | password: newpass |
- | parameters: | + | parameters: |
- | unix_socket_directories: | + | unix_socket_directories: |
- | external_pid_file: | + | external_pid_file: |
- | logging_collector: | + | logging_collector: |
- | log_directory: | + | log_directory: |
- | log_filename: | + | log_filename: |
archive_command: | archive_command: | ||
archive_mode: | archive_mode: | ||
Line 556: | Line 573: | ||
That code will create 3 node cluster with everything. | That code will create 3 node cluster with everything. | ||
+ | |||
+ | |||
+ | =====Backup===== | ||
+ | Below you can see a method to backup, encrypt and upload a backup to OCI. The backup uses the replica for a backup source: | ||
+ | |||
+ | < | ||
+ | #!/bin/bash | ||
+ | |||
+ | switch_logfiles(){ | ||
+ | v_leader=$1 | ||
+ | |||
+ | while : | ||
+ | do | ||
+ | echo " | ||
+ | psql -Ubarman -h ${v_leader} -c " | ||
+ | sleep 10 | ||
+ | done | ||
+ | } | ||
+ | |||
+ | backup() { | ||
+ | |||
+ | environment=$1 | ||
+ | echo " | ||
+ | |||
+ | echo " | ||
+ | REPLICA_HOST=postgres-${environment} | ||
+ | |||
+ | v_first_replica=$(curl -sk https:// | ||
+ | v_leader=$(curl -sk https:// | ||
+ | |||
+ | echo " | ||
+ | sed -i " | ||
+ | |||
+ | echo " | ||
+ | barman -c / | ||
+ | |||
+ | echo " | ||
+ | barman -c / | ||
+ | |||
+ | echo " | ||
+ | |||
+ | switch_logfiles ${v_leader} & | ||
+ | |||
+ | barman -c / | ||
+ | |||
+ | kill $! | ||
+ | |||
+ | echo " | ||
+ | sed -i " | ||
+ | } | ||
+ | |||
+ | clean_old_backups(){ | ||
+ | find / | ||
+ | } | ||
+ | |||
+ | encrypt_backup(){ | ||
+ | date | ||
+ | environment=$1 | ||
+ | backup_dir=$(ls -rtd / | ||
+ | cd $(dirname ${backup_dir}) | ||
+ | backup_file=$(echo ${backup_dir} | cut -d '/' | ||
+ | enc_key=$(cat / | ||
+ | tar zcvf - ${backup_file}/ | ||
+ | date | ||
+ | } | ||
+ | |||
+ | copy_files_to_os(){ | ||
+ | date | ||
+ | environment=$1 | ||
+ | echo "Set BUCKET_NAME and OCI config file variable" | ||
+ | case ${environment} in | ||
+ | " | ||
+ | BUCKET_NAME=postgres-dev-backup | ||
+ | OCI_CONFIG_FILE=/ | ||
+ | ;; | ||
+ | " | ||
+ | BUCKET_NAME=postgres-stg-backup | ||
+ | OCI_CONFIG_FILE=/ | ||
+ | ;; | ||
+ | " | ||
+ | BUCKET_NAME=postgres-prod-backup | ||
+ | OCI_CONFIG_FILE=/ | ||
+ | ;; | ||
+ | *) | ||
+ | esac | ||
+ | |||
+ | export BUCKET_NAME | ||
+ | export OCI_CONFIG_FILE | ||
+ | |||
+ | encrypted_file=$(ls -rtd / | ||
+ | oci os object put --bucket-name ${BUCKET_NAME} --file ${encrypted_file} | ||
+ | rm -f ${encrypted_file} | ||
+ | date | ||
+ | } | ||
+ | |||
+ | backup $1 | ||
+ | encrypt_backup $1 | ||
+ | copy_files_to_os $1 | ||
+ | |||
+ | </ |