一提到HA大家都知道heartbeat,heartbeat是个非常不错的HA开源软件,今天我们来介绍一下利用keepalived来做HA,keepalived的最大特点就是切换比较快,废话少说
我们来操作吧。
一.实验环境
两台服务器IP分别为:
192.168.4.188 master
192.16.4.190 slaved
192.168.4.189 virtual_ip
二.安装步骤
1.首先进行nginx的安装,其他的web软件都可以例如apache,lighttpd等等
tar -zxvf nginx-0.7.5.tar.gz
cd nginx-0.7.5
./configure --with-http_stub_status_module
make && make install
2.进行keepalived的安装
tar -zxvf keepalived-1.1.15.tar.gz
cd keepalived-1.1.15
./configure
make && make install
两台服务器的安装步骤完全一样
三.配置文件的修改
1.nginx配置文件的修改,由于我们只是简单的测试所以只要显示的页面能识别是哪台服务器的即可
master服务器:
cd /usr/local/nginx/html
echo "nginx master" > index.html
slaved服务器:
cd /usr/local/nginx/html
echo "nginx slaved" > index.html
2.keepalived配置文件的修改:
master服务器;
cd /usr/local/etc/keepalived
vi keepalived.conf
内容如下:
global_defs {
notification_email {
}
notification_email_from
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER ############ 辅机为 BAUCK
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.4.188
priority 102 ########### 权值要比 bauck 高
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.189 ############ 虚IP地址
}
}
slaved服务器:
cd /usr/local/etc/keepalived
vi keepalived.conf
内容如下:
global_defs {
notification_email {
}
notification_email_from
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BAUCK
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.4.190
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.4.189
}
}
3.nginx和keepalived的启动(启动顺序先master后slaved)
nginx的启动:
/usr/local/nginx/sbin/nginx -t 测试配置文件是否正确
/usr/local/nginx/sbin/nginx 启动nginx
keepalived的启动:
/usr/local/sbin/keepalived -D -f /usr/local/etc/keepalived/keepalived.conf
四. 测试
1.先测试一下master和slaved的nginx的服务是否正常
会显示:nginx master
会显示:nginx slaved
证明两台服务器的nginx服务都正常
2.测试虚拟IP的连通性和查看虚拟ip在master还是在slaved上
测试虚拟IP的连通性:
在本地ping 192.168.4.189能ping通证明虚拟IP已经启动
查看虚拟ip在的主机:
分别在master和slave上执行ip a命令,例如:
[root@glusterfs_client keepalived]# ip a
1: lo: mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:a9:71:5e brd ff:ff:ff:ff:ff:ff
inet 192.168.4.188/24 brd 192.168.4.255 scope global eth0
inet 192.168.4.189/32 scope global eth0
inet6 fe80::20c:29ff:fea9:715e/64 scope link
valid_lft forever preferred_lft forever
3: sit0: mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
哪台主机能看到这行"inet 192.168.4.189/32 scope global eth0" 证明虚拟主机就在哪台机器上
3. 测试虚拟IP的应用:
能正常显示:nginx master
4.测试keepalived的切换功能
在本地一直ping 192.168.4.189,到192.168.4.188-master上切断网络,例如:service network stop
本地ping 192.168.4.189什么时候通证明切换成功,不通的话证明切换失败
验证切换到slaved上:
显示:nginx slaved 表示OK!!!
可以到192.168.4.190-slave上执行:ip a 产看虚拟IP:
好了到此我们已经成功的实现了keepalived的HA功能,将192.168.4.188-master的网络恢复虚拟IP又自动切换到master的上来
阅读(1340) | 评论(0) | 转发(0) |