HA專題: corosync+pacemaker實現nginx高可用
前言
這幾天都會學習高可用集群, 也會將其中的一些實驗寫出來分享給大家, 這個專題估計會寫5篇左右,
p.s: 寫博客很累的
實驗介紹
這次的實驗比較簡單, 在
CentOS7
使用corosync
+pacemaker
實現兩個節點的nginx
高可用
實驗拓撲
實驗環境
主機 | IP | 功用 |
---|---|---|
node1.anyisalin.com | 172.16.1.2 | web服務, HA節點 |
node2.anyisalin.com | 172.16.1.3 | web服務, HA節點 |
nfs.anyisalin.com | 172.16.1.4 | 提供nfs服務,網站頁面文件 |
注意: 本文實驗中所有主機SElinux和iptables都是關閉的
實驗步驟
準備工作
高可用集群必須保證所有節點主機互信, 主機名解析一致, 時間同步
配置hosts文件同步
配置互信
時間同步
安裝HA集群組件
在RH系6.4之后就可以通過RedHat提供的
pcs
來進行對集群”全生命周期”的管理, 我們這里先通過pcs
安裝集群并自動生成配置文件
[root@node1 ~]# yum install pcs -y #node1安裝pcs [root@node1 ~]# ssh node2.anyisalin.com 'yum install pcs -y ' #node2安裝pcs [root@node1 ~]# echo passwd | passwd --stdin hacluster #設置hacluster用戶密碼 [root@node1 ~]# ssh node2.anyisalin.com "echo passwd | passwd --stdin hacluster" #為node2設置hacluster密碼 [root@node1 ~]# systemctl start pcsd ; ssh node2.anyisalin.com "systemctl start pcsd" #啟動pcsd [root@node1 ~]# pcs cluster auth node1.anyisalin.com node2.anyisalin.com -u hacluster #進行認證 Password: node1.anyisalin.com: Authorized node2.anyisalin.com: Authorized [root@node1 ~]# pcs cluster setup --name mycluster node1.anyisalin.com node2.anyisalin.com #安裝集群 [root@node1 ~]# pcs cluster start --all #啟動集群
安裝nginx和配置nfs
安裝nginx [root@node1 ~]# yum install nginx -y 注意: 需epel源 [root@node1 ~]# ssh node2.anyisalin.com "yum install nginx -y " [root@node1 ~]# systemctl enable nginx #由于systemd的特性, 需將nginx設置開機自動啟動 [root@node1 ~]# ssh node2.anyisalin.com "systemctl enable nginx" 配置nfs [root@nfs ~]# mkdir /www/htdocs -pv [root@nfs ~]# echo "<h1>This is NFS on 172.16.1.4</h1>" > /www/htdocs/index.html #創建網頁文件 [root@nfs ~]# vim /etc/exports /www/htdocs 172.16.1.0/24(ro) [root@nfs ~]# systemctl start rpcbind.service [root@nfs ~]# systemctl start nfs-server.service #啟動nfs-server 測試nfs [root@node1 ~]# mount -t nfs 172.16.1.4:/www/htdocs /usr/share/nginx/html/ #掛載成功 [root@node1 ~]# df Filesystem 1K-blocks Used Available Use% Mounted on 172.16.1.4:/www/htdocs 52403200 1134336 51268864 3% /usr/share/nginx/html [root@node1 ~]# umount /usr/share/nginx/html/
使用crmsh配置集群資源
pcs
用來安裝集群還可以, 但是配置集群資源, 我用了一次再也不想用了, 實在是不好用, 個人比較喜歡crmsh
來配置集群,crmsh
大家可以自行去下載, 安裝時可能需要epel
源 中的一些軟件
[root@node1 ~]# yum install crmsh-2.1.4-1.1.x86_64.rpm #安裝crmsh 我們的rpm包是自行下載的
配置資源
[root@node1 ~]# crm configure
crm(live)configure# primitive webip ocf:heartbeat:IPaddr params ip=172.16.1.8
crm(live)configure# primitive nginx systemd:nginx
crm(live)configure# primitive webstore ocf:heartbeat:Filesystem params device="172.16.1.4:/www/htdocs" directory="/usr/share/nginx/html" fstype="nfs" op start timeout=60s op stop timeout=60s op monitor interval=20s timeout=40s
定義資源組
crm(live)configure# group webservice webip webstore nginx
crm(live)configure# verify
crm(live)configure# property stonith-enabled=false
crm(live)configure# commit
測試
總結
本文簡單演示HA集群的簡單實現, 非常的簡單. HA最重要的是它的原理, 我以前寫過一篇HA原理的博客, 大家可以看看
作者水平很低, 如果有錯誤及時指出, 如果你覺得本文寫的好請點一波贊~(≧▽≦)/~
作者: AnyISaIln QQ: 1449472454
感謝: MageEdu
原創文章,作者:Net18-AnyISalIn,如若轉載,請注明出處:http://www.www58058.com/14614