本次使用環(huán)境centos7,
共3臺(tái)服務(wù)器,基礎(chǔ)配置采用4核4G內(nèi)存200G機(jī)械硬盤
在初裝系統(tǒng)機(jī)器中開始進(jìn)行以下操作
使用root賬號(hào)開始進(jìn)行以下操作
目前安裝的xl兼容版本為postgresql10版本。
第一步:所有集群中的服務(wù)器均進(jìn)行初始修改/etc/hosts,增加內(nèi)容如下:
10.228.0.45 gtm
10.228.0.46 datanode1
10.228.0.47 datanode2
第二步關(guān)閉防火墻
systemctl stop firewalld.service
systemctl disable firewalld.service
第三步 關(guān)閉selinux
vim /etc/selinux/config
SELINUX=disabled
第四步基礎(chǔ)依賴包安裝
yum install -y flex bison readline-devel zlib-devel openjade docbook-style-dsssl git gcc
第五步重啟使其之前配置生效
reboot
第六步: 創(chuàng)建postgres用戶
useradd postgres
passwd postgres
第七步:設(shè)置ssh免密,這里注意要每個(gè)節(jié)點(diǎn)均創(chuàng)建.ssh目錄和其權(quán)限設(shè)置,免密的公鑰私鑰可以不進(jìn)行創(chuàng)建
su - postgres
mkdir ~/.ssh
chmod 700 ~/.ssh
ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
第八步:將gtm節(jié)點(diǎn)的ssh免密文件復(fù)制到datanode存儲(chǔ)節(jié)點(diǎn)
scp ~/.ssh/authorized_keys postgres@datanode1:~/.ssh/
scp ~/.ssh/authorized_keys postgres@datanode2:~/.ssh/
第九步:開始安裝postgresql-xl
su root
cd /opt
git clone git://git.postgresql.org/git/postgres-xl.git
cd postgres-xl/
./configure --prefix=/home/postgres/pgxl/
make
make install
cd contrib
make
make install
第十步:配置基礎(chǔ)環(huán)境變量
su - postgres
vim .bashrc
文件最后增加以下內(nèi)容:
export PGHOME=/home/postgres/pgxl
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH
第十一步:使環(huán)境變量生效并測(cè)試是否配置成功
source .bashrc
echo $PGHOME
mkdir pgxc
mkdir pgxc_bak
基礎(chǔ)環(huán)境安裝完成,以下步驟為集群配置,集群發(fā)起從GTM所在服務(wù)器進(jìn)行執(zhí)行即可
第一步:在postgres用戶根目錄下生成pgxc_ctl配置文件,創(chuàng)建集群文件存放目錄和備份目錄
pgxc_ctl
PGXC prepare
PGXG exit
image.png
第二步:修改pgxc_ctl.conf文件配置
cd pgxc_ctl/
vim pgxc_ctl.conf
#!/usr/bin/env bash
#
# Postgres-XC Configuration file for pgxc_ctl utility.
#
# Configuration file can be specified as -c option from pgxc_ctl command. Default is
# $PGXC_CTL_HOME/pgxc_ctl.org.
#
# This is bash script so you can make any addition for your convenience to configure
# your Postgres-XC cluster.
#
# Please understand that pgxc_ctl provides only a subset of configuration which pgxc_ctl
# provide. Here's several several assumptions/restrictions pgxc_ctl depends on.
#
# 1) All the resources of pgxc nodes has to be owned by the same user. Same user means
# user with the same user name. User ID may be different from server to server.
# This must be specified as a variable $pgxcOwner.
#
# 2) All the servers must be reacheable via ssh without password. It is highly recommended
# to setup key-based authentication among all the servers.
#
# 3) All the databases in coordinator/datanode has at least one same superuser. Pgxc_ctl
# uses this user to connect to coordinators and datanodes. Again, no password should
# be used to connect. You have many options to do this, pg_hba.conf, pg_ident.conf and
# others. Pgxc_ctl provides a way to configure pg_hba.conf but not pg_ident.conf. This
# will be implemented in the later releases.
#
# 4) Gtm master and slave can have different port to listen, while coordinator and datanode
# slave should be assigned the same port number as master.
#
# 5) Port nuber of a coordinator slave must be the same as its master.
#
# 6) Master and slave are connected using synchronous replication. Asynchronous replication
# have slight (almost none) chance to bring total cluster into inconsistent state.
# This chance is very low and may be negligible. Support of asynchronous replication
# may be supported in the later release.
#
# 7) Each coordinator and datanode can have only one slave each. Cascaded replication and
# multiple slave are not supported in the current pgxc_ctl.
#
# 8) Killing nodes may end up with IPC resource leak, such as semafor and shared memory.
# Only listening port (socket) will be cleaned with clean command.
#
# 9) Backup and restore are not supported in pgxc_ctl at present. This is a big task and
# may need considerable resource.
#
#========================================================================================
#
#
#使用pgxc_ctl部署時(shí)創(chuàng)建的基礎(chǔ)目錄,這里采用的是當(dāng)前postgres用戶根目錄下的pgxc文件夾。
pgxcInstallDir=$HOME/pgxc
#---- OVERALL -----------------------------------------------------------------------------
#
pgxcOwner=$USER # 用來(lái)操作Postgres-XC集群的服務(wù)器賬號(hào)和數(shù)據(jù)庫(kù)賬號(hào),數(shù)據(jù)庫(kù)賬號(hào)需要具有超級(jí)管理員權(quán)限,同時(shí)該用戶還應(yīng)是coordinator節(jié)點(diǎn)和datanode節(jié)點(diǎn)的超級(jí)管理員。這里我們直接采用的是postgres用戶。
pgxcUser=$pgxcOwner # 設(shè)置Postgres-XC 集群的超級(jí)管理員,直接使用剛預(yù)設(shè)的用戶就行。
tmpDir=/tmp # xc集群使用的臨時(shí)文件存放目錄
localTmpDir=$tmpDir # 本地使用的臨時(shí)目錄
configBackup=y # 是否開啟文件備份策略,n為關(guān)閉,y為開啟
configBackupHost=pgxc-linker #主備份配置文件 host to backup config file
configBackupDir=$HOME/pgxc_bak # 備份目錄
configBackupFile=pgxc_ctl.bak # 備份文件名稱
#---------------------------------------------------------------------------------------
#---- GTM配置 ------------------------------------------------------------------------------------
# GTM 配置是不可以缺少的,Postgres-XC集群中GTM至少具有一個(gè),通常結(jié)構(gòu)是一個(gè)GTM和一個(gè)備份GTM,如果GTM崩潰了,可以使用pgxc_update_gtm命令更新,同時(shí)也可以使用pgxc_remove_gtm進(jìn)行刪除它,刪除命令不會(huì)直接停止當(dāng)前的GTM,需要管理員先預(yù)先斷開其與GTM的連接。
#---- GTM master 主節(jié)點(diǎn)配置-----------------------------------------------
#---- Overall ----
gtmName=gtm #gtm的名字
gtmMasterServer=gtm #gtm所屬服務(wù)器,之前已經(jīng)配置了/etc/hosts中的主機(jī)名與IP的關(guān)系了,所以這里可以直接使用gtm,也可以使用IP 10.228.0.45
gtmMasterPort=20001 #gtm交互使用的端口號(hào)
gtmMasterDir=$HOME/pgxc/nodes/gtm #gtm所在目錄
#---- Configuration ---
gtmExtraConfig=none # master節(jié)點(diǎn)和slave節(jié)點(diǎn)使用的gtm.conf文件,只做初始化使用。默認(rèn)設(shè)置為無(wú)
gtmMasterSpecificExtraConfig=none # 主GTM的初始化配置文件,默認(rèn)設(shè)置為無(wú),上面已經(jīng)配置了基礎(chǔ)參數(shù)了,如果有自定義配置GTM,可以創(chuàng)建gtm.conf文件并配置到此處即可。
#---- GTM Slave -----------------------------------------------
# GTM的從節(jié)點(diǎn),用于GTM備份,在GTM主節(jié)點(diǎn)出現(xiàn)問(wèn)題時(shí),可以由該從節(jié)點(diǎn)進(jìn)行支撐處理。生產(chǎn)中配置該節(jié)點(diǎn)可以有效避免單點(diǎn)故障。
#---- Overall ------
gtmSlave=n # 開啟GTM從節(jié)點(diǎn)配置,y開啟,n關(guān)閉
#gtmSlaveName=gtmSlave #GTM從節(jié)點(diǎn)名稱
#gtmSlaveServer=node12 #GTM從節(jié)點(diǎn)部署服務(wù)器IP地址,如果在服務(wù)端已經(jīng)配置了hosts的映射關(guān)系,則可以直接使用名稱,否則需要配置為IP地址。
#gtmSlavePort=20001 # GTM從節(jié)點(diǎn)交互端口號(hào)
#gtmSlaveDir=$HOME/pgxc/nodes/gtm # GTM從節(jié)點(diǎn)部署目錄。
# 如果不配置GTM從節(jié)點(diǎn),則GTM主節(jié)點(diǎn)發(fā)生故障時(shí)無(wú)法有效進(jìn)行切換。管理員可以通過(guò)pgxc_add_gtm_slave進(jìn)行添加處理。
#---- Configuration ----
#gtmSlaveSpecificExtraConfig=none #GTM從節(jié)點(diǎn)初始化安裝時(shí)使用的配置文件,gtm.conf文件。
#---- GTM Proxy -------------------------------------------------------------------------------------------------------
# GTM Proxy將根據(jù)每個(gè)組件在哪個(gè)服務(wù)器上運(yùn)行來(lái)選擇
# 當(dāng)GTM從節(jié)點(diǎn)接替主節(jié)點(diǎn)時(shí),主節(jié)點(diǎn)的 gtm proxy需要指向到GTM從節(jié)點(diǎn)。因此GTM proxy應(yīng)該配置到一個(gè)新的位置,從而防止因?yàn)橹鞴?jié)點(diǎn)出現(xiàn)問(wèn)題時(shí),proxy也同時(shí)出現(xiàn)問(wèn)題無(wú)法指向到GTM從節(jié)點(diǎn)。
#
# GTM從節(jié)點(diǎn)重啟時(shí),更改pg_ctl的基礎(chǔ)文件,postgresql.conf和recovery.conf配置文件,再進(jìn)行執(zhí)行pg_ctl restart進(jìn)行重啟服務(wù)
#
# 如果不使用GTM slave,或者由GTM主節(jié)點(diǎn)直接管理各個(gè)節(jié)點(diǎn),則不需要進(jìn)行配置Proxy節(jié)點(diǎn),但配置了GTM slave,則必須配置Proxy節(jié)點(diǎn)。Proxy節(jié)點(diǎn)可以分擔(dān)GTM主節(jié)點(diǎn)的工作量,因此我們通常在配置時(shí)都將該P(yáng)roxy節(jié)點(diǎn)進(jìn)行配置。
#---- Shortcuts ------
gtmProxyDir=$HOME/pgxc/nodes/gtm_pxy #Proxy的存放路徑。
#---- Overall -------
gtmProxy=y # 是否開啟proxy節(jié)點(diǎn)配置,y開啟,n關(guān)閉,關(guān)閉時(shí)相關(guān)的配置信息將被設(shè)置為空值。
gtmProxyNames=(gtm_pxy1 gtm_pxy2) # proxy節(jié)點(diǎn)名稱
gtmProxyServers=(datanode1 datanode2) # 存放的服務(wù)器,可以配置多個(gè),這里配置在各個(gè)存儲(chǔ)節(jié)點(diǎn)服務(wù)器上,因其已經(jīng)在/etc/hosts中配置了映射關(guān)系,因此這里直接寫的主機(jī)名,如果沒(méi)有配置,則需要寫IP地址。
gtmProxyPorts=(20001 20001) # 交互使用的端口號(hào),上面配置了幾臺(tái)服務(wù)器,這里就需要配置幾個(gè)端口號(hào),每個(gè)端口號(hào)的所在位置和上面的服務(wù)器所在位置對(duì)應(yīng)。
gtmProxyDirs=($gtmProxyDir $gtmProxyDir) # 各服務(wù)器中proxy的存放路徑,這里直接統(tǒng)一使用一樣的,方便各個(gè)存儲(chǔ)節(jié)點(diǎn)中查找。
#---- Configuration ----
gtmPxyExtraConfig=none # proxy節(jié)點(diǎn)使用的 gtm_proxy配置文件
gtmPxySpecificExtraConfig=(none none) #各節(jié)點(diǎn)服務(wù)器中的具體配置文件
#------------------------------------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------------------------------
#---- Coordinators 配置----------------------------------------------------------------------------------------------------
#---- shortcuts ----------
coordMasterDir=$HOME/pgxc/nodes/coord
coordSlaveDir=$HOME/pgxc/nodes/coord_slave
coordArchLogDir=$HOME/pgxc/nodes/coord_archlog
#---- Overall ------------
coordNames=(coord1) # master和slave節(jié)點(diǎn)使用相同的名稱
coordPorts=(20004) # 主節(jié)點(diǎn)端口號(hào)
poolerPorts=(20010) # Master pooler ports
coordPgHbaEntries=(10.228.0.0/25) # 所有的coordinator (master/slave) 節(jié)點(diǎn)均接受的服務(wù)端IP段,該配置只接受 $pgxcOwner連接。如果不想放開指定段,可以使用coordExtraPgHba或 coordSpecificExtraPgHba參數(shù)進(jìn)行設(shè)置,只需要在()中填寫指定的IP即可。
#coordPgHbaEntries=(::1/128) # IPv6地址段。
#---- Master -------------
coordMasterServers=(gtm) # 主節(jié)點(diǎn)所在服務(wù)器,同樣的使用的是主機(jī)名映射,可以設(shè)置IP
coordMasterDirs=($coordMasterDir) #Coordinators 主節(jié)點(diǎn)存放地址
coordMaxWALsernder=5 # 配置從節(jié)點(diǎn)最大數(shù)量,如果配置0,則按照下面配置的外部文件進(jìn)行查找該參數(shù),如果不配置從節(jié)點(diǎn),可以將該值設(shè)置為0.
coordMaxWALSenders=($coordMaxWALsernder)
# 每個(gè) coordinator節(jié)點(diǎn)的最大協(xié)調(diào)數(shù)量,即從節(jié)點(diǎn)在交互時(shí)需要和各個(gè)節(jié)點(diǎn)進(jìn)行交互,因此這里直接配置和上面從節(jié)點(diǎn)最大數(shù)量一致即可。
#---- Slave -------------
coordSlave=n # 凡是具有 coordiantor slave節(jié)點(diǎn),則該配置必須設(shè)置為y,否則設(shè)置為n,當(dāng)設(shè)置為n時(shí),以下關(guān)于slave節(jié)點(diǎn)的配置將被設(shè)置為none.
coordSlaveSync=y # 開啟同步模式連接。
coordSlaveServers=(datanode1 datanode2) #從節(jié)點(diǎn)所在服務(wù)器,同上設(shè)置一樣,使用主機(jī)名或者IP的方式進(jìn)行設(shè)置。
coordSlavePorts=(20004 20005 20004 20005) # 各節(jié)點(diǎn)主要交互端口
coordSlavePoolerPorts=(20010 20011 20010 20011) #主交互池端口 pooler ports
coordSlaveDirs=($coordSlaveDir $coordSlaveDir)
coordArchLogDirs=($coordArchLogDir $coordArchLogDir)
#---- Configuration files---
# 設(shè)置特定的非默認(rèn)配置,可以通過(guò)bash腳本或者額外的pg_hba.conf提供。
coordExtraConfig=coordExtraConfig #coordinators的額外配置文件,即上面提到的外部配置文件,額外的配置將應(yīng)用到所有的coordinators節(jié)點(diǎn)的postgresql.conf配置文件中。以下為設(shè)置的最小參數(shù),這里可以將自己的postgresql.conf配置增加到下面。
cat > $coordExtraConfig <<EOF
#================================================
# Added to all the coordinator postgresql.conf
# Original: $coordExtraConfig
log_destination = 'stderr'
logging_collector = on
log_directory = 'pg_log'
listen_addresses = '*'
max_connections = 100
EOF
#針對(duì)coordinator master節(jié)點(diǎn)附加的配置文件。
# You can define each setting by similar means as above.
coordSpecificExtraConfig=(none none)
coordExtraPgHba=none # 設(shè)置 pg_hba.conf文件,該配置將應(yīng)用到所有的coordinator節(jié)點(diǎn)的 pg_hba.conf文件中。
coordSpecificExtraPgHba=(none none)
#----- Additional Slaves -----
#
# 以下配置為額外的多重級(jí)聯(lián)復(fù)制配置,該配置不在當(dāng)前使用的版本中使用。
#
coordAdditionalSlaves=n # Additional slave can be specified as follows: where you
coordAdditionalSlaveSet=(cad1) # Each specifies set of slaves. This case, two set of slaves are
# configured
cad1_Sync=n # All the slaves at "cad1" are connected with asynchronous mode.
# If not, specify "y"
# The following lines specifies detailed configuration for each
# slave tag, cad1. You can define cad2 similarly.
cad1_Servers=(datanode1 datanode2) # Hosts
cad1_dir=$HOME/pgxc/nodes/coord_slave_cad1
cad1_Dirs=($cad1_dir $cad1_dir)
cad1_ArchLogDir=$HOME/pgxc/nodes/coord_archlog_cad1
cad1_ArchLogDirs=($cad1_ArchLogDir $cad1_ArchLogDir)
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
#---- Datanodes 配置-------------------------------------------------------------------------------
#---- Shortcuts --------------
datanodeMasterDir=$HOME/pgxc/nodes/dn_master
datanodeSlaveDir=$HOME/pgxc/nodes/dn_slave
datanodeArchLogDir=$HOME/pgxc/nodes/datanode_archlog
#---- Overall ---------------
#primaryDatanode=datanode1
# At present, xc has a priblem to issue ALTER NODE against the primay node. Until it is fixed, the test will be done
# without this feature.
primaryDatanode=datanode1 # 主節(jié)點(diǎn)
datanodeNames=(datanode1 datanode2) #所有節(jié)點(diǎn)名稱
datanodePorts=(20008 20008) #主交互端口
datanodePoolerPorts=(20012 20012) # 主交互池端口
datanodePgHbaEntries=(10.228.0.0/25) # 所有 coordinator (master/slave) 節(jié)點(diǎn)可接受的連接IP段,該通訊只允許pg_hba.conf中的$pgxcOwner 用戶通過(guò)。如果要設(shè)置其他的策略,需通過(guò)下面配置的額外配置文件進(jìn)行制定。如果要設(shè)置指定的IP通過(guò),可以通過(guò)datanodeExtraPgHba 和 datanodeSpecificExtraPgHba 參數(shù)進(jìn)行設(shè)置。
#datanodePgHbaEntries=(::1/128) # 和上面一樣的策略,但過(guò)濾的IP為IPv6協(xié)議。
#---- Master ----------------
datanodeMasterServers=(datanode1 datanode2) # 所有節(jié)點(diǎn)的服務(wù)器IP地址,可以設(shè)置hosts中映射的值或IP地址。不可設(shè)置為無(wú),否則集群將不能運(yùn)行。
datanodeMasterDirs=($datanodeMasterDir $datanodeMasterDir)
datanodeMaxWalSender=5 # 最大配置從節(jié)點(diǎn),如果為0則需要在外部配置文件中進(jìn)行設(shè)置,如果不配置從節(jié)點(diǎn),則此值可以設(shè)置為0。
datanodeMaxWALSenders=($datanodeMaxWalSender $datanodeMaxWalSender)
# 每個(gè)節(jié)點(diǎn)發(fā)起數(shù)量。
#---- Slave -----------------
datanodeSlave=n # 存儲(chǔ)從節(jié)點(diǎn)是否開啟,設(shè)置y開啟,則每個(gè)節(jié)點(diǎn)至少配置一個(gè)Coordinator從節(jié)點(diǎn)。如果沒(méi)有有效的服務(wù)器名稱,則以下相關(guān)參數(shù)值將采用none。
datanodeSlaveServers=(datanode1 datanode2) # 各存儲(chǔ)從節(jié)點(diǎn)服務(wù)器地址,可用映射名或IP
datanodeSlavePorts=(20008 20008) # 各存儲(chǔ)從節(jié)點(diǎn)主交互端口
datanodeSlavePoolerPorts=(20012 20012) # 各存儲(chǔ)從節(jié)點(diǎn)主交互池端口
datanodeSlaveSync=y # 是否開啟存儲(chǔ)從節(jié)點(diǎn)同步模式
datanodeSlaveDirs=($datanodeSlaveDir $datanodeSlaveDir)
datanodeArchLogDirs=( $datanodeArchLogDir $datanodeArchLogDir)
# ---- Configuration files ---
# 以下設(shè)置為配置自定義外部bash腳本或外部pg_hba.conf文件
datanodeExtraConfig=none # 額外的datanodes配置文件. 該文件中的配置將添加到所有存儲(chǔ)節(jié)點(diǎn)的postgresql.conf中。
datanodeSpecificExtraConfig=(none none)
datanodeExtraPgHba=none # 額外的 pg_hba.conf。
datanodeSpecificExtraPgHba=(none none)
#----- Additional Slaves -----
datanodeAdditionalSlaves=n # 是否開啟額外的slave節(jié)點(diǎn)配置
# datanodeAdditionalSlaveSet=(dad1 dad2) # 各節(jié)點(diǎn)名稱
# dad1_Sync=n # 所有節(jié)點(diǎn)都以異步的方式進(jìn)行連接,如果不采用異步,則設(shè)置為y。
# dad1_Servers=(node08 node09 node06 node07) # 各節(jié)點(diǎn)所在服務(wù)器地址,映射名或IP地址
# dad1_dir=$HOME/pgxc/nodes/coord_slave_cad1
# dad1_Dirs=($cad1_dir $cad1_dir $cad1_dir $cad1_dir)
# dad1_ArchLogDir=$HOME/pgxc/nodes/coord_archlog_cad1
# dad1_ArchLogDirs=($cad1_ArchLogDir $cad1_ArchLogDir $cad1_ArchLogDir $cad1_ArchLogDir)
#---- WAL archives -------------------------------------------------------------------------------------------------
walArchive=n # wal日志記錄,開啟則設(shè)置y
#
# 基礎(chǔ)配置完成
#
#==========================================================================================================================
#========================================================================================================================
# The following is for extension. Just demonstrate how to write such extension. There's no code
# which takes care of them so please ignore the following lines. They are simply ignored by pgxc_ctl.
# No side effects.
#=============<< Beginning of future extension demonistration >> ========================================================
# You can setup more than one backup set for various purposes, such as disaster recovery.
walArchiveSet=(war1 war2)
war1_source=(master) # you can specify master, slave or ano other additional slaves as a source of WAL archive.
# Default is the master
wal1_source=(slave)
wal1_source=(additiona_coordinator_slave_set additional_datanode_slave_set)
war1_host=node10 # All the nodes are backed up at the same host for a given archive set
war1_backupdir=$HOME/pgxc/backup_war1
wal2_source=(master)
war2_host=node11
war2_backupdir=$HOME/pgxc/backup_war2
#=============<< End of future extension demonistration >> ========================================================
更多參數(shù)參考:http://postgres-xc.sourceforge.net/docs/1_1/pgxc-ctl.html
第三步:執(zhí)行初始化
pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf init all
第四步:?jiǎn)?dòng)集群
默認(rèn)初始化完成后已經(jīng)啟動(dòng)了集群了,平常進(jìn)行開啟關(guān)閉使用以下語(yǔ)句
pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf start all
pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf stop all
第五步:進(jìn)入psql
psql -p 20004
進(jìn)入其他遠(yuǎn)程節(jié)點(diǎn)
psql -h datanode2 -p 20008 -U postgres
查詢集群狀態(tài)
select * from pgxc_node;
image.png