Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。
二、keepalived的安装
我们采用源代码的方式来安装keepalived.
点击(此处)折叠或打开
- cd /usr/local/src
- wget http://www.keepalived.org/software/keepalived-1.2.20.tar.gz
- tar -xvf keepalived-1.2.20.tar.gz
- cd keepalived-1.2.20
- ./configure --prefix=/usr/local/keepalived
- make
- make install
1.建立服务启动脚本,以便使用service命令控制;
(1)复制文件
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
(2)添加执行权限
chmod +x /etc/init.d/keepalived
2.修改/etc/init.d/keepalived文件
将/etc/sysconfig/keepalived修改为. /usr/local/keepalived/etc/sysconfig/keepalived
3.在上述行下添加内容(将keepalived主程序所在路径导入到环境变量PATH中)
PATH="$PATH:/usr/local/keepalived/sbin"
export PATH
4.修改/usr/local/keepalived/etc/sysconfig/keepalived文件,设置正确的服务启动参数
KEEPALIVED_OPTIONS="-D -f /usr/local/keepalived/etc/keepalived/keepalived.conf"
5.至此,keepalived配置完毕,启动
service keepalived restart
6.设置服务为开机启动
chkconfig keepalived on
1.主机说明
本次共涉及到4台主机。
主机1(安装nginx+keepalived)为主负载均衡机器,IP:192.168.109.210
主机2(安装nginx+keepalived)为备负载均衡机器,IP:192.168.109.207
主机3(安装apache)为Web服务器,IP:192.168.109.208
主机4(安装apache)为Web服务器,IP:192.168.109.209
2.搭建说明
(1)首先使用主机1为负载均衡机器(停掉主机2上的nginx),并启动主机3、4上的apache。
(2)通过配置主机1上nginx,使其实现负载均衡,让所有请求均匀分配到主机3、4上来处理请求;
(3)在实现负载均衡后,为了实现服务持续可用,需对负载均衡器配置主从切换。当主机1出现故障时,通过keepalived迅速切换到主机2上。
(4)主机3和主机4上分别在/var/www/html目录下放置有页面index.html,页面内容为
web1.test.com
web2.test.com
3.搭建步骤
(1)配置nginx,主机1和主机2的配置文件相同
配置文件名:/usr/local/nginx/conf/nginx.conf
点击(此处)折叠或打开
- #配置进程数,一般与CPU核心数相同或者核心的倍数
- worker_processes 1;
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #access_log logs/access.log main;
- events {
- #设置单个进程的最大连接数
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- # access_log logs/access.log main;
- server {
- listen 80;
- server_name localhost;
- location / {
- root html;
- #自定义错误页面
- index index.html index.htm;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
- #设置负载均衡服务器组
- upstream myserv {
- server 192.168.109.208:80 weight=1;
- server 192.168.109.209:80 weight=1;
- }
- server {
- #设置监听VIP(虚拟IP),,该IP在主机3、4的配置文件中有配置
- listen 192.168.109.215:80;
- server_name myserv;
- # access_log /var/logs/access.log main;
- location / {
- index index.html index.htm;
- #设置反向代理
- proxy_pass http://myserv;
- proxy_set_header X-Real-IP $remote_addr;
- }
- }
- }
说明:分别在主机1和2上安装keepalived. 其中1为主,2为备。配置文件名:/etc/keepalived/keepalived.conf
(1)主机1上的keepalived.conf
点击(此处)折叠或打开
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- #设置邮件收件人
- [email protected]
- [email protected]
- }
- #设置邮件发件人
- notification_email_from [email protected]
- smtp_connect_timeout 3
- smtp_server 127.0.0.1
- router_id LVS_DEVEL
- }
- cript chk_schedown {
- script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
- interval 2
- weight -2
- }
- #第一种主备切换方案[当监测到主的nginx为0时,启动它;如果启动不了就杀掉主的keepalived]
vrrp_script chk_http_port {
# script "/usr/local/nginx/nginx_check.sh"
# interval 2
# weight -2
#}
#第二种主备切换方案[当监测到主的nginx进程是否存活,若没有则降低权重]
vrrp_script chk_http_port {
script "killall -0 nginx"
interval 2
weight -2
}
# script "/usr/local/nginx/nginx_check.sh"
# interval 2
# weight -2
#}
#第二种主备切换方案[当监测到主的nginx进程是否存活,若没有则降低权重]
vrrp_script chk_http_port {
script "killall -0 nginx"
interval 2
weight -2
}
- rrp_script chk_schedown {
- vrrp_instance VI_1 {
- interface eth0
- #配置主从,MASTER为主,BACKUP为备
- state MASTER
- #设置优先级,数值越大,优先级越高,都是主比备大
- priority 101
- virtual_router_id 51
- garp_master_delay 1
- authentication {
- auth_type PASS
- auth_pass password
- }
- track_interface {
- eth0
- }
- #设置虚拟IP地址,主备虚拟IP地址相同
- virtual_ipaddress {
- 192.168.109.215
- }
- track_script {
- chk_schedown
- }
- }
点击(此处)折叠或打开
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- #设置邮件收件人
- [email protected]
- [email protected]
- }
- #设置邮件发件人
- notification_email_from [email protected]
- smtp_connect_timeout 3
- smtp_server 127.0.0.1
- router_id LVS_DEVEL
- }
- cript chk_schedown {
- script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
- interval 2
- weight -2
- }
- rrp_script chk_schedown {
- vrrp_instance VI_1 {
- interface eth0
- #配置主从,MASTER为主,BACKUP为备
- state BACKUP
- #设置优先级,数值越大,优先级越高,都是主比备大
- priority 100
- virtual_router_id 51
- garp_master_delay 1
- authentication {
- auth_type PASS
- auth_pass password
- }
- track_interface {
- eth0
- }
- #设置虚拟IP地址,主备虚拟IP地址相同
- virtual_ipaddress {
- 192.168.109.215
- }
- track_script {
- chk_schedown
- }
- }
4.测试验证
(1)验证负载均衡
在浏览器地址栏输入虚拟IP:192.168.109.215,看是否会看到主机3和主机4对应的页面内容:
并且这2个页面是交替出现。此时即到达了负载均衡。
(2)验证keepalived主备切换
停止主机1上的nginx和keepalived,然后重新刷新页面,看是否还能打开,若能打开,说明keepalived切换成功。
注意:同一台机器上的nginx和keepalived必须同时停止才可以!!不能只停nginx!不然不会生效!
5.编写nginx监控脚本
点击(此处)折叠或打开
- vim /usr/local/nginx/nginx_check.sh
- #!/bin/bash
- # varsion 0.0.2
- # 根据一网友说这样做不科学,如果nginx服务起来了,但是我把keepalived 杀掉了,我的理由是,如果nginx死掉了,我觉得就很难在起来,再有就是nagios当然要给你报警了啊。不过这位同学说的有道理,所以就稍加改了一下脚本
- A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx进程 把值赋给变量A
- if [ $A -eq 0 ];then ## 如果没有进程值得为零
- /usr/local/nginx/sbin/nginx
- sleep 3
- if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
- killall keepalived ## 则结束 keepalived 进程
- fi
- fi