LVS+heartbeat
IP
172.16.1.2 HA1.example.com
172.16.1.3 HA2.example.com
172.16.1.5 web1.example.com
172.16.1.6 web2.example.com
172.16.1.254 isc.example.com
172.16.1.100 VIP
所有主机关闭防火墙
iptables-F
setenforce 0
添加DNS 或者写入 host文件
[root@localhost ~]# vim /etc/resolv.conf
nameserver 172.16.1.254
vim /etc/hosts
172.16.1.2 HA1.example.com
172.16.1.3 HA2.example.com
172.16.1.5 web1.example.com
172.16.1.6 web2.example.com
172.16.1.254 isc.example.com
172.16.1.100
搭建DNS、iscsi存储
isc.example.com
搭建DNS #或者将下列主机写入host文件
[root@localhost yum.repos.d]# yum -y install bind* caching-nameserver
[
root@localhost ~]# vim /var/named/chroot/etc/named/named.conf
options {
directory "/var/named";
};
zone "example.com" {
type master;
file "example.com.zone";
};
[root@localhost Packages]# vim /var/named/example.com
$TTL 86400
@ SOA example.com root (
2015031001
1H
1H
1H
1H
)
IN NS isc.example.com.
HA1 IN A 172.16.1.2
HA2 IN A 172.16.1.3
web1 IN A 172.16.1.5
web2 IN A 172.16.1.6
isc IN A 172.16.1.254
www IN A 172.16.1.100
[root@localhost Packages]# service named restart
搭建iscsi存储
创建lvm卷
[root@localhost Packages]# fdisk /dev/sdb
n-----p-----1----enter-----enter------t-----8e-----w
[root@localhost Packages]# partprobe /dev/sdb
[root@localhost Packages]# pvcreate /dev/sdb1
[root@localhost Packages]# vgcreate vg_test /dev/sdb1
[root@localhost Packages]# lvcreate -L 19.9G -n lv_test vg_test
安装配置iscsi
[root@localhost Packages]# yum -y install iscsi-initiator-utils perl-Config-General scsi-target-utils
[root@localhost Packages]# vim /etc/tgt/targets.conf
backing-store /dev/vg_test/lv_test
initiator-address 172.16.0.0/16
[root@localhost Packages]# service tgtd restart
讲iscsi加入开机自启动
[root@localhost Packages]# chkconfig --add tgtd
[root@localhost Packages]# chkconfig tgtd on
主调度器搭建心跳协议
HA1.example.com
[root@localhost 桌面]# vim /etc/sysconfig/network
HOSTNAME=HA1.example.com
创建用户和组
[root@localhost 桌面]# groupadd haclient
[root@localhost 桌面]# useradd -g haclient -M -s /sbin/nologin hacluster
安装依赖
yum -y install gcc gcc-c++ autoconf automake libtool glib2-devel libxml2-devel bzip2-devel e2fsprogs-devel libxslt-devel libtool-ltdl-devel make wget docbook-dtds docbook-style-xsl
安装Reusable
Resource Agents 下载地址
tar jxf Reusable-Cluster-Components-glue--glue-1.0.8.tar.bz2 -C /usr/src/
[root@localhost 桌面]# cd /usr/src/Reusable-Cluster-Components-glue--glue-1.0.8/
[root@localhost Reusable-Cluster-Components-glue--glue-1.0.8]# ./autogen.sh
[root@localhost Reusable-Cluster-Components-glue--glue-1.0.8]# ./configure --prefix=/usr/local/heartbeat --sysconfdir=/etc/heartbeat libdir=/usr/local/heartbeat/lib64 LIBS='/lib64/libuuid.so.1'
[root@localhost Reusable-Cluster-Components-glue--glue-1.0.8]# make && make install
安装ClusterLabs
Cluster-Glue 下载地址
tar zxf ClusterLabs-resource-agents-v3.9.2-0-ge261943.tar.gz -C /usr/src/
[root@localhost src]# cd ClusterLabs-resource-agents-b735277/
[root@localhost ClusterLabs-resource-agents-b735277]# ./autogen.sh
[root@localhost ClusterLabs-resource-agents-b735277]# ./configure --prefix=/usr/local/heartbeat --sysconfdir=/etc/heartbeat libdir=/usr/local/heartbeat/lib64 CFLAGS=-I/usr/local/heartbeat/include LDFLAGS=-L/usr/local/heartbeat/lib64 LIBS='/lib64/libuuid.so.1'
[root@localhost ClusterLabs-resource-agents-b735277]# ln -s /usr/local/heartbeat/lib64/* /lib64/
[root@localhost ClusterLabs-resource-agents-b735277]# make && make install
安装Heartbeat
Heartbeat-3-0-958e11be8686.tar.bz2 下载地址:
tar jxf Heartbeat-3-0-958e11be8686.tar.bz2 -C /usr/src/
[root@localhost src]# cd Heartbeat-3-0-958e11be8686/
[root@localhost Heartbeat-3-0-958e11be8686]# ./bootstrap
[root@localhost Heartbeat-3-0-958e11be8686]# vim /usr/local/heartbeat/include/heartbeat/glue_config.h
删除 #define HA_HBCONF_DIR "/etc/heartbeat/ha.d/"
[root@localhost Heartbeat-3-0-958e11be8686]# ./configure --prefix=/usr/local/heartbeat --sysconfdir=/etc/heartbeat CFLAGS=-I/usr/local/heartbeat/include LDFLAGS=-L/usr/local/heartbeat/lib64 LIBS='/lib64/libuuid.so.1'
[root@localhost Heartbeat-3-0-958e11be8686]# make && make install
优化路径
[root@localhost Heartbeat-3-0-958e11be8686]# cp doc/ha.cf /etc/heartbeat/ha.d/
[root@localhost Heartbeat-3-0-958e11be8686]# cp doc/haresources /etc/heartbeat/ha.d/
[root@localhost Heartbeat-3-0-958e11be8686]# cp doc/authkeys /etc/heartbeat/ha.d/
[root@localhost Heartbeat-3-0-958e11be8686]# sed -i 's#/usr/lib/ocf#/usr/local/heartbeat/usr/lib/ocf#g' /etc/heartbeat/ha.d/shellfuncs
[root@localhost Heartbeat-3-0-958e11be8686]# sed -i 's#/usr/lib/ocf#/usr/local/heartbeat/usr/lib/ocf#g' /etc/heartbeat/ha.d/resource.d/hto-mapfuncs
[root@localhost Heartbeat-3-0-958e11be8686]# sed -i 's#/usr/lib/ocf#/usr/local/heartbeat/usr/lib/ocf#g' /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/ocf-shellfuncs
[root@localhost Heartbeat-3-0-958e11be8686]# ln -s /usr/local/heartbeat/usr/lib/ocf /usr/lib/ocf
修改配置文件(以下为开启项)
[root@localhost ha.d]# vim ha.cf
keepalive 2 #指定心跳间隔时间为2秒(即每两秒钟在eth0上发送一次广播)
deadtime 30 #指定备用节点在30秒内没有收到主节点的心跳信号后,则立即接管主节点的服务资源
warntime 10 #指定心跳延迟的时间为十秒。当10秒钟内备份节点不能接收到主节点的心跳信号时,就会往日志中写入一个警告日志,但此时不会切换服务
initdead 120 #在某些系统上,系统启动或重启之后需要经过一段时间网络才能正常工作,该选项用于解决这种情况产生的时间间隔。取值至少为deadtime的两倍
udpport 694 #设置广播通信使用的端口,694为默认使用的端口号
bcast eth0 #指明心跳使用以太网广播方式,并且是在eth0接口上进行广播
auto_failback on #用来定义当主节点恢复后,是否将服务自动切回,heartbeat的两台主机分别为主节点和备份节点。主节点在正常情况下占用资源并运行所有的服务,遇到故障时把资源交给备份节点并由备份节点运行服务。在该选项设为on的情况下,一旦主节点恢复运行,则自动获取资源并取代备份节点,如果该选项设置为off,那么当主节点恢复后,将变为备份节点,而原来的备份节点成为主节点
watchdog /dev/watchdog #该选项是可选配置,是通过Heartbeat来监控系统的运行状态
node HA1.example.com #主节点主机名,可以通过命令“uanme –n”查看
node HA2.example.com #备用节点主机名
[root@localhost ha.d]# vim haresources
HA1.example.com 172.16.1.100 httpd
node-name network
node-name表示主节点的主机名,必须和ha.cf文件中指定的节点名一致,network用于设定集群的IP地址、子网掩码、网络设备标识等,需要注意的是,这里指定的IP地址就是集群对外服务的IP地址,resource-group用来指定需要heartbeat托管的服务,也就是这些服务可以由heartbeat来启动和关闭,如果要托管这些服务,必须将服务写成可以通过start/stop来启动和关闭的脚步,然后放到/etc/init.d/或者/etc/heartbeat/ha.d/resource.d/目录下,heartbeat会根据脚本的名称自动去/etc/init.d或者/etc/heartbeat/ha.d/resource.d/目录下找到相应脚步进行启动或关闭操作
[root@localhost ha.d]# vim authkeys
auth 3
3 md5 Hello!
[root@localhost:/etc/heartbeat/ha.d]#chmod 600 authkeys
无论auth后面指定的是什么数字,在下一行必须作为关键字再次出现,例如指定了“auth 6”,下面一定要有一行“6 认证类型”。
最后确保这个文件的权限是600(即-rw-------)。
开启服务,查看Ip
[root@localhost ha.d]# chkconfig --add heartbeat
[root@localhost ha.d]# chkconfig heartbeat on
[root@HA1 桌面]# service heartbeat restart
[root@HA1 桌面]# ip a
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:80:79:00 brd ff:ff:ff:ff:ff:ff
inet 172.16.1.2/24 brd 172.16.1.255 scope global eth0
inet 172.16.1.100/24 brd 172.16.1.255 scope global secondary eth0:0
inet6 fe80::20c:29ff:fe80:7900/64 scope link
valid_lft forever preferred_lft forever
安装ipvsadm
[root@HA1 桌面]# yum -y install ipvsadm
加载LVS模块
[root@HA1 桌面]# modprobe ip_vs
[root@HA1 桌面]# cat /proc/net/ip_vs
启用ipvsadm
[root@HA1 桌面]# ipvsadm -A -t 172.16.1.100:80 -s rr
[root@HA1 桌面]# ipvsadm -a -t 172.16.1.100:80 -r 172.16.1.5:80 -m
[root@HA1 桌面]# ipvsadm -a -t 172.16.1.100:80 -r 172.16.1.6:80 -m
[root@HA1 桌面]# service ipvsadm save
调整内核
[root@HA1 桌面]# vim /etc/sysctl.conf
[root@HA1 桌面]# sysctl -p
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
HA2.example.com
#同HA1
web1.example.com
安装iscsi
[root@localhost ~]# yum -y install iscsi-initiator-utils lsscsi
[root@localhost ~]# service iscsi restart
[root@localhost ~]# iscsiadm -m discovery -t sendtargets -p 172.16.1.254 #发现iscsi服务器
[root@localhost ~]# iscsiadm -m node -T iqn.2015-09.com.example.isc:lvm --login 讲iscsi服务器挂在到本机
分区
fdisk -l
[root@localhost ~]# fdisk /dev/sdb
n-----p-----1----enter-----enter------w
格式化
[root@localhost ~]# partprobe /dev/sdb
[root@localhost ~]# mkfs.ext3 /dev/sdb1
注:本该将iscsi挂载到http网页目录/var/www/html下 但是需要不同的网页测试LVS是否搭建成功 所以在测试完毕后在进行挂载
创建测试网页
[root@localhost ~]# echo web1 > /var/www/html/index.html
[root@localhost ~]# service httpd restart
[root@localhost ~]# cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-lo:0
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
IPADDR=172.16.1.100
NETMASK=255.255.255.255
ONBOOT=yes
[root@localhost ~]# ifup lo:0
[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
[root@localhost ~]# sysctl -p
[root@localhost ~]# route add -host 172.16.1.100 dev lo:0
[root@localhost ~]# vim /etc/rc.local
/sbin/route add -host 172.16.1.100 dev lo:0
web2.example.com
创建测试网页
[root@localhost ~]# echo web2 > /var/www/html/index.html
[root@localhost ~]# service httpd restart
余者与web1相同
客户端验证:firefox://http:// &
----------------成功-----------------
将iscsi挂载到web网页目录并写入开机启动文件中
[root@localhost ~]# vim /etc/fstab
/dev/sdb1 /var/www/html ext3 defaults,_netdev 0 0
阅读(1587) | 评论(0) | 转发(0) |