在Centos7環境下進行搭建postgresql-xl集群環境


本次使用環境centos7,
共3台服務器,基礎配置采用4核4G內存200G機械硬盤

在初裝系統機器中開始進行以下操作

使用root賬號開始進行以下操作
目前安裝的xl兼容版本為postgresql10版本。
第一步:所有集群中的服務器均進行初始修改/etc/hosts,增加內容如下:
10.228.0.45 gtm
10.228.0.46 datanode1
10.228.0.47 datanode2
第二步關閉防火牆
systemctl stop firewalld.service
systemctl disable firewalld.service
第三步 關閉selinux
vim /etc/selinux/config
SELINUX=disabled
第四步基礎依賴包安裝
yum install -y flex bison readline-devel zlib-devel openjade docbook-style-dsssl git gcc
第五步重啟使其之前配置生效
reboot
第六步: 創建postgres用戶
useradd postgres
passwd postgres
第七步:設置ssh免密,這里注意要每個節點均創建.ssh目錄和其權限設置,免密的公鑰私鑰可以不進行創建
su - postgres
mkdir ~/.ssh
chmod 700 ~/.ssh
ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
第八步:將gtm節點的ssh免密文件復制到datanode存儲節點
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
第十步:配置基礎環境變量
su - postgres
vim .bashrc
文件最后增加以下內容:
export PGHOME=/home/postgres/pgxl
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH
第十一步:使環境變量生效並測試是否配置成功
source .bashrc
echo $PGHOME
mkdir pgxc
mkdir pgxc_bak

基礎環境安裝完成,以下步驟為集群配置,集群發起從GTM所在服務器進行執行即可

第一步:在postgres用戶根目錄下生成pgxc_ctl配置文件,創建集群文件存放目錄和備份目錄
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部署時創建的基礎目錄,這里采用的是當前postgres用戶根目錄下的pgxc文件夾。
pgxcInstallDir=$HOME/pgxc
#---- OVERALL -----------------------------------------------------------------------------
#
pgxcOwner=$USER			# 用來操作Postgres-XC集群的服務器賬號和數據庫賬號,數據庫賬號需要具有超級管理員權限,同時該用戶還應是coordinator節點和datanode節點的超級管理員。這里我們直接采用的是postgres用戶。
pgxcUser=$pgxcOwner		# 設置Postgres-XC 集群的超級管理員,直接使用剛預設的用戶就行。

tmpDir=/tmp					# xc集群使用的臨時文件存放目錄
localTmpDir=$tmpDir			# 本地使用的臨時目錄

configBackup=y				# 是否開啟文件備份策略,n為關閉,y為開啟
configBackupHost=pgxc-linker	#主備份配置文件 host to backup config file
configBackupDir=$HOME/pgxc_bak		# 備份目錄
configBackupFile=pgxc_ctl.bak	# 備份文件名稱

#---------------------------------------------------------------------------------------
#---- GTM配置 ------------------------------------------------------------------------------------

# GTM 配置是不可以缺少的,Postgres-XC集群中GTM至少具有一個,通常結構是一個GTM和一個備份GTM,如果GTM崩潰了,可以使用pgxc_update_gtm命令更新,同時也可以使用pgxc_remove_gtm進行刪除它,刪除命令不會直接停止當前的GTM,需要管理員先預先斷開其與GTM的連接。


#---- GTM master 主節點配置-----------------------------------------------

#---- Overall ----
gtmName=gtm #gtm的名字
gtmMasterServer=gtm #gtm所屬服務器,之前已經配置了/etc/hosts中的主機名與IP的關系了,所以這里可以直接使用gtm,也可以使用IP 10.228.0.45
gtmMasterPort=20001 #gtm交互使用的端口號
gtmMasterDir=$HOME/pgxc/nodes/gtm #gtm所在目錄

#---- Configuration ---
gtmExtraConfig=none			# master節點和slave節點使用的gtm.conf文件,只做初始化使用。默認設置為無
gtmMasterSpecificExtraConfig=none	# 主GTM的初始化配置文件,默認設置為無,上面已經配置了基礎參數了,如果有自定義配置GTM,可以創建gtm.conf文件並配置到此處即可。

#---- GTM Slave -----------------------------------------------

# GTM的從節點,用於GTM備份,在GTM主節點出現問題時,可以由該從節點進行支撐處理。生產中配置該節點可以有效避免單點故障。

#---- Overall ------
gtmSlave=n					# 開啟GTM從節點配置,y開啟,n關閉
#gtmSlaveName=gtmSlave              #GTM從節點名稱
#gtmSlaveServer=node12		#GTM從節點部署服務器IP地址,如果在服務端已經配置了hosts的映射關系,則可以直接使用名稱,否則需要配置為IP地址。
#gtmSlavePort=20001			# GTM從節點交互端口號
#gtmSlaveDir=$HOME/pgxc/nodes/gtm	# GTM從節點部署目錄。
# 如果不配置GTM從節點,則GTM主節點發生故障時無法有效進行切換。管理員可以通過pgxc_add_gtm_slave進行添加處理。

#---- Configuration ----
#gtmSlaveSpecificExtraConfig=none #GTM從節點初始化安裝時使用的配置文件,gtm.conf文件。

#---- GTM Proxy -------------------------------------------------------------------------------------------------------
# GTM Proxy將根據每個組件在哪個服務器上運行來選擇
# 當GTM從節點接替主節點時,主節點的 gtm proxy需要指向到GTM從節點。因此GTM proxy應該配置到一個新的位置,從而防止因為主節點出現問題時,proxy也同時出現問題無法指向到GTM從節點。
#
# GTM從節點重啟時,更改pg_ctl的基礎文件,postgresql.conf和recovery.conf配置文件,再進行執行pg_ctl restart進行重啟服務
#
# 如果不使用GTM slave,或者由GTM主節點直接管理各個節點,則不需要進行配置Proxy節點,但配置了GTM slave,則必須配置Proxy節點。Proxy節點可以分擔GTM主節點的工作量,因此我們通常在配置時都將該Proxy節點進行配置。
#---- Shortcuts ------
gtmProxyDir=$HOME/pgxc/nodes/gtm_pxy #Proxy的存放路徑。

#---- Overall -------
gtmProxy=y				# 是否開啟proxy節點配置,y開啟,n關閉,關閉時相關的配置信息將被設置為空值。
gtmProxyNames=(gtm_pxy1 gtm_pxy2)	# proxy節點名稱
gtmProxyServers=(datanode1 datanode2)			# 存放的服務器,可以配置多個,這里配置在各個存儲節點服務器上,因其已經在/etc/hosts中配置了映射關系,因此這里直接寫的主機名,如果沒有配置,則需要寫IP地址。
gtmProxyPorts=(20001 20001)				# 交互使用的端口號,上面配置了幾台服務器,這里就需要配置幾個端口號,每個端口號的所在位置和上面的服務器所在位置對應。
gtmProxyDirs=($gtmProxyDir $gtmProxyDir)	# 各服務器中proxy的存放路徑,這里直接統一使用一樣的,方便各個存儲節點中查找。

#---- Configuration ----
gtmPxyExtraConfig=none		# proxy節點使用的 gtm_proxy配置文件
gtmPxySpecificExtraConfig=(none none)  #各節點服務器中的具體配置文件

#------------------------------------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------------------------------
#---- Coordinators 配置----------------------------------------------------------------------------------------------------

#---- shortcuts ----------
coordMasterDir=$HOME/pgxc/nodes/coord
coordSlaveDir=$HOME/pgxc/nodes/coord_slave
coordArchLogDir=$HOME/pgxc/nodes/coord_archlog

#---- Overall ------------
coordNames=(coord1)		# master和slave節點使用相同的名稱
coordPorts=(20004)			# 主節點端口號
poolerPorts=(20010)			# Master pooler ports
coordPgHbaEntries=(10.228.0.0/25)				# 所有的coordinator (master/slave) 節點均接受的服務端IP段,該配置只接受 $pgxcOwner連接。如果不想放開指定段,可以使用coordExtraPgHba或 coordSpecificExtraPgHba參數進行設置,只需要在()中填寫指定的IP即可。
#coordPgHbaEntries=(::1/128)	# IPv6地址段。

#---- Master -------------
coordMasterServers=(gtm)		# 主節點所在服務器,同樣的使用的是主機名映射,可以設置IP
coordMasterDirs=($coordMasterDir) #Coordinators 主節點存放地址
coordMaxWALsernder=5	# 配置從節點最大數量,如果配置0,則按照下面配置的外部文件進行查找該參數,如果不配置從節點,可以將該值設置為0.
coordMaxWALSenders=($coordMaxWALsernder)
						# 每個 coordinator節點的最大協調數量,即從節點在交互時需要和各個節點進行交互,因此這里直接配置和上面從節點最大數量一致即可。

#---- Slave -------------
coordSlave=n			# 凡是具有 coordiantor slave節點,則該配置必須設置為y,否則設置為n,當設置為n時,以下關於slave節點的配置將被設置為none.  
coordSlaveSync=y		# 開啟同步模式連接。
coordSlaveServers=(datanode1 datanode2)			#從節點所在服務器,同上設置一樣,使用主機名或者IP的方式進行設置。
coordSlavePorts=(20004 20005 20004 20005)			# 各節點主要交互端口
coordSlavePoolerPorts=(20010 20011 20010 20011)			#主交互池端口 pooler ports
coordSlaveDirs=($coordSlaveDir $coordSlaveDir)
coordArchLogDirs=($coordArchLogDir $coordArchLogDir)

#---- Configuration files---
# 設置特定的非默認配置,可以通過bash腳本或者額外的pg_hba.conf提供。
coordExtraConfig=coordExtraConfig	#coordinators的額外配置文件,即上面提到的外部配置文件,額外的配置將應用到所有的coordinators節點的postgresql.conf配置文件中。以下為設置的最小參數,這里可以將自己的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


#針對coordinator master節點附加的配置文件。
# You can define each setting by similar means as above.
coordSpecificExtraConfig=(none none)
coordExtraPgHba=none	# 設置 pg_hba.conf文件,該配置將應用到所有的coordinator節點的 pg_hba.conf文件中。
coordSpecificExtraPgHba=(none none)

#----- Additional Slaves -----
#
# 以下配置為額外的多重級聯復制配置,該配置不在當前使用的版本中使用。
#
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				# 主節點
datanodeNames=(datanode1 datanode2) #所有節點名稱
datanodePorts=(20008 20008)	#主交互端口
datanodePoolerPorts=(20012 20012)	# 主交互池端口
datanodePgHbaEntries=(10.228.0.0/25)	# 所有 coordinator (master/slave) 節點可接受的連接IP段,該通訊只允許pg_hba.conf中的$pgxcOwner 用戶通過。如果要設置其他的策略,需通過下面配置的額外配置文件進行制定。如果要設置指定的IP通過,可以通過datanodeExtraPgHba 和 datanodeSpecificExtraPgHba 參數進行設置。
#datanodePgHbaEntries=(::1/128)	# 和上面一樣的策略,但過濾的IP為IPv6協議。

#---- Master ----------------
datanodeMasterServers=(datanode1 datanode2)	# 所有節點的服務器IP地址,可以設置hosts中映射的值或IP地址。不可設置為無,否則集群將不能運行。
datanodeMasterDirs=($datanodeMasterDir $datanodeMasterDir)
datanodeMaxWalSender=5								# 最大配置從節點,如果為0則需要在外部配置文件中進行設置,如果不配置從節點,則此值可以設置為0。
datanodeMaxWALSenders=($datanodeMaxWalSender $datanodeMaxWalSender)
						# 每個節點發起數量。

#---- Slave -----------------
datanodeSlave=n			# 存儲從節點是否開啟,設置y開啟,則每個節點至少配置一個Coordinator從節點。如果沒有有效的服務器名稱,則以下相關參數值將采用none。
datanodeSlaveServers=(datanode1 datanode2)	# 各存儲從節點服務器地址,可用映射名或IP
datanodeSlavePorts=(20008 20008)	# 各存儲從節點主交互端口
datanodeSlavePoolerPorts=(20012 20012)	# 各存儲從節點主交互池端口
datanodeSlaveSync=y		# 是否開啟存儲從節點同步模式
datanodeSlaveDirs=($datanodeSlaveDir $datanodeSlaveDir)
datanodeArchLogDirs=( $datanodeArchLogDir $datanodeArchLogDir)

# ---- Configuration files ---
# 以下設置為配置自定義外部bash腳本或外部pg_hba.conf文件
datanodeExtraConfig=none	# 額外的datanodes配置文件. 該文件中的配置將添加到所有存儲節點的postgresql.conf中。
datanodeSpecificExtraConfig=(none none)
datanodeExtraPgHba=none		# 額外的 pg_hba.conf。
datanodeSpecificExtraPgHba=(none none)

#----- Additional Slaves -----
datanodeAdditionalSlaves=n	# 是否開啟額外的slave節點配置
# datanodeAdditionalSlaveSet=(dad1 dad2)		# 各節點名稱
# dad1_Sync=n		  		# 所有節點都以異步的方式進行連接,如果不采用異步,則設置為y。
# dad1_Servers=(node08 node09 node06 node07)	# 各節點所在服務器地址,映射名或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日志記錄,開啟則設置y
#
#		基礎配置完成
#
#==========================================================================================================================

#========================================================================================================================
# 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 >> ========================================================

更多參數參考:http://postgres-xc.sourceforge.net/docs/1_1/pgxc-ctl.html

第三步:執行初始化
pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf init all 
第四步:啟動集群

默認初始化完成后已經啟動了集群了,平常進行開啟關閉使用以下語句

pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf start all
pgxc_ctl -c /home/postgres/pgxc_ctl/pgxc_ctl.conf stop all 
第五步:進入psql
psql -p 20004
進入其他遠程節點
psql -h datanode2 -p 20008 -U postgres
查詢集群狀態
select * from pgxc_node;

image.png

至此整個集群環境搭建完成。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM