Linux安装keepalived

直接yum安装 yum install keepalived

第一步:先安装依赖包     yum install -y libselinux-devel openssl-devel krb5-devel 

第二步:在官网下载安装包:https://www.keepalived.org/download.html

第三步:把安装包传上服务器,解压。

 

 

第四步:进入解压的文件夹,编译安装  ./configure --prefix=/software/keepalived

 

 

//没有权限的话用sudo或者root,但我在make install 时 在/usr/lib/systemd/system下创建文件时sudo还是不够权限,所以只能用root

 

 

第五步:make && make install

第六步:cp /software/tools/keepalived-2.2.2/keepalived/etc/init.d/keepalived /etc/init.d

 

第七步:创建文件夹 mkdir /etc/keepalived 

第八步:cp  /software/tools/keepalived-2.2.2/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/

 

cp /software/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

cp /software/keepalived/sbin/keepalived /usr/sbin/

 

 第九步:启动keepalived:systemctl start keepalived

查看状态 systemctl status keepalived

 

 

如果起不来 检查下网卡名称是否一致,如果不对,请修改一致:

vi /etc/sysconfig/network-scripts/ifcfg-ens160

 

 vi /etc/keepalived/keepalived.conf

 修改后重新用命令启动。启动keepalived:systemctl start keepalived

第10步:在另外一台机器同样搭建keepalived。

第11步:配置keepalived

进入目录 cd /etc/keepalived

创建添加内容:vi  chk_nginx_pid.sh 

#!/bin/bash

A=`ps -C nginx --no-header |wc -l`

if [ $A -eq 0 ]

                then

                        echo 'nginx server is died'

                        killall keepalived

fi

保存后,授权。

chmod +x  chk_nginx_pid.sh

配置keepalived:

vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

   notification_email {

     acassen@firewall.loc

     failover@firewall.loc

     sysadmin@firewall.loc

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

   vrrp_skip_check_adv_addr

   #vrrp_strict

   vrrp_garp_interval 0

   vrrp_gna_interval 0

}

 

vrrp_script chk_http_port {  

   script "/etc/keepalived/chk_nginx_pid.sh"  

   interval 2        #(检测脚本执行的间隔)

   weight 2  

  }

 

 

vrrp_instance VI_1 {

    state MASTER

    interface eth0(网卡名称是否配错)

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        19.130.218.66(虚拟IP)

    }

 

track_script {                     

   chk_http_port                 

}

}

第12步 配置完成后,可以停掉keepalived,在重启,然后ip addr 查看是否增加了虚拟IP

 

第13步骤:把主的nginx停掉后,虚拟IP会自动切换到备机。

注意事项:

--> PS: 一旦ping不通(dns的问题我不管)首先把看下是否去掉了配置文件里的 vrrp_strict [它表严格执行vvrp协议,还不行再把防火墙搞掉], 否则搞死你半天都不知咋回事.

在有“vrrp_strict”的时候,就会有下面的iptables规则:

[root@centos ~]# iptables -nL

Chain INPUT (policy ACCEPT)

target prot opt source destination 

DROP all -- 0.0.0.0/0 192.168.111.130

 

把这个选项去掉了之后,重新启动keepalived就完全没有了。

 

 

posted @ 2021-08-13 09:30  lidingyi  阅读(927)  评论(0编辑  收藏  举报