corosync+pacemaker高可用

本文介绍了如何使用HAProxy实现负载均衡,并结合Corosync和Pacemaker构建高可用集群。详细讲解了安装配置过程及资源管理。

参考
https://www.linuxidc.com/Linux/2016-08/133864.htm
http://www.toxingwang.com/linux-unix/haorlb/1660.html
https://blog.youkuaiyun.com/tantexian/article/details/50056199
https://www.iyunv.com/thread-40135-1-1.html
简介
OpenAIS概述

OpenAIS是基于SA Forum 标准的集群框架的应用程序接口规范。OpenAIS提供一种集群模式,这个模式包括集群框架,集群成员管理,通信方式,集群监测等,能够为集群软件或工具提供满足 AIS标准的集群接口,但是它没有集群资源管理功能,不能独立形成一个集群。OpenAIS组件包括AMF,CLM,CKPT,EVT,LCK,MSG,TMR,CPG,EVS等,因OpenAIS分支不同,组件略有不同。(下面介绍)OpenAIS主要包含三个分支:Picacho,Whitetank,Wilson。Wilson把Openais核心架构组件独立出来放在Corosync(Corosync是一个集群管理引擎)里面。(详细请查看官方文档)

Corosync概述

Corosync是OpenAIS发展到Wilson版本后衍生出来的开放性集群引擎工程(可以说Corosync是OpenAIS工程的一部分)。Corosync包含OpenAIS的核心框架用来对Wilson的标准接口的使用、管理。它为商用的或开源性的集群提供集群执行框架。目前Corosync分为1版本和2版本,1版本不具有投票功能,2版本具有投票功能。
Pacemaker概述

Pacemaker是一个集群资源管理器,它利用首选集群基础设施(OpenAIS 或heartbeat)提供的消息和成员能力,由辅助节点和系统进行故障检测和回收,实现性群集服务(亦称资源)的高可用性。Pacemaker是从Heartbeat V3版本中分裂出来的专门用于提供高可用集群的CRAM组件。

Pacemaker特点
主机和应用程序级别的故障检测和恢复
几乎支持任何冗余配置
同时支持多种集群配置模式
配置策略处理法定人数损失(多台机器失败时)
支持应用启动/关机顺序
支持,必须/必须在同一台机器上运行的应用程序
支持多种模式的应用程序(如主/从)
可以测试任何故障或群集的群集状态
crmd(资源管理守护进程):主要作为pengine和lrm的消息代理,它同时也得选举一个leader去协调管理集群的活动(包括集群资源的停止和启动)
stonithd(心跳系统):在 Pacemaker 中,STONITH设备被当成资源(并且是在CIB中配置)从而轻松地监控,然而Stonithd会注意理解STONITH拓扑,比如它的客户端请求隔离一个节点,它会重启那个机器。
cib(集群信息库):CIB在系统中充当的是当前集群中各资源原始配置以及之后动态变化了的状态,统计信息收集分发中心,是一个不断更新的信息库(包含所有群集选项,节点,资源以及他们的相互关系和当前状态的定义)。当他收集到任何资源的变化,以及节点统计信息的变化后,都会集成整合到一起组成当前集群最新的信息,并分发到集群各个节点。
pengine(策略引擎):主要负责将CRM发过来的一些信息按照配置文件中的各种设置(基于目前的状态和配置)计算集群的下一个状态。产生一个包括一系列行动和依赖关系的过渡图。

haproxy
HAProxy的特点是:
1、支持两种代理模式:TCP(四层)和HTTP(七层),支持虚拟主机;
2、能够补充Nginx的一些缺点比如Session的保持,Cookie的引导等工作
3、支持url检测后端的服务器出问题的检测会有很好的帮助。
4、更多的负载均衡策略比如:动态加权轮循(Dynamic Round Robin),加权源地址哈希(Weighted Source Hash),加权URL哈希和加权参数哈希(Weighted Parameter Hash)已经实现
5、单纯从效率上来讲HAProxy更会比Nginx有更出色的负载均衡速度。
6、HAProxy可以对Mysql进行负载均衡,对后端的DB节点进行检测和负载均衡。
9、支持负载均衡算法:Round-robin(轮循)、Weight-round-robin(带权轮循)、source(原地址保持)、RI(请求URL)、rdp-cookie(根据cookie)
10、不能做Web服务器即Cache。

主机名:
192.168.122.1 server 我的物理机
192.168.122.2 server2 haproxy/corosync/pacemker/crm
192.168.122.3 server3 haproxy/corosync/pacemker/crm
192.168.122.4 server4 httpd 主页为server4
192.168.122.5 server5 httpd 主页为server5
准备工作
1.配置/etc/hosts的本地解析
2.时间同步工作
3.ssh 免登陆

正式开始:
1.安装配置haproxy

[root@server2 ~]# ll haproxy-1.6.11.tar.gz 
-rw------- 1 root root 1574029 May 13 17:24 haproxy-1.6.11.tar.gz
[root@server2 ~]# yum install -y rpm-build
[root@server2 ~]# yum install -y pcre-devel gcc
[root@server2 ~]# rpmbuild -tb haproxy-1.6.11.tar.gz
[root@server2 ~]# ll rpmbuild/RPMS/x86_64/haproxy-1.6.11-1.x86_64.rpm 
-rw-r--r-- 1 root root 786636 May 13 17:26 rpmbuild/RPMS/x86_6/haproxy-1.6.11-1.x86_64.rpm
[root@server2 x86_64]# yum install haproxy-1.6.11-1.x86_64.rpm -y

通过上述的步骤,可以得到haproxy的rpm包,但是此时的rpm包,但是此时的rpm是安装后是没有配置文件的,所以依然要将原来的包解压,将配置文件复制出来,放到/etc/haproxy下就可以了。
配置haproxy

[root@server2 ~]# vim /etc/haproxy/haproxy.cfg 
#
# This is a sample configuration. It illustrates how to separate static objects
# traffic from dynamic traffic, and how to dynamically regulate the server load.
#
# It listens on 192.168.1.10:80, and directs all requests for Host 'img' or
# URIs starting with /img or /css to a dedicated group of servers. URIs
# starting with /admin/stats deliver the stats page.
#

global
        maxconn         65535   ##最大连接数
        stats socket    /var/run/haproxy.stat mode 600 level admin
        log             127.0.0.1 local0 ##全局日志配置
        uid             200  ##所属用户
        gid             200  ##所属组
        chroot          /var/empty ##工作目录
        daemon           ## 以守护进程的方式运行haproxy
defaults
        mode            http   ##默认的模式
        log             global  ##应用全局的日志配置
        option          httplog   ##启用日志记录http请求
        option          dontlognull  ##启用后,日志将不会记录空连接
        monitor-uri     /monitoruri  
        maxconn         8000  ##每个进程可用的最大连接数
        timeout client  30s  ##检测超时
        retries         2  ##两次没有连接上,则标记RS不可用
        option redispatch  ##cookie相关
        timeout connect 5s  ##连接超时
        timeout server  5s  ##服务器超时
        stats uri       /admin/stats  



# The public 'www' address in the DMZ
frontend public
        bind            *:80 name clear   ##监听本机的80端口
        #bind            192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem

        #use_backend     static if { hdr_beg(host) -i img }
        #use_backend     static if { path_beg /img /css   }
        default_backend dynamic

# The static backend backend for 'Host: img', /img and /css.
backend dynamic
        balance         roundrobin    ##使用的负载均衡算法
        server          web1 192.168.122.4:80 check inter 1000  ##两个RS
        server          web2 192.168.122.5:80 check inter 1000

此时还有一个特别重要的问题,就是haproxy的最大连接数问题,在配置文件中,我们声明了haproxy的最大连接数为65535个,而且也声明了haproxy是以u=200,g=200的用户身份去运行的,但是本机并没有200这个用户,所以就需要创建这个用户,但是所创建的这个用户不是超级用户,所以他的最大连接数是受限制的,而且不可以以超级用户去运行haproxy,因为超级用户的最大连接数是没有限制的,当连接数超级大的时候,主机会奔溃,所以必须创建这个用户,并且设定最大连接数

[root@server2 ~]# groupadd -g 200 haproxy
[root@server2 ~]# useradd -u 200 -g 200 haproxy
[root@server2 ~]# vim /etc/security/limits.conf
#*               hard    rss             10000
#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#@student        -       maxlogins       4
haproxy - nofile 65535

# End of file

然后启动haproxy,测试:

###balance roundrobin算法
[root@server cluster]# curl 192.168.122.2
server4
[root@server cluster]# curl 192.168.122.2
server5
[root@server cluster]# curl 192.168.122.2
server4

下一步是做haproxy的高可用,此时需要将上述的所有都在server3上做一次。
corosync+pacemaker的使用
配置yum 源

[rhel6.5]
name=rhel6.5
baseurl=http://192.168.122.1/rhel6.5
gpgcheck=0

[HighAvailability]
name=HighAvailability
baseurl=http://192.168.122.1/rhel6.5/HighAvailability
gpgcheck=0

[LoadBalancer]
name=LoadBalancer
baseurl=http://192.168.122.1/rhel6.5/LoadBalancer
gpgcheck=0

[ResilientStorage]
name=ResilientStorage
baseurl=http://192.168.122.1/rhel6.5/ResilientStorage
gpgcheck=0

[ScalableFileSystem]
name=ScalableFileSystem
baseurl=http://192.168.122.1/rhel6.5/ScalableFileSystem
gpgcheck=0

安装corosync,pacemaker,crm

[root@server2 ~]# yum install -y pacemaker corosync
[root@server2 ~]# yum install pssh-2.3.1-2.1.x86_64.rpm crmsh-1.2.6-0.rc2.2.1.x86_64.rpm -y

修改配置文件

[root@server2 ~]# vim /etc/corosync/corosync.conf
# Please read the corosync.conf.5 manual page
compatibility: whitetank

totem {
    version: 2  ##版本号,只能是2,不能修改
    secauth: off     ##安全认证,当使用aisexec时,会非常消耗CPU
    threads: 1   ####线程数,根据CPU个数和核心数确定
    interface {
        ringnumber: 0    ##冗余环号,节点有多个网卡是可定义对应网卡在一个环内
        bindnetaddr: 192.168.122.0   ##绑定心跳网段
        mcastaddr: 226.94.1.1  ##心跳组播地址
        mcastport: 5405  ##心跳组播使用端口
        ttl: 1  ##广播包的ttl,直接到达另一个节点
    }
}

logging {
    fileline: off   ##指定要打印的行
    to_stderr: no    ##是否发送到标准错误输出
    to_logfile: yes     ##记录到文件
    to_syslog: yes        ##记录到syslog
    logfile: /var/log/cluster/corosync.log  ##日志文件路径
    debug: off       ##是否启动调试
    timestamp: on   ##是否打印时间戳,利于定位错误,但会消耗CPU 
    logger_subsys {        ##日志的子系统
        subsys: AMF       
        debug: off
    }
}

amf {               ##跟编程接口相关的
    mode: disabled
}
service {         //定义一个服务来启动pacemaker
    name:pacemaker        ##定义corosync启动时同时启动pacemaker
    ver:0              //定义版本
}

将上述的动作也发给server3,确保上个虚拟机上的所有配置的都一样,另外需要将server2和server3上面的haproxy停止。
然后再启动corosync,此时pacemaker也会启动,然后使用crm集群资源管理器管理集群资源
注意!!!!!!
本次试验环境只有两个haproxy,在测试过程中会关闭其中的一个,只剩下一个,不能构成集群,所以是需要配置的,另外crm_verify -LV也会报错,是因为没有stonith设备,所以也需要关闭
开始配置资源

[root@server2 ~ ]# crm
crm(live)# configure
crm(live)configure# property stonith-enabled=false
crm(live)configure# property no-quorum-policy=ignore
crm(live)configure# primitive  haproxy lsb:haproxy op monitor interval=1min
primitive vip ocf:heartbeat:IPaddr2 params ip=192.168.122.100 cidr_netmask=24 op monitor interval=30
group linxu vip haproxy

查看集群信息

crm(live)configure# show
node server2
node server3
primitive haproxy lsb:haproxy \
    op monitor interval="1min"
primitive vip ocf:heartbeat:IPaddr2 \
    params ip="192.168.122.100" cidr_netmask="24" \
    op monitor interval="30"
group linux vip haproxy
property $id="cib-bootstrap-options" \
    dc-version="1.1.10-14.el6-368c726" \
    cluster-infrastructure="classic openais (with plugin)" \
    expected-quorum-votes="2" \
    no-quorum-policy="ignore" \
    stonith-enabled="false"

监控

Last updated: Sun May 13 18:33:53 2018
Last change: Sun May 13 17:07:16 2018 via cibadmin on server2
Stack: classic openais (with plugin)
Current DC: server2 - partition with quorum
Version: 1.1.10-14.el6-368c726
2 Nodes configured, 2 expected votes
2 Resources configured


Online: [ server2 server3 ]

 Resource Group: linux
     vip        (ocf::heartbeat:IPaddr2):   Started server2
     haproxy    (lsb:haproxy):  Started server2

测试vip和高可用

[root@server cluster]# curl 192.168.122.100
server5
[root@server cluster]# curl 192.168.122.100
server4
[root@server cluster]# curl 192.168.122.100
server5
[root@server cluster]# curl 192.168.122.100
server4

关闭server2

Last updated: Sun May 13 18:37:18 2018
Last change: Sun May 13 17:07:16 2018 via cibadmin on server2
Stack: classic openais (with plugin)
Current DC: server3 - partition WITHOUT quorum
Version: 1.1.10-14.el6-368c726
2 Nodes configured, 2 expected votes
2 Resources configured


Online: [ server3 ]
OFFLINE: [ server2 ]

 Resource Group: linux
     vip        (ocf::heartbeat:IPaddr2):   Started server3
     haproxy    (lsb:haproxy):  Started server3

关于crm

[root@server2 ~]# crm
crm(live)# help    # 获取当前可用命令
# 一级子命令
This is crm shell, a Pacemaker command line interface.
Available commands:
cib              manage shadow CIBs # cib沙盒
resource         resources management # 所有的资源都在这个子命令后定义
configure        CRM cluster configuration # 编辑集群配置信息
node             nodes management # 集群节点管理子命令
options          user preferences # 用户优先级
history          CRM cluster history
site             Geo-cluster support
ra               resource agents information center # 资源代理子命令(所有与资源代理相关的程都在此命令之下)
status           show cluster status # 显示当前集群的状态信息
help,?           show help (help topics for list of topics)# 查看当前区域可能的命令
end,cd,up        go back one level # 返回第一级crm(live)#
quit,bye,exit    exit the program  # 退出crm(live)交互模式

resource 子命令 # 定义所有资源的状态

crm(live)resource# help
vailable commands:
        status           show status of resources # 显示资源状态信息
        start            start a resource # 启动一个资源
        stop             stop a resource # 停止一个资源
        restart          restart a resource # 重启一个资源
        promote          promote a master-slave resource # 提升一个主从资源
        demote           demote a master-slave resource # 降级一个主从资源
        manage           put a resource into managed mode
        unmanage         put a resource into unmanaged mode
        migrate          migrate a resource to another node # 将资源迁移到另一个节点上
        unmigrate        unmigrate a resource to another node
        param            manage a parameter of a resource # 管理资源的参数
        secret           manage sensitive parameters # 管理敏感参数
        meta             manage a meta attribute # 管理源属性
        utilization      manage a utilization attribute
        failcount        manage failcounts # 管理失效计数器
        cleanup          cleanup resource status # 清理资源状态
        refresh          refresh CIB from the LRM status # 从LRM(LRM本地资源管理)更新CIB(集群信息库),在
        reprobe          probe for resources not started by the CRM # 探测在CRM中没有启动的资源
        trace            start RA tracing # 启用资源代理(RA)追踪
        untrace          stop RA tracing # 禁用资源代理(RA)追踪
        help             show help (help topics for list of topics) # 显示帮助
        end              go back one level # 返回一级(crm(live)#)
        quit             exit the program # 退出交互式程序

configure 子命令 # 资源粘性、资源类型、资源约束

crm(live)configure# help
Available commands:
        node             define a cluster node # 定义一个集群节点
        primitive        define a resource # 定义资源
        monitor          add monitor operation to a primitive # 对一个资源添加监控选项(如超时时间,启动失败后的操作)
        group            define a group # 定义一个组类型(将多个资源整合在一起)
        clone            define a clone # 定义一个克隆类型(可以设置总的克隆数,每一个节点上可以运行几个克隆)
        ms               define a master-slave resource # 定义一个主从类型(集群内的节点只能有一个运行主资源,其它从的做备用)
        rsc_template     define a resource template # 定义一个资源模板
        location         a location preference # 定义位置约束优先级(默认运行于那一个节点(如果位置约束的值相同,默认倾向性那一个高,就在那一个节点上运行))
        colocation       colocate resources # 排列约束资源(多个资源在一起的可能性)
        order            order resources # 资源的启动的先后顺序
        rsc_ticket       resources ticket dependency
        property         set a cluster property # 设置集群属性
        rsc_defaults     set resource defaults # 设置资源默认属性(粘性)
        fencing_topology node fencing order # 隔离节点顺序
        role             define role access rights # 定义角色的访问权限
        user             define user access rights # 定义用用户访问权限
        op_defaults      set resource operations defaults # 设置资源默认选项
        schema           set or display current CIB RNG schema
        show             display CIB objects # 显示集群信息库对
        edit             edit CIB objects # 编辑集群信息库对象(vim模式下编辑)
        filter           filter CIB objects # 过滤CIB对象
        delete           delete CIB objects # 删除CIB对象
        default-timeouts set timeouts for operations to minimums from the meta-data
        rename           rename a CIB object # 重命名CIB对象
        modgroup         modify group # 改变资源组
        refresh          refresh from CIB # 重新读取CIB信息
        erase            erase the CIB # 清除CIB信息
        ptest            show cluster actions if changes were committed
        rsctest          test resources as currently configured
        cib              CIB shadow management
        cibstatus        CIB status management and editing
        template         edit and import a configuration from a template
        commit           commit the changes to the CIB # 将更改后的信息提交写入CIB
        verify           verify the CIB with crm_verify # CIB语法验证
        upgrade          upgrade the CIB to version 1.0
        save             save the CIB to a file # 将当前CIB导出到一个文件中(导出的文件存于切换crm 之前的目录)
        load             import the CIB from a file # 从文件内容载入CIB
        graph            generate a directed graph
        xml              raw xml
        help             show help (help topics for list of topics) # 显示帮助信息
        end              go back one level # 回到第一级(crm(live)#)

node子命令 # 节点管理和状态

crm(live)# node
crm(live)node# help
Node management and status commands.
Available commands:
    status           show nodes status as XML # 以xml格式显示节点状态信息
    show             show node # 命令行格式显示节点状态信息
    standby          put node into standby # 模拟指定节点离线(standby在后面必须的FQDN)
    online           set node online # 节点重新上线
    maintenance      put node into maintenance mode
    ready            put node into ready mode
    fence            fence node # 隔离节点
    clearstate       Clear node state # 清理节点状态信息
    delete           delete node # 删除 一个节点
    attribute        manage attributes
    utilization      manage utilization attributes
    status-attr      manage status attributes
    help             show help (help topics for list of topics)
    end              go back one level
    quit             exit the program

ra子命令 # 资源代理类别都在此处

crm(live)# ra
crm(live)ra# help
Available commands:
        classes          list classes and providers # 为资源代理分类
        list             list RA for a class (and provider)# 显示一个类别中的提供的资源
        meta             show meta data for a RA # 显示一个资源代理序的可用参数(如meta ocf:heartbeat:IPaddr2)
        providers        show providers for a RA and a class
        help             show help (help topics for list of topics)
        end              go back one level
        quit             exit the program
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值