利用heartbeat构建高可用http

一、实验准备:

 1)实验环境:     

  1.png

 2)同步时间;确保可以使用主机名通信;确保可以使用ssh秘钥方式进行彼此登录;由于是两台设备,需要仲裁;

 确保可以使用主机名通信

web1修改hosts文件如下:
172.16.2.12 web1.linux.com web1
172.16.2.14 web2.linux.com web2

web2修改hosts文件如下:
172.16.2.12 web1.linux.com web1
172.16.2.14 web2.linux.com web2

 确保主机可以使用ssh秘钥彼此登录

[root@web1~]# ssh-keygen -P '' \\生成秘钥

[root@web1~]# ssh-copy-id -i .ssh/id_rsa.pub root@172.16.2.14  \\将公钥复制给web2

[root@web2 ~]# ssh-keygen -P ''  \\生成秘钥

[root@web2 ~]# ssh-copy-id  -i .ssh/id_rsa.pub  root@172.16.2.12    \\将公钥复制给web1

确保主机之间时间同步

[root@web1 ~]# crontab -e
*/5 * * * * /usr/sbin/ntpdate   172.16.2.13
   
[root@web2 ~]# crontab -e                                              
*/5 * * * * /usr/sbin/ntpdate 172.16.2.13

 测试

[root@web1 ~]# date; ssh web2 'date'
Sat Jun 27 11:32:56 CST 2015
Sat Jun 27 11:32:56 CST 2015

[root@web2 ~]# date; ssh web1 'date'
Sat Jun 27 11:33:42 CST 2015
Sat Jun 27 11:33:42 CST 2015

二、安装配置heartbeat v1

 1)安装依赖包;前提准备好epel源

阿里云eple源
wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-6.repo
[root@web1 heartbeat]# yum -y install net-snmp-libs PyXML gettext-devel libtool-ltdl pygtk2-devel  libnet
[root@web1 heartbeat]# rpm -ivh heartbeat-pils-2.1.4-12.el6.x86_64.rpm 
[root@web1 heartbeat]# rpm -ivh heartbeat-stonith-2.1.4-12.el6.x86_64.rpm 
[root@web1 heartbeat]# rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm 

[root@web2 heartbeat]# yum -y install net-snmp-libs PyXML gettext-devel libtool-ltdl pygtk2-devel  libnet
[root@web2 heartbeat]# rpm -vih heartbeat-pils-2.1.4-12.el6.x86_64.rpm
[root@web2 heartbeat]# rpm -ivh heartbeat-stonith-2.1.4-12.el6.x86_64.rpm 
[root@web2 heartbeat]# rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm

 2)对heartbeat进行配置

[root@web1 ha.d]# rpm -ql heartbeat \\查看heartbeat生成文件

[root@web1 ~]# cd  /usr/share/doc/heartbeat-2.1.4/

[root@web1 heartbeat-2.1.4]# cp authkeys ha.cf haresources  /etc/ha.d/
      authkeys :是主机之间信息传递使用的秘钥
      ha.cf:是heartbeat的主配置文件
      haresource: 资源配置文件
      
[root@web1 heartbeat-2.1.4]# cd /etc/ha.d/

[root@web1 ha.d]# openssl rand -hex 6
e5b50e897cb4   \\生成秘钥

[root@web1 ha.d]# vim authkeys  \\配置秘钥
auth 1
1  md5 e5b50e897cb4

[root@web1 ha.d]# chmod 400 authkeys  \\修改秘钥文件的权限;建议使用400;如果不修改权限heartbeat无法启动

[root@web1 ha.d]# vim ha.cf
 logfile /var/log/ha-log \\ 定义日志文件
 mcast eth0 226.10.10.1 694 1 0 \\定义组播地址以及传递的相关属性(mcast组播;bcast广播;ucast单播)
 auto_failback on  \\抢占模式;主节点恢复故障后从备用节点那里抢回主节点功能
 node   web1.linux.com  \\添加ha节点;此名称必须是“uname -n”看到的主机名
  node   web2.linux.com   \\添加ha节点;此名称必须是“uname -n”看到的主机名
 ping 172.16.2.1  \\ 定义仲裁设备
 compression     bz2 \\启用压缩功能
 compression_threshold 2 \\大于多少k之后开始压缩
 
[root@web1 ha.d]# vim haresources 
 web1.linux.com  172.16.2.100/24/eth0/172.16.2.100 httpd \\定义资源(定义VIP,httpd)
 
[root@web1 ha.d]# scp -p ha.cf haresources authkeys  web2:/etc/ha.d \\将配置好的文件复制给web2一份

 3)安装httpd

[root@web1 ~]# yum -y install httpd ; ssh web2 'yum -y install httpd'  \\安装httpd
[root@web1 ~]# echo  "<h1>web1</h1>" > /var/www/html/index.html    \\添加默认文档
[root@web2 ~]#  echo  "<h1>web2</h1>" > /var/www/html/index.html    \\添加默认文档
 
[root@web1 ~]# service httpd start   \\启动http
Starting httpd:                                            [  OK  ]
[root@web1 ~]# curl     \\测试httpd 
<h1>web1</h1>
[root@web1 ~]# service httpd stop     \\停止httpd
Stopping httpd:                                            [  OK  ]
[root@web1 ~]# chkconfig httpd off      \\禁止开机启动httpd

[root@web2 ~]# service httpd start
Starting httpd:                                            [  OK  ]
[root@web2 ~]# curl  http://172.16.2.14
<h1>web2</h1>
[root@web2 ~]# service httpd stop
Stopping httpd:                                            [  OK  ]
[root@web2 ~]# chkconfig httpd off

 4)启动heartbeat

[root@web1 ~]# service heartbeat start; ssh web2 'service heartbeat start'
Starting High-Availability services: 
2015/06/27_12:40:25 INFO:  Resource is stopped
Done.

Starting High-Availability services: 
2015/06/27_12:40:25 INFO:  Resource is stopped
Done.

 5)查看启动结果:

[root@web1 ~]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:0E:C4:29  
          inet addr:172.16.2.12  Bcast:172.16.2.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe0e:c429/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:55852 errors:0 dropped:0 overruns:0 frame:0
          TX packets:32064 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:64541607 (61.5 MiB)  TX bytes:9222342 (8.7 MiB)

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:0E:C4:29  
          inet addr:172.16.2.100  Bcast:172.16.2.100  Mask:255.255.255.0     \\VIP地址
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:453 errors:0 dropped:0 overruns:0 frame:0
          TX packets:453 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:44156 (43.1 KiB)  TX bytes:44156 (43.1 KiB)
          
 
[root@web1 ~]# service httpd status
httpd (pid  11984) is running...         \\httd服务已启动

[root@web1 ~]# ss -upln    \\查看heartbet v1版监听端口,为udp的694
State       Recv-Q Send-Q                                                               Local Address:Port                                                                 Peer Address:Port 
UNCONN      0      0                                                                                *:647                                                                             *:*      users:(("portreserve",934,6))
UNCONN      0      0                                                                      226.10.10.1:694                                                                             *:*      users:(("heartbeat",12952,7),("heartbeat",12953,7))
UNCONN      0      0                                                                                *:847                                                                             *:*      users:(("portreserve",934,8))
UNCONN      0      0                                                                                *:42975                                                                           *:*      users:(("heartbeat",12952,6),("heartbeat",12953,6))

 6)web1是主节点,web2是备节点;访问测试

  2.png

  将web1切换为备节点,web2为主节点

[root@web1 ~]# cd /usr/lib64/heartbeat/
      ha_propagate \\此脚本是将heartbeat的配置文件复制给其他节点
      hb_standby  \\此脚本是将当前节点切换为备节点
      hb_takeover \\此脚本是将当前节点切换为主节点
      
[root@web1 heartbeat]# ./hb_standby  \\将web1切换为备节点
2015/06/27_12:47:03 Going standby [all].

 访问测试

 3.png

扩展:使用NFS为两台高可用主机提供NFS文件系统

  1)在时间服务器开启NFS功能

[root@time-or-nfs ~]# vim /etc/exports 
/web/html    172.16.2.0/24(rw,no_root_squash)  \\定义共享文件访问权限以及不压缩root用户权限
[root@time-or-nfs ~]# mkdir -pv /web/html  \\创建此目录
[root@time-or-nfs ~]# service nfs start   \\ 启动nfs
[root@time-or-nfs ~]# exportfs  -arv   \\查看nfs
exporting 172.16.2.0/24:/web/html
[root@time-or-nfs ~]# echo "<h1>NFS Page</h1>"  > /web/html/index.html \\定义web主页

 2)挂载测试

[root@web1 ~]# mount -t nfs 172.16.2.13:/web/html /mnt
[root@web1 ~]# mount
172.16.2.13:/web/html on /mnt type nfs (rw,vers=4,addr=172.16.2.13,clientaddr=172.16.2.12)
[root@web1 ~]# cd /mnt/
[root@web1 mnt]# ls
index.html
[root@web2 ~]# umount /mnt \\卸载

[root@web2 ~]# mount -t nfs 172.16.2.13:/web/html /mnt
[root@web2 ~]# mount
172.16.2.13:/web/html on /mnt type nfs (rw,vers=4,addr=172.16.2.13,clientaddr=172.16.2.14)
[root@web2 ~]# cd /mnt/
[root@web2 mnt]# ls
index.html
[root@web2 ~]# umount /mnt  \\卸载

 3)修改资源配置文件;

[root@web1 ~]# vim /etc/ha.d/haresources \\编辑heartbeat的资源配置文件
web1.linux.com  172.16.2.100/24/eth0/172.16.2.100 Filesystem::172.16.2.13:/web/html/::/var/www/html/::nfs httpd
[root@web1 ~]# scp /etc/ha.d/haresources web2:/etc/ha.d/ \\复制新的资源配置文件给web2一份

 4)重启启动heartbeat

[root@web1 ~]# service heartbeat restart;ssh web2 'service heartbeat restart'
Stopping High-Availability services: 
Done.

Waiting to allow resource takeover to complete:
Done.

Starting High-Availability services: 
2015/06/27_13:08:30 INFO:  Resource is stopped
Done.

Stopping High-Availability services: 
Done.

Waiting to allow resource takeover to complete:
Done.

Starting High-Availability services: 
2015/06/27_13:08:50 INFO:  Resource is stopped
Done.

 5)查看启动状态

[root@web1 heartbeat]# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:0E:C4:29  
          inet addr:172.16.2.12  Bcast:172.16.2.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe0e:c429/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:176947 errors:0 dropped:0 overruns:0 frame:0
          TX packets:117322 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:212876014 (203.0 MiB)  TX bytes:18332012 (17.4 MiB)

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:0E:C4:29  
          inet addr:172.16.2.100  Bcast:172.16.2.100  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:1246 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1246 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:118420 (115.6 KiB)  TX bytes:118420 (115.6 KiB)
          
[root@web1 ~]# mount
172.16.2.13:/web/html/ on /var/www/html type nfs (rw,vers=4,addr=172.16.2.13,clientaddr=172.16.2.12)

 6)访问web页面测试

4.png

原创文章,作者:马行空,如若转载,请注明出处:http://www.178linux.com/5715

(0)
马行空马行空
上一篇 2015-07-08 09:31
下一篇 2015-07-08 09:36

相关推荐

  • linux命令格式,获取帮助及其目录结构简要理解

    我们都知道,一台计算机要是没通电,和一堆废铁没什么区别。那么,通电开机进入系统后,会进入交互界面,等待用户操作,人与计算机交互界面有两种: GUI:图形用户接口。如我们平时使用的Windows  ,linux的X window,有KDE和GOME.   CLI:命令行接口,使用的SHELL类型有bash ,csh,tcshell,zshell等。 …

    2017-09-14
  • 一起学DHCP系列(五)指派、获取

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://jeffyyko.blog.51cto.com/28563/163168     本节将主要讲述DHCP客户端获取IP的过程,也是此系列中非常重要的一节。   &…

    Linux干货 2015-03-25
  • 在linux中创建虚拟网卡(网卡别名)的方法

    由于业务需要,要在单个物理网卡上建立多个虚拟网卡,操作如下:cd /etc/sysconfig/network-scripts/   #进入网卡目录cp ifcfg-eth0 ifcfg-eth0:1   # 复制出ifcfg-eth0:1虚拟网卡vim ifcfg-eth0:1    #配置ifcfg-eth0:1虚…

    Linux干货 2016-09-06
  • 互联网安全之iptables/netfilter入门到进阶

    随着互联网技术的方兴未艾,各种网络应用层出不穷,网络攻击、黑客入侵也成了网民畅游互联网的心头大患,互联网安全也愈加受到了人们的重视。网络防火墙,作为一种简单高效的互联网防御手段,逐渐成为了网民畅游网络世界的保护伞。下面笔者介绍下Linux系统的守卫者——iptables/netfilter。  一 兄弟齐心,其利断金  iptables/netfilter就…

    Linux干货 2017-05-06
  • 文件系统层次标准FHS

    FHS针对目录树架构仅定义出三层目录下应该放置哪些数据,分别是下面三个目录: /(根目录):与开机系统有关; /usr(unix software resource):与软件安装执行有关; /var(variable):与系统运作过程有关。   下面分别对上述三层目录进行详细的阐述。   (1) /(根目录)   根目录是整个系统最重要的一个目录,…

    Linux干货 2016-10-19
  • 网络分层基础概念与TCP,IP协议解析

    网络分层基础概念:   与ISO的OSI网络分层模型不同。网络分层事实上的标准为TCP/IP 的网络分层模型。   下图是他们是他们之间的对应关系。   左边为OSI网络分层模型。右边为TCP/IP的网络分层模型。 网络分层,是将一个大而复杂的网络,拆分为多个层次,每个层次单独完成自己的任务,多个层次再结合起来,完成一个复杂的通…

    2017-05-04