Centos7 HA操作指南

本教程是基於CENTOS7 的WEB負載與數據庫高可用方案
本方案採用主主模式

首先進行系統的初步設置,這對整個集羣有特別重要的作用
1.修改主機名稱
2.關停NetworkManager服務
systemctl stop NetworkManager
systemclt disable NetworkManager
3.修改網卡名稱,這個設置是可選的,我喜歡用eth0來表示網卡。
vi /etc/sysconfig/grub
在“GRUB_CMDLINE_LINUX=”這行最後
添加 net.ifnames=0 biosdevname=0
修改後:GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/swap vconsole.font=latarcyrheb-sun16 rd.lvm.lv=centos/root crashkernel=auto vconsole.keymap=us rhgb quiet net.ifnames=0 biosdevname=0"
然後執行:grub2-mkconfig -o /boot/grub2/grub.cfg
然後修改 /etc/sysconfig/network-scripts/ifcfg-*
爲想要的名稱 ifcfg-eth0 ,修改配置文件裏面的名稱 NAME=eth0
重啓操作系統,即可將網卡名稱修改爲 eth0
4.關停防火牆
systemctl stop firewalld
systemctl disable firewalld
5.設置selinux配置選項
/etc/selinux/config
把enforcing 改爲:disabled
6.給集羣內的所有主機添加主機列表
/etc/hosts
122.0.113.142 website01
122.0.113.143 website02
122.0.113.144 website03
這項操作是在所有機器上進行的
7.設置ssh無密碼訪問
ssh-keygen
一路回車,完成後做如下操作:
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 644 ~/.ssh/authorized_keys
chmod 755 ~/.ssh
vi ~/.ssh/config (加入如下一行)
StrictHostKeyChecking no
chmod 600 config
然後賦予相應的權限
chmod 755 ~/.ssh/
chmod 600 ~/.ssh/id_rsa ~/.ssh/id_rsa.pub
操作完畢後,要把/root/.ssh內的密鑰及配置文件全部下載到本地,然後在其它節點上進行ssh-keygen的操作。完成後,進到/root/.ssh目當,清除目錄裏的內容,把本地的文件上傳上去。
然後執行:
chmod 755 ~/.ssh/
chmod 600 ~/.ssh/id_rsa ~/.ssh/id_rsa.pub

前面的準備工作完成後,開始LAMP的操作
先要在/etc/yum.repo.d/目錄下創建yum源
[mariadb]
name = MariaDB Galera Cluster
baseurl = http://yum.mariadb.org/5.5.48/centos7-amd64/
gpgkey = https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck = 1
進行數據庫的安裝
yum install MariaDB-Galera-server MariaDB-client galera
service mysql start
chkconfig mysql on
mysql_secure_installation 進行root密碼的設置
安裝apache
yum install -y httpd
systemctl start httpd
systemctl enable httpd
安裝php
yum install -y php
安裝php擴展
yum install -y php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash

現在進行基於Pacemake corosync的操作
yum install pacemaker pcs corosync fence-agnets resource-agents -y 三個節點都需要安裝
開啓pcs服務並設置爲自啓動
systemctl start pcsd
systemctl enable pcsd 三個節點都需要這樣操作
設置兩個控制節點的hacluster用戶名和密碼,兩個節點都要操作
echo my-secret-password-no-dont-use-this-one | passwd –stdin hacluster #website01
echo my-secret-password-no-dont-use-this-one | passwd –stdin hacluster #website02
echo my-secret-password-no-dont-use-this-one | passwd –stdin hacluster #website03
設置集羣密碼
passwd hacluster
1qaz2wsx,./
配置corosync
[root@controller01 ~]# pcs cluster auth website01 website02 website03
創建一個集羣,並設置命令,啓用服務
[root@controller01 ~]# pcs cluster setup --name my-cluster website01 website02 website03
啓動集羣
pcs cluster start --all
systemctl start corosync
systemctl enable corosync
systemctl start pacemaker
systemctl enable pacemaker
檢查pcs status 輸出
WARNING: no stonith devices and stonith-enabled is not false 會有這樣一個警告信息
這個是沒有Fencing設備時,沒有禁用stonith功能,禁用就可以了
pcs property set stonith-enabled=false
設置基本的羣集屬性
pcs property set pe-warn-series-max=1000 \

pe-input-series-max=1000 \
pe-error-series-max=1000 \
cluster-recheck-interval=3min
設置資源默認粘性(防止資源回切)
pcs resource defaults resource-stickiness=100
pcs resource defaults
設置資源超時時間
pcs resource op defaults timeout=90s
pcs resource op defaults
配置集羣的VIP
pcs resource create vip ocf:heartbeat:IPaddr2 params ip="122.0.113.146" cidr_netmask="25" op monitor interval="30s"
VIP已經能Ping通了
配置Haporxy
安裝haproxy 用yum
yum install haproxy -y
添加haproxy日誌
cd /var/log
mkdir haproxy
cd haproxy
touch haproxy.log
chmod a+w haproxy.log
vi /etc/rsyslog.conf
$ModLoad imudp
$UDPServerRun 514
兩行前的#去掉

#Save boot messages also to boot.log
local7. /var/log/boot.log
local3.
/var/log/haproxy/haproxy.log
修改/etc/sysconfig/rsyslog 文件
SYSLOGD_OPTIONS=""
改爲
SYSLOGD_OPTIONS="-r -m 0 -c 2"
配置haproxy
修改/etc/haproxy/haproxy.cfg文件,在global區段添加 #我會準備好haproxy的配置文件
log 127.0.0.1 local3
systemctl restart rsyslog
然後進行下面的操作
pcs resource create lb-haproxy systemd:haproxy --clone
pcs constraint order start vip then lb-haproxy-clone kind=Optional
pcs constraint colocation add vip with lb-haproxy-clone
echo net.ipv4.ip_nonlocal_bind=1 >> /etc/sysctl.d/haproxy.conf
echo 1 > /proc/sys/net/ipv4/ip_nonlocal_bind
還需要在三個控制節點裏,把haproxy設置爲自啓動。systemctl enable haproxy
進行重啓操作,重啓完成後,用pcs status 查看集羣狀態
[root@website01 ~]# pcs status
Cluster name: my-cluster
Last updated: Tue Apr 19 20:15:20 2016 Last change: Tue Apr 19 18:11:30 2016 by root via cibadmin on website01
Stack: corosync
Current DC: website01 (version 1.1.13-10.el7_2.2-44eb2dd) - partition with quorum
3 nodes and 4 resources configured

Online: [ website01 website02 website03 ]

Full list of resources:

vip (ocf::heartbeat:IPaddr2): Started website01
Clone Set: lb-haproxy-clone [lb-haproxy]
Started: [ website01 website02 website03 ]

PCSD Status:
website01: Online
website02: Online
website03: Online

Daemon Status:
corosync: active/enabled
pacemaker: active/enabled
pcsd: active/enabled
當所有這些狀態顯示後,會發現httpd服務與mariadb沒有啓來,我們需要做一些操作
在/etc/httpd/conf/httpd.conf配置文件裏修改
Listen website01:80
保存退出,systemctl restart httpd 重啓服務
在/etc/my.cnf.d/server.cnf 配置文件裏修改
在mysqld 選項裏添加相應的節點主機名稱
bind_address = website01
保存退出,service mysql restart 重啓服務
以上操作都需要在三個節點上進行
下面進行mariadb基於galera做的主主數據庫集羣,我們需要把三個節點的mariadb服務給停掉,service mysql stop
設置羣集配置文件
vi /etc/my.cnf.d/server.cnf
在[mariadb-5.5]選項下面添加配置內容
query_cache_size=0
binlog_format=ROW
default_storage_engine=innodb
innodb_autoinc_lock_mode=2
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address=gcomm://website01,website02,website03
wsrep_cluster_name='websitecluster'
wsrep_node_address='122.0.113.142'
wsrep_node_name='webstie01'
wsrep_sst_method=rsync
完成後,以上操作還需要在其它兩個節點進行,需要修改wsrep_node_address='122.0.113.142'的ip爲當前的節點ip wsrep_node_name='webstie01' 爲當前的主機名稱
現在開始啓動集羣
在主服務器上運行:service mysql start --wsrep-new-cluster
在從服務器上運行:service mysql start
登陸到任意一臺節點,進到數據庫裏
mysql -uroot -p
輸入SHOW STATUS LIKE 'wsrep%'; 回車
wsrep_incoming_addresses | website01:3306,website02:3306,website03:3306 看到有如下信息,說明我們的集羣配置正常
最後在每個節點上設置數據庫健康檢測
/etc/sysconfig/clustercheck:
MYSQL_USERNAME="clustercheck"
MYSQL_PASSWORD="my_clustercheck_password"
MYSQL_HOST="localhost"
MYSQL_PORT="3306"
在數據庫裏創建檢測賬號
GRANT PROCESS ON . TO 'clustercheck'@'localhost'IDENTIFIED BY 'my_clustercheck_password';
FLUSH PRIVILEGES;
設置galera檢測文件
/etc/xinetd.d/galera-monitor:# 這點非常的重要,如果格式不對,9200端口會不通的。
service galera-monitor
{
port = 9200
disable = no
socket_type = stream
protocol = tcp
wait = no
user = root
group = root
groups = yes
server = /usr/bin/clustercheck
type = UNLISTED
per_source = UNLIMITED
log_on_success =
log_on_failure = HOST
flags = REUSE
}
由於系統本身沒有clustercheck文件的,我們需要在/usr/bin/clustercheck裏新建一個,我已經創建好了,直接上傳到目錄就可以了。
然後給文件權限 chmod -R a+x /usr/bin/clustercheck
檢查狀態信息,執行如下命令:/usr/bin/clustercheck
HTTP/1.1 200 OK
Content-Type: text/plain
Connection: close
Content-Length: 32

Galera cluster node is synced.
安裝xinetd服務
yum install xinetd
啓動xinetd服務
systemctl daemon-reload
systemctl enable xinetd
systemctl start xinetd
最後,我們需要允許VIP能夠訪問到我們的數據庫,因此,要在三個節點的數據庫裏進行操作
update mysql.user set Host = '%' where Host = 'localhost' and User = 'root';
flush privileges;
到這裏,一個穩定的集羣服務就配置完成了。

centos安裝ab工具給網站進行壓力測試
使用方法(直接輸入ab命令查看參數)
ab -c 10 -n 1000 http://www.test.com/
上條命令的意思是,對test.com的首頁進行壓力測試,模擬同時10個用戶總共進行1000個請求
測試完成後會顯示報告,會顯示一些參數供我們判斷

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