Postgres-xl 簡介
Postgres的-XL是一個基於PostgreSQL數據庫的橫向擴展開源SQL數據庫集羣,具有足夠的靈活性來處理不同的數據庫工作負載:
- 完全ACID,保持事務一致性
- OLTP 寫頻繁的業務
- 需要MPP並行性商業智能/大數據分析
- 操作數據存儲
- Key-value 存儲
- GIS的地理空間
- 混合業務工作環境
- 多租戶服務提供商託管環境
- Web 2.0
如圖所示:(系統圖)
組件介紹
Global Transaction Monitor (GTM)
全局事務管理器,確保羣集範圍內的事務一致性。
GTM負責發放事務ID和快照作爲其多版本併發控制的一部分。
集羣可選地配置一個備用GTM,以改進可用性。
此外,可以在協調器間配置代理GTM,可用於改善可擴展性,減少GTM的通信量。GTM Standby
GTM的備節點,在pgxc,pgxl中,GTM
控制所有的全局事務分配,如果出現問題,就會導致整個集羣不可用,
爲了增加可用性,增加該備用節點。當GTM出現問題時,GTMStandby
可以升級爲GTM,保證集羣正常工作。GTM-Proxy
GTM需要與所有的Coordinators通信,爲了降低壓力,
可以在每個Coordinator機器上部署一個GTM-Proxy。Coordinator
協調員管理用戶會話,並與GTM和數據節點進行交互。
協調員解析,並計劃查詢,並給語句中的每一個組件發送下一個序列化的全局性計劃。
爲節省機器,通常此服務和數據節點部署在一起。Data Node
數據節點是數據實際存儲的地方。數據的分佈可以由
DBA來配置。爲了提高可用性,可以配置數據節點的熱備以便進行故障轉移準備。
總結:
gtm是負責ACID的,保證分佈式數據庫全局事務一致性。得益於此,就算數據節點是分佈的,但是你在主節點操作增刪改查事務時,就如同只操作一個數據庫一樣簡單。
Coordinator是調度的,將操作指令發送到各個數據節點。
datanodes是數據節點,分佈式存儲數據。
安裝規劃表:
注意:PGHOME=/home/pgxl_9.5
安裝環境介紹
1).操作環境
Window7 64位系統 電腦一臺
2).使用工具
putty:連接服務器
pscp:主要用於windows向linux發送文件,或由linux系統向windows發送文件;
3).服務器
3臺服務器,並各自建立有3個虛擬機,系統版本統一爲 Ubuntu(16.04.3)系統;
4).postgres-xl 版本: postgres-xl-9.5r1.4.tar.gz
安裝準備
概述:安裝準備,主要進行安裝時,一些準備工作。
1)下載方式:
a.git方式下載:
git clone git://git.postgresql.org/git/postgres-xl.git
注意:請安裝git,可採用sudo apt-get install git
b.網頁下載:
網址爲:https://sourceforge.net/projects/postgres-xl/
下載完後將軟件包放入D盤根目錄下
注意:這裏採用網頁方式下載,主要是因爲ubuntu安裝的git,下載該軟件包比較麻煩。
2)將下載好的壓縮包傳送到規劃表中IP地址172.17.30.15 -- 172.17.30.22 共8個系統中
a.採用Putty連接服務器:
(1)點擊Session:
Host Name(or IP address):輸入連接服務器的IP,點擊Open連接到服務器
(2)在根目錄下新建文件夾 postgres-xl_installPackage ,用以存放postgres-xl壓縮包
b.採用pscp傳送文件:
(1)下載pscp工具:
下載網址:https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
(2)將下載好的pscp.exe文件放到putty.exe一樣的目錄
(3)開始-運行-打開cmd窗口:
1.進入到pscp.exe目錄
2.輸入如下命令進行傳送:
pscp 傳送文件 主機名@IP:傳送目錄
如:
pscp D:\postgres-xl-9.5r1.4.tar.gz [email protected]:./postgres-xl_installPackage
安裝Postgres-xl前配置
概述:介紹在Postgres-xl安裝前,一些配置。
注意:除了安裝負載均衡的服務器外,都要安裝。
分爲:
1:解壓postgres-xl壓縮包
2:創建一個用戶,用戶名爲postgres,密碼爲123456
3:爲postgres用戶設置免密登錄
4:修改/etc/hosts中內容
5:安裝postgrex-xl相關依賴包
6:編譯安裝postgres-xl
7:設置postgres-xl環境變量
1 解壓postgrex-xl壓縮包
1)將文件夾postgres_installPackage 目錄下的壓縮包解壓
a.進入壓縮包目錄cd /postgres_installPackage
b.解壓文件:sudo tar –zvxf postgres-xl-9.5rl.4.tar.gz
2 創建一個用戶,用戶名爲postgres,密碼爲123456
1)創建用戶,用戶名:posgres,密碼爲:123456
a.創建用戶:sudo adduser postgres
b.設置用戶密碼:按提示輸入即可
c.如下圖所示
d.刪除用戶時,使用命令:sudo deluser 用戶名
注意:建議不使用sudo useradd postgres創建,如果創建了,請輸入sudo userdel postgres刪除。
注意:除了安裝負載均衡的服務器外,都要安裝。
如圖所示:
3 爲postgres用戶設置免密登錄
在ubuntu中用戶輸入:sudo usermod –a –G sudo postgres
切換用戶爲postgres: 輸入如下命令sudo postgres
1)設置密鑰
輸入命令 sudo ssh-keygen,一直接按enter即可
注意:如果sudo ssh-keygen有錯誤,可嘗試ssh-keygen
如圖所示:
2)密鑰傳輸(除了當前IP地址,每個IP地址都傳輸一次)
命令:ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
注意:!!!
1.除了安裝負載均衡的服務器外,都要進行密鑰傳輸。可以簡單理解:假如有三臺服務器用來部署postgres-xl,分別A,B,C.在進行密鑰傳輸時,A的密鑰,傳輸給B,C;B的密鑰,傳輸給A,C;C的密鑰,傳送給A,B;
2.主要修改[email protected]。將IP=172.17.30.204改爲要傳輸服務器的IP即可
3.可以通過ifconfig 進行查詢Ip
如圖所示:配置IP:172.17.30.204 傳送到172.17.30.204(例子)
4 修改/etc/hosts中內容
概述:配置主機IP及對應的主機名
1).採用命令:sudo vi /etc/hosts
2).驗證:ping Ip eg: ping 172.17.30.204
3).如果驗證不通過,可能需要重啓:sudo reboot
注意: !!!
1.除了安裝負載均衡的服務器外,都要進行修改。
2.每臺服務器(除了安裝負載均衡的服務器),都要添加(除了安裝負載均衡的服務器)的所有IP和主機名
eg:如果有三臺服務器,每臺服務器添加
添加內容:
172.17.30.15 devcenter015
172.17.30.16 devcenter016
172.17.30.17 devcenter017
172.17.30.18 devcenter018
172.17.30.19 devcenter019
172.17.30.20 devcenter020
172.17.30.21 devcenter021
172.17.30.22 devcenter022
172.17.30.204 devcenter04
172.17.30.215 devcenter15
172.17.30.220 devcenter20
如圖所示:
5 安裝postgres-xl相關依賴包
概述:安裝postgres-xl進行編譯時,所需要的依賴包
依賴包:
make 、flex、bison、openjade、docbook-dsssl
readline、zlib
ubuntu:安裝命令sudo apt-get install 安裝包名
注意:
1.readline,zlib在ubuntu就存在了
2.可以通過如下命令進行查看:
ldconfig -p | grep readline
3.可以安裝: sudo apt-get install libreadline6-dev
6 編譯安裝postgres-xl
概述:如何安裝postgres-xl
注意:如下操作,請切換到postgres用戶下進行。採用
su postgres切換
1).編譯安裝postgres-xl
a.在/home/postgres下創建目錄
採用命令: sudo mkdir /home/postgres/pgxl_9.5
b.進入到postgres-xl的解壓目錄
eg:
cd /postgres-xl_installPackpage/pgxlgres-xl-9.5rl.4
c.執行如下命令:
./configure --prefix=/home/postgres/pgxl_9.5 --without-readline --without-zlib
主要是將編譯好的一些文件放在指定目錄下
d.執行如下命令:(編譯)
make
e.執行安裝命令:
sudo make install
注意:sudo make install有錯誤時,請執行make install安裝
如圖所示:(./configure –prefex=/home/postgres/pgxl_9.5)
如圖所示:(make)
如圖所示:(sudo make install)
7 設置postgres環境變量
概述:主要將/home/postgres/.bashrc添加內容
內容如下:
#set postgres path
export PGHOME=/home/postgres/pgxl_9.5
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH
1). 使得修改的內容生效:source /home/postgres/.bashrc
2).驗證如下:輸入echo $PGHOME 出現:/home/postgres/pgxl_9.5
初始化gtm,coordinator,datanode
概述:初始化gtm,coordinator,datanode生成相關配置文件
1).根據規劃表格,創建相應目錄gtm,coordinator,datanode目錄
eg:
在172.17.30.16的創建/home/postgres/pgxl_9.5/data,以及/home/postgres/pgxl_9.5/data/dn1和/home/postgres/pgxl_9.5/data/dn2目錄
2).初始化gtm角色
a.設置目錄權限:eg:sudo chown postgres /home/postgres/pgxl_9.5/data/gtm
b.執行命令:initgtm -Z gtm -D /home/postgres/pgxl_9.5/data/gtm
3.初始化coordinator角色
a.設置目錄權限:eg:sudo chown postgres /home/postgres/pgxl_9.5/data/coord1
b.執行命令:initdb -D /home/postgres/pgxl_9.5/data/coord1 --nodename coord1 -E UTF8 --locale=C -U postgres -W
4.初始化datanode角色
a.設置目錄權限:eg:sudo chown postgres /home/postgres/pgxl_9.5/data/dn1
b.執行命令:initdb -D /home/postgres/pgxl_9.5/data/dn1 --nodename dn1 -E UTF8 --locale=C -U postgres –W
注意:
nodename dn1:根據創建目錄名字修改。如/home/postgres/pgxl_9.5/data/dn3初始化時,爲initdb -D /home/postgres/pgxl_9.5/data/dn3 --nodename dn3 -E UTF8 --locale=C -U postgres –W
注意:
1.請在安規表格中分配角色,進行相應的初始化操作。如,172.17.30.215中分配兩個datanode角色,相應目錄爲/home/postgres/pgxl_9.5/data/dn1,/home/postgres/pgxl_9.5/data/dn2,所以初始化時執行initdb -D /home/postgres/pgxl_9.5/data/dn1 --nodename dn1 -E UTF8 --locale=C -U postgres –W和initdb -D /home/postgres/pgxl_9.5/data/dn2 --nodename dn2 -E UTF8 --locale=C -U postgres –W
2.如果不執行設置目錄權限,可能直接執行初始化命令,會出錯
如圖所示:(初始化gtm)
如圖所示:(初始化coordinator)
如圖所示:(初始化datanode)
修改postgrex-xl配置文件
概述:根據初始化gtm,coordinator,datanode生成的配置文件,進行相應的修改
1).修改gtm生成的配置文件:gtm.conf.文件所在目錄爲:/home/postgres/pgxl_9.5/data/gtm
修改內容爲:
nodename = 'gtm'
listen_addresses = '*'
port =6666 #gtm端口
startup = ACT
注意:有的前面有‘#’,去掉#號
2).修改coordinator生成的配置文件:postgresql.conf文件。文件所在目錄:/home/postgres/pgxl_9.5/coordinator角色目錄。eg:/home/postgres/pgxl_9.5/coord1
修改內容:
# - Connection Settings - (修改一)
listen_addresses = '*'
port = 5301 #coordinator端口設置,coord1和coord2端口都爲5301
max_connections = 100
# DATA NODES AND CONNECTION POOLING (修改二)
#----------------------------------
pooler_port = 6701 #coord2這個端口也改爲6701
max_pool_size = 100
# GTM CONNECTION
#--------------------------
gtm_host = '172.17.30.16' # gtm所在的主機地址
gtm_port = 6666 #gtm配置中,gtm端口號配置爲6666
注意:根據實際情況進行修改
3).修改coordinator,datanode中的pg_hba.conf文件,文件所在目錄:/home/postgres/pgxl_9.5/相應角色目錄。eg:/home/postgres/pgxl_9.5/coord1
修該內容:
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 0.0.0.0/0 trust
# IPv6 local connections:
host all all ::1/128 trust
4).修改datanode生成的配置文件:postgresql.conf文件。文件所在目錄:/home/postgres/pgxl_9.5/datanode角色目錄。eg:/home/postgres/pgxl_9.5/coord1
修改內容:
# - Connection Settings - (修改一)
listen_addresses = '*'
port = 5401 #注意:同一臺服務器中端口不同,但不同服務器的端口可以相同dn2,dn3,dn4配置文件分別改爲5402、5401、5402,dn3和dn4在一臺服務器上
max_connections = 100
# DATA NODES AND CONNECTION POOLING (修改二)
#----------------------------------
pooler_port = 6801 #dn2,dn3,dn4配置文件都改爲6801
#同一臺機器要使用不同的端口,如dn1,dn2在同一臺機器上分別是6801,6802
#dn3,dn4上端口號也是6801,6802。就是同一機器端口號不同即可。
max_pool_size = 100
# GTM CONNECTION
#--------------------------
gtm_host = '172.17.30.16' # gtm所在的主機地址
gtm_port = 6666 #gtm配置中,gtm端口號配置爲6666
注意:根據實際情況進行修改
5).重啓gtm,coordinator,datanode服務器
採用命令:sudo reboot
注意:當sudo reboot有問題時,可以採用reboot命令
如圖所示:(修改gtm)
如圖所示:(修改coordinator)
修改一:
修改二:
修改三:
如圖所示:(修改datanode)
修改一:
修改二:
修改三:
啓動postgres-xl
啓動順序:
1.啓動gtm
2.啓動全部DataNode
3.最後啓動全部coordinator
1).啓動gtm:gtm_ctl start -Z gtm -D /home/postgres/pgxl_9.5/data/gtm
2).啓動datanode:pg_ctl start -Z datanode -D /home/postgres/pgxl_9.5/data/dn1
注意:有多少個datanode,就得啓動多少個datanode,注意修改目錄,如啓動datanode2:pg_ctl start -Z datanode -D /home/postgres/pgxl_9.5/data/dn2
3).啓動coordinator:pg_ctl start -Z coordinator -D /home/postgres/pgxl_9.5/data/coord1
注意:有多少個coordinator,就得啓動多少個coordinator,注意修改目錄,如啓動pg_ctl start -Z coordinator -D /home/postgres/pgxl_9.5/data/coord2
如圖所示:(啓動gtm)
如圖所示:(啓動datanode)
如圖所示: (啓動coordinator)
如圖所示:(修改coordinator,datanode中的pg_hba.conf文件)
postgres-xl集羣部署
概述:介紹配置postgres-xl集羣,這裏的配置針對角色爲coordinator和datanodede的服務器
1).以postgres用戶進入psql:psql -p 端口號
如:psql -p 5301
2).配置coordinator
a.查詢節點select * from pgxc_node; 查詢存在的配置的節點
b.更新存在的節點:alter node 節點名字 with(type=節點類型,host='對應角色Ip',
port='對應角色Ip');
如:如果查詢存在coord2,就更新:alter node coord2 with (type=coordinator,host='172.17.30.18', port=5301);其中172.17.30.18
對應的是所角色所在的服務器IP.5301:對應角色所在服務器的端口。
注意:根據實際情況修改
c.製造節點coordinator,datanode節點:alter node 節點名字 with(type=節點類型, host='對應角色Ip', port='對應角色Ip');
如:create node coord1(節點名字) with (type=coordinator,host='172.17.30.18', port=5301);
d.設置第一個數據數據節點:爲第一優先
如:
create node dn1 with (type=datanode, host='172.17.30.19',port=5401,primary,preferred);
e.重新載入修改的節點:select pgxc_pool_reload();
f.查詢修改後的節點:select * from pgxc_node;
3).配置datanode節點:
a.和coordinator配置一樣
注意:上面的配置針對角色爲coordinator和datanodede的服務器都要設置
具體修改可參考:
coord1:
psql (PGXL 9.5r1.5, based on PG 9.5.6 (Postgres-XL 9.5r1.5))
Type "help" for help.
postgres=# select * from pgxc_node;
postgres=# alter node coord1 with (type=coordinator,host='172.17.30.17', port=5301);
postgres=# create node coord2 with (type=coordinator,host='172.17.30.18', port=5301);
postgres=# create node dn1 with (type=datanode, host='172.17.30.19',port=5401,primary,preferred);
postgres=# create node dn2 with (type=datanode, host='172.17.30.20',port=5401);
postgres=# create node dn3 with (type=datanode, host='172.17.30.21',port=5401);
postgres=# create node dn4 with (type=datanode, host='172.17.30.22',port=5401);
postgres=# select pgxc_pool_reload();
postgres=# select * from pgxc_node;
coord2
psql (PGXL 9.5r1.5, based on PG 9.5.6 (Postgres-XL 9.5r1.5))
Type "help" for help.
postgres=# select * from pgxc_node;
postgres=# alter node coord2 with (type=coordinator,host='172.17.30.18', port=5301);
postgres=# create node coord1 with (type=coordinator,host='172.17.30.17', port=5301);
postgres=# create node dn1 with (type=datanode, host='172.17.30.19',port=5401,primary,preferred);
postgres=# create node dn2 with (type=datanode, host='172.17.30.20',port=5401);
postgres=# create node dn3 with (type=datanode, host='172.17.30.21',port=5401);
postgres=# create node dn4 with (type=datanode, host='172.17.30.22',port=5401);
postgres=# select pgxc_pool_reload();
postgres=# select * from pgxc_node;
dn1
psql (PGXL 9.5r1.5, based on PG 9.5.6 (Postgres-XL 9.5r1.5))
Type "help" for help.
postgres=# select * from pgxc_node;
postgres=# create node coord2 with (type=coordinator,host='172.17.30.18', port=5301);
postgres=# create node coord1 with (type=coordinator,host='172.17.30.17', port=5301);
postgres=# alter node dn1 with (type=datanode, host='172.17.30.19',port=5401,primary,preferred);
postgres=# create node dn2 with (type=datanode, host='172.17.30.20',port=5401);
postgres=# create node dn3 with (type=datanode, host='172.17.30.21',port=5401);
postgres=# create node dn4 with (type=datanode, host='172.17.30.22',port=5401);
postgres=# select pgxc_pool_reload();
postgres=# select * from pgxc_node;
dn2
psql (PGXL 9.5r1.5, based on PG 9.5.6 (Postgres-XL 9.5r1.5))
Type "help" for help.
postgres=# select * from pgxc_node;
postgres=# create node coord2 with (type=coordinator,host='172.17.30.18', port=5301);
postgres=# create node coord1 with (type=coordinator,host='172.17.30.17', port=5301);
postgres=# create node dn1 with (type=datanode, host='172.17.30.19',port=5401,primary,preferred);
postgres=# alter node dn2 with (type=datanode, host='172.17.30.20',port=5401);
postgres=# create node dn3 with (type=datanode, host='172.17.30.21',port=5401);
postgres=# create node dn4 with (type=datanode, host='172.17.30.22',port=5401);
postgres=# select pgxc_pool_reload();
postgres=# select * from pgxc_node;
dn3
psql (PGXL 9.5r1.5, based on PG 9.5.6 (Postgres-XL 9.5r1.5))
Type "help" for help.
postgres=# select * from pgxc_node;
postgres=# create node coord2 with (type=coordinator,host='172.17.30.18', port=5301);
postgres=# create node coord1 with (type=coordinator,host='172.17.30.17', port=5301);
postgres=# create node dn1 with (type=datanode, host='172.17.30.19',port=5401,primary,preferred);
postgres=# create node dn2 with (type=datanode, host='172.17.30.20',port=5401);
postgres=# alter node dn3 with (type=datanode, host='172.17.30.21',port=5401);
postgres=# create node dn4 with (type=datanode, host='172.17.30.22',port=5401);
postgres=# select pgxc_pool_reload();
postgres=# select * from pgxc_node;
dn4
psql (PGXL 9.5r1.5, based on PG 9.5.6 (Postgres-XL 9.5r1.5))
Type "help" for help.
postgres=# select * from pgxc_node;
postgres=# create node coord2 with (type=coordinator,host='172.17.30.18', port=5301);
postgres=# create node coord1 with (type=coordinator,host='172.17.30.17', port=5301);
postgres=# create node dn1 with (type=datanode, host='172.17.30.19',port=5401,primary,preferred);
postgres=# create node dn2 with (type=datanode, host='172.17.30.20',port=5401);
postgres=# create node dn3 with (type=datanode, host='172.17.30.21',port=5401);
postgres=# alter node dn4 with (type=datanode, host='172.17.30.22',port=5401);
postgres=# select pgxc_pool_reload();
postgres=# select * from pgxc_node;
測試安裝
概述:主要測試postgres-xl安裝是否成功
1).測試分佈模式的測試表並插入數據
a.以postgres用戶進入172.17.30.17:psql -p 端口號
如:psql -p 5301
b.coordinator角色建立表和插入數據
如:
postgres=# create table t1(id integer, name varchar(20));
postgres=# insert into t1 values(1,'測試1');
postgres=# insert into t1 values(2,'測試2');
postgres=# insert into t1 values(3,'測試3');
postgres=# insert into t1 values(4,'測試4');
c.查詢數據是否插入成功:
(1)datanode:有點數據節點可能查詢不到數據
(2)coordinator:每個coordinator角色都能查詢插入的全部數據
d.數據查詢:
(1)這裏採用:postgres=# select * from t1;
2).測試複製模式的測試表並插入數據
a.以postgres用戶進入172.17.30.18:psql -p 端口號
如:psql -p 5301
b.coordinator角色建立表和插入數據
如:
postgres=# create table t2(id integer, name varchar(20))distribute by replication;;
postgres=# insert into t2 values(1,'測試1');
postgres=# insert into t2 values(2,'測試2');
postgres=# insert into t2 values(3,'測試3');
postgres=# insert into t2 values(4,'測試4');
c.查詢數據是否插入成功:
(1)datanode:每個datanode角色都能查到數據
(2)coordinator:每個coordinator角色都能查詢插入的全部數據
d.數據查詢
可以採用:postgres=# select * from t2;注意這裏每個節點都能查看相同數據
安裝Haproxy
概述:安裝Haproxy負載均衡。 注意:選擇單獨服務器安裝,172.17.30.14
1).安裝Haporxy
a.安裝命令:sudo apt-get install haproxy
2).修改配置文件haproxy.conf
a.文件所在目錄:/etc/haproxy/haproxy.cfg
b.修改時進入文件:
出現:
Swap file "/etc/haproxy/.haproxy.cfg.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:
請選擇:e進入
c.修改如下內容:
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
maxconn 4095 #指定同步連上服務的最大連線數
user haproxy
group haproxy
daemon
# Default SSL material locations
ca-base /etc/ssl/certs
crt-base /etc/ssl/private
# Default ciphers to use on SSL-enabled listening sockets.
# For more information, see ciphers(1SSL). This list is from:
# https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECD H+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS
ssl-default-bind-options no-sslv3
defaults
log global
mode tcp #修改:mode http ---->mode tcp
option tcplog #修改:option httplog--->option tcplog
option dontlognull
timeout connect 10s #修改:連接超時時間
timeout client 60s #修改:連接客戶端超時時間
timeout server 60s #修改:連接服務器端超時時間
#---------------------添加內容----------------------------
frontend http-in
bind *:8888 #對外提供的接口
mode tcp
default_backend psql_server
backend psql_server
balance leastconn #配置排程演算法:最小連續線排程法
option pgsql-check user postgres
server pgsql_coord1 172.17.30.204:5301 weight 1 check inter 1s rise 2 fall 3
server pgsql_coord2 172.17.30.204:5302 weight 1 check inter 1s rise 2 fall 3
#-----------設置統計頁面配置------------
listen admin-stats
mode http
option httplog
bind 0.0.0.0:8888 #監聽端口
stats enable
stats refresh 30s #設置統計頁面自動刷新時間
stats uri /dbs
stats realm welcome login\ Haproxy
stats auth admin:admin #設置登錄統計頁面的用戶名:admin,密碼:admin
stats admin if TRUE
3)檢測修改文件是否有錯
a.輸入:haproxy -f /etc/haproxy/haproxy.cfg -c
出現:
Configuration file is valid (配置有效)
b.重啓服務
sudo service haproxy restart
4).驗證如下:
a.在瀏覽器中輸入:http://172.17.30.13:8888/dbs
(1).彈出登錄對話框:
用戶名:admin 密碼爲:admin
(2).如圖所示
b.連接測試:
(1).下載postgresql客戶端sudo apt-get install postgresql-client
(2).psql -h 172.17.30.13 -p 8888 -U postgres
(3).如圖所示
5)Haproxy配置文件解析:(服務器,也稱伺服器)
a.glabal(全域):設定能配置用於執行Haproxy的所有伺服器。
eg:
global
log 127.0.0.1 local2
maxconn 4000
user haproxy
group haproxy
daemon
說明:
log::(記錄)所有條目到本地的syslog伺服器。這個可以是/var/log/syslog或使用者指定的地方
maxconn:指定同步連上服務的最大連線數量。
user與group能用來指定haproxy程序所屬的使用者名與羣組名稱
daemon:能讓haproxy後臺運行
b.default設定:(預設)能用來配置套用frontend(前端)、backend(後端),listen(監聽)等配置項目的所有代理小結。
注意:任何配置於proxy子節(frontend,backend,或listen)的參數,都會蓋過default中參數
eg:
defaults
mode http
log global
option httplog
option dontlognull
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
說明:
mode:指定了Haproxy instance所使用的通訊協定。使用http模式將來源請求連接至根基於http的真實服務器,試用網頁服務器的負載平衡。對於其他應用程式來說,請使用tcp模式
log:會指定日誌將條目寫入的日誌與syslog的設施。global值參照了global一節中的log參數中所指定的Harproxy instance
option httplog能記錄HTTP session的多種值,包括http請求,session狀態、連線數,來源位址,以及連線計數器等等
option dontlognull:停用了對null連線的記錄,表示Haproxy不會記錄不會記錄未傳任何資料的連線。這不建議在用在網絡路上的環境中(例如網頁應用程式),因爲null可能表示惡意活動
retries:指定了在無法第一次就連上時,真實服務器會重試連線的次數
timeout:指定某個給定請求的非活動時間,單位爲秒或微秒
c.frontend:設定會配置服務器監聽來自用戶端的socket.
eg:
frontend mian
bind 192.168.0.10:80
說明:
名爲main的fronend透過bind參數將Ip位址設爲192.168.0.10,並監聽連接80後,user backend會指定所有來自session的連線連往app後端
d.backend:設定真實服務器的IP地址,以及負載平衡程式的排程演算法。
eg:
backend app
balance roundrobin
server app1 192.168.1.1:80 check
server app2 192.168.1.2:80 check
server app3 192.168.1.3:80 check inter 2s rise 4 fall 3
server app4 192.168.1.4:80 backup
說明:
後端服務器名爲app。balance會指定負載平衡程式的排程演算法。
server 行指定了後端可以使用的服務器。ap1到ap4時內部服務器的名稱
位址:指定的Ip地址,Ip冒號後面的是服務器連接端口號。check 選項標誌定期【健康檢查】的服務器,以確保服務器的可用性,得以發送資料並服務session的請求。服務器app3也配置健康檢查的間隔,是時間爲2秒,app3用來決定服務器是否健康的檢查次數(rise 4)、以及服務器直至認定爲失敗的重試次數(fall 3).
6)排程演算法介紹:
a.leastconn:將請求分散到連線數較少的真實伺服器。當處於一個動態式且包含各種session或連線長的環境中,對管理員來說,這個排程可能會較適合其環境。這也適用包含了一組擁有不同生產力的真實伺服器的環境中,因爲透過排程器管理。
結合上述配置理解:
backend psql_server
balance leastconn #配置排程演算法:最小連續線排程法
option pgsql-check user postgres
server pgsql_coord1 172.17.30.204:5301 weight 1 check inter 1s rise 2 fall 3
server pgsql_coord2 172.17.30.204:5302 weight 1 check inter 1s rise 2 fall 3
假如:172.17.30.204:5301被連接的次數較少時,下一次,連接時,會連接172.17.30.204:5301;
總結一句話:'誰比較閒,就用誰'
b.roundrobin:將請求依序發給集區中的伺服器。使用此算法,所有真實伺服器不管能力如何,都會被視爲平等。此排程模式類似循環DNS(round-robin DNS)法,但更爲細緻,因爲這樣是以網絡連線爲基礎,而非以主機爲基礎。Load Balancer的循環配置資源排程也不會因爲快取DNS的查詢項目而導致不平衡。然而,在Haproxy中,因爲伺服器權重的配置能輕易透過此排程器。因此各個後端的啓用中的伺服器數量會被限制爲4095。
假如:上述配置爲:roundrobin,時,172.17.30.204:5301和172.17.30.204:5302在被連接時,將採用循環調用的。
總結一句話:'循環調用'
c.靜態循環法(static-rr):一般地在一組伺服器之間循環性地分散各項請求。不過不允許動態式配置伺服器的權重。然而,基於伺服器權重的靜態配置,後端中不會有伺服器啓用數量上的限制。
比較: roundrobin 和 static-rr
權重:
(1)roundrobin:可以動態配置伺服器權重
(2)static-rr:不允許動態配置伺服器權重
伺服器啓用數量:
(1)roundrobin:數量上的限制,數量限制爲4095.
(2)static-rr:數量上沒有限制
如圖所示: