keepalived简单部署

环境
作用,实现web服务器的高可用集群
Server1:192.168.0.118
Server2:192.168.0.119
VIP:192.168.0.10

Server1
创建etc下的keepalived目录,编辑配置文件
yum install -y keepalived 
vim /etc/keepalived/keepalived.conf

配置文件
!Configuration File for keepalived 
golbal_defs{
 router_id 1   #VRRP组ID
}
#vrrp_script che_nginx {   #健康检查
#scrip "etc/keepalived/ck_ng.sh"    #检查脚本
#interval 2               #检查频率.秒
#weight  -5               #priority  优先级减5
#fall  3                  #失败3次 
#}
vrrp_instance V_1{
state MASTER  #主或者从状态
interface ens32  #监控网
mcast_src_ip   192.168.0.118  #心跳源IP 
virtual_router_id  55   #VRRP组内IP
priority 100           #优先级
advert int 1           #心跳间隔
}
authentication{         #秘钥认证
auth_type PASS
auth_pass 123456 
} 
virtual_ipaddress{    #VIP
192.168.0.10/24
}
#track_script{      #引用脚本
# chk_nginx
#}
scp -r  /etc/keepalived/keepalived.conf     192.168.0.119:/etc/keepalived/
systemctl  enable  keepalived.service 

安装Nginx 
yum -y install nginx 
systemctl enable nginx.service
systemctl start nginx.service
vim /usr/share/nginx/html/index.html
配置修改 
state MASTER  修改为 state BACKUP
mcast_scr_ip 192.168.0.118 改为backup服务器实际的IP mcast_src_ip 192.168.0.119
priority 100 改为 priority 99
systemctl enable keepalived.service 
curl -i localhost
systemctl start keepalived.service
Server2
BACKUP服务器的配置需要几处修改
yum -y install keepalived 
vim /etc/keepalived/keepalived.conf
安装NGINX

client
访问VIP http://192.168.0.10
拔掉master的网线
访问VIP http://192.168.0.10 观察网页已经切换

关于keepalived对nginx状态未知的问题

启动两台主机的keepalived和nginx ,确保页面访问正常, 关闭master的nginx服务,Systemctl stop nginx 继续访问VIP, 页面是否会切换到slave?

服务的状态可keepalived 的关系?
keepalived 监控的是端口IP 状态,无法监控服务的状态
解决方法:编辑keepalived对nginx的监控脚本

Server1 :
vim /etc/keepalived/ck_ng.sh
 #!/usr/bin/bash
#检查nginx进程是否存在
counter=$(ps -C nginx --no-heading |wc -l)
if [  "$(counter)"="0"  ];then
#尝试启动一次nginx,停止5秒后再次检测
  service nginx start
  sleep 5 
    counter=$(ps -C nginx --no-heading |wc -l) 
    if [  "$(counter)"="0"  ];then
  #如果没启动成功,就杀掉keepalived触发主备切换
    service keepalived stop 
    fi
fi
chmod +x /etc/keepalived/ck_ng.sh
Server2 同 Service1
vim /etc/keepalived/ck_ng.sh
 #!/usr/bin/bash
#检查nginx进程是否存在
counter=$(ps -C nginx --no-heading |wc -l)
if [  "$(counter)"="0"  ];then
#尝试启动一次nginx,停止5秒后再次检测
  service nginx start
  sleep 5 
    counter=$(ps -C nginx --no-heading |wc -l) 
    if [  "$(counter)"="0"  ];then
  #如果没启动成功,就杀掉keepalived触发主备切换
    service keepalived stop 
    fi
fi
chmod +x /etc/keepalived/ck_ng.sh

启动监控脚本
清除掉配置文件中的注释
重启keepalived

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据