部署keepalive+lvs
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
欢迎加入:高级运维工程师之路 598432640
安装LVS的步骤我已经在另外的一遍文章详细介绍了它的特性,在这里我就话不多说直接干~
1.准备LVS环境.需要四台服务器
LVS服务器[DIP]: 172.30.1.60
LVS服务器[DIP]: 172.30.1.148
LAMP服务器[RIP]:172.30.1.152
LAMP服务器[RIP]:172.30.1.154
2.安装LVS【172.30.1.60和172.30.1.148都做相同的动作】
[root@yinzhengjie ~]# yum -y install kernel-devel
[root@yinzhengjie ~]# rpm -qa | grep kernel
[root@yinzhengjie ~]# ln -s /usr/src/kernels/2.6.32-431.el6.x86_64/ /usr/src/linux
[root@yinzhengjie ~]# ll /usr/src/
[root@yinzhengjie ~]# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
[root@yinzhengjie ~]# yum -y install libnl* popt*
[root@yinzhengjie ~]# tar -xzf ipvsadm-1.26.tar.gz
[root@yinzhengjie ~]# cd ipvsadm-1.26
[root@yinzhengjie ipvsadm-1.26]# make && make install
[root@yinzhengjie ipvsadm-1.26]# ipvsadm
3.介绍Keepalive
keepalived服务的两大用途:
healthcheck:检查服务健康状态
failover:接管资源的能力(通过VRRP协议)
VRRP协议介绍:
全称Virtual Router Redundancy Protocol,中文名称虚拟路由器冗余协议,VRRPP的出现就是为了解决静态路由的单点故障,VRRP是通过一种竞选协议机制将路由任务交给某台VRRP路由器。
4.安装keepalive软件【172.30.1.60和172.30.1.148都需要安装】
[root@yinzhengjie tools]# ll /usr/src //检查是否做了链接文件,具体的可以参考我的LVS的配置过程
[root@yinzhengjie ~]# yum -y install openssl* //安装依赖包
[root@yinzhengjie ~]# wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz
//下载软件包
[root@yinzhengjie ~]# tar -zxf keepalived-1.1.19.tar.gz
[root@yinzhengjie ~]# cd keepalived-1.1.19
[root@yinzhengjie ~]# ./configure
![](https://i-blog.csdnimg.cn/blog_migrate/ac1c8b70f2970d6972daad22472ca7af.png)
上图表示编译成功,以第一个yes表示开启了ipvs功能【也就是支持LVS功能】,第二个yes是对ipcvs的同步支持,第三个yes是对vrrp的框架支持
[root@yinzhengjie keepalived-1.1.19]# make && make install
[root@yinzhengjie keepalived-1.1.19]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ #生成启动脚本
[root@yinzhengjie keepalived-1.1.19]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ #配置启动脚本的参数
[root@yinzhengjie keepalived-1.1.19]# mkdir -p /etc/keepalived #创建默认的keepalived配置文件路径
[root@yinzhengjie keepalived-1.1.19]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ #将keepalived模板文件拷贝到新建的目录中去
[root@yinzhengjie keepalived-1.1.19]# cp /usr/local/sbin/keepalived /usr/bin/ #配置系统变量(这样当敲击keepalive的时候就不用打绝对路径了)
[root@yinzhengjie keepalived-1.1.19]# /etc/init.d/keepalived start #启动keepalive服务
Starting keepalived: [ OK ]
[root@yinzhengjie keepalived-1.1.19]# ps -ef | grep keepalive #检查进程是否启动成功
root 10638 1 0 08:52 ? 00:00:00 keepalived -D
root 10639 10638 0 08:52 ? 00:00:00 keepalived -D
root 10640 10638 0 08:52 ? 00:00:00 keepalived -D
root 10643 5869 0 08:53 pts/1 00:00:00 grep keepalive
[root@yinzhengjie keepalived-1.1.19]# /etc/init.d/keepalived stop #停止服务
Stopping keepalived: [ OK ]
[root@yinzhengjie keepalived-1.1.19]# ps -ef | grep keepalive #检查是否成功停止脚本
root 10652 5869 0 08:53 pts/1 00:00:00 grep keepalive
[root@yinzhengjie keepalived-1.1.19]#
5.配置keepalived文件
172.30.1.148的配置:
[root@yinzhengjie keepalived]# cp keepalived.conf keepalived.conf.`date +%F` //备份模板文件
![](https://i-blog.csdnimg.cn/blog_migrate/1edd3589bdd87a8617f0a473f04811f0.png)
[root@yinzhengjie keepalived]# more keepalived.conf //修改配置文件
! Configuration File for keepalived
global_defs {
notification_email {
y1053419035@qq.com //定义一个收件人邮箱;
}
notification_email_from yinzhengjie@thinktrader.net //定义一个发件人邮箱,用以发送邮件;
smtp_server exmail.qq.com //发邮件的服务器
smtp_connect_timeout 30 //设置链接超时时间;这是
router_id LVS_7
}
#上面是定义收发邮件的对象可以不定义,尽管他自己有监控的设置,我们可以通过zabbix来监控,配置起来更简单,至于下面的配置就是定义一个实例;
vrrp_instance VI_1 {
state MASTER //标识这台机器的状态,其实这个参数也就是一个空壳,真正起作用的是下面的 priority这个参数
interface eth0 //绑定IP地址的接口,提供服务的接口,下面的VIP( virtual_ipaddress )地址会默认绑定到这个接口上来的哟
virtual_router_id 55 //虚拟的路由ID(需要注意的是,在另外的一台服务器上这个参数必须一致【我测试用2台服务器高可用】,当然同一个配置文件[也就是这个配置文件]中的IP是不能一致的,也是就是不能冲突哟~因为他起到了唯一标识的作用!)
priority 150 //定义优先级,谁的数字大谁就是MASTER(官方建议2个服务器相差值是50),所以我们可以说上面的state就是一个空客,就好像主席和人名大会堂一样~你可以这么理解~哈哈!
advert_int 1 //间隔时间,超出1s如果对方没有反应它就接管~
authentication { //定义一个加密口令
auth_type PASS //认真密钥,建议用明文,不要用密文
auth_pass 6666 //这个参数自己自定义,建议不要改的太复杂,尤其是特殊字符
}
virtual_ipaddress {
172.30.1.200/24 //定义一个VIP
}
}
[root@yinzhengjie keepalived]# /etc/init.d/keepalived restart
Stopping keepalived: [ OK ]
Starting keepalived: [ OK ]
[root@yinzhengjie keepalived]#
172.30.1.60的配置:
[root@yinzhengjie ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
[root@yinzhengjie ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@yinzhengjie ~]# mkdir -p /etc/keepalived
[root@yinzhengjie ~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@yinzhengjie ~]# cp /usr/local/sbin/keepalived /usr/bin/
[root@yinzhengjie ~]# /etc/init.d/keepalived start
[root@yinzhengjie ~]# ps -ef | grep keepalive
root 4853 1 0 06:32 ? 00:00:00 keepalived -D
root 4855 4853 0 06:32 ? 00:00:00 keepalived -D
root 4856 4853 0 06:32 ? 00:00:00 keepalived -D
root 4871 2758 0 06:36 pts/0 00:00:00 grep keepalive
[root@yinzhengjie ~]# cd /etc/keepalived/
[root@yinzhengjie keepalived]# cp keepalived.conf keepalived.conf.`date +%F`
[root@yinzhengjie keepalived]# more keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
1053419035@qq.com
}
notification_email_from yinzhengjie@thinktrader.net
smtp_server smtp.exmail.qq.com
smtp_connect_timeout 30
router_id LVS_2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 55
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 6666
}
virtual_ipaddress {
172.30.1.200/24
}
}
[root@yinzhengjie keepalived]# /etc/init.d/keepalived restart
Stopping keepalived: [ OK ]
Starting keepalived: [ OK ]
[root@yinzhengjie keepalived]#
6.实验测试是否搭建成功:
172.30.1.148:
[root@yinzhengjie ~]# ip add | grep 1.200
172.30.1.60
![](https://i-blog.csdnimg.cn/blog_migrate/abe75b93ecfac6559ee7a3ef214c98d8.png)
说明服务还在master服务器上,这个时候我们停掉172.30.1.148的keepalive服务,检查VIP是否成功切换~
172.30.1.148:
172.30.1.60:
![](https://i-blog.csdnimg.cn/blog_migrate/1d4d29b1554e12f49c306db4ba1e671a.png)
好了,通过这个实验,发现这个机器的确是接管成功VIP啦~那么你是否成功啦?这就是简单实现了web的高可用,
keepalive默认存放日志文件是在/var/log/message中,由于该文件是存放系统日志文件,不仅仅存放Keepalive的日志还存放着其他服务的日志。看起来不是很方便。所以我们可以将日志文件重新放到一个文件中。
1.修改keepalive配置文件
将原有的KEEPALIVED_OPTIONS="-D"前面加个”#“注释掉,然后重新加一行KEEPALIVED_OPTIONS="-D -d -S -0",如下图
[root@yinzhengjie ~]# vim /etc/sysconfig/keepalived
![](https://i-blog.csdnimg.cn/blog_migrate/85a07ffc6b8ac37c2196676847fa248b.png)
2.编辑rsyslog配置文件,添加如下2行
[root@yinzhengjie ~]# tail -2 /etc/rsyslog.conf
#add keepalived log by yinzhengjie
local0.* /var/log/keepalived.log
[root@yinzhengjie ~]#
3.重启服务
[root@yinzhengjie ~]# /etc/init.d/rsyslog restart
[root@yinzhengjie ~]# /etc/init.d/keepalived restart
![](https://i-blog.csdnimg.cn/blog_migrate/1d217b90e5948e5642b21c8e605c5833.png)