3213213 发表于 2016-4-29 16:36:09

Ansible+Corosync+Pacemaker+nfs实现http高可用

目录:

(一)实验环境

(二)准备工作

(三)为node1和node2配置基础配置

(四)使用ansible部署nfs

(五)使用ansible部署corosync和pacemaker

(六)使用ansible安装crmsh工具

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

(八)验证

(九)需要注意的地方
(一)实验环境
1.1、环境拓扑

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源


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

    备注:因为ansible所需的程序包在epel源有提供

2)、安装ansible

# yum -y install ansible

2.2、创建ansble-playbook所需使用到的目录

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

# touch corosync/ha.yml
# touch corosync/site.yml   

    备注:此文件虽可不配置,但此文件必须存在

2.4、配置ansible下的hosts文件

# vim /etc/ansible/hosts
   #node1和node2的组
192.168.80.153
192.168.80.152
   #nfs-server组
192.168.80.168

2.5 、使用秘钥让两台主机互相通信

# ssh-keygen -t rsa -P ''    #生成密钥串
# 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文件,用于节点间互相通信

# 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.152node1.windchaser.com node1
192.168.80.153node2.windchaser.com node2

用于实现node1和node2主机互相通信
3.3、设置本地光盘yum源

# vim corosync/roles/common/files/local.repo

name=local repo
baseurl=file:///mnt
enabled=1
gpgcheck=0

3.4、定义common的tasks

目标:

    自动挂载光驱

    移除所有默认yum源,拷贝local.repo源至对应的目录

    使用计划任务设置时间自动同步

# 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

# vim corosync/ha.yml
- name: install and config corosync
remote_user: root
hosts: hbhosts
roles:
    - common

3.6、执行ansible-play自动部署基础配置

# ansible-playbook corosync/ha.yml

此时会自动部署先前我们所做的操作,如果全部都是OK状态,表示为正常,如果出现错误,请检查对应的配置项是否出错。
(四)使用ansible部署nfs
4.1、设定nfs-server共享目录

# vim corosync/roles/nfs/files/exports
/web/htdocs   192.168.80.0/24(rw)

4.2、创建http默认访问文件index.html,为后面做测试使用

# vim corosync/roles/nfs/files/index.html
<h1>nfs-storage</h1>

4.3、定义nfs的tasks

# 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

# 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设置

# ansible-playbook corosync/ha.yml

(五)使用ansible部署corosync和pacemaker
5.1、定义corosync配置信息

# 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所需的秘钥信息

# 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配置文件到各节点

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

# vim corosync/roles/ha/handlers/main.yml
- name: restart corosynce
service: name=corosynce state=restart

5.5、定义YAML文件

# 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设置

# ansible-playbook corosync/ha.yml

(六)使用ansible安装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 root49960 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文件

# 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

# ansible-playbook corosync/ha.yml

(七)使用crmsh配置http高可用
7.1、准备工作

# 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,可以正常查看到对应的网址



2)、将node1下线

# crm node standby

3)、再次查看节点以及资源状态

# 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节点重新上线,此时可正常使用。

# crm node online

(九)需要注意的地方

    node1和node2的时间必须同步

    node1和node2必须可以正常解析对方的主机名和IP地址
页: [1]
查看完整版本: Ansible+Corosync+Pacemaker+nfs实现http高可用