keepalived

2 篇文章 0 订阅

keepalived安装

环境说明

主机IP
master192.168.72.135
slave192.168.72.138

配置主keepalived

[root@master ~]# systemctl stop firewalld
[root@master ~]# getenforce 
Disabled
[root@master ~]# setenforce 0
setenforce: SELinux is disabled
[root@master ~]# 

//安装keepalived
[root@kvm ~]# yum -y install keepalived
上次元数据过期检查:1:05:23 前,执行于 2021年10月20日 星期三 21时23分50秒。
依赖关系解决。
=========================================================================================================
 软件包                          架构        版本                                   仓库            大小
=========================================================================================================

//查看安装生成的文件
[root@master ~]#  rpm -ql keepalived
/etc/keepalived
/etc/keepalived/keepalived.conf
/etc/sysconfig/keepalived
/usr/bin/genhash
/usr/lib/.build-id
/usr/lib/.build-id/0a
/usr/lib/.build-id/0a/410997e11c666114ca6d785e58ff0cc248744e
/usr/lib/.build-id/6f
/usr/lib/.build-id/6f/ba0d6bad6cb5ff7b074e703849ed93bebf4a0f
/usr/lib/systemd/system/keepalived.service
/usr/libexec/keepalived
/usr/sbin/keepalived
/usr/share/doc/keepalived
/usr/share/doc/keepalived/AUTHOR
/usr/share/doc/keepalived/CONTRIBUTORS
/usr/share/doc/keepalived/COPYING
/usr/share/doc/keepalived/ChangeLog
/usr/share/doc/keepalived/README

用同样的方法在备服务器上安装keepalived

[root@web ~]# systemctl stop firewalld
[root@web ~]# getenforce 
Disabled
[root@web ~]# setenforce 0
setenforce: SELinux is disabled
[root@web ~]# 

//安装keepalived
[root@web ~]# yum -y install keepalived
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                              | 4.3 kB  00:00:00     
 * base: mirrors.bfsu.edu.cn
 * epel: fedora.ipserverone.com
 * extras: mirrors.tuna.tsinghua.edu.cn

在主备机上分别安装nginx

在master上安装nginx

[root@master ~]# yum -y install httpd
[root@master html]# systemctl start httpd
[root@master ~]# cd /var/www/html/
[root@master html]# ls
index.html  index.php
[root@master html]# systemctl start httpd
[root@master html]# cat index.html 
web01
[root@master html]# 
[root@master html]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port            Peer Address:Port     Process     
LISTEN      0           5                    127.0.0.1:25151                0.0.0.0:*                    
LISTEN      0           128                    0.0.0.0:111                  0.0.0.0:*                    
LISTEN      0           128                    0.0.0.0:80                   0.0.0.0:*                    
LISTEN      0           32               192.168.122.1:53                   0.0.0.0:*                    
LISTEN      0           128                    0.0.0.0:22                   0.0.0.0:*                    
LISTEN      0           128                       [::]:111                     [::]:*                    
LISTEN      0           128                       [::]:80                      [::]:*                    
LISTEN      0           128                       [::]:22                      [::]:*                    
[root@master html]# 

在slave上安装nginx


[root@slave scripts]# yum -y install httpd
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
上次元数据过期检查:1:23:34 前,执行于 2021年10月20日 星期三 23时08分33秒。
依赖关系解决。
==============================================
 软件包   架构   版本         仓库       大小
==============================================
安装:
 httpd    x86_64 2.4.37-21.module+el8.2.0+5008+cca404a3
                              AppStream 1.4 M
安装依赖关系:
 apr      x86_64 1.6.3-9.el8  AppStream 125 k
 apr-util x86_64 1.6.1-6.el8  AppStream 105 k
 httpd-filesystem
          noarch 2.4.37-21.module+el8.2.0+5008+cca404a3
                              AppStream  36 k
 httpd-tools
          x86_64 2.4.37-21.module+el8.2.0+5008+cca404a3
                              AppStream 103 k
 mailcap  noarch 2.1.48-3.el8 BaseOS     39 k
 mod_http2
          x86_64 1.11.3-3.module+el8.2.0+4377+dc421495
                              AppStream 158 k
 redhat-logos-httpd
          noarch 81.1-1.el8   BaseOS     26 k
安装弱的依赖:
 apr-util-bdb
          x86_64 1.6.1-6.el8  AppStream  25 k
 apr-util-openssl
          x86_64 1.6.1-6.el8  AppStream  27 k
启用模块流:
 httpd           2.4                         

事务概要
==============================================
安装  10 软件包

总计:2.0 M
安装大小:5.5 M
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                               1/1 
  安装    : apr-1.6.3-9.el8.x86_64       1/10 
  运行脚本: apr-1.6.3-9.el8.x86_64       1/10 
  安装    : apr-util-bdb-1.6.1-6.el8.    2/10 
  安装    : apr-util-openssl-1.6.1-6.    3/10 
  安装    : apr-util-1.6.1-6.el8.x86_    4/10 
  运行脚本: apr-util-1.6.1-6.el8.x86_    4/10 
  安装    : httpd-tools-2.4.37-21.mod    5/10 
  安装    : redhat-logos-httpd-81.1-1    6/10 
  安装    : mailcap-2.1.48-3.el8.noar    7/10 
  运行脚本: httpd-filesystem-2.4.37-2    8/10 
  安装    : httpd-filesystem-2.4.37-2    8/10 
  安装    : mod_http2-1.11.3-3.module    9/10 
  安装    : httpd-2.4.37-21.module+el   10/10 
  运行脚本: httpd-2.4.37-21.module+el   10/10 
  验证    : apr-1.6.3-9.el8.x86_64       1/10 
  验证    : apr-util-1.6.1-6.el8.x86_    2/10 
  验证    : apr-util-bdb-1.6.1-6.el8.    3/10 
  验证    : apr-util-openssl-1.6.1-6.    4/10 
  验证    : httpd-2.4.37-21.module+el    5/10 
  验证    : httpd-filesystem-2.4.37-2    6/10 
  验证    : httpd-tools-2.4.37-21.mod    7/10 
  验证    : mod_http2-1.11.3-3.module    8/10 
  验证    : mailcap-2.1.48-3.el8.noar    9/10 
  验证    : redhat-logos-httpd-81.1-1   10/10 
Installed products updated.

已安装:
  apr-1.6.3-9.el8.x86_64                      
  apr-util-1.6.1-6.el8.x86_64                 
  apr-util-bdb-1.6.1-6.el8.x86_64             
  apr-util-openssl-1.6.1-6.el8.x86_64         
  httpd-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64
  httpd-filesystem-2.4.37-21.module+el8.2.0+5008+cca404a3.noarch
  httpd-tools-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64
  mailcap-2.1.48-3.el8.noarch                 
  mod_http2-1.11.3-3.module+el8.2.0+4377+dc421495.x86_64
  redhat-logos-httpd-81.1-1.el8.noarch        

完毕!
[root@slave scripts]# cd
[root@slave ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@slave ~]# ss -antl
State   Recv-Q  Send-Q    Local Address:Port                 Peer Address:Port              
LISTEN  0       128             0.0.0.0:22                        0.0.0.0:*                 
LISTEN  0       128                [::]:22                           [::]:*                 
LISTEN  1       128                   *:80                              *:*                 
[root@slave ~]# 

在浏览器上访问试试,确保master上的nginx服务能够正常访问
在这里插入图片描述
在这里插入图片描述

keepalived配置

配置主keepalived

[root@master ~]# cd /etc/keepalived
[root@master keepalived]# ls
keepalived.conf
[root@master keepalived]# cp keepalived.conf /opt/
[root@master keepalived]# ls /opt/
demo  docs  examples  host-manager  jenkins.war  keepalived.conf  manager  repo  ROOT  test
[root@master keepalived]# > keepalived.conf 
[root@master keepalived]# vi keepalived.conf 
[root@master keepalived]# vim keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id lb01
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass wangqing
    }
    virtual_ipaddress {
        192.168.72.250
    }
}

virtual_server 192.168.72.250 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.72.135 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.72.138 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

[root@master keepalived]# systemctl enable --now keepalived
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
[root@master keepalived]# ss -antl
State        Recv-Q       Send-Q                Local Address:Port               Peer Address:Port       
LISTEN       0            128                         0.0.0.0:80                      0.0.0.0:*          
LISTEN       0            128                         0.0.0.0:22                      0.0.0.0:*          
LISTEN       0            128                  192.168.72.135:9000                    0.0.0.0:*          
LISTEN       0            128                            [::]:80                         [::]:*          
LISTEN       0            128                            [::]:22                         [::]:*          
[root@master keepalived]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-10-20 23:45:05 EDT; 28s ago
  Process: 87122 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 87136 (keepalived)
    Tasks: 3 (limit: 4842)
   Memory: 4.8M
   CGroup: /system.slice/keepalived.service
           ├─87136 /usr/sbin/keepalived -D
           ├─87138 /usr/sbin/keepalived -D

[root@master keepalived]# ip a
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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:04:d8:b6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.72.135/24 brd 192.168.72.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 192.168.72.250/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe04:d8b6/64 scope link 
       valid_lft forever preferred_lft forever
[root@master keepalived]# 

配置备keepalived

[root@slave yum.repos.d]# cd /etc/keepalived/
[root@slave keepalived]# ls
keepalived.conf
[root@slave keepalived]# cp keepalived.conf /opt/
[root@slave keepalived]# ls /opt/
keepalived.conf
[root@slave keepalived]# >keepalived.conf 
[root@slave keepalived]# vi keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id lb02
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass wangqing
    }
    virtual_ipaddress {
        192.168.72.250
    }
}

virtual_server 192.168.72.250 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.72.138 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.72.135 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

[root@slave keepalived]# systemctl enable --now keepalived
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
[root@slave keepalived]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-10-20 23:49:39 EDT; 9s ago
  Process: 91456 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 91457 (keepalived)
    Tasks: 3 (limit: 23789)
   Memory: 2.6M
   CGroup: /system.slice/keepalived.service
           ├─91457 /usr/sbin/keepalived -D
           ├─91458 /usr/sbin/keepalived -D

[root@slave keepalived]# ip a
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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:2e:a5:d7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.72.138/24 brd 192.168.72.255 scope global dynamic noprefixroute ens160
       valid_lft 1069sec preferred_lft 1069sec
    inet6 fe80::2d22:fc96:1bdb:33f4/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@slave keepalived]# 

让keepalived监控nginx负载均衡机

keepalived通过脚本来监控nginx负载均衡机的状态
在master上编写脚本

[root@master ~]# mkdir /scripts
[root@master ~]# cd /scripts/
[root@master scripts]# vi check_n.sh
#!/bin/bash
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bhttpd\b'|wc -l)
if [ $httpd_status -lt 1 ];then
    systemctl stop keepalived
fi

[root@master scripts]# chmod +x check_n.sh
[root@master scripts]# vi notify.sh
#!/bin/bash
}
case "$1" in
  master)
        nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bhttpd\b'|wc -l)
        if [ $httpd_status -lt 1 ];then
            systemctl start httpd
        fi
        sendmail
  ;;
  backup)
        httpd_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bhttpd\b'|wc -l)
        if [ $httpd_status -gt 0 ];then
            systemctl stop httpd
        fi
  ;;
  *)
        echo "Usage:$0 master|backup VIP"
  ;;
esac


[root@master scripts]# chmod +x notify.sh
[root@master scripts]# ll
总用量 8
-rwxr-xr-x 1 root root 142 10月 21 00:04 check_n.sh
-rwxr-xr-x 1 root root 662 10月 21 00:05 notify.sh
[root@master scripts]# 

在slave上编写脚本

[root@slave ~]# mkdir /scripts
[root@slave ~]# cd /scripts/
[root@slave scripts]# vi notify.sh
#!/bin/bash
VIP=$2
sendmail (){
        subject="${VIP}'s server keepalived state is translate"
        content="`date +'%F %T'`: `hostname`'s state change to master"
        echo $content | mail -s "$subject" 1470044516@qq.com
}
case "$1" in
  master)
        httpd_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bhttpd\b'|wc -l)
        if [ $httpd_status -lt 1 ];then
            systemctl start httpd
        fi
        sendmail
  ;;
  backup)
        httpd_status=$(ps -ef|grep -Ev "grep|$0"|grep '\httpd\b'|wc -l)
        if [ $httpd_status -gt 0 ];then
            systemctl stop httpd
        fi
  ;;
  *)
        echo "Usage:$0 master|backup VIP"
  ;;
esac

[root@slave scripts]# chmod +x notify.sh 
[root@slave scripts]# ll
总用量 4
-rwxr-xr-x. 1 root root 662 10月 21 00:07 notify.sh
[root@slave scripts]# 

配置keepalived加入监控脚本的配置

配置主keepalived

[root@master scripts]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id lb01
}

vrrp_script httpd_check {
    script "/scripts/check_n.sh"
    interval 1
    weight -20
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass wangqing
    }
    virtual_ipaddress {
        192.168.72.250
    }
    track_script {
        nginx_check
    }
    notify_master "/scripts/notify.sh master 192.168.72.250"
    notify_backup "/scripts/notify.sh backup 192.168.72.250"
}

virtual_server 192.168.72.250 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.72.135 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.72.138 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

[root@master scripts]# systemctl restart keepalived

配置备keepalived

[root@slave scripts]# vi /etc/keepalived/keepalived.conf
[root@slave scripts]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id lb02
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass wangqing
    }
    virtual_ipaddress {
        192.168.72.250
    }
    notify_master "/scripts/notify.sh master 192.168.72.250"
    notify_backup "/scripts/notify.sh backup 192.168.72.250"
}

virtual_server 192.168.72.250 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.72.138 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.72.135 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}


访问
在这里插入图片描述

停止master的httpd

[root@master scripts]# systemctl stop httpd
[root@master scripts]# ss -antl
State        Recv-Q       Send-Q                Local Address:Port               Peer Address:Port       
LISTEN       0            128                         0.0.0.0:22                      0.0.0.0:*          
LISTEN       0            128                  192.168.72.135:9000                    0.0.0.0:*          
LISTEN       0            128                            [::]:22                         [::]:*          
[root@master scripts]# 

访问
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值