一、什么是 keepalived
Keepalived是一个免费开源的,用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层、第4层和第7层交换机的功能。主要提供loadbalancing(负载均衡)和 high-availability(高可用)功能,负载均衡实现需要依赖Linux的虚拟服务内核模块(ipvs),而高可用是通过VRRP协议实现多台机器之间的故障转移服务。
上图是Keepalived的功能体系结构,大致分两层:用户空间(user space)和内核空间(kernel space)。
内核空间:主要包括IPVS(IP虚拟服务器,用于实现网络服务的负载均衡)和NETLINK(提供高级路由及其他相关的网络功能)两个部份。
用户空间:
- WatchDog:负载监控checkers和VRRP进程的状况
- VRRP Stack:负载负载均衡器之间的失败切换FailOver,如果只用一个负载均稀器,则VRRP不是必须的。
- Checkers:负责真实服务器的健康检查healthchecking,是keepalived最主要的功能。换言之,可以没有VRRP
Stack,但健康检查healthchecking是一定要有的。 - IPVS wrapper:用户发送设定的规则到内核ipvs代码
- Netlink Reflector:用来设定vrrp的vip地址等。
Keepalived的所有功能是配置keepalived.conf文件来实现的。
二、安装 keepalived
下载keepalived地址:http://www.keepalived.org/download.html
wget http://www.keepalived.org/software/keepalived-1.4.1.tar.gz #指定解压路径 tar -zxvf keepalived-1.4.1.tar.gz -C /usr/local/ #安装依赖的软件包 yum install -y openssl openssl-devel #安装和编译 cd /usr/local/keepalived-1.4.1/ && ./configure --prefix=/usr/local/keepalived
make && make install
keepalived安装成Linux系统服务
keepalived的默认安装路径(默认路径:/usr/local)此处没有使用默认安装方式
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
然后复制keepalived脚本文件:
cp /usr/local/keepalived-1.4.1/keepalived/etc/init.d/keepalived /etc/init.d/
(或 : 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
service keepalived start
service keepalived stop
三、配置nginx主备自动重启
两台 linux 电脑安装了 nginx
备注:nginx 已成功安装在默认路径 /usr/local/nginx
修改 keepalived 的配置文件 vim /etc/keepalived/keepalived.conf
可以备份源配置文件粘贴下面配置 mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf :wq
vim /etc/keepalived/nginx_check.sh
赋予可执行权限:chmod +x /etc/keepalived/nginx_check.sh
/usr/local/nginx/sbin/nginx
service keepalived start
ps -ef | grep nginx
ps -ef | grep keepalived
! Configuration File for keepalived 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 enth1 # 绑定虚拟 IP 的网络接口,根据自己的机器填写 virtual_router_id 121 # 虚拟路由的 ID 号, 两个节点设置必须一样 mcast_src_ip 192.168.110.141 #填写本机ip priority 100 # 节点优先级,主要比从节点优先级高 nopreempt # 优先级高的设置 nopreempt 解决异常恢复后再次抢占的问题 advert_int 1 # 组播信息发送间隔,两个节点设置必须一样,默认 1s authentication { auth_type PASS auth_pass 1111 } # 将 track_script 块加入 instance 配置块 track_script { chk_nginx #执行 Nginx 监控的服务 }i viirtual_ipaddress { 192.168.110.110 # 虚拟ip,也就是解决写死程序的ip怎么能切换的ip,也可扩展,用途广泛。可配置多个。 } }
keepalived.conf
nginx_check.sh
实际配置如下:
测试
首先看一下俩台机器的ip a 命令下 都会出现一个虚拟ip,可以停掉一个机器的keepalived,然后测试,命令:service keepalived stop。结果发现当前停掉的机器已经不可用,keepalived 配置的虚拟 ip 会自动切换到另一台机器上。
停止后:
通过该虚拟 ip 可以进行访问 nginx
四、测试 keepalived 重启 nginx
强杀掉nginx进程即可,发现也会实现自动切换服务器节点。
kill -9 7456
原文地址:https://www.cnblogs.com/SacredOdysseyHD/p/8685140.html