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地址

原创文章,作者:螃蟹,如若转载,请注明出处:http://www.178linux.com/15677

(0)
螃蟹螃蟹
上一篇 2016-04-28 21:37
下一篇 2016-04-30 23:11

相关推荐

  • Homework Week-8 网络及脚本编程

    1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。 设备 功能 使用场景 网桥 用于连接不同网段,将相似的网络连接起来,隔离信息。 连接不同部门间的局域网;连接地理位置分散并且相距较远的局域网,可以增加工作的物理距离;采用由网桥连接的多个局域网调节负载;网桥可以设置在局域网的关键部位,防止单点失常而破坏整个系统等。 集线器 对接收…

    Linux干货 2016-10-09
  • 第四周作业

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@wlm ~]# useradd tuser1 [root@wlm ~]# cp -r etc/skel/ /home/tuser1/…

    Linux干货 2016-11-21
  • 程序员的相关笑话(二)

    从前,有一个牧羊人,他有很多的羊。一天他赶着他的那群羊到了一条公路边上。突然,有一辆保时洁急驶过来,上面坐着一个年轻人人,穿着Armani的衣服,和Cerutti的皮鞋,Ray-Ban的太阳眼镜,TAG-Heuer的手表,以前Versace的领带。 他走到牧羊人面前问牧羊人:“如果我能说出你有多少只羊,你能给我一只吗?” 牧羊人看了看他那一大群数都数不过来的…

    Linux干货 2016-07-10
  • bash编程初体验(二)

    bash编程初体验(二) read if case 概述 在本篇文章中,我们将介绍bash编程中有关if语句的简单用法,if语句的基本思路是判断给定的条件是否满足,即结果是真还是假,从而选择执行哪种操作。如此,如果条件为真,if会执行一种指令,如果条件为假,if会选择执行另一种指令,这种执行就是所谓的选择结构,它能够改变命令的基本顺序流结构,以选择流的形式运…

    Linux干货 2016-08-19
  • AWK文本工具和软件包管理

    AWK文本工具 两种版本1.nawk   2.gawk gawk    模式扫描和处理语言 选项: -F 指明输入时用到的字段分隔符 -v  var=value:自定义变量 基本格式: awk [options] ’program’   file…. program:pattern{action statrments;………

    Linux干货 2018-03-15
  • N25 _WSH 第二周

    2、bash 命令执行的状态结果:        * bash通过状态返回值来输出此结果:            * 成功:0            失败:1-255     &n…

    系统运维 2016-12-12

评论列表(6条)

  • wxdz_2004(古雨秋)
    wxdz_2004(古雨秋) 2016-07-03 21:12

    试验的系统和corosync,pacemaker ,crmsh ,pssh 的版本是多少啊;
    我用centos6.5 corosync1.4.7 pacemaker1.1.14 crmsh 1.2.6 pssh 2.3.1 部署完后运行crm ->configure–>
    报这个错ERROR: CIB not supported: validator ‘pacemaker-2.4’, release ‘3.0.10’

    • 螃蟹
      螃蟹 2016-07-03 21:39

      @wxdz_2004(古雨秋)crmsh-1.2.6-4.el6.x86_64.rpm
      pssh-2.3.1-2.el6.x86_64.rpm
      这个是我在做这个实验的时候所需要使用的程序包,剩下的程序包直接使用yum安装即可
      不过你这个问题我之前也遇到过,曾经也有小伙伴给出答案。不过当时实验已经完成,所以没去试。我下面列出方法,你试试看看能否解决
      出现这种问题,应该是pacemaker-2.0发现crm shell版本相对较低,所以不能被CIB锁支持,需要更新crmsh的版本
      执行 cibadmin –query | grep validate 可以查看这条信息
      <cib crm_feature_set=“3.0.0” validate-with="pacemaker-2.0"
      所以尝试另一个办法,将检验器的版本降低
      cibadmin –modify –xml-text '’
      你试试这个方法,看是否能解决您的问题

    • wxdz_2004(古雨秋)
      wxdz_2004(古雨秋) 2016-07-03 23:30

      @螃蟹cibadmin –modify –xml-text ”
      更改版本后可以解决;
      你是第几期的?

    • 螃蟹
      螃蟹 2016-07-04 09:59

      @wxdz_2004(古雨秋)第12期的,请多多指教 (*^__^*) ……

    • wxdz_2004(古雨秋)
      wxdz_2004(古雨秋) 2016-07-04 17:54

      @螃蟹我11,还没你快,现在做运维吗?

    • 螃蟹
      螃蟹 2016-07-04 20:47

      @wxdz_2004(古雨秋)现在还没呢,不过已经辞职,准备找工作