1.思路
LVS是linux自带的虚拟IP(VIP)的技术(阿里云和腾讯云不支持)
keepalived则是对VIP做了引用和增强,它可以利用心跳机制定时去执行一个任务(脚本),从而检测响应软件服务的启动状态
nginx则是做负载均衡等功能
那么可以这么去设计
nginx在做负载均衡的时候难免遇到nginx宕机,所以nginx也是需要去做集群的,而nginx又有反向代理的任务,
所以他的访问地址不好轻易变更,keepalived可以利用linux的LVS去设置虚拟IP,将多个服务器通过局域网链接
设置相同的VIP,然后在每个keepalived设置其对应的主从角色,再去监听每台服务器上的nginx。
这样一来,当请求统一去访问VIP时,会根据keepalived的主从机制找到主机将请求转发至对应nginx,nginx再实现负载均衡
2.Linux环境下载keepAlived
1.下载keepalived
wget http://www.keepalived.org/software/keepalived-1.2.18.tar.gz
2.解压安装:
tar -zxvf keepalived-1.2.18.tar.gz -C /usr/local/
3.下载插件openssl
yum install -y openssl openssl-devel(需要安装一个软件包)
4.开始编译keepalived
cd keepalived-1.2.18/ && ./configure --prefix=/usr/local/keepalived
5.make一下
make && make install
常见错误1
执行yum install 报错如下:
File contains no section headers.
解决方案:
步骤1
删除yum.repos.d目录下所有文件
rm -f /etc/yum.repos.d/*
步骤2
然后重新下载阿里的镜像
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
步骤3
清理缓存
yum clean all
步骤4
测试下载安装
yum install gcc
常见错误2
报错: eepalived执行./configure --prefix=/usr/local/keepalived时报错:configure: error: Popt libraries is required
出现此错误的原因:
未安装popt的开发包
解决方法:
yum install popt-devel
安装好popt的开发包。重新./configure 即可。
3.keepalived安装成Linux系统服务
将keepalived安装成Linux系统服务,因为没有使用keepalived的默认安装路径(默认路径:/usr/local),安装完成之后,需要做一些修改工作:
首先创建文件夹,将keepalived配置文件进行复制:
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
然后复制keepalived脚本文件:
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
可以设置开机启动:chkconfig keepalived on,到此我们安装完毕!
4.Keepalived常用的命令
service keepalived start
service keepalived stop
启动报错Starting keepalived (via systemctl): Job for keepalived.service failed. See 'systemctl status keepalived.service' and 'journalctl -xn' for details.
解决办法
[root@edu-proxy-01 sbin]# cd /usr/sbin/
[root@edu-proxy-01 sbin]# rm -f keepalived
[root@edu-proxy-01 sbin]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
5.Keepalived虚拟vip功能
vi /etc/keepalived/keepalived.conf
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" #运行脚本,脚本内容下面有,就是起到一个nginx宕机以后,自动开启服务
interval 2 #检测时间间隔
weight -20 #如果条件成立的话,则权重 -20
}
# 定义虚拟路由,VI_1 为虚拟路由的标示符,自己定义名称
vrrp_instance VI_1 {
state MASTER #来决定主从
interface ens33 # 绑定虚拟 IP 的网络接口,根据自己的机器填写
virtual_router_id 121 # 虚拟路由的 ID 号, 两个节点设置必须一样
mcast_src_ip 192.168.1.29 #填写本机ip
priority 100 # 节点优先级,主要比从节点优先级高
nopreempt # 优先级高的设置 nopreempt 解决异常恢复后再次抢占的问题
advert_int 1 # 组播信息发送间隔,两个节点设置必须一样,默认 1s
authentication {
auth_type PASS
auth_pass 1111
}
# 将 track_script 块加入 instance 配置块
track_script {
chk_nginx #执行 Nginx 监控的服务
}
virtual_ipaddress {
192.168.1.110 # 虚拟ip,也就是解决写死程序的ip怎么能切换的ip,也可扩展,用途广泛。可配置多个。
}
}
6.定时的脚本
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
7.总结
当主服务器宕机后,访问VIP,会切换至从服务器的keepalived上,执行其上nginx的负载均衡和反向代理,
主服务器重启后,再次请求恢复原功能