VMware準備三臺服務器html
地址:node
ha1:192.168.70.150 ha2:192.168.70.151 ha3:192.168.70.152
一、環境初始化linux
1.一、配置hostsweb
把上面信息:替換成空格寫入/etc/hosts便可 ha1 192.168.70.150 ha2 192.168.70.151 ha3 192.168.70.152
1.二、設置主機名:shell
hostnamectl set-hotname ha1|ha2|ha3
上面均可以經過ansible cp shell模塊實現,這裏就三臺手動加入了vim
1.三、配置祕鑰互信服務器
ssh-keygen -t rsa -f id_rsa -P "" #默認root的,能夠指定其餘用戶 ssh-copy-id -i id_rsa root[@ha2](https://my.oschina.net/ha2) #拷貝公鑰至其餘節點, ssh-copy-id -i id_rsa root[@ha3](https://my.oschina.net/Ha3) 上述操做每一個節點都要執行
1.四、ansible安裝配置dom
yum -y install ansible 其中一臺安裝便可 配置一個ansible 組 cat /etc/ansible/hosts \[ha\] 192.168.70.150 192.168.70.151 192.168.70.152
1.五、關閉防火牆及selinuxssh
ansible ha -m command -a "systemctl stop firewalld" ansible ha -m command -a "systemctl disable firewalld" ansible ha -m command -a "sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config"
1.六、安裝及配置ntpcurl
ansible ha -m command -a "yum -y install ntp" 配置ntp(註釋掉默認server開頭的) Ha1-Server: server 127.127.1.0 fudge 127.127.1.0 stratum 10 其餘兩臺client: server 192.168.70.150 burst iburst ssh ha1 「systemctl start ntpd」 ansible ha -m command -a "ntpdate 192.168.70.150" ansible ha -m command -a "hwclock -w" ansible ha -m command -a "systemctl start ntpd" ansible ha -m command -a "systemctl enable ntpd" ansible ha -m command -a "ntpq -p"
二、安裝配置pacemaker、corosync
2.一、準備yum源
準備pacemaker命令管理接口crmsh接口源,也能夠是pcs(每一個節點都要準備) cat crm.repo \[network\_ha-clustering\_Stable\] name=Stable High Availability/Clustering packages (CentOS_CentOS-7) type=rpm-md baseurl=http://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-7/ gpgcheck=1 gpgkey=http://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-7/repodata/repomd.xml.key enabled=1 ansible ha -m command –a 「yum clean all」 ansible ha -m command –a 「yum makecache」 ansible ha -m command –a 「yum repolist」
2.二、安裝
ansible ha -m command -a "yum -y install pacemaker corosync crmsh"
2.三、配置
配置corosync cd /etc/corosync/ cp corosync.conf.example corosync.conf grep -e "Corosync Cluster Engine" -e "configuration file" /var/log/cluster/corosync.log totem { version: 2 crypto_cipher: aes256 crypto_hash: sha1 interface { ringnumber: 0 bindnetaddr: 192.168.70.0 mcastaddr: 239.255.1.1 mcastport: 5405 ttl: 1 } } logging { fileline: off to_stderr: no to_logfile: yes logfile: /var/log/cluster/corosync.log to_syslog: no debug: off timestamp: on logger_subsys { subsys: QUORUM debug: off } } quorum { provider: corosync_votequorum expected_votes:2 } nodelist { node { ring0_addr: ha1 nodeid:1 } node { ring0_addr: ha2 nodeid:2 } node { ring0_addr: ha3 nodeid:3 } } mv /dev/random /dev/random.bak ln -s /dev/urandom /dev/random corosync-keygen scp authkey corosync.conf ha2:/etc/corosync/ scp authkey corosync.conf ha3:/etc/corosync/
三、啓動 ansible ha -m command -a "systemctl start corosync"
ansible ha -m command -a "systemctl enable corosync" ansible ha -m command -a "systemctl start pacemaker" ansible ha -m command -a "systemctl enable pacemaker"
四、服務狀態檢查
4.一、查看服務是否啓動
ss -unl
4.二、查看corosync引擎是否正常啓動
grep -e "Corosync Cluster Engine" -e "configuration file" /var/log/cluster/corosync.log
4.三、查看初始化成員節點通知是否正常發出
grep TOTEM /var/log/cluster/corosync.log
4.4 查看服務啓動是否有報錯
grep ERROR: /var/log/cluster/corosync.log
4.五、檢查各節點通訊狀態(no faults 表明OK)
ansible ha -m command -a "corosync-cfgtool -s "
4.六、檢查集羣成員關係及Quorum API:
corosync-cmapctl | grep members
4.七、查看集羣節點狀態及DC:
crm_mon -1
五、安裝須要建立資源的服務及測試
5.一、安裝
ansible ha -m command -a "yum -y install httpd"
5.二、配置啓動
ha1: echo 「<h1>ceshi ha1</h1>」>/var/www/html/ha.html ha2: echo 「<h1>ceshi ha2</h1>」>/var/www/html/ha.html ha3: echo 「<h1>ceshi ha3</h1>」>/var/www/html/ha.html ansible ha -m command –a 「systemctl start httpd」
5.三、測試
每一個節點測試確保能正常輸出 curl [http://192.168.70.150/ha.html](http://192.168.70.150/ha.html)
六、pacemaker命令接口crm經常使用模塊命令介紹
6.一、node (切換上下線、隔離、刪除)
6.二、ra(查看資源代理類型)
6.三、Resource(集羣管理的資源列表)
Start|stop|restart 啓動|中止|重啓資源 Locate 定位資源當前運行於哪一個節點 Status 查看集羣當前全部資源的狀態 Move|migrate 遷移資源至其它節點 Cleanup 清除當前資源在全部節點上的狀態
6.四、Configure(爲集羣配置資源、約束)
Show 展現已配置的全部資源 Edit 編輯已配置的資源,使用方法與vim編輯器同樣 Primitive 定義建立資源 Monitor 給資源添加監控,好比某個資源多久檢測一次,查過多長時間算失敗 Group 添加組資源,捆綁,對於clone資源無效 Clone 定義克隆資源 Location 定義位置約束優先級 Property 設置集羣屬性 Refresh 從新從cib庫讀取數據 Verify 檢查配置有沒有語法錯誤 Commit 配置提交 Save 保存當前配置至其餘文件至關於備份 Load 從其餘文件導入配置
七、實例添加資源
property stonith-enabled=false #沒有fence設備的狀況下關閉此功能 property no-quorum-policy=ignore #當只有兩節點,沒法仲裁忽略 property default-resource-stickiness=infinity #inf,資源默認分數爲正無窮大 property cluster-infrastructure=corosync #默認集羣心跳是corosync primitive vip IPaddr parms ip="192.168.70.250" primitive webserver systemd:httpd monitor vip 30s:20s monitor weserver 30s:100s group vip_webserver vip webserver
八、測試
經過結果能夠看到,當我把ha1置換爲備份節點的時候,VIP和web都切換至ha3
由於添加了監控monitor,當資源down以後,檢測的時間內服務沒有起來時,pacemaker會主動去啓動對應服務
九、clone資源添加及測試
9.1 添加
clone clone_web webserver meta notify="True" globally-unique=false interleave=true clone clone_vip VIP meta notify="True" globally-unique=false interleave=true
9.2 測試
十、擴展 資源建立及約束詳解見:
[https://blog.51cto.com/ittony/1566945](https://blog.51cto.com/ittony/1566945) crm_simulate –sL 查看當前資源得分 crm configure save all.xml 保存全部資源至指定文件 crm configure save VIP vip.xml 保存某個指定資源至文件 crm configure save type:primitive vip.xml 保存某種類型資源至文件 crm_resource –C 清除失敗的記錄(處理完問題以後能夠使用)