ansible-playpook搭建Postgresql主從複製

準備
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

[root@m8-9p52-duanzhengnan templates]# cat /etc/ansible/roles/postgresql_ha/vars/main.yml 

postgresql_version: postgresql-9.6.3.tar.gz
#解壓目錄地址
dir: /opt
#解壓目錄名
postgresql_dir: /postgresql-9.6.3
#二進制編譯目錄
data_dir: /opt/pgsql
#數據目錄
datadir: /opt/pgsql/data
#歸檔的文件夾,根據配置定義
file_path: /opt/pgsql/archive
#主從用戶
postgres_user: replica
#主從用戶密碼
postgres_password: 123456
[root@m8-9p52-duanzhengnan templates]# cat /etc/ansible/roles/postgresql_ha/tasks/main.yml 

- name: "解壓軟件包"
  unarchive: src=/opt/software_package/dataflow{{postgresql_version}} dest={{dir}}

- name: create user
  shell: id postgres || useradd postgres && echo '123456' | passwd --stdin postgres

- name: "安裝依賴包"
  yum: name="{{item}}" state=installed
  with_items:
      - gcc
      - gcc-c++
      - readline-devel
      - zlib-devel

- name: "編譯源碼,安裝"
  shell: cd {{dir}}{{postgresql_dir}} && ./configure --prefix={{data_dir}} && make && make install

- name: "配置環境變量"
  shell: |
         echo -e "export POSTGRESQL_HOME={{data_dir}}\nexport PATH={{data_dir}}/bin:\$PATH\nexport PGDATA={{data_dir}}/data">/etc/profile.d/postgresql.sh
         source /etc/profile.d/postgresql.sh 
         source /etc/profile

- name: "修改服務屬主,屬組"
  shell: chown postgres:postgres -R /opt/pgsql/

- name: "初始化數據庫"
  shell: ls {{datadir}} || su - postgres -c "initdb  -E utf8 -D {{datadir}}"

- name: config
  template: src="templates/{{item.key}}" dest="{{item.value}}" owner=postgres group=postgres mode=0644
  with_dict:
    - {"pg_hba.conf":"{{datadir}}/pg_hba.conf"}
    - {"postgresql_master.conf":"{{datadir}}/postgresql.conf"}
  when: inventory_hostname == groups.postgresql_ha[0]

- name: "在數據庫安裝目錄下創建一個存放歸檔的文件夾"
  file:  path={{file_path}} state=directory owner=postgres group=postgres mode=0755 recurse=yes
  when: inventory_hostname == groups.postgresql_ha[0]

- name: "啓動postgresql"
  shell: ss -anpt|grep 5432 && su - postgres -c "pg_ctl -D {{datadir}} -l {{datadir}}/logfile restart" || su - postgres -c "pg_ctl -D {{datadir}} -l {{datadir}}/logfile start"
  when: inventory_hostname == groups.postgresql_ha[0]

- name: "推送sql語句到目標主機"
  template: src="create_master.sql" dest="/tmp/" owner=postgres group=postgres mode=0644
  when: inventory_hostname == groups.postgresql_ha[0]      
 
- name: "遠程執行推送過去的sql語句"
  shell: su - postgres -c "psql -f /tmp/create_master.sql"
  when: inventory_hostname == groups.postgresql_ha[0]

- name: "保證基礎數據的一致,從主數據庫將起data目錄下的數據複製到從數據庫的data目錄下"
  shell: |
        rm -rf /opt/pgsql/data/* 
        su - postgres -c "pg_basebackup -h {{groups.postgresql_ha[0]}} -U {{postgres_user}} -D {{datadir}} -X stream"
  when: inventory_hostname == groups.postgresql_ha[1]

- name: config
  template: src="templates/{{item.key}}" dest="{{item.value}}" owner=postgres group=postgres mode=0644
  with_dict:
    - {"recovery.conf":"{{datadir}}/recovery.conf"}
    - {"postgresql_slave.conf":"{{datadir}}/postgresql.conf"}
  when: inventory_hostname == groups.postgresql_ha[1]

- name: "啓動postgresql"
  shell: ss -anpt|grep 5432 && su - postgres -c "pg_ctl -D {{datadir}} -l {{datadir}}/logfile restart" || su - postgres -c "pg_ctl -D {{datadir}} -l {{datadir}}/logfile start"
  when: inventory_hostname == groups.postgresql_ha[1]
[root@m8-9p52-duanzhengnan templates]# cat /etc/ansible/roles/postgresql_ha/templates/create_master.sql 
CREATE ROLE  {{postgres_user}} login replication encrypted password '{{postgres_password}}';
[root@m8-9p52-duanzhengnan templates]# cat /etc/ansible/roles/postgresql_ha/templates/pg_hba.conf 
local   all             all                                     trust
host    all             all             127.0.0.1/32            trust
host    all             all             ::1/128                 trust
host     all             all          172.18.9.0/24          md5   #允許從服務器連接到主服務器,md5加密
host   replication      replica       172.18.9.0/24          trust   #允許從服務器使用replica用戶來複制,trust表示pg_basebackup同步配置文件時不用密碼
[root@m8-9p52-duanzhengnan templates]# cat /etc/ansible/roles/postgresql_ha/templates/postgresql_master.conf 
listen_addresses = '*'   # 監聽所有IP
archive_mode = on  # 允許歸檔
archive_command = 'cp %p /home/postgres/pgsql/archive/%f'  # 用該命令來歸檔logfile segment
wal_level = hot_standby 
max_wal_senders = 32 # 這個設置了可以最多有幾個流複製連接,差不多有幾個從,就設置幾個wal_keep_segments = 256  #設置流複製保留的最多的xlog數目
wal_sender_timeout = 60s #設置流複製主機發送數據的超時時間
max_connections = 100 # 這個設置要注意下,從庫的max_connections必須要大於主庫的
[root@m8-9p52-duanzhengnan postgresql_ha]# cat /etc/ansible/roles/postgresql_ha/templates/postgresql_slave.conf 
wal_level = hot_standby
max_connections = 1000 # 一般查多於寫的應用從庫的最大連接數要比較大
hot_standby = on # 說明這臺機器不僅僅是用於數據歸檔,也用於數據查詢
max_standby_streaming_delay = 30s #數據流備份的最大延遲時間
wal_receiver_status_interval = 10s # 多久向主報告一次從的狀態,當然從每次數據複製都會向主報告狀態,這裏只是設置最長的間隔時間
hot_standby_feedback = on # 如果有錯誤的數據複製,是否向主進行反饋
[root@m8-9p52-duanzhengnan templates]# cat /etc/ansible/roles/postgresql_ha/templates/recovery.conf 
standby_mode = on    # 說明該節點是從服務器
primary_conninfo = 'host={{groups.postgresql_ha[0]}} port=5432 user={{postgres_user}} password={{postgres_password}}'  # 主數據庫的信息以及連接的用戶
recovery_target_timeline = 'latest'

2、執行腳本

 ansible-playbook /etc/ansible/playbook/postgresql_ha.yml

3、測試結果

su - postgres -c "psql"
select client_addr,sync_state from pg_stat_replication;

在這裏插入圖片描述
3、通過查看兩個服務器的進程來判斷,分別在兩個服務器上執行命令ps -ef | grep postgres,在主服務器上我們可以看到一個walsender的進程;在從服務器上我們可以看到一個walreceiver的進程,這樣就證明主從配置完成。

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章