1. 程式人生 > >postgresql使用RHCS套件搭建HA高可用叢集

postgresql使用RHCS套件搭建HA高可用叢集

環境: 資料庫伺服器 2臺 伺服器一 作業系統:CentOS 6.8 x86_64 IP(eth0):192.168.11.61 主機名:node1 伺服器二 作業系統:CentOS 6.8 x86_64 IP_1(eth0):192.168.11.62 主機名:node2 儲存伺服器 作業系統:CentOS 6.8 x86_64 IP_1(eth0):192.168.11.63 主機名:disk 共享盤:/dev/sdb(30G、LVM) 虛擬IP 192.168.11.69 1、共享磁碟的設定 [[email protected] ~]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xa5927bb4. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +10G
Command (m for help): w The partition table has been altered! [[email protected] /]# fdisk -l Disk /dev/sda: 53.7 GB, 53687091200 bytes 255 heads, 63 sectors/track, 6527 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x0007f236 Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 64 6528 51915776 8e Linux LVM Disk /dev/sdb: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x2eb7246b Device Boot Start End Blocks Id System /dev/sdb1 1 1305 10482381 83 Linux Disk /dev/mapper/VolGroup-lv_root: 51.1 GB, 51078234112 bytes 255 heads, 63 sectors/track, 6209 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/mapper/VolGroup-lv_swap: 2080 MB, 2080374784 bytes 255 heads, 63 sectors/track, 252 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 格式化磁碟 [[email protected] ~]# mkfs.ext3 /dev/sdb mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 655360 inodes, 2620506 blocks 131025 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2684354560 80 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 39 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. 2、使用者及目錄(兩臺資料庫服務上都執行): 建立 postgres使用者 [[email protected] ~]# useradd postgres 建立資料庫的data目錄,並配置目錄的許可權 [[email protected] ~]# mkdir -p /home/postgres/pgsql9.6 [[email protected] ~]# mkdir -p /home/postgres/data [[email protected] ~]# chown -R postgres:postgres /home/postgres 在postgres 使用者下設定環境變數 export PATH=/home/postgres/pgsql9.6/bin:$PATH export PGDATA=/home/postgres/data export LD_LIBRARY_PATH=/home/postgres/pgsql9.6/lib 3、網路配置(三臺機器都執行): root使用者配置/etc/hosts檔案,最終內容如下: 127.0.0.1 localhost.localdomain localhost.localdomain localhost4 localhost4.localdomain4 localhost ::1 localhost.localdomain localhost.localdomain localhost6 localhost6.localdomain6 localhost 192.168.11.61 node1 192.168.11.62 node2 192.168.11.63 disk root使用者關閉防火牆,命令如下: [[email protected] ~]# chkconfig iptables off [[email protected] ~]# service iptables stop root使用者編輯檔案/etc/sysconfig/selinux,關閉selinux,命令如下: [[email protected] ~]# vi /etc/sysconfig/selinux # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of these two values: # targeted - Targeted processes are protected, # mls - Multi Level Security protection. SELINUXTYPE=targeted 4、SSH互信 root使用者互信 [[email protected] ~]# ssh-keygen -t rsa -P ' ' [[email protected] ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] [[email protected] ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] [[email protected] ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] postgres使用者互信 [[email protected] ~]# su - postgres [[email protected] ~]$ ssh-keygen -t rsa -P ' ' [[email protected] ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] [[email protected] ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] [[email protected] ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub[email protected] root使用者與postgres使用者互信 [[email protected] ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] [[email protected] ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] [[email protected] ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] [[email protected] ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] [[email protected] ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] [[email protected] ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] 5、安裝共享儲存 共享儲存採用icsci + multipath的形式。 ISCSI 儲存伺服器 伺服器端安裝scsi-target-utils包。 編輯/etc/tgt/targets.conf檔案新增儲存分割槽,新增內容如下: <target iqn.2017-04-05.dev:server.target1> backing-store /dev/sdb1 </target> 配置服務,命令如下: [[email protected] ~]# service tgtd start [[email protected] ~]# chkconfig tgtd on 節點伺服器(兩臺都安裝) 客戶端安裝iscsi-initiator-utils包。 編輯/etc/iscsi/initiatorname.iscsi檔案新增儲存目標,檔案內容如下: InitiatorName=iqn.2017-04-05.dev:server.target1 配置服務: [[email protected] ~]# chkconfig iscsi on [[email protected] ~]# chkconfig iscsid on [[email protected] ~]# service iscsi restart [[email protected] ~]# service iscsid restart 建立目錄,聯機iscsi共享盤,命令如下: [[email protected] ~]# iscsiadm -m discovery -t sendtargets -p 192.168.11.63:3260 [[email protected] ~]# iscsiadm -m node -T iqn.2017-04-05.dev:server.target1 -l [[email protected] ~]# mount /dev/sdb /home/postgres/data [[email protected] ~]# chown -R postgres:postgres /home/postgres/data 6、安裝postgresql資料庫 安裝過程: 節點一 [[email protected] ~]$ tar -xvf postgresql-9.6.1.tar.bz2 [[email protected] ~]$ cd postgresql-9.6.1 [[email protected] postgresql-9.6.1]$ ./configure --prefix=/home/postgres/pgsql9.6/ [[email protected] postgresql-9.6.1]$ make [[email protected] postgresql-9.6.1]$ make install [[email protected] postgresql-9.6.1]$ /home/postgres/pgsql9.6/initdb -D /home/postgres/data (注意要確保你的/dev/sdb /掛載到了/home/postgres/data 可以df -h檢視一下) 節點一關閉資料庫服務,umount共享盤 在節點二安裝資料庫,將共享盤至/home/postgres/data目錄下,刪除data目錄下的所有檔案,之後安裝資料庫(重複節點一安裝資料庫的過程)。 節點二關閉資料庫服務,umount共享盤 將共享盤掛載至節點一,重啟資料庫服務,進行檢查 7、HA軟體安裝配置 安裝事項 儲存伺服器:yum安裝luci 節點伺服器:yum安裝ricci、cman、rgmanager 這四個軟體均設定為開機自啟 [[email protected] ~]# chkconfig luci on [[email protected] ~]# chkconfig ricci on [[email protected] ~]# chkconfig cman on [[email protected] ~]# chkconfig rgmanager on 所有伺服器關閉NetworkManager服務並設定為開機不啟動(否則無法啟動cman) [[email protected] ~]# service NetworkManager stop [[email protected] ~]# chkconfig NetworkManager off 節點伺服器:passwd ricci設定密碼 1.登陸

2.建立叢集
  • 點選面板左側的“Manage Clusters”
  • 點選“Create”

3.、新增失效域 新增完叢集之後,會自動重啟節點伺服器。  重新登入管理介面,點選剛建立的叢集  點選“Failover Domains”,新增失效域
4.新增資源  點選“Resource”,新增資源  在下拉選單中選擇“Fielsystem”,新增共享盤資源(data)  在下拉選單中選擇“Ip Addres”,新增虛擬ip  在下拉選單中選擇“Script”,新增資料庫服務指令碼,注意指令碼的可執行許可權



5.叢集狀態
6.資料庫服務指令碼(注意指令碼許可權) [[email protected] postgres]# cat service.sh #!/bin/bash # environment. # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs export PGDATA=/home/postgres/data export PGUSER=postgres export PGDATABASE=postgres export PGHOST=localhost export PGPORT=5432 export PATH=$PATH:$HOME/bin:/home/postgres/pgsql9.6/bin export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/postgres/pgsql9.6/lib function start { su - ${PGUSER} -c "pg_ctl start" return 0 } function stop { su - ${PGUSER} -c "pg_ctl stop -m fast" return 0 } function reload { su - ${PGUSER} -c "pg_ctl reload" return 0 } function status { su - ${PGUSER} -c "pg_ctl status" return $? } # See how we were called. case "$1" in "start") start exit $? ;; "stop") stop exit $? ;; "restart") stop start exit $? ;; "reload") reload exit $? ;; "status") status exit $? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status}" exit 0 esac 7.伺服器切換測試 將階段一的伺服器關閉,檢視是否能切換到節點二上 [[email protected] postgres]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_node1-lv_root 37G 4.6G 31G 14% / tmpfs 491M 26M 466M 6% /dev/shm /dev/sda1 477M 35M 418M 8% /boot /dev/sdb 20G 83M 19G 1% /home/postgres/data [[email protected] postgres]# su - postgres [[email protected] ~]$ psql psql (9.6.1) Type "help" for help. postgres=# 伺服器正常切換,資料庫能正常使用

可以看出節點一掉了 ,服務執行中在節點二上