目前需要做一个项目需要高可用    项目采用Keepalived+VIP(虚拟IP)进行一个故障转移。Keepalived和VIP的相关知识就不在这里废话介绍了  介绍一下当前的高可用环境实现思路。

目前有三台服务器。分别为三台服务器上部署keepalived 进行故障转移,当用户访问虚拟IP时,其keepalived会判断其优先级,主要根据监控的相关的服务端口  如

nginx 监听的端口 80

数据库服务端口  3306 

php-fpm的9000端口   

将优先级的进行虚拟IP(VIP)的地址漂移。使用ip a 可以查看到其绑定的虚拟IP在哪一台服务器上,或通过相关日志进行查看。

服务器相关IP配置

资源

IP 

服务器1

192.168.1.101

服务器1

192.168.1.102

服务器1

192.168.1.103

步骤一    先从部署或交付那边拿到配置好的虚拟IP    如:192.168.1.86   

步骤二   启动关键的端口服务  如  ngngx php-fpm  mysql 

步骤三  修改或新增一个keepalived的配置   如  keepalived.conf

下面贴出我的配置文件   

! Configuration File for keepalived

global_defs {
   notification_email {
	yuluolishang@qq.com	
   }
   notification_email_from yuluolishang@yisa.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   script_user root
   enable_script_security
   vrrp_skip_check_adv_addr
   #vrrp_strict
   #vrrp_iptables
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_mysql_port {
       script "</dev/tcp/127.0.0.1/3306" # connects and exits
       interval 1                      # check every second
       weight -20                       # default priority: -20 if connect fails
}

vrrp_script chk_php_port {
       script "</dev/tcp/127.0.0.1/9000" # connects and exits
       interval 1                      # check every second
       weight -20                       # default priority: -20 if connect fails
}

vrrp_script chk_nginx_port {
       script "</dev/tcp/127.0.0.1/80" # connects and exits
       interval 1                      # check every second
       weight -20                       # default priority: -20 if connect fails
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 66
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass Feipeng$
    }
    unicast_src_ip 192.168.1.101
    unicast_peer {
        192.168.1.102
        192.168.1.103
    }
    virtual_ipaddress {
        192.168.1.86/22
    }
    track_script {
       chk_mysql_port
       chk_php_port
       chk_nginx_port
    }
   
}

配置相关说明  :

state BACKUP   三台服务器都被BACKUP 角色

interface   etho   这个为该服务器的网卡名称

virtual_router_id   虚拟路由  这个三台服务器为同一个  必须同一个

priority   100   优先级

authentication  认证密码

unicast_src_ip    广播地址   即本机IP地址  

unicast_peer  广播接收地址  即另外两台服务器的IP地址

virtual_ipaddress   虚拟IP(VIP)地址   后面的/22 代表子网掩码  一般与三台服务器的真实IP在同一网段中。

track_script   里面为各监控的端口脚本名称   一行写一个检测

步骤四   启动keepalived 进程   启动故障转移    本keepalived 为离线部署  离线包目录结构如下:

keepalived虚拟ip无法飘逸 keepalived虚拟ip冲突_nginx

启动命令   /keepalived/sbin/keepalived -f /keepalived/etc/keepalived/keepalived.conf  

/keepalived/sbin/keepalived -f /keepalived/etc/keepalived/keepalived.conf

步骤五   验证启动    

查看keepalived 进程是否有启动   输入 ps aux|grep keepalived

ps aux|grep keepalived

会输出如下:

ps aux|grep keepalived
root     182037  0.0  0.0  44096   888 ?        Ss   17:13   0:00 /keepalived/sbin/keepalived  -f /keepalive/etc/keepalived/keepalived.conf
root     182038  0.1  0.0  44096  1308 ?        S    17:13   0:00 /keepalived/sbin/keepalived  -f /keepalive/etc/keepalived/keepalived.conf
root     182343  0.0  0.0 112812   968 pts/0    S+   17:13   0:00 grep --color=auto keepalive

ip a 查看当前漂移VIP 绑定情况

步骤六   模拟故障   比如说nginx 挂掉了   

输入命令   pkill nginx

然后输入lsof -i:80   此时可以得出  nginx 服务被停掉了  

pkill nginx
lsof -i:80

此时  刚才绑定的VIP的所在服务器上  此VIP不在其服务器了,而是跑到另一台服务器上绑定了VIP虚拟IP 

至此   keepalived 故障转移部署成功!!

坑:

1.同一组故障转移集群   virtual_router_id  虚拟路由IP 必须一样  要不然部署不成功。

2.interface   etho  网卡名称一定要填写当前服务器IP的所在网卡名称

3.若同一个局域网内,有多个keepalived 实例,keepalived两台机器同时出现vip问题!keepalived默认为广播,应改为单播