kuyu 发表于 2014-10-14 13:10:36

高可用集群corosync+pacemaker+drbd+httpd----自动配置篇

本文结合shell+ansible实现高可用集群
实验拓扑图如下


首先利用yum安装ansible

配置过程如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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文件

1
2
3

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 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/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 "*********************DATE SYNC IS NOT
               OK WAITING ^^********************"
       rm -rf /tmp/status.txt
       continue;

    else
       echo -e "*********************DATE SYNC IS
               OK ^^*************************"
       break;
    fi
done
ansible-playbook $ROLEFIL2







drbdha_1.yaml文件


1
2
3
4
5
- name: deploy hadrbd cluster
remote_user: root
hosts: drbdservers
roles:
- drbdha_1







crmdrbd.txt文件


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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文件


1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 文件内容


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
- name: build disk partition
shell: echo -e "np3   +2Gw " |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 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- 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文件



1
2
3
4
5
- name: deploy hadrbd cluster
remote_user: root
hosts: drbdservers
roles:
- drbdha_2







handlers-- main.yaml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
- 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 文件内容



1
2
3
4
5
6
7
- name: part2
shell: echo "part2 is start"
notify:
    - mke2fs
    - stop service
    - configure corosync
- service restart





下面执行安装脚本
Bash deploy.sh
执行内容如下图








执行结束后到部署的节点查看结果

页: [1]
查看完整版本: 高可用集群corosync+pacemaker+drbd+httpd----自动配置篇