Chinaunix首页 | 论坛 | 博客
  • 博客访问: 175526
  • 博文数量: 46
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 490
  • 用 户 组: 普通用户
  • 注册时间: 2017-03-26 14:22
个人简介

做最Low逼的DBA

文章分类

全部博文(46)

文章存档

2017年(46)

我的朋友

分类: LINUX

2017-05-01 11:03:54

高可用集群原理详解之多节点集群_Heartbeat v2  
Heartbeat v1 自带的资源管理器   
    haresources:
//默认节点通信使用udp 694号端口    

Heartbeat v2 自带的资源管理器   
    haresources
    crm
//CRM进程mgmtd默认使用TCP 5560号端口,每个节点都会启动  

Heartbeat v3: 资源管理器crm发展为独立的项目,pacemaker

前提:
1)本配置共有两个测试节点,分别node1.heyuxuan.com和node2.heyuxuan.com,相应的IP地址分别为192.168.1.10和192.168.1.12;
2)集群服务为apache的httpd服务;
3)提供web服务的地址为192.168.1.100;
4)系统为rhel5.8,并挂载安装镜像,设置Yum;

1、准备工作
为了配置一台Linux主机成为HA的节点,通常需要做出如下的准备工作:
1)所有节点的主机名称和对应的IP地址解析服务可以正常工作,且每个节点的主机名称需要跟"uname -n“命令的结果保持一致;因此,需要保证两个节点上的/etc/hosts文件均为下面的内容:
192.168.1.10   node1.heyuxuan.com node1
192.168.1.12   node2.heyuxuan.com node2

为了使得重新启动系统后仍能保持如上的主机名称,还分别需要在各节点执行类似如下的命令:
【Node1】:
# sed -i 's@\(HOSTNAME=\).*@\1node1.heyuxuan.com@g'  /etc/sysconfig/network
# hostname node1.heyuxuan.com

【Node2】:
# sed -i 's@\(HOSTNAME=\).*@\1node2.heyuxuan.com@g' /etc/sysconfig/network
# hostname node2.heyuxuan.com

2)设定两个节点可以基于密钥进行ssh通信,这可以通过类似如下的命令实现:
【Node1节点执行】:

# ssh-keygen -t rsa

或者执行以下命令:
[root@node1 ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
Generating public/private rsa key pair.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
cd:70:b7:64:2e:b7:11:e0:71:98:13:17:c6:57:81:7a root@node1

复制密钥至节点二
# ssh-copy-id -i ~/.ssh/id_rsa.pub root@node2

[root@node1 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@node2

root@node2's password:
Now try logging into the machine, with "ssh 'root@node2'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

【Node2节点执行】:
# ssh-keygen -t rsa

或者执行以下命令:
[root@node2 ~]# ssh-keygen -t rsa -f  ~/.ssh/id_rsa -P ''
Generating public/private rsa key pair.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
b3:72:eb:d0:8b:32:a7:e2:e3:b4:07:25:b5:31:22:cf root@node2

复制密钥至节点一
# ssh-copy-id -i ~/.ssh/id_rsa.pub root@node1

Warning: Permanently added 'node1,192.168.1.10' (RSA) to the list of known hosts.
root@node1's password:
Now try logging into the machine, with "ssh 'root@node1'", and check in:

  .ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.

3)确保iptables防火墙状态,本实验环境需要,关闭iptables服务与自启动选项
-----注意:服务器生产环境禁止关闭防火墙自启动选项
【node1节点】
[root@node1 ~]# service iptables status
Firewall is stopped.
[root@node1 ~]# chkconfig iptables off
[root@node1 ~]# chkconfig --list iptables
iptables        0:off   1:off   2:off   3:off   4:off   5:off   6:off

【node2节点】
[root@node2 ~]# service iptables status
Firewall is stopped.
[root@node2 ~]# chkconfig iptables off
[root@node2 ~]# chkconfig --list iptables
iptables        0:off   1:off   2:off   3:off   4:off   5:off   6:off

4)确保selinux处于disable状态
【node1节点】
[root@node1 ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#       enforcing - SELinux security policy is enforced.
#       permissive - SELinux prints warnings instead of enforcing.
#       disabled - SELinux is fully disabled.
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
#       targeted - Only targeted network daemons are protected.
#       strict - Full SELinux protection.
SELINUXTYPE=targeted

【node2节点】
[root@node2 ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#       enforcing - SELinux security policy is enforced.
#       permissive - SELinux prints warnings instead of enforcing.
#       disabled - SELinux is fully disabled.
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
#       targeted - Only targeted network daemons are protected.
#       strict - Full SELinux protection.
SELINUXTYPE=targeted

5)下载安装包
EPEL/zh-cn - FedoraProject站点提供Heartbeat安装包下载
http://dl.fedoraproject.org/pub/epel/5/x86_64/repoview/letter_h.group.html

【需下载的安装包】
【heartbeat】 - Heartbeat subsystem for High-Availability Linux
【heartbeat-devel】 - Heartbeat development package
【heartbeat-gui】 - Provides a gui interface to manage heartbeat clusters               --图形化的组件
【heartbeat-ldirectord】 - Monitor daemon for maintaining high availability resources   --为ipvs高可用提供规则自动生成以及后端realserver健康状态检查的组件
【heartbeat-pils】 - Provides a general plugin and interface loading library
【heartbeat-stonith】 - Provides an interface to Shoot The Other Node In The Head

6)下载安装依赖包
【node1节点】、【node2节点】分别安装如下依赖包:

【perl-MailTools-1.77】

[root@node1 home]# rpm -ivh perl-MailTools-1.77-1.el5.noarch.rpm
warning: perl-MailTools-1.77-1.el5.noarch.rpm: Header V3 DSA signature: NOKEY, key ID 217521f6
Preparing...                ########################################### [100%]
   1:perl-MailTools         ########################################### [100%]

【libnet-1.1.6-7】

[root@node1 home]# yum --nogpgcheck localinstall libnet-1.1.6-7.el5.x86_64.rpm
Loaded plugins: katello, product-id, security, subscription-manager
Updating certificate-based repositories.
Unable to read consumer identity
Setting up Local Package Process
Examining libnet-1.1.6-7.el5.x86_64.rpm: libnet-1.1.6-7.el5.x86_64
Marking libnet-1.1.6-7.el5.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package libnet.x86_64 0:1.1.6-7.el5 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================================================================
 Package                             Arch                                Version                                   Repository                                               Size
=================================================================================================================================================================================
Installing:
 libnet                              x86_64                              1.1.6-7.el5                               /libnet-1.1.6-7.el5.x86_64                              138 k

Transaction Summary
=================================================================================================================================================================================
Install       1 Package(s)
Upgrade       0 Package(s)

建议安装方式都采用Yum方式进行安装,自动解决依赖关系,减少不必要的安装开销;

7)安装Heartbeat软件
【node1节点】、【node2节点】同时进行安装
[root@node1 heartbeat]# ls
heartbeat-2.1.4-11.el5.x86_64.rpm        heartbeat-gui-2.1.4-11.el5.x86_64.rpm   heartbeat-stonith-2.1.4-11.el5.x86_64.rpm
heartbeat-devel-2.1.4-11.el5.x86_64.rpm  heartbeat-pils-2.1.4-11.el5.x86_64.rpm

[root@node1 heartbeat]# yum --nogpgcheck localinstall *.rpm

Total                                                                                                                                             22 MB/s | 205 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : libtool-ltdl                                                                                                                                              1/7
  Installing     : heartbeat-pils                                                                                                                                            2/7
  Installing     : openhpi-libs                                                                                                                                              3/7
  Installing     : heartbeat-stonith                                                                                                                                         4/7
  Installing     : heartbeat                                                                                                                                                 5/7
  Installing     : heartbeat-gui                                                                                                                                             6/7
  Installing     : heartbeat-devel                                                                                                                                           7/7
Installed products updated.

Installed:
  heartbeat.x86_64 0:2.1.4-11.el5               heartbeat-devel.x86_64 0:2.1.4-11.el5       heartbeat-gui.x86_64 0:2.1.4-11.el5       heartbeat-pils.x86_64 0:2.1.4-11.el5      
  heartbeat-stonith.x86_64 0:2.1.4-11.el5      

Dependency Installed:
  libtool-ltdl.x86_64 0:1.5.22-7.el5_4                                                     openhpi-libs.x86_64 0:2.14.0-5.el5                                                    

Complete!

8)安装http服务
【node1节点】安装如下:
1.[root@node1 ~]# yum install httpd

2.[root@node1 ~]# echo "

node1.heyuxuan.con

" >> /var/www/html/index.html
//在本机上配置index.html信息用于测试节点Web访问信息是否能够正常切换。正常情况下节点上的Web配置信息应该一致;

3.[root@node1 ~]# service httpd start
Starting httpd: [  OK  ]
//在配置完成后需要手工启动一次httpd服务,验证是否能够正常访问,在集群信息配置完成后,httpd服务一定不能配置自启动;

4.[root@node1 ~]# curl http://192.168.1.10

node1.heyuxuan.con


node1.heyuxuan.con


[root@node1 ~]#
//验证配置的http信息是否能够正常访问,再次确认,能够正常访问后需要把httpd自启动关闭;

5.[root@node1 ~]# service httpd stop
Stopping httpd: [  OK  ]
[root@node1 ~]# chkconfig httpd off
//关闭httpd服务与关闭其服务自启动信息

【node2节点】 httpd服务启动与测试与【node1节点】相同;

到此,Heartbeat集群两个节点准备工作安装配置完成!

二、配置heartbeat集群

【2.1】要想成功启动heartbeat集群,必须具备以下三个配置文件:
1、密钥文件,权限:600      authkeys   (密钥文件)
2、heartbeat服务的配置文件  ha.cf      (核心配置文件)
3、资源管理配置文件         haresources(资源管理配置)

此时目录下没有这三个文件,需要创建,我们可以在 /usr/share/doc/heartbeat目录里找到ha.cf、haresources、authkeys三个文件,只需将其拷贝到/etc/ha.d目录下,即可
拷贝三个范例来进行配置
[root@node1 ~]# cp /usr/share/doc/heartbeat-2.1.4/ha.cf        /etc/ha.d/
[root@node1 ~]# cp /usr/share/doc/heartbeat-2.1.4/haresources  /etc/ha.d/
[root@node1 ~]# cp /usr/share/doc/heartbeat-2.1.4/authkeys     /etc/ha.d/

【2.2】密钥配置文件配置
1.修改authkeys密钥文件权限,使其权限限制使用在600
[root@node1 ha.d]# chmod 600 authkeys
[root@node1 ha.d]# ll authkeys
-rw------- 1 root root 645 Mar 27 23:11 authkeys
[root@node1 ha.d]#

2.编辑authkeys文件;
[root@node1 ha.d]# vim authkeys
#
#       Authentication file.  Must be mode 600
#
#
#       Must have exactly one auth directive at the front.
#       auth    send authentication using this method-id
#
#       Then, list the method and key that go with that method-id
#
#       Available methods: crc sha1, md5.  Crc doesn't need/want a key.
#
#       You normally only have one authentication method-id listed in this file
#
#       Put more than one to make a smooth transition when changing auth
#       methods and/or keys.
#
#
#       sha1 is believed to be the "best", md5 next best.
#
#       crc adds no security, except from packet corruption.
#               Use only on physically secure networks.
#
#auth 1
#1 crc            //主节点与备用节点间数据校验采用crc算法
#2 sha1 HI!       
#3 md5 Hello!
~
~
authkeys文件用于设定heartbeat的认证方式,共有三种可用的认证方式:crc、md5和sha1,三种认证方式的安全性依次提高,但是占用的系统资源也依次增加。
如果heartbeat集群运行在安全的网络上,可以使用crc方式;
如果HA每个节点的硬件配置很高,建议使用sha1,这种认证方式安全级别最高;
如果是处于网络安全和系统资源之间,可以使用md5认证方式。

本次使用MD5校验算法,使用如下方式取得一段随机md5值用于密钥信息:
[root@node1 ~]# dd if=/dev/random count=1 bs=512 | md5sum
0+1 records in
0+1 records out
128 bytes (128 B) copied, 0.000191 seconds, 670 kB/s
31be51c63e144792110658e7a7650f75  -

将512位随机编码添加至authkeys文件末尾中:
auth 1
1 md5 31be51c63e144792110658e7a7650f75

//保存退出,authkeys密钥文件配置完毕!

【2.3】核心配置文件ha.cf文件配置
核心配置里只需要在ha.cf文件末尾配置一条参数:

crm   respawn

###表示启用crm机制管理heartbeat集群资源;
###CRM和Resources并不兼容,即便配置了haresources文件内容,在启用了crm机制管理后,并不会读取resources配置文件内容;

【2.4】haresource配置文件
Haresources文件用于指定双机系统的主节点、集群IP、子网掩码、广播地址以及启动的服务等集群资源,文件每一行可以包含一个或多个资源脚本名,资源之间使用空格隔开,参数之间使用两个冒号隔开,在两个HA节点上该文件必须完全一致,此文件的一般格式为:

node1(node-name) 192.168.1.100(network)  httpd zabbix_server(resource-group)

node-name表示主节点的主机名,必须和ha.cf文件中指定的节点名一致;
network用于设定集群的IP地址、子网掩码、网络设备标识等,需要注意的是,这里指定的IP地址就是集群对外服务的IP地址;
resource-group用来指定需要heartbeat托管的服务,也就是这些服务可以由heartbeat来启动和关闭,如果要托管这些服务,必须将服务写成可以通过start/stop来启动和关闭的脚本,然后放到/etc/init.d/或者/etc/ha.d/resource.d/目录下,heartbeat会根据脚本的名称自动去/etc/init.d或者/etc/ha.d/resource.d/目录下找到相应脚步进行启动或关闭操作。
在haresources配置文件末尾加上:

#       Regarding the node-names in this file:
#
#       They must match the names of the nodes listed in ha.cf, which in turn
#       must match the `uname -n` of some node in the cluster.  So they aren't
#       virtual in any sense of the word.
#
node1 IPaddr::192.168.1.100/24/eth0 httpd

//保存退出,haresources文件配置完毕!
###注:在启用了CRM之后,Resources配置文件即便被配置,也不会被CRM读取;

【2.5】集群信息库CIB(Cluster Information Base)
###CRM实现管理高可用集群的时候资源定义的配置文件,或者叫配置文件的机制;
###CIB在集群每个节点上必须一样,不同于haresources,CIB文件是xml格式的文件;
###在早期heartbeat避免xml格式不友好导致配置文件阅读配置困难,于是提供一个python脚本:haresources2cib.py,在执行完毕制定CIB文件后,该脚本会把/etc/ha.d/haresouces文件转换为CIB的xml,并保存在/var/lib/heartbeat下的crm目录下,在启动高可用集群的时候,会直接读取该文件内容;
###对于CIB,只有工作在DC(主节点)上的文档是可以修改的,其他CIB都是复制DC上的那个文档而来的。

[root@node1 heartbeat]# pwd
/usr/lib64/heartbeat
[root@node1 heartbeat]# ll haresources2cib.py
-rwxr-xr-x 1 root root 19541 Mar 21  2010 haresources2cib.py

【2.6】关于ha_propagate脚本
###heartbeat v1版本haresources文件同步必须采用手工复制的方式保证节点上的配置文件保持一致;
###关于haresources2cib.py,早期heartbeat集群提供的转换CIB的脚本,依赖ha_propagate脚本将CIB信息同步至其他的集群节点;

[root@node1 heartbeat]# /usr/lib64/heartbeat/ha_propagate
Propagating HA configuration files to node node2.
ha.cf                                                   100%   10KB  10.4KB/s   00:00    
authkeys                                                100%  691     0.7KB/s   00:00    
Setting HA startup configuration on node node2.
chkconfig version 1.3.30.2 - Copyright (C) 1997-2000 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License.

usage:   chkconfig --list [name]
         chkconfig --add
         chkconfig --del
         chkconfig [--level ]
//启动该脚本后,Heartbeat集群自动将/etc/ha.d目录下的authkeys,ha.cf配置文件传传输到节点二上;

【2.7】启动heartbeat集群,虽然CIB暂时没有配置,但是不影响heartbeat集群启动;
[root@node1 heartbeat]# service heartbeat start
Starting High-Availability services:
2016/03/29_23:50:15 INFO:  Resource is stopped
[  OK  ]
[root@node1 heartbeat]# ssh node2 'service heartbeat start'
Starting High-Availability services:
2016/03/29_23:50:27 INFO:  Resource is stopped
[  OK  ]
//在启动Heartbeat集群后,就可以调用CRM各种命令进行配置,并且使用Gui界面进行集群资源配置;

###采用crm命令行的方式配置高可用集群的资源
[root@node1 heartbeat]# crm_sh
crm # help
Usage: crm (nodes|config|resources)

三、Gui界面配置heartbeat前准备工作
###本次测试使用图形化界面的方式配置heartbeat集群;
【3.1】Heartbeat集群用户配置
hacluster:x:102:157:heartbeat user:/var/lib/heartbeat/cores/hacluster:/sbin/nologin
###在安装时候就已经初始化了一个叫做hacluster的用户,此用户不带密码,需要手工对密码进行维护,重点在于,在哪个节点上连接图形化界面,就在哪个节点上配置密码;

【3.2】调用hb_gui脚本图形化配置heartbeat高可用集群
[root@node1 ~]# hb_gui

【3.3】开始图形化配置
【1.1】调出Gui界面图形化配置heartbeat集群
[root@node1 ~]# hb_gui
#必须要有xshell功能才能调用此界面,此时仍然是在Windows上打开一个Linux的图形界面

#点击login to cluster界面打开hacluster用户登录界面

【1.2】Login to cluster界面

#输入之前安装的时候已经配置维护好了hacluster用户与密码;
Username:hacluster
Passwd: hacluster
说明:此连接方式的就是连接的mgmtd进程监听的5560端口;
1. 默认连接本机节点,也可远程连接其他的节点;
2. 尽管IP配置为127.0.0.1,但是cluster会自动连接到DC节点上进行配置维护操作;

【1.3】集群配置界面
#Updating data from server 等待过后,连接到了Heartbeat集群配置主界面

#读取ha.cf后显示为3个节点,以及其他的一些配置信息
Ping Node 为ha.cf中有意加入的ping节点;
Node2状态为running,并被集群选举为DC主节点,点击该节点详细信息在右边显示;
Node1状态为running,为备节点,点击该节点详细信息在右边显示;
#说明:Linux-ha 的status 是 with quorum,Node1、Node2、Ping Node均在线,,表示满足法定票数;
#Heartbeat集群全局配置信息,Configurations界面显示如下:

#某些参数说明:
No Quorum Policy :不满足法定票数时候的策略,一旦集群出现分裂,配置的服务使用如下策略:
Stop ; Freeze; Ignore; Symmetric Cluster:是否为对称集群,如果故障,可以转移到其它任意节点上;
Stonith Achion:如果加载stonith设备的操作方式;
Stonith Enable: 是否启用stonith设备;
Default Resource Stickiness:默认资源粘性,默认值为0,值越高,资源越倾向留在当前节点;
#如果有需要,可以切换到Heartbeat集群高级配置界面 Advanced

#Node节点操作界面

说明:
    可以将当前的节点状态切换为 备用节点状态【make the node standby】

  此处可以对集群进行资源添加,但是不能添加节点,节点信息在ha.cf中进行配置


【1.4】集群资源配置

点击Resource条目,选择+号按钮对集群资源进行添加

#可以添加的集群资源的条目类型:
资源粘性:资源对某点的依赖程度,通过score定
资源约束:location: 资源对节点倾向程度;
coloation: 资源间依赖性; order: 资源的采取动作的次序;

二、配置高可用集群资源
【2.1】添加Group资源

#关于资源约束的补充说明: Resource Location(资源位置): 位置约束定义资源可以、不可以或首选在哪些节点上运行。 Resource Collocation(资源排列): 排列约束告诉群集资源可以或不可以在某个节点上一起运行。 Resource Order(资源顺序):排序约束定义操作的顺序。



#点击ADD Paramter进行IP资源选择性配置




最终配置结果:

#点击Add结束,第一个组资源和IP资源都配置完毕;



【2.2】配置Http资源
#在组资源上点击添加按钮,添加native资源,可以看到添加的资源已经默认加入到webservice组内


#点击Add结束资源配置


#右键点击webservice资源组,点击Start,启动该组资源


#start,可以看到,webservice资源已经启动,如下图:

#并且资源都是运行在同一个节点上【运行在node2上】

#测试访问:

#可以看到,http服务运行到node2节点上,并且已经成功打开http界面。

【2.3】故障切换
#选择node2节点,选择make the node standby按钮,将节点切换至备节点


#切换完成后如下图:

#可以看到,node2节点状态已变成running(dc)-standby,组资源运行在node1节点
#测试访问:

#切换成功!
#模拟故障切换完成后,右键node2节点选项active,该节点将资源切换回node2节点,因为ha.cf中配置参数auto_failback on的缘故;

三、添加NFS存储资源
#利用之前添加的nfs共享存储,向http提供共享服务,Type选择Filesystem

#参数说明:
Device:需要挂载的或者说是提供共享服务的设备与共享路径
Directory:被挂载的路径
Fstype:文件类型
#点击Add按钮结束配置,回到webservice资源界面

#修改NFS共享资源webstore的启动顺序,如下图:

#说明:这里需要按次序新建资源节点,如果不能在GUI界面调整上下次序需要删除原有资源再新建(由于httpd与nfs不在同一台主机,事先需要先启动文件服务再启动IP服务,最后才启动httpd服务),这样就会保证在集群启动时访问资源的顺序不会错误。

#测试访问:

#可以看到,由之前的访问本节点上的http已经指向到nfs共享服务挂载的html文件







阅读(2288) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~