高可用集羣corosync+pacemaker+drbd+httpd----自動配置篇

               高可用集羣corosync+pacemaker+drbd+httpd----自動配置篇

本文結合shell+ansible實現高可用集羣

實驗拓撲圖如下

wKioL1Q7NjnBnONJAAD9tvHTb6A711.jpg

首先利用yum安裝ansible

 

配置過程如下

[root@node1 ~]# tree /etc/ansible/
/etc/ansible/
|-- ansible.cfg    ---------------ansible配置文件
|-- deploy.sh     ----------------安裝腳本
|-- drbdha_1.yaml   
|-- drbdha_2.yaml
|-- hosts        ----------------ansible配置文件
`-- roles  
    |-- drbdha_1  ------------amsible角色1
    |   |-- files
    |   |   |-- ansible-1.5.4-1.el6.noarch.rpm
    |   |   |-- authkey   ------與手動篇文件相同
    |   |   |-- corosync.conf ------與手動篇文件相同
    |   |   |-- crmdrbd.txt
    |   |   |-- crmsh-1.2.6-4.el6.x86_64.rpm
    |   |   |-- drbd-8.4.3-33.el6.x86_64.rpm
    |   |   |-- drbd-kmdl-2.6.32-431.el6-8.4.3-33.el6.x86_64.rpm
    |   |   |-- global_common.conf  -------與手動篇文件相同
    |   |   |-- hosts
    |   |   |-- pssh-2.3.1-2.el6.x86_64.rpm
    |   |   `-- web.res
    |   |-- handlers
    |   |   `-- main.yaml
    |   |-- tasks
    |   |   `-- main.yaml
    |   |-- templates
    |   `-- vars
`-- drbdha_2  -----------------------ansible角色2
        |-- handlers
        |   `-- main.yaml
        `-- tasks
            `-- main.yaml
 
10 directories, 20 files

各文件內容如下

Ansible.cfg使用默認配置

Hosts文件

[drbdservers]
172.16.34.2 ansible_ssh_user=root ansible_ssh_pass=hzm132
172.16.34.3 ansible_ssh_user=root ansible_ssh_pass=hzm132


deploy.sh 文件

#!/bin/bash
#DES:install drbd+corosync
#author:centod
#date:2014-10-12
#ver:0.1
 
ROLEFIL1=./drbdha_1.yaml
ROLEFIL2=./drbdha_2.yaml
ansible-playbook $ROLEFIL1
[ -f /tmp/status.txt ] && rm -rf /tmp/status.txt
until false; do
    ansible-playbook -t filecopy $ROLEFIL1
    sleep 5
    if grep '%' /tmp/status.txt;then
       echo -e "\033[31m*********************DATE SYNC IS NOT \
               OK WAITING ^^********************\033[0m"
       rm -rf /tmp/status.txt
       continue;
 
    else
       echo -e "\033[32m*********************DATE SYNC IS \
               OK ^^*************************\033[0m"
       break;
    fi
done
ansible-playbook $ROLEFIL2


 

drbdha_1.yaml文件

 

- name: deploy hadrbd cluster
  remote_user: root
  hosts: drbdservers
  roles:
- drbdha_1


 

 crmdrbd.txt文件

 

configure property stonith-enabled=false
configure property no-quorum-policy=ignore
configure commit
configure primitive drbd ocf:linbit:drbd \
        params drbd_resource="web" \
        op monitor role="Master" interval="20s" timeout="30s" \
        op monitor role="Slave" interval="40s" timeout="30" \
        op start timeout="240s" interval="0" \
        op stop timeout="100s" interval="0"
configure primitive httpd lsb:httpd \
        op monitor interval="20s" timeout="20s" \
        op start timeout="20s" interval="0" \
        op stop timeout="20s" interval="0" \
        meta target-role="Started"
configure primitive httpd-storage ocf:heartbeat:Filesystem \
        params device="/dev/drbd0" directory="/var/www/html" fstype="ext4" \
        op monitor interval="20s" timeout="40s" \
        op start timeout="60s" interval="0" \
        op stop timeout="60s" interval="0" \
        meta target-role="Started"
configure primitive vip ocf:heartbeat:IPaddr \
        params ip="172.16.101.220" \
        meta target-role="Started"
configure ms ms-webdrbd drbd \
        meta master-max="1" master-node-max="1" clone-max="2" \
        clone-node-max="1" notify="true" target-role="Started"
configure colocation storage-nrbd-master-httpd-vip inf: httpd-storage ms-webdrbd:Master httpd vip
configure order nrbd-storage-vip-httpd inf: ms-webdrbd:promote httpd-storage:start vip httpd
configure commit


 

web.res文件

 

resource web {
        on node2.centod.com {
        device /dev/drbd0;
        disk   /dev/sda3;
        address 172.16.34.2:7789;
        meta-disk internal;
        }
        on node3.centod.com {
        device /dev/drbd0;
        disk  /dev/sda3;
        address 172.16.34.3:7789;
        meta-disk internal;
      }
 }


 

handlers-- main.yaml 文件內容

 

- name: build disk partition
  shell: echo -e "n \n p \n 3 \n \n +2G \n w \n" |fdisk /dev/sda || /bin/true
- name: partition flush
  shell: kpartx -af /dev/sda
  shell: partx  -a /dev/sda
  shell: partx  -a /dev/sda
- name: service configure
  copy: src={{ item.conf }} dest={{ item.destdir }}
  with_items:
    - { conf: global_common.conf,destdir: /etc/drbd.d/ }
    - { conf: web.res           ,destdir: /etc/drbd.d/ }
    - { conf: corosync.conf     ,destdir: /etc/corosync/ }
    - { conf: authkey           ,destdir: /etc/corosync  }
    - { conf: crmdrbd.txt       ,destdir: /etc/corosync  }
- name: init drbd device
  shell: drbdadm create-md web
  ignore_errors: True
- name: service restart
  service: name=corosync state=restarted
- name: modprobe
  shell: modprobe drbd
- name: chkconf off
  shell: chkconfig drbd off
  shell: chkconfig httpd off
- name: drbd start
  service: name=drbd state=restarted
- name: drbd promote
  shell: drbdadm primary --force web
  when: ansible_hostname == "node2"


-- tasks-- main.yaml 文件

- name: deploy rpm pakages
  copy: src={{ item }} dest=/root/
  with_items:
        - crmsh-1.2.6-4.el6.x86_64.rpm
        - pssh-2.3.1-2.el6.x86_64.rpm
        - drbd-8.4.3-33.el6.x86_64.rpm
        - drbd-kmdl-2.6.32-431.el6-8.4.3-33.el6.x86_64.rpm
  notify:
        - install rpm
        - build disk partition
        - partition flush
        - service configure
        - init drbd device
        - service restart
        - modprobe
        - chkconf off
        - drbd start
        - drbd promote
- name: status file copy
  fetch: src=/proc/drbd dest=/tmp/status.txt flat=yes validate_md5=no
  when: ansible_hostname == "node2"
  tags: filecopy


 

drbdha_2.yaml文件

 

 

- name: deploy hadrbd cluster
  remote_user: root
  hosts: drbdservers
  roles:
- drbdha_2


 

handlers-- main.yaml文件

- name: mke2fs
  shell: mke2fs -t ext4 /dev/drbd0
  when: ansible_hostname == "node2"
- name: stop service
  service: name={{ item }} state=stopped
  with_items:
  - httpd
  - drbd
- name: configure corosync
  shell: crm -f /etc/corosync/crmdrbd.txt
  when: ansible_hostname == "node2"
- name: service restart
  service: name=corosync state=restarted


 

 

tasks-- main.yaml 文件內容



- name: part2
  shell: echo "part2 is start"
  notify:
    - mke2fs
    - stop service
    - configure corosync
- service restart

下面執行安裝腳本

Bash deploy.sh

執行內容如下圖

wKioL1Q7N0LgVNcDAARZEkjod8M128.jpg

wKiom1Q7NwvA7uo_AAVp14pRfVM360.jpg

wKioL1Q7N0LQJAMzAAPKltBo3lo993.jpg

wKiom1Q7NwzDE2WrAAPLrQiw27I594.jpg

wKioL1Q7N0OC8qB0AAODdwZQIFc817.jpg

wKioL1Q7N0PhVElxAAKXFM6VEHE088.jpg


執行結束後到部署的節點查看結果

wKioL1Q7N2Xj8uwYAALCDm0SPbo892.jpg 


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