keepalived

1.浮动ip

浮动ip就是浮动在两台机器之间的ip,两个问题:

1.浮动ip是自己手动写的吗?如果是手动写的,网段分配完了怎么弄?
	单网卡创建浮动ip:https://www.cnblogs.com/victorwu/p/7061168.html,在配置kp时,不用事先在网卡上添加浮动ip,kp配置文件写上,当kp服务启动后会自动在网卡添加浮动ip。
    浮动ip一定是没有实例对象的,无论是在内网还是外网。内网中手动分配,外网的要买公网ip(无实例的)
    
2.在配置keepalived双机热备时候(主从模式),备服务器conf文件用不用配置浮动ip
	-备用服务器不用配置,在主服务器上的kp的配置文件中写上对应的浮动ip就好,主服务器启动kp服务后浮动ip会自动添加到相应的网卡上,当主服务器的kp服务关了之后,备服务器才会将浮动ip添加到自己的网卡上
2.keepalived编译安装
https://www.cnblogs.com/zpitbolg/p/5476978.html
1.机器列表:
10.0.90.213 #主
10.0.90.214 #备
10.0.90.30  #浮动ip
2.安装:
yum install keepalived  #自动安装到/etc/keepalived/下
3.主机配置ip文件:
global_defs {
  notification_email {
    sysadmin@firewall.loc
  }
  notification_email_from Alexandre.Cassen@firewall.loc
  smtp_server 192.168.200.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
}
vrrp_script chk_http_port {
   script "cat </dev/tcp/127.0.0.1/80"
   interval 2              #/* 监控时间间隔 */
   weight -10
}
vrrp_script chk_mysql {
   script "mysqladmin status"
   interval 2
   weight -10
}
vrrp_instance VI_1 {
   state MASTER            #/* 设置为master */
   #state BACKUP            #/* 设置为backup */
   interface eth0          #/* 监控网卡 */
   #mcast_src_ip 192.168.50.107  #/* 多播的源IP,设置为本机IP,与VIP同一网卡,可以不设置
   virtual_router_id 8     #/* 这个两台服务器必须一样 */
   priority 100            #/* 权重值 MASTRE 一定要高于 BAUCKUP */
   nopreempt               #/*不抢占ip,只在master上设置*/
   advert_int 1            #/* VRRP Multicast广播周期秒数*/
   authentication {
       auth_type PASS      #/* 加密 */
       auth_pass cnic.cn   #/* 加密的密码,两台服务器一定要一样,不然会出错 */
   }
   virtual_ipaddress {
       10.0.90.30/24      #提供服务的ip
   }
}
4.备机配置ip文件:
global_defs {
  notification_email {
    sysadmin@firewall.loc
  }
  notification_email_from Alexandre.Cassen@firewall.loc
  smtp_server 192.168.200.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
}
vrrp_script chk_http_port {
   script "cat </dev/tcp/127.0.0.1/80"
   interval 2              #/* 监控时间间隔 */
   weight -10
}
vrrp_script chk_mysql {
   script "mysqladmin status"
   interval 2
   weight -10
}
vrrp_instance VI_1 {
   #state MASTER            #/* 设置为master */
   state BACKUP            #/* 设置为backup */
   interface eth0          #/* 监控网卡 */
   #mcast_src_ip 192.168.50.107  #/* 多播的源IP,设置为本机IP,与VIP同一网卡,可以不设置
   virtual_router_id 8     #/* 这个两台服务器必须一样 */
   priority 50            #/* 权重值 MASTRE 一定要高于 BAUCKUP */
   #nopreempt               #/*不抢占ip,只在master上设置*/
   advert_int 1            #/* VRRP Multicast广播周期秒数*/
   authentication {
       auth_type PASS      #/* 加密 */
       auth_pass cnic.cn   #/* 加密的密码,两台服务器一定要一样,不然会出错 */
   }
   virtual_ipaddress {
       10.0.90.30/24      #提供服务的ip
   }
}
5.操作:
systemctl start keepalived   #主机和备机都启动keepalived服务
ip addr  #查看10.0.90.30的ip,只能在其中一台上看到,主机关闭服务之后,备机才能有

端口文件

global_defs {
   lvs_id BACKUP
}
 
vrrp_sync_group VGM {
    group {
       VI_1
    }
}
 
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 80
    priority 50
    advert_int 5
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
 
    172.16.254.16  
        }  
}  
   
virtual_server 172.16.254.16 0 {    #0代表映射所有端口
        delay_loop 3  
        lb_algo wrr                  #定义负载均衡算法,这里是权重轮训  
        lb_kind DR                   #定义模式,这里是Direct route  
        persistence_timeout 50        #会话保存时长(秒),0表示不使用stickyness会话    (重要)
        protocol TCP  
        // 后端真实服务器定义  
        real_server 172.16.2.10 0 {   #0代表映射所有端口
           weight 1                   #权重  
           TCP_CHECK {
              connect_timeout 3
              nb_get_retry 3
              delay_before_retry 3
              connect_port 80
            }
  
        }  
}
6.脚本检测nginx

当keepalived服务宕掉之后,会自动转到另一台服务器,但是当nginx宕掉之后,另一台nginx不会自动切换过来,keepalived感知不到nginx是否可用, 我们可以通过shell来实现nginx挂了之后的自动切换。

脚本主要实现:

检查本机nginx,发现nginx挂了之后,把本机keepalived服务关掉,这样另一台机器就能直接用了。

while true
do
if [ $(netstat -tlnp|grep nginx|wc -l) -ne 1 ]
then
    systemctl stop keepalived
fi
sleep 2
done


7.配置完成vip无法ping通

默认安装的keepalived会开启VRRP协议,注释掉vrrp_strict就可以

为什么把vrrp_strict注释掉就可以ping通?

大概原因是,vrrp_strict 开启之后,会导致keepalived 严格遵守VRRP协议,禁止以下状况:
1.无VIP地址
2.配置了单播邻居
3.在VRRP版本2中有IPv6地址,开启动此项会自动开启iptables防火墙规则。
因此通常建议关闭此设置。

posted @ 2023-02-26 16:35  MISF  阅读(47)  评论(0编辑  收藏  举报
     JS过度和变形效果演示   
  
    html5.png