centos 7 安装配置Keepalived (脚本,实例)

环境:

两台NGINX,使用keepalived实现故障漂移

MASTER机器脚本内容:

#/bin/bash
#auto install keepalived
#202073112:05:54
#by author toyix
##########################
ETH=ens33

yum install -y keepalived*
cd /etc/keepalived/
cp keepalived.conf{,.bak}
sed -i '1,$d' keepalived.conf
cat>keepalived.conf <<-EOF
! Configuration File for Keepalived
global_defs {
        notification_email {
                631646620@qq.com
        }
        notification_email_from keepalived@yjy.com
        smtp_server 127.0.0.1
        smtp_connect_timeout 30
        router_id LVS_DEVEL
}
vrrp_script chk_nginx {
        script "/data/sh/check_nginx.sh"
        interval 2
}

#VIP1
vrrp_instance VI_1 {
        state BACKUP
        interface ${ETH}
        virtual_router_id 151
        priority 100
        advert_int 5
        nopreempt
        authentication {
                auth_type       PASS
                auth_pass       1111
}
        virtual_ipaddress {
                192.168.1.31
        }
        track_script {
                chk_nginx
        }
}
EOF
mkdir -p /data/sh
cat>/data/sh/check_nginx.sh <<-EOF
#!/bin/bash
#202072921:17:29
#auto check nginx service
###############################
CHECK_NUM=\`netstat -tnlp|grep -w "nginx: master"|grep 80|wc -l\`
if [ \${CHECK_NUM} -eq 0 ];then
        systemctl stop keepalived.service
        pkill keepalived
fi
EOF
chmod +x /data/sh/check_nginx.sh
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface ${ETH} --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --reload
systemctl start keepalived.service

BACKUP机器脚本内容:

#/bin/bash
#auto install keepalived
#202073112:05:54
#by author toyix
##########################
ETH=ens33

yum install -y keepalived*
cd /etc/keepalived/
cp keepalived.conf{,.bak}
sed -i '1,$d' keepalived.conf
cat>keepalived.conf <<-EOF
! Configuration File for Keepalived
global_defs {
        notification_email {
                631646620@qq.com
        }
        notification_email_from keepalived@yjy.com
        smtp_server 127.0.0.1
        smtp_connect_timeout 30
        router_id LVS_DEVEL
}
vrrp_script chk_nginx {
        script "/data/sh/check_nginx.sh"
        interval 2
}

#VIP1
vrrp_instance VI_1 {
        state BACKUP
        interface ${ETH}
        virtual_router_id 151
        priority 90
        advert_int 5
        nopreempt
        authentication {
                auth_type       PASS
                auth_pass       1111
}
        virtual_ipaddress {
                192.168.1.31
        }
        track_script {
                chk_nginx
        }
}
EOF
mkdir -p /data/sh
cat>/data/sh/check_nginx.sh <<-EOF
#!/bin/bash
#202072921:17:29
#auto check nginx service
###############################
CHECK_NUM=\`netstat -tnlp|grep -w "nginx: master"|grep 80|wc -l\`
if [ \${CHECK_NUM} -eq 0 ];then
        systemctl stop keepalived.service
        pkill keepalived
fi
EOF
chmod +x /data/sh/check_nginx.sh
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface ${ETH} --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --reload
systemctl start keepalived.service

执行过程

[root@localhost src]# sh autoinstallkeepalived.sh 
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package keepalived.x86_64 0:1.3.5-16.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================================================
 Package                                   Arch                                  Version                                     Repository                           Size
=======================================================================================================================================================================
Installing:
 keepalived                                x86_64                                1.3.5-16.el7                                base                                331 k

Transaction Summary
=======================================================================================================================================================================
Install  1 Package

Total download size: 331 k
Installed size: 1.0 M
Downloading packages:
keepalived-1.3.5-16.el7.x86_64.rpm                                                                                                              | 331 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : keepalived-1.3.5-16.el7.x86_64                                                                                                                      1/1 
  Verifying  : keepalived-1.3.5-16.el7.x86_64                                                                                                                      1/1 

Installed:
  keepalived.x86_64 0:1.3.5-16.el7                                                                                                                                     

Complete!
Warning: ALREADY_ENABLED: rule '['--in-interface', 'ens33', '--destination', '224.0.0.18', '--protocol', 'vrrp', '-j', 'ACCEPT']' already is in 'ipv4:filter:INPUT'
success
success
[root@localhost src]# 

}
[root@localhost src]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:30:51:a0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.21/24 brd 192.168.1.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.1.31/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::22b6:b244:5f25:faf4/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@localhost src]# 

当关闭nginx时,发生MASTER漂移

[root@localhost src]# pkill nginx

Jul 31 14:48:20 localhost Keepalived_vrrp[17222]: Sending gratuitous ARP on ens33 for 192.168.1.31
Jul 31 14:48:20 localhost Keepalived_vrrp[17222]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.1.31
Jul 31 14:48:20 localhost Keepalived_vrrp[17222]: Sending gratuitous ARP on ens33 for 192.168.1.31
Jul 31 14:48:20 localhost Keepalived_vrrp[17222]: Sending gratuitous ARP on ens33 for 192.168.1.31
Jul 31 14:48:20 localhost Keepalived_vrrp[17222]: Sending gratuitous ARP on ens33 for 192.168.1.31
Jul 31 14:48:20 localhost Keepalived_vrrp[17222]: Sending gratuitous ARP on ens33 for 192.168.1.31

Jul 31 14:50:02 localhost systemd: Stopped LVS and VRRP High Availability Monitor.
Jul 31 14:50:02 localhost Keepalived[17220]: Stopping
Jul 31 14:50:02 localhost Keepalived_healthcheckers[17221]: Stopped
Jul 31 14:50:02 localhost Keepalived_vrrp[17222]: VRRP_Instance(VI_1) sent 0 priority
Jul 31 14:50:02 localhost Keepalived_vrrp[17222]: VRRP_Instance(VI_1) removing protocol VIPs.
Jul 31 14:50:02 localhost Keepalived_vrrp[17222]: Stopped
Jul 31 14:50:02 localhost Keepalived[17220]: Stopped Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2





Jul 31 14:47:54 localhost Keepalived_vrrp[13746]: VRRP_Instance(VI_1) removing protocol VIPs.
Jul 31 14:47:54 localhost Keepalived_vrrp[13746]: Using LinkWatch kernel netlink reflector...
Jul 31 14:47:54 localhost Keepalived_vrrp[13746]: VRRP_Instance(VI_1) Entering BACKUP STATE
Jul 31 14:47:54 localhost Keepalived_vrrp[13746]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Jul 31 14:47:54 localhost Keepalived_vrrp[13746]: VRRP_Script(chk_nginx) succeeded

Jul 31 14:50:02 localhost Keepalived_vrrp[13746]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jul 31 14:50:07 localhost Keepalived_vrrp[13746]: VRRP_Instance(VI_1) Entering MASTER STATE
Jul 31 14:50:07 localhost Keepalived_vrrp[13746]: VRRP_Instance(VI_1) setting protocol VIPs.
Jul 31 14:50:07 localhost Keepalived_vrrp[13746]: Sending gratuitous ARP on ens33 for 192.168.1.31
Jul 31 14:50:07 localhost Keepalived_vrrp[13746]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.1.31
Jul 31 14:50:07 localhost Keepalived_vrrp[13746]: Sending gratuitous ARP on ens33 for 192.168.1.31
Jul 31 14:50:07 localhost Keepalived_vrrp[13746]: Sending gratuitous ARP on ens33 for 192.168.1.31

----------------------end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CentOS 7上安装Keepalived的过程如下: 1. 首先,下载Keepalived的压缩包,并将其解压到指定目录。可使用以下命令进行解压: tar -zxf keepalived-2.0.20.tar.gz -C /app/ 2. 进入解压后的Keepalived目录,并进行编译和安装。使用以下命令完成这一步骤: cd keepalived-2.0.20/ && ./configure --prefix=/app/keepalived make && make install 3. 安装完成后,需要将Keepalived安装为系统服务。这可以通过创建一个systemd服务单元来实现。可以按照以下步骤进行操作: a. 创建一个名为keepalived.service的文件,将以下内容复制到该文件中: ``` [Unit] Description=Keepalived After=syslog.target network.target [Service] Type=forking ExecStart=/app/keepalived/sbin/keepalived --dont-fork --log-console ExecReload=/bin/kill -HUP $MAINPID KillMode=process KillSignal=SIGTERM [Install] WantedBy=multi-user.target ``` b. 将该文件移动到系统服务目录中: mv keepalived.service /etc/systemd/system/ 4. 最后,启用并启动Keepalived服务。使用以下命令完成这一步骤: systemctl enable keepalived systemctl start keepalived 现在,Keepalived已经成功安装并以系统服务的形式运行在CentOS 7上了。请确保按照上述步骤进行操作,并替换相关路径和文件名为实际使用的路径和文件名。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [centos7 linux 安装 keeplived](https://blog.csdn.net/weixin_40816738/article/details/125258576)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值