CentOS7 集群配置

来源:互联网 发布:知乎日报rss 编辑:程序博客网 时间:2024/05/21 09:49


我一直以为的集群是一批服务器组成一个群体-有任务过来时快速分配,快速完成,所以在集群环境搭建好之后,我就傻眼了,为什么服务器A上Nginx启动,服务器B上Nginx就会停掉。
去百度百科查了概念,虽表述的不简洁,但勉强能懂,用我自己的话翻译一下:集群是将一些独立的服务器,通过集群技术组成组,即多合一的模式,对外提供服务。首先保证的是高可用性,其次是可扩展性。
高可用性:一直都能提供服务,如业界常说的5个9。集群实现的就是当服务器A坏了,服务器B能立马接管服务器A的职能,而广大客户是感知不到其中的变化的。
可扩展性:通俗地讲 可横向增加/减少服务器。


  1. 需求
    在硬负载没到位的情况下,使用Nginx提供负载服务。
  2. 可用环境
    同网段的IP地址尾号为20、25服务器,操作系统:CentOS7.1
  3. 检查工作
    因之前有过类似的经验,通过heartbeat启动VIP和Nginx服务,每隔一定时间进行健康检查,检测到主服务器坏时,切换到备服务器上,主服务器复原后,自动切回来。
    提前做如下检查:
    一:需要一个VIP -----可用IP 为26
    二:确定Nginx版本 -------CentOS7.1支持Nginx1.10.1
    三:确定CentOS7.1上是否可用Heartbeat-------Heartbeat已拆分成多个组件,改用pcs,pacemaker,corosync,fence-agents-all来实现。
  4. 部署
    1) 禁用防火墙和selinux

    systemctl disable firewalld.servicesystemctl stop firewalld.service

    修改/etc/sysconfig/selinux确保SELINUX=disabled,然后执行setenforce 0或者reboot服务器以生效
    2) 两节点主机名设置

    vi /etc/hosts  #两台服务器一样配置192.168.220.20 yd20192.168.220.25 yd25

    3) 两节点ssh无密码密银访问设置
    在之前的文章Hadoop 2.7.1 搭建 有详细写过,这里不重写。
    4) 创建集群用户
    使用pacemaker管理
    每个节点上操作相同

    passwd hacluster#设置密码为hacluster2016

    5) 安装相应软件

    yum install -y resource-agents pacemaker pcs corosync fence-agents-allsystemctl enable pcsd.servicesystemctl enable corosync.servicesystemctl enable pacemaker.servicesystemctl start pcsd.servicesystemctl start corosync.servicesystemctl start pacemaker.service

    6) 集群节点之间认证

    pcs cluster auth yd25 yd20

    7) 创建并启动集群

    pcs cluster setup --start --name mycluster yd25 yd20

    8) 设置集群自启动

    pcs cluster enable --all

    9) 查看集群状态信息

    pcs cluster status

    10) 设置fence设备
    corosync默认启用了stonith,而当前集群并没有相应的stonith设备,因此此默认配置目前尚不可用,这可以通过如下命令验证:

    [root@yd25 ~]# crm_verify -L -Verror: unpack_resources:    Resource start-up disabled since no STONITH resources have been definederror: unpack_resources:    Either configure some or disable STONITH with the stonith-enabled optionerror: unpack_resources:    NOTE: Clusters with shared data need STONITH to ensure data integrity[root@yd25 ~]# pcs property set stonith-enabled=false;#关闭stonith

    11) 设置VIP

    pcs resource create VIP ocf:heartbeat:IPaddr2 ip=192.168.220.26 cidr_netmask=24 nic=bond0 op monitor interval=30s

    12)设置Nginx

    pcs resource create Web nginx configfile=/etc/nginx/nginx.conf status10url="http://localhost/nginx_status" op monitor interval=1min

    因参数statusurl改成status10url,在这阶段消耗了些时间。
    可用pcs resource describe ocf:heartbeat:nginx 查看支持哪些参数

    13)将VIP和Nginx设置同组

    pcs resource group add ydjq VIPpcs resource group add ydjq Web

    非同组时会出现VIP在20服务器上,而Web是在25服务器上的情况。
    14)配置服务启动顺序

    pcs constraint order start Web then start VIP

    我看到网上大部分人都是先启动VIP,然后再启动web服务的,个人觉得先保证Web正常后,再启动VIP-比较靠谱,因VIP启动后-就对外服务了,而如果此时Web还没有起来,接收到的请求就会报错。
    15)指定优先的Location
    原因:当服务器硬件配置不一样的,可通过该设置,优先使用硬件配置好的服务器。

    pcs constraint localtion Web prefers yd20=50pcs constraint localtion Web prefers yd25=45

    因我两台服务器配置相同,未做此配置。
    16) 资源粘性
    实现:避免资源在节点间迁移-而引起的停止服务。
    控制服务保持在正在运行的节点上。
    Pacemaker为了达到最优分布各个资源的目的,默认设置这个值为0

    pcs resource  defaults resource-stickiness=100

    至此,集群配置完毕。

  5. 常用
    1) 手动切换
    两台都在online状态时,服务运行在yd20服务器上,想切换到yd25服务器上。
    pcs cluster standby yd20
    将服务器设置成备用节点时,服务就会切换走。
    2) 命令
    查看集群状态:#pcs status查看集群当前配置:#pcs config开机后集群自启动:#pcs cluster enable --all启动集群:#pcs cluster start --all停止集群:#pcs cluster destroy --all查看集群资源状态:#pcs resource show设置节点为备用状态:#pcs cluster standby node1取消节点备用状态:#pcs cluster unstandby node1
原创粉丝点击