阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

145次阅读
没有评论

共计 14090 个字符,预计需要花费 36 分钟才能阅读完成。

目录:

(一)实验环境
(二)准备工作
(三)为 node1 和 node2 配置基础配置
(四)使用 ansible 部署 nfs
(五)使用 ansible 部署 corosync 和 pacemaker
(六)使用 ansible 安装 crmsh 工具
(七)使用 crmsh 配置 http 高可用
(八)验证
(九)需要注意的地方

(一)实验环境
1.1、环境拓扑

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

使用 Ansible 高效交付 Docker 容器  http://www.linuxidc.com/Linux/2015-10/124233.htm

使用 Ansible 批量管理远程服务器  http://www.linuxidc.com/Linux/2015-05/118080.htm

Ansible 安装配置与简单使用  http://www.linuxidc.com/Linux/2015-07/120399.htm

在 CentOS 7 中安装并使用自动化工具 Ansible  http://www.linuxidc.com/Linux/2015-10/123801.htm

Ansible 和 Docker 的作用和用法  http://www.linuxidc.com/Linux/2014-11/109783.htm

Ansible 批量搭建 LAMP 环境 http://www.linuxidc.com/Linux/2014-10/108264.htm

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

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2016-04/130844p2.htm

(五)使用 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,可以正常查看到对应的网址

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

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

Ansible:一个配置管理和 IT 自动化工具  http://www.linuxidc.com/Linux/2014-11/109365.htm

Ansible 的详细介绍:请点这里
Ansible 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-04/130844.htm

目录:

(一)实验环境
(二)准备工作
(三)为 node1 和 node2 配置基础配置
(四)使用 ansible 部署 nfs
(五)使用 ansible 部署 corosync 和 pacemaker
(六)使用 ansible 安装 crmsh 工具
(七)使用 crmsh 配置 http 高可用
(八)验证
(九)需要注意的地方

(一)实验环境
1.1、环境拓扑

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

使用 Ansible 高效交付 Docker 容器  http://www.linuxidc.com/Linux/2015-10/124233.htm

使用 Ansible 批量管理远程服务器  http://www.linuxidc.com/Linux/2015-05/118080.htm

Ansible 安装配置与简单使用  http://www.linuxidc.com/Linux/2015-07/120399.htm

在 CentOS 7 中安装并使用自动化工具 Ansible  http://www.linuxidc.com/Linux/2015-10/123801.htm

Ansible 和 Docker 的作用和用法  http://www.linuxidc.com/Linux/2014-11/109783.htm

Ansible 批量搭建 LAMP 环境 http://www.linuxidc.com/Linux/2014-10/108264.htm

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

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2016-04/130844p2.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计14090字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中