Replica PostgreSQL slavo multi

Da It Ikoula wiki.
Jump to navigation Jump to search

it:Replica PostgreSQL slavo multi en:Replication PostgreSQL Slavic multi he:שכפול מרובה PostgreSQL סלאבית ro:Replicare PostgreSQL slave multi ru:Multi PostgreSQL славянских репликации pl:Replikacja wielu słowiańskich PostgreSQL ja:レプリケーション PostgreSQL スラブ マルチ ar:النسخ المتماثل السلافية كيو متعدد zh:复制 PostgreSQL 斯拉夫语多 de:Replikation PostgreSQL slawischen multi nl:Replicatie PostgreSQL Slavische multi pt:Replicação PostgreSQL eslavo multi es:Replicación multi de PostgreSQL Slavic fr:Replication PostgreSQL multi slaves

Introduzione

Questa pagina riguarda l'implementazione di un asincrono PostgreSQL streaming replica con un master e due schiavi. Nostro slavo sarà configurato come hot-standby, vale a dire che sarà possibile eseguire query (sola lettura ) su questi.


Contesto di questo posizionamento :


Abbiamo 3 Server Debian 8 (Ultimo aggiornamento ) :

Postgres 01 (10.1.1.75) : sarà il nostro Maestro

Postgres02 (10.1.1.90) : sarà uno schiavo

Postgres03 (10.1.1.199) : sarà un secondo slave


Specificati i comandi saranno da eseguire come root quando sono precedute da "#" e come utente di sistema Postgres quando sono precedute da "$".

Installazione di PostgreSQL e pre-configurazione dei tre server

Tutti i comandi in questa parte devono essere fatte su ciascun server.

Come primo passo, si consiglia di avere un unico file /etc/hosts popolato con le partite di ogni server, come questo (adattando con gli indirizzi ip e nomi host dei vostri propri assistenti ) :

# cat /etc/hosts
127.0.0.1       localhost

10.1.1.75       Postgres 01
10.1.1.90       Postgres 02
10.1.1.199      Postgres 03


Aggiungere il deposito APT PostgreSQL :

# echo "deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main" > /etc/apt/sources.list.d/postgres.list


Importiamo la chiave pubblica di questo deposito :

# gpg --keyserver pgpkeys.mit.edu --recv-key 7FCC7D46ACCC4CF8 && gpg -a --export 7FCC7D46ACCC4CF8 | apt-key add -
gpg: directory `/root/.gnupg' created
gpg: new configuration file `/root/.gnupg/gpg.conf' created
gpg: WARNING: options in `/root/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/root/.gnupg/secring.gpg' created
gpg: keyring `/root/.gnupg/pubring.gpg' created
gpg: requesting key ACCC4CF8 from hkp server pgpkeys.mit.edu
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key ACCC4CF8: public key "PostgreSQL Debian Repository" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
OK


Aggiorna la cache di APT in modo da tenere conto il deposito :

# apt-get update


L'installazione di pacchetto postgresql (installare l'ultima versione stabile è la 9.5 nel momento in cui sono scritte queste righe ) :

# apt-get install postgresql


Definiamo una password (di sua scelta ma sicura ) e genera una coppia di chiavi ssh (senza passphrase ) per l'utente di sistema postgres :

# passwd postgres
# su - postgres
$ ssh-keygen


Copiamo l'utente postgres da e tra ogni server pubblica ssh chiave :


Poiché il server primo (postgres01 nel nostro caso ) verso la seconda (postgres02 per noi ) e troisiseme (postgres03) :

root@postgres01:~# su - postgres
postgres@postgres01:~$ ssh-copy-id postgres02
postgres@postgres01:~$ ssh-copy-id postgres03


Dal secondo al primo e il terzo server :

root@postgres02:~# su - postgres
postgres@postgres02:~$ ssh-copy-id postgres01
postgres@postgres02:~$ ssh-copy-id postgres03


Dal terzo al primo e al secondo server :

root@postgres03:~# su - postgres
postgres@postgres03:~$ ssh-copy-id postgres01
postgres@postgres03:~$ ssh-copy-id postgres02

La configurazione Master

Fare solo sul master (postgres01 nel nostro caso ) :


Crea il ruolo /utente di replica :

# su - postgres
$ psql -c "CREATE USER repuser REPLICATION LOGIN CONNECTION LIMIT 2 ENCRYPTED PASSWORD '<mot de passe repuser>';"

Nota : Esso definisce un limite di connessioni di 2 perché abbiamo 2 Slava


Modifica del file di configurazione principale postgresql /etc/postgresql/9.5/main/postgresql.conf per configurare le seguenti direttive :

listen_addresses = '*'
wal_level = hot_standby
archive_mode = on
archive_command = 'rsync -av %p postgres@<ip du premier slave>:/var/lib/postgresql/wal_archive/%f'
max_wal_senders = 2
wal_keep_segments = 256
hot_standby = on

Nota : Permettiamo al nostro secondo server di archiviazione (Personalizzare il tuo indirizzo ip server ) per ancora più precauzioni. Definiamo anche la hot_standby di parametri, Beh questo ignorato su un master, dove dovrebbe essere abbassato a slave in futuro.


Adesso editing file di autenticazione postgresql /etc/postgresql/9.5/main/pg_hba.conf per aggiungere l'autorizzazione del nostro utente di replica per connettersi da nostro slavo o la riga seguente alla fine del file :

hostssl replication     repuser <ip du réseau de vos serveurs>/24     md5

Si prega di adattare questa linea in base al nome del vostro utente replica e rete (o gli indirizzi ip rispettivi ) del tuo slava


Si riavvia il servizio di postgresql per prendere in considerazione la nostra configurazione (nella radice ) :

# systemctl restart postgresql


Questo assicura che il servizio viene avviato correttamente :

root@postgres01:~# systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
   Loaded: loaded (/lib/systemd/system/postgresql.service; enabled)
   Active: active (exited) since Thu 2016-06-02 12:06:28 CEST; 22s ago
  Process: 77056 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
 Main PID: 77056 (code=exited, status=0/SUCCESS)


root@postgres01:~# ps -u postgres u
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
postgres  77038  0.0  4.4 227300 21836 ?        S    12:06   0:00 /usr/lib/postgresql/9.5/bin/postgres -D /var/lib/postgresql/9.
postgres  77040  0.0  0.8 227300  4160 ?        Ss   12:06   0:00 postgres: checkpointer process
postgres  77041  0.0  1.0 227300  4968 ?        Ss   12:06   0:00 postgres: writer process
postgres  77042  0.0  1.7 227300  8776 ?        Ss   12:06   0:00 postgres: wal writer process
postgres  77043  0.0  1.3 227728  6708 ?        Ss   12:06   0:00 postgres: autovacuum launcher process
postgres  77044  0.0  0.6  82244  3392 ?        Ss   12:06   0:00 postgres: archiver process
postgres  77045  0.0  0.8  82244  4244 ?        Ss   12:06   0:00 postgres: stats collector process

Configurazione degli slavi

Il primo slave

Iniziamo dalla fermata del servizio di postgresql :

# systemctl stop postgresql


Modifica del file di configurazione principale postgresql /etc/postgresql/9.5/main/postgresql.conf per configurare le stesse linee guida come il Maestro, adattando solo l'ip del primo slave di secondo slave nel comando rsync per archiviare sia :

listen_addresses = '*'
wal_level = hot_standby
archive_mode = on
archive_command = 'rsync -av %p postgres@<ip du second slave>:/var/lib/postgresql/wal_archive/%f'
max_wal_senders = 2
wal_keep_segments = 256
hot_standby = on


Adesso editing file di autenticazione postgresql /etc/postgresql/9.5/main/pg_hba.conf per aggiungere l'autorizzazione del nostro utente di replica per la connessione da altri server del nostro (Questo sarà utilizzato quando questo schiavo previsto promosso maestro ) :

hostssl replication     repuser <ip du réseau de vos serveurs>/24     md5

Nota : Con questa configurazione simile al nostro Maestro, ciò consentirà di promuovere facilmente questo schiavo come master in caso di necessità.


Per le seguenti operazioni, accedere come utente di sistema postgres :

# su – postgres


Creiamo la directory di destinazione per il wal degli archivi master :

$ mkdir /var/lib/postgresql/wal_archive


Rimuove la directory di dati di postgresql :

$ rm -rf /var/lib/postgresql/9.5/main


Fa il backup di base (personalizzare l'ip del tuo padrone e il nome del vostro utente replica ), la password del tuo utente replica vi verrà chiesto :

$ pg_basebackup -h <ip de votre master> -D /var/lib/postgresql/9.5/main -U repuser -v -P --xlog


Configura la replica creando il file /var/lib/postgresql/9.5/main/recovery.conf con i seguenti parametri :

standby_mode = on
primary_conninfo = 'host=<ip de votre master> port=5432 user=repuser password=<mot de passe de repuser> sslmode=require application_name=<hostname de votre slave>'
trigger_file = '/var/lib/postgresql/9.5/postgres.trigger'

Nota : Il grilletto è il file che creiamo quando vogliamo suo schiavo fermate replica e comincerà ad accettare le voci — quando si desidera promuovere come maestro. Prima di promuovere uno schiavo in caso di crash del suo Maestro, assicurarsi che il master iniziale non salirà per evitare il danneggiamento.


Torniamo radice e avvia il servizio :

# systemctl start postgresql


Questo assicura che il servizio avviato correttamente :

# systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
   Loaded: loaded (/lib/systemd/system/postgresql.service; enabled)
   Active: active (exited) since Thu 2016-06-02 12:53:42 CEST; 1min 6s ago
  Process: 8894 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
 Main PID: 8894 (code=exited, status=0/SUCCESS)


# ps -u postgres u
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
postgres   8878  0.0  4.4 227308 21892 ?        S    12:53   0:00 /usr/lib/postgresql/9.5/bin/postgres -D /var/lib/postgresql/9.
postgres   8879  0.0  1.1 227376  5700 ?        Ss   12:53   0:00 postgres: startup process   recovering 00000001000000000000000
postgres   8880  0.0  0.8 227308  4140 ?        Ss   12:53   0:00 postgres: checkpointer process
postgres   8881  0.0  1.0 227308  5236 ?        Ss   12:53   0:00 postgres: writer process
postgres   8882  0.0  0.6  82252  3316 ?        Ss   12:53   0:00 postgres: stats collector process
postgres   8883  0.0  1.7 238064  8520 ?        Ss   12:53   0:00 postgres: wal receiver process   streaming 0/30003E0


Se si connette al nostro Maestro, già possiamo controllare la nostra replica tra il master e lo slave primo è funzionale :

Si connette al nostro maestro e collega in utente di sistema postgres :

# su - postgres


Quindi la connessione a postgresql :

$ psql


Consente la visualizzazione estesa e controlliamo la nostra replica :

postgres=# \x
Expanded display is on.
postgres=# select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid              | 78879
usesysid         | 16384
usename          | repuser
application_name | postgres02
client_addr      | 10.1.1.90
client_hostname  |
client_port      | 49009
backend_start    | 2016-06-02 12:53:36.641823+02
backend_xmin     |
state            | streaming
sent_CRElocation    | 0/30004C0
write_location   | 0/30004C0
flush_location   | 0/30004C0
replay_location  | 0/30004C0
sync_priority    | 0
sync_state       | async

La nostra replica asincrona in streaming con il nostro primo slave è ben fatto. Questa fase ha un Master replica /Classico schiavo (un singolo slave flusso asincrono )

Il secondo slave

Abbiamo Redid la stessa cosa è stato fatto il primo slave ovviamente adattando il nome host nel file recovery.conf e ip indirizzo nel comando di archiviazione nel file di configurazione principale in se anche voi volete abilitare l'archiviazione su questo secondo slave.

Verifica e collaudo dei nostri multi master replica slava

Si collega al master e viene registrato in utente postgres :

# su – postgres

Quindi la connessione a postgresql :

$ psql


Consente la visualizzazione estesa e controlliamo la nostra replica :

postgres=# \x
Expanded display is on.
postgres=# select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid              | 78879
usesysid         | 16384
usename          | repuser
application_name | postgres02
client_addr      | 10.1.1.90
client_hostname  |
client_port      | 49009
backend_start    | 2016-06-02 12:53:36.641823+02
backend_xmin     |
state            | streaming
sent_location    | 0/5000140
write_location   | 0/5000140
flush_location   | 0/5000140
replay_location  | 0/5000140
sync_priority    | 0
sync_state       | async
-[ RECORD 2 ]----+------------------------------
pid              | 82725
usesysid         | 16384
usename          | repuser
application_name | postgres03
client_addr      | 10.1.1.199
client_hostname  |
client_port      | 51754
backend_start    | 2016-06-02 14:31:43.759683+02
backend_xmin     |
state            | streaming
sent_location    | 0/5000140
write_location   | 0/5000140
flush_location   | 0/5000140
replay_location  | 0/5000140
sync_priority    | 0
sync_state       | async

Così là bene nostro 2 replica con i nostri due server slave (postgres02 e postgres 03).


Ancora connesso a postgresql sul master, crea un data base (ex : checkrep ) :

postgres=# CREATE DATABASE checkrep;


Controlla che il database è replicato anche su nostro 2 Slava :

root@postgres02:~# su - postgres
postgres@postgres02:~$ psql
psql (9.5.3)
Type "help" for help.

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 checkrep  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)


root@postgres03:~# su - postgres
postgres@postgres03:~$ psql
psql (9.5.3)
Type "help" for help.

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 checkrep  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

Il database che abbiamo creato il nostro Maestro è è così ben automaticamente replicato al nostro due slavi.


Per eliminare, dal Maestro :

postgres=# DROP DATABASE checkrep;



Non si dispone dei permessi necessari per inviare commenti.