设为首页 收藏本站
查看: 1060|回复: 0

[经验分享] Ansible+Corosync+Pacemaker+nfs实现http高可用

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-4-29 16:36:09 | 显示全部楼层 |阅读模式
目录:

(一)实验环境

(二)准备工作

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

(四)使用ansible部署nfs

(五)使用ansible部署corosync和pacemaker

(六)使用ansible安装crmsh工具

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

(八)验证

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

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

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

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

[iyunv@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文件

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

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

2.4、配置ansible下的hosts文件

[iyunv@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 、使用秘钥让两台主机互相通信

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

[iyunv@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源

[iyunv@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源至对应的目录

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

[iyunv@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

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

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

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

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

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

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

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

4.3、定义nfs的tasks

[iyunv@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

[iyunv@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设置

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

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

[iyunv@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所需的秘钥信息

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

[iyunv@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文件

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

5.5、定义YAML文件

[iyunv@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设置

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

(六)使用ansible安装crmsh工具

所需程序包:

[iyunv@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文件

[iyunv@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

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

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

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

1461907726236137.jpg


2)、将node1下线

[iyunv@node1 ~]# crm node standby

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

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

[iyunv@node1 ~]# crm node online

(九)需要注意的地方

    node1和node2的时间必须同步

    node1和node2必须可以正常解析对方的主机名和IP地址

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-210383-1-1.html 上篇帖子: ansible 中如何取消ssh交换式 yes/no 下篇帖子: Ansible(一)基础
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表