Ansible+Corosync+Pacemaker+nfs實現http高可用

目錄:

(一)實驗環境

(二)準備工作

(三)爲node1和node2配置基礎配置

(四)使用ansible部署nfs

(五)使用ansible部署corosync和pacemaker

(六)使用ansible安裝crmsh工具

(七)使用crmsh配置http高可用

(八)驗證

(九)需要注意的地方

(一)實驗環境

1.1、環境拓撲

2016-04-28_163124.png

1.2、所需系統

4檯安裝了CentOS6.5虛擬機

1.3、網絡、主機及其他準備工作

  • 主機IP地址和主機名

  • 關閉主機防火牆及Selinux

1.4、各主機用途說明

  • node1和node2:安裝corosync+pacemaker實現httpd的高可用

  • ansible-server:安裝ansible,實現基礎層面的自動部署、安裝、配置

  • nfs-server:安裝了nfs,實現磁盤共享

(二)準備工作

2.1、ansible-server安裝ansible

1)、配置epel源

[epel]
name=epel
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-$releasever&arch=$basearch
enabled=1
gpgcheck=0

備註:因爲ansible所需的程序包在epel源有提供

2)、安裝ansible

[root@ansible-server ~]# yum -y install ansible

2.2、創建ansble-playbook所需使用到的目錄

[root@ansible-server ~]# mkdir -pv corosync/roles/{common,ha,crmsh,nfs}/{files,tasks,handlers,templates,vars,meta,default} 

各目錄簡要說明

  • common:用於一些基本的軟件安裝及配置,包括ntp時間同步,local源,掛載光盤等等

  • ha:用於安裝corosync、httpd、pacemaker程序包,及配置corosync認證和配置文件等

  • crmsh:用於安裝crmsh、pssh程序包

  • nfs:用於安裝nfs、及啓動nfs服務等

2.3、創建site.yml和ha.yml文件

[root@ansible-server ~]# touch corosync/ha.yml
[root@ansible-server ~]# touch corosync/site.yml  

備註:此文件雖可不配置,但此文件必須存在

2.4、配置ansible下的hosts文件

[root@ansible-server ~]# vim /etc/ansible/hosts
[hbhosts]   #node1和node2的組
192.168.80.153
192.168.80.152
[nfs-Server]   #nfs-server組
192.168.80.168

2.5 、使用祕鑰讓兩臺主機互相通信

[root@ansible-server ~]# ssh-keygen -t rsa -P ''    #生成密鑰串
[root@ansible-server ~]# ansible hbhosts -m copy -a 'src=/root/.ssh/id_rsa.pub dest=/root/.ssh/authorized_keys owner=root group=root mode=600' –k   #將祕鑰串通過ansible拷貝到各節點中

(三)爲node1和node2配置基礎配置

3.1、目標

  • 掛載本地磁盤

備註:之後請在各節點上配置/etc/fstab,讓其自動掛載。

  • 將所有的yum源移除

  • 配置本地yum源,並將其拷貝到各節點中

  • 安裝ntpdate和crontab,並使用計劃任務設置時間同步

  • 拷貝本地解析文件到各節點中的/etc/hosts中,讓node1和node2可通過名稱解析 

    備註:以下操作均在ansible-server上操作

3.2、 配置hosts文件,用於節點間互相通信

[root@ansible-server ~]# vim corosync/roles/common/files/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.80.152  node1.windchaser.com node1
192.168.80.153  node2.windchaser.com node2

用於實現node1和node2主機互相通信

3.3、設置本地光盤yum源

[root@ansible-server ~]# vim corosync/roles/common/files/local.repo
[local]
name=local repo
baseurl=file:///mnt
enabled=1
gpgcheck=0

3.4、定義common的tasks

目標:

  • 自動掛載光驅

  • 移除所有默認yum源,拷貝local.repo源至對應的目錄

  • 使用計劃任務設置時間自動同步

[root@ansible-server ~]# vim corosync/roles/common/tasks/main.yml
- name: mount media        #自動掛載光盤
 mount: name=/mnt src=/dev/sr0 fstype=iso9660 opts=ro state=mounted
- name: mkdir /tmp/repo
 shell: mkdir /tmp/repo
 args:
   creates: /tmp/repo
- name: move *repo to /tmp
 shell: mv /etc/yum.repos.d/* /tmp/repo
- name: copy local.repo to yum
 copy: src=local.repo dest=/etc/yum.repos.d/local.repo
- name: yum ntpdate and crontab    #安裝ntpdate 和 crontab
 yum: name=` item `  state=present
 with_items:
    - ntp
    - cronie
 tags: inst ntp
- name: hosts file
 copy: src=hosts dest=/etc/hosts
- name: sync time    #設置時間自動同步
 cron: name="sync time" minute="*/3" job="/usr/sbin/ntpdate ntp.api.bz &> /dev/null"

3.5、定義YAML

[root@ansible-server ~]# vim corosync/ha.yml
- name: install and config corosync
 remote_user: root
 hosts: hbhosts
 roles:
   - common

3.6、執行ansible-play自動部署基礎配置

[root@ansible-server ~]# ansible-playbook corosync/ha.yml

此時會自動部署先前我們所做的操作,如果全部都是OK狀態,表示爲正常,如果出現錯誤,請檢查對應的配置項是否出錯。

(四)使用ansible部署nfs

4.1、設定nfs-server共享目錄

[root@ansible-server ~]# vim corosync/roles/nfs/files/exports
/web/htdocs   192.168.80.0/24(rw)

4.2、創建http默認訪問文件index.html,爲後面做測試使用

[root@ansible-server ~]# vim corosync/roles/nfs/files/index.html
<h1>nfs-storage</h1>

4.3、定義nfs的tasks

[root@ansible-server ~]# vim corosync/roles/nfs/tasks/main.yml
- name: install nfs
 yum: name=nfs-utils state=present
- name: copy exports  
 copy: src=exports dest=/etc/exports
- shell: mkdir /web/htdocs -pv
 args:
   creates: /web/htdocs
- name: copy index.html
 copy: src=index.html dest=/web/htdocs
- service: name=nfs state=started enabled=yes
 tags: start

4.4、定義YAML

[root@ansible-server ~]# vim corosync/ha.yml
- name: install and config corosync
 remote_user: root
 hosts: hbhosts
 roles:
   - common
- name: install nfs         #新增下面這些項,目的是不會影響node1和node2
 remote_user: root
 hosts: nfs-Server
 roles:
   - nfs

4.5、執行ansible-play自動部署nfs設置

[root@ansible-server ~]# ansible-playbook corosync/ha.yml

(五)使用ansible部署corosync和pacemaker

5.1、定義corosync配置信息

[root@ansible-server ~]# vim corosync/roles/ha/files/corosync.conf
compatibility: whitetank   #是否兼容舊版本的corosync
totem {     #定義心跳信息傳遞信息
       version: 2   #定義corosync版本
       secauth: on  #是否需要安全認證
       threads: 0   #啓動多少個線程處理心跳信息
       interface {
               ringnumber: 0   #起始號
               bindnetaddr: 192.168.80.0   #綁定在哪個網絡地址
               mcastaddr: 226.94.1.1   #組播地址,爲了與另一個節點傳遞心跳信息
               mcastport: 5405   #組播地址端口號
               ttl: 1
       }
}
logging {   #定義日誌功能
       fileline: off
       to_stderr: no  #是否將錯誤日誌輸出到終端
       to_logfile: yes  #是否啓用專門的日誌文件
       to_syslog: no   #是否將日誌記錄到linux默認日誌文件中,即/var/log/messages,此項和to_logfile啓動一項即可
       logfile: /var/log/cluster/corosync.log   #日誌文件存放位置
       debug: off   #是否開啓debug日誌信息
       timestamp: on   #是否開啓日誌記錄時間戳
       logger_subsys {
               subsys: AMF
               debug: off
       }
}
amf {
       mode: disabled
}
service{    #設定使用pacemaker服務
   ver:  0
   name: pacemaker
}
aisexec{   #定義運行時使用的用戶和組
 user: root
 group: root
}

備註:此文件可以在已安裝的corosync下/etc/corosync/下有一corosync.conf.example模板信息,做好修改之後再傳遞給ansible-server即可。

5.2、定義node1和node2之間corosync所需的祕鑰信息

[root@ansible-server ~]# ls corosync/roles/ha/files/authkey 
corosync/roles/ha/files/authkey

備註:此文件可以在已安裝好的corosync上執行corosync-keygen,此時需要你輸入數據來產生隨機數,建議使用重複安裝某個程序來加快生成速度,然後拷貝到ansibe-server即可。

5.3、定義ha的tasks

目標:

  • 安裝corosync、pacemaker和httpd

  • 拷貝authkey認證文件和corosync配置文件到各節點

[root@ansible-server ~]# vim corosync/roles/ha/tasks/main.yml
- name: install corosync、pacemaker and httpd
 yum: name=` item ` state=present   #安裝對應所需的程序包
 with_items:
   - corosync
   - pacemaker
   - httpd
 tags: inst
- name: auth key file    #拷貝認證文件到各節點
 copy: src=authkey dest=/etc/corosync/authkey owner=root group=root mode=4600
 tags: authkey
- name: configuration file   #拷貝配置文件到各節點
 copy: src=corosync.conf dest=/etc/corosync/corosync.conf
 tags: config
 notify:   #當配置改變了,通知重啓corosync
   - restart corosync
- name: start corosync   #啓動corosync服務,並設置開機不自動啓動
 service: name=corosync state=started enabled=no
 tags: start
- name: start httpd  #啓動httpd服務,並設定開機不自動啓動
 service: name=httpd state=started enabled=no
 tags: start

5.4、定義ha的handlers文件

[root@ansible-server ~]# vim corosync/roles/ha/handlers/main.yml
- name: restart corosynce
 service: name=corosynce state=restart

5.5、定義YAML文件

[root@ansible-server ~]# vim corosync/ha.yml
- name: install and config corosync
 remote_user: root
 hosts: hbhosts
 roles:
   - common
   - ha
- name: install nfs
 remote_user: root
 hosts: nfs-Server
 roles:
- nfs

5.6、執行ansible-play自動部署corosync和pacemaker設置

[root@ansible-server ~]# ansible-playbook corosync/ha.yml

(六)使用ansible安裝crmsh工具

所需程序包:

[root@ansible-server crmsh]# ll files/
-rw-r--r-- 1 root root 495332 4月  27 23:44 crmsh-1.2.6-4.el6.x86_64.rpm
-rw-r--r-- 1 root root  49960 4月  27 23:44 pssh-2.3.1-2.el6.x86_64.rpm

6.1、使用ansible安裝crmsh

- name: copy crmsh and pssh   #拷貝程序包到各節點
 copy: src=` item ` dest=/tmp/
 with_items:
   - crmsh-1.2.6-4.el6.x86_64.rpm
   - pssh-2.3.1-2.el6.x86_64.rpm
- name: install crmsh and pssh   #安裝兩個程序包
 yum: name=` item ` state=present
 with_items:
  - /tmp/pssh-2.3.1-2.el6.x86_64.rpm
  - /tmp/crmsh-1.2.6-4.el6.x86_64.rpm

6.2、定義YAML文件

[root@ansible-server ~]# vim corosync/ha.yml
- name: install and config corosync
 remote_user: root
 hosts: hbhosts
 roles:
   - common
   - ha
   - crmsh
- name: install nfs
 remote_user: root
 hosts: nfs-Server
 roles:
   - nfs

6.3、執行ansible-play安裝crmsh

[root@ansible-server ~]# ansible-playbook corosync/ha.yml

(七)使用crmsh配置http高可用

7.1、準備工作

[root@node1 ~]# crm
crm(live)# configure
crm(live)configure# property stonith-enabled=false   #默認情況下,如果沒有stonith設備,會不允許啓用,所以我們要設置爲安全忽略
crm(live)configure# property no-quorum-policy=ignore  #因爲我們只有2個節點,當我們其中一個節點下線了,那麼其將無法定票數達不到一半以上,所有如果只有兩個節點,必須將其使用安全忽略,否則節點將無法轉移
crm(live)configure# verify   #校驗是配置否存在問題
crm(live)configure# commit   #如無問題的話,提交所修改的配置

7.2、定義資源

包括webip,webserver,webstore

crm(live)configure# primitive webip ocf:IPaddr params ip=192.168.80.200 op monitor interval="30s" timeout="20s"
crm(live)configure# primitive webserver lsb:httpd op monitor interval="30s" timeout="20s"
crm(live)configure# primitive webstore ocf:Filesystem params device="192.168.80.188:/web/htdocs" directory="/var/www/html" fstype="nfs" op monitor interval="60s" timeout="40s" op start timeout="60s" interval="0" op stop timeout="60s" interval="0"
crm(live)configure# verify

7.3、定義組和順序約束

crm(live)configure# group webservice webip webstore webserver
crm(live)configure# order webip_before_webstore_before_webserver inf: webip webstore webserver
crm(live)configure# verify
crm(live)configure# commit

7.4、檢查節點和資源是否正常

crm(live)# status 
Last updated: Fri Apr 29 05:46:15 2016
Last change: Thu Aug 13 17:23:52 2015 via cibadmin on node1.windchaser.com
Stack: classic openais (with plugin)
Current DC: node2.windchaser.com - partition with quorum
Version: 1.1.10-14.el6-368c726
2 Nodes configured, 2 expected votes
3 Resources configured
Online: [ node1.windchaser.com node2.windchaser.com ]
Resource Group: webservice
    webip  (ocf::heartbeat:IPaddr):    Started node1.windchaser.com
    webstore   (ocf::heartbeat:Filesystem):    Started node1.windchaser.com
    webserver  (lsb:httpd):    Started node1.windchaser.com

(八)驗證

1)、使用客戶端訪問webip,可以正常查看到對應的網址

1.jpg

2)、將node1下線

[root@node1 ~]# crm node standby

3)、再次查看節點以及資源狀態

[root@node1 ~]# crm status
Online: [ node2.windchaser.com ]
Resource Group: webservice
    webip  (ocf::heartbeat:IPaddr):    Started node2.windchaser.com
    webstore   (ocf::heartbeat:Filesystem):    Started node2.windchaser.com
    webserver  (lsb:httpd):    Started node2.windchaser.com

發現資源已轉移至node2,重新使用客戶端訪問webip,發現可正常使用 
4)、將node1節點重新上線,此時可正常使用。

[root@node1 ~]# crm node online

(九)需要注意的地方

  • node1和node2的時間必須同步

  • node1和node2必須可以正常解析對方的主機名和IP地址



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