3 Node'lu Consul, Patroni, HAProxy, Keepalived and pg_backrest Kurulumu
Published: 2023-12-26 11:57:47

# Kurulum Öncesi Yapılması Gerekenler

####Kurulum için hazırlanılan tüm Rocky Sunucularında yapılması gereken adımlar şu şekilde olmalıdır.

1. Hostname değiştirlmesi 

```
hostnamectl set-hostname new_name
```

2. Firewalld kapatılıp devre dışı bırakılması

```
systemctl stop firewalld
systemctl disable firewalld
```

3. Selinux'un kapatılması
 
- SELINUX = disabled diye değiştiriyoruz.
- `vi /etc/sysconfig/selinux` ile configuration dosyasına gidiyoruz.
 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pheq9rs9wzk4aesxcm2q.PNG)

4. Gerekli Repo ve Paketlerin Eklenmesi

```
yum -y install epel-release
yum -y install yum-utils
yum -y update
```

5. İşlemlerin Tamamlanması İçin Yeniden Başlatılır.

```
shutdown -r now
```

# Consul Kurulumu

```
yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
yum -y install consul
```

1. Node1 ve Node2 için configuration yapıyoruz.

- `consul.service` dosyasının yolunu `systemctl status consul` yazarak bulabiliriz.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/itqhwl5qglsr8wtlwh8x.PNG)

- `vi /usr/lib/systemd/system/consul.service` dosyasının içine giriyoruz.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1y16fwm28bo66h77yksh.PNG)

```
systemctl daemon-reload
```

- consul keygen alıyoruz.

```
consul keygen
```

- `touch /etc/consul.d/config.json` dosyasını olusturuyoruz.

- `vi /etc/consul.d/config.json ` içine giriyoruz.

```
{
  "advertise_addr": "192.168.56.139",
  "bind_addr": "192.168.56.139",
  "bootstrap_expect": 2,
  "client_addr": "0.0.0.0",
  "datacenter": "DC1",
  "data_dir": "/var/lib/consul",
  "domain": "consul",
  "enable_script_checks": true,
  "dns_config": {
    "enable_truncate": true,
    "only_passing": true
  },
  "enable_syslog": true,
  "encrypt": "ONSeUzBvLTk2gqcXwnfaBd7XPyWhSxxbeySAGV5RGMo=",
  "leave_on_terminate": true,
  "log_level": "INFO",
  "rejoin_after_leave": true,
  "retry_join": [
    "192.168.56.139",
    "192.168.56.140"
  ],
  "server": true,
  "ui_config": {
   "enabled": true
  }
}
```

- Bu aşamadan sonra leader Node1 için başarılı şekilde configuration edildiği için başlatılabilir.

```
systemctl start consul
systemctl enable consul
consul members
```

- Consul Web Arayüzüne erişmek için 192.168.56.139:8500 portuna bağlan
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93wa5645cqc3k7b0ce81.PNG)

# Patroni Kurulumu

- Paketlerin yüklemesi ile başlayalım.

- Patroni'yi yüklerken PostgreSQL'i de yükleriz.

```
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo dnf -qy module disable postgresql
sudo dnf install -y postgresql16-server
yum install -y patroni patroni-consul
```

- Kurulum yapılacak dizinler oluşturulur ve izinleri tanımlanır.

```
mkdir -p /database/patroni
chown -R postgres:postgres /database/
chmod -R 750 /database/
```

- Patroni anadizisine gidilir ve config dosyası oluşturulur.

```
cd /etc/patroni
touch patroni.yml
vi patroni.yml
```

```
scope: postgres
name: node1

restapi:
  listen: 192.168.56.139:8008
  connect_address: 192.168.56.139:8008

consul:
  host: 127.0.0.1:8500
  protocol: http
  register_service: true

bootstrap:
  method: pitr_restore_by_pgbackrest
  pitr_restore_by_pgbackrest:
    command: 'pgbackrest --stanza=db --delta --type=time --target="2023-12-08 02:28:58" restore'
    keep_existing_recovery_conf: True
    no_params: True
    recovery_conf:
      recovery_target_action: promote
      restore_command: 'pgbackrest --stanza=db archive-get %f %p'
      recovery_target_time: "2023-12-08 02:28:58"
      recovery_target_inclusive: True

  dcs:
    ttl: 30
    loop_wait: 10
    retry_timeout: 10
    maximum_lag_on_failover: 1048576
    postgresql:
      use_pg_rewind: true
      use_slots: true
      parameters:
        max_connections: 300
        shared_buffers: 256MB

  initdb:
  - encoding: UTF8
  - data-checksums

  pg_hba:
  - host replication replicator 127.0.0.1/32 md5
  - host replication replicator 192.168.56.139/32 md5
  - host replication replicator 192.168.56.140/32 md5
  - host all all 0.0.0.0/0 trust

  users:
    admin:
      password: admin
      options:
      - createrole
      - createdb

postgresql:
  listen: "*"
  connect_address: 192.168.56.139:5432
  data_dir: /database/patroni
  bin_dir: /usr/pgsql-16/bin
  pgpass: /tmp/pgpass
  authentication:
    replication:
      username: replicator
      password: replicator
    superuser:
      username: postgres
      password: postgres
  parameters:
    unix_socket_directories: '/var/run/postgresql/,/tmp'

tags:
  nofailover: false
  noloadbalance: false
  clonefrom: false
  nosync: false
```

- NODE1 ve NODE2 için patroni configuration yüklendikten sonra start ve enable işlemleri yapılır.

```
systemctl start patroni
systemctl enable patroni
systemctl status patroni
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54n76qkrrw6dfm0wb35u.PNG)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/do9a9pauou9zumhzbb1c.PNG)

# HAProxy Kurulumu 

- HAProxy Paketlerinin Yüklenmesi

```
yum -y install haproxy
```

- Configuration Eklenmesi

- Node1 ve Node2 için aynı configuration yüklenir.

```
vi /etc/haproxy/haproxy.cfg
```

```
global
    maxconn 500

defaults
    log global
    mode tcp
    retries 2
    timeout client 30m
    timeout connect 4s
    timeout server 30m
    timeout check 5s

listen stats
    mode http
    bind *:7000
    stats enable
    stats uri /

listen MASTER_5433
    bind *:5433
    option httpchk
    option httpchk OPTIONS /master
    http-check expect status 200
    default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions
    server node1_10.151.241.121_5432 10.151.241.121:5432 maxconn 500 check port 8008
    server node2_10.151.241.206_5432 10.151.241.206:5432 maxconn 500 check port 8008
	server node3_10.151.241.135_5432 10.151.241.135:5432 maxconn 500 check port 8008

listen STANDBY_5434
    bind *:5434
    option httpchk
    option httpchk OPTIONS /replica
    http-check expect status 200
    default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions
    server node1_10.151.241.121_5432 10.151.241.121:5432 maxconn 500 check port 8008
    server node2_10.151.241.206_5432 10.151.241.206:5432 maxconn 500 check port 8008
	server node3_10.151.241.135_5432 10.151.241.135:5432 maxconn 500 check port 8008
```

- Node1 ve Node2 için HAProxy configuration yüklendikten sonra start ve enable işlemleri yapılır.

```
systemctl start haproxy
systemctl enable haproxy
```

- Web arayüzüne erişmek için `192.168.56.157:8500` bağlanıyoruz.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4kt522szltr9wuyz7tf.PNG)

# Keepalived Kurulumu 

- Keepalived Yüklenmesi

```
yum install -y keepalived
```

- Configuration dosyaların yüklenmesi

```
vi /etc/keepalived/keepalived.conf
```

```
#master
vrrp_instance keepalive_instance {
    interface enp0s3
    state MASTER
    priority 101
    virtual_router_id 42
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 8888
    }
    virtual_ipaddress {
        192.168.56.142/32
    }
}

--------------------------------------------------
#backup
vrrp_instance keepalive_instance {
    interface enp0s3
    state BACKUP
    priority 100
    virtual_router_id 42
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 8888
    }
    virtual_ipaddress {
        192.168.56.142/32
    }
}
```

```
systemctl stop keepalived
systemctl start keepalived
systemctl status keepalived
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c83pz8wnkeh94mhewrgz.PNG)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ohulo5a7i5d04ldug7ny.PNG)

# pg_backrest Kurulumu

- Giriş

```
passwd postgres ile şifre belirliyoruz.
patronictl -c /etc/patroni/patroni.yml edit-config 
```

```
loop_wait: 10
maximum_lag_on_failover: 1048576
postgresql:
  basebackup:
    checkpoint: fast
  create_replica_methods:
  - pgbackrest
  - basebackup
  parameters:
    archive_command: pgbackrest --stanza=db archive-push %p
    archive_mode: 'on'
    max_connections: 300
    shared_buffers: 256MB
  pgbackrest:
    command: pgbackrest --stanza=db restore
    keep_data: true
    no_params: true
  recovery_conf:
    recovery_target_timeline: latest
    restore_command: pgbackrest --stanza=db archive-get %f %p
  use_pg_rewind: true
  use_slots: true
retry_timeout: 10
ttl: 30
```

```
patronictl -c /etc/patroni/patroni.yml list => Cluster ismini görürüz
patronictl -c /etc/patroni/patroni.yml reload postgres
patronictl -c /etc/patroni/patroni.yml start postgres
```

- Bunları patroni1 ve patroni2 de yap

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rfpexuv73baotgbvufrl.PNG)

- Yüklemeler ile başlayalım. Tüm sunucularda aşağıdaki komutlar çalıştırılır.

```
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install -y pgbackrest
su - postgres
```

- Node1 Makinesi

```
ssh-keygen
ssh-copy-id postgres@192.168.56.141
```

- Node2 Makinesi

```
ssh-keygen
ssh-copy-id postgres@192.168.56.141
```

- pg_backrest Makinesi

```
ssh-keygen
ssh-copy-id postgres@192.168.56.139
ssh-copy-id postgres@192.168.56.140
```

- Node1 ve Node2 Makineleri için

`vi /etc/pgbackrest.conf`

```
[global]
log-level-file=detail
log-level-console=info
repo1-host=192.168.56.141
repo1-host-user=postgres

[db]
pg1-path=/database/patroni
pg1-port=5432
pg1-socket-path=/var/run/postgresql
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wn03ucyxcjcq4addktlh.PNG)

- pg_backrest Node’unda işlemlerin yapılması

```
log ve backup için dizinlerin oluşturulması
mkdir -p /database/backup
mkdir -p /database/log
chown -R postgres. /database/backup
```

```
[global]
repo1-path=/database/backup
log-path=/database/log
log-level-file=detail
log-level-console=info
backup-user=postgres
retention-full=3
process-max=4
start-fast=y

[db]
pg1-path=/database/patroni
pg1-port=5432
pg1-host=192.168.56.139
pg1-socket-path=/var/run/postgresql
pg2-path=/database/patroni
pg2-port=5432
pg2-host=192.168.56.140
pg2-socket-path=/var/run/postgresql

[global:archive-push]
compress-level=3
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/34u1n70z6j3umem0xyhl.PNG)

```
pgbackrest --stanza=db stanza-create
pgbackrest --stanza=db check
pgbackrest --stanza=db –-type=full  backup
```