ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

keepalived

2021-10-22 13:00:47  阅读:164  来源: 互联网

标签:httpd slave ## keepalived master root


keepalived

简介

Keepalived 软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能。因此,Keepalived除了能够管理LVS软件外,还可以作为其他服务(例如:Nginx、Haproxy、MySQL等)的高可用解决方案软件。

Keepalived软件主要是通过VRRP协议实现高可用功能的。VRRP是Virtual Router RedundancyProtocol(虚拟路由器冗余协议)的缩写,VRRP出现的目的就是为了解决静态路由单点故障问题的,它能够保证当个别节点宕机时,整个网络可以不间断地运行。

所以,Keepalived 一方面具有配置管理LVS的功能,同时还具有对LVS下面节点进行健康检查的功能,另一方面也可实现系统网络服务的高可用功能。

官网:http://www.keepalived.org/

功能

管理LVS负载均衡软件

实现LVS集群节点的健康检查

作为系统网络服务的高可用性(failover)

高可用故障转移的原理

Keepalived 高可用服务之间的故障切换转移,是通过 VRRP 来实现的。

在 Keepalived 服务正常工作时,主 Master 节点会不断地向备节点发送(多播的方式)心跳消息,用以告诉备 Backup 节点自己还活看,当主 Master 节点发生故障时,就无法发送心跳消息,备节点也就因此无法继续检测到来自主 Master 节点的心跳了,于是调用自身的接管程序,接管主 Master 节点的 IP 资源及服务。而当主 Master 节点恢复时,备 Backup 节点又会释放主节点故障时自身接管的IP资源及服务,恢复到原来的备用角色。

  • VRRP :全 称 Virtual Router Redundancy Protocol ,中文名为虚拟路由冗余协议 ,VRRP的出现就是为了解决静态踣甶的单点故障问题,VRRP是通过一种竞选机制来将路由的任务交给某台VRRP路由器的。

  • VRRP用 IP多播的方式(默认多播地址(224.0_0.18))实现高可用对之间通信。

  • 工作时主节点发包,备节点接包,当备节点接收不到主节点发的数据包的时候,就启动接管程序接管主节点的开源。备节点可以有多个,通过优先级竞选,但一般 Keepalived系统运维工作中都是一对。

  • VRRP使用了加密协议加密数据,但Keepalived官方目前还是推荐用明文的方式配置认证类型和密码。

工作原理

Keepalived高可用是通过 VRRP 进行通信的, VRRP是通过竞选机制来确定主备的,主的优先级高于备,因此,工作时主会优先获得所有的资源,备节点处于等待状态,当主挂了的时候,备节点就会接管主节点的资源,然后顶替主节点对外提供服务。

在 Keepalived 服务之间,只有作为主的服务器会一直发送 VRRP 广播包,告诉备它还活着,此时备不会枪占主,当主不可用时,即备监听不到主发送的广播包时,就会启动相关服务接管资源,保证业务的连续性.接管速度最快可以小于1秒。

部署

## master ##
关闭防火墙selinux
[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# bash
[root@master ~]# systemctl disable --now firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@master ~]# vim /etc/selinux/config 
[root@master ~]# setenforce 0
[root@master ~]# reboot 
[root@master ~]# getenforce 
Disabled

配置网络源
[root@master ~]# curl -o /etc/yum.repos.d/CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1572  100  1572    0     0   4837      0 --:--:-- --:--:-- --:--:--  4851
[root@master ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@master ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo

安装相关组件
[root@master ~]# yum -y install epel-release vim wget gcc gcc-c++

安装keepalived
[root@master ~]# yum -y install keepalived
[root@master ~]# rpm -ql keepalived
/etc/keepalived      //配置目录
/etc/keepalived/keepalived.conf  //主配置文件
/etc/sysconfig/keepalived
/usr/bin/genhash
/usr/lib/systemd/system/keepalived.service  //服务控制文件
/usr/libexec/keepalived
/usr/sbin/keepalived   //命令存放位置


## slave ##
关闭防火墙selinux
[root@localhost ~]# hostnamectl set-hostname slave
[root@localhost ~]# bash
[root@slave ~]# systemctl disable --now firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@slave ~]# vim /etc/selinux/config 
[root@slave ~]# setenforce 0
[root@slave ~]# reboot 
[root@slave ~]# getenforce 
Disabled

配置网络源
[root@slave ~]# curl -o /etc/yum.repos.d/CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1572  100  1572    0     0   2335      0 --:--:-- --:--:-- --:--:--  2332
[root@slave ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@slave ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo

安装相关组件
[root@slave ~]# yum -y install epel-release vim wget gcc gcc-c++

安装keepalived
[root@slave ~]# yum -y install keepalived

## master ##
在master上安装httpd
[root@master ~]# yum -y install httpd

设置开机自启
[root@master ~]# systemctl enable --now httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@master ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since 五 2021-10-22 00:18:50 EDT; 8s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 13792 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─13792 /usr/sbin/httpd -DFOREGROUND
           ├─13793 /usr/sbin/httpd -DFOREGROUND
           ├─13794 /usr/sbin/httpd -DFOREGROUND
           ├─13795 /usr/sbin/httpd -DFOREGROUND
           ├─13796 /usr/sbin/httpd -DFOREGROUND
           └─13797 /usr/sbin/httpd -DFOREGROUND

10月 22 00:18:50 master systemd[1]: Starting The Apache HTTP Server...
10月 22 00:18:50 master httpd[13792]: AH00558: httpd: Could not reliably determ...ge
10月 22 00:18:50 master systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

[root@master ~]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128            *:22                         *:*                  
LISTEN      0      100    127.0.0.1:25                         *:*                  
LISTEN      0      128           :::80                        :::*                  
LISTEN      0      128           :::22                        :::*                  
LISTEN      0      100          ::1:25                        :::*      

配置网站
[root@master ~]# echo 'master' > /var/www/html/index.html
[root@master ~]# curl 192.168.145.149
master

## slave ##
在slave上安装httpd
[root@slave ~]# yum -y install httpd

设置开机自启
[root@slave ~]# systemctl enable --now httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

配置网站
[root@slave ~]# echo 'slave' > /var/www/html/index.html
[root@slave ~]# curl 192.168.145.150
slave
[root@slave ~]# curl 192.168.145.149
master

## master ##
配置主keepalived
[root@master ~]# vim /etc/keepalived/keepalived.conf
[root@master ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id lb01
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass lll
    }
    virtual_ipaddress {
       192.168.145.250
    }
}

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

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

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

重启使配置生效
[root@master ~]# systemctl restart keepalived
[root@master ~]# systemctl enable keepalived
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.

## slave ##
配置备keepalived
[root@slave ~]# vim /etc/keepalived/keepalived.conf
[root@slave ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id lb02
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass lll
    }
    virtual_ipaddress {
        192.168.145.250
    }
}

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

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

    real_server 192.168.145.150 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
使配置生效
[root@slave ~]# systemctl restart keepalived
[root@slave ~]# systemctl enable keepalived
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.

查看VIP
[root@master ~]# 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:20:9e:66 brd ff:ff:ff:ff:ff:ff
    inet 192.168.145.149/24 brd 192.168.145.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.145.250/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe20:9e66/64 scope link 
       valid_lft forever preferred_lft forever
[root@slave ~]# 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:b4:70:bf brd ff:ff:ff:ff:ff:ff
    inet 192.168.145.150/24 brd 192.168.145.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feb4:70bf/64 scope link 
       valid_lft forever preferred_lft forever

## master ##
修改内核参数,开启监听VIP功能
[root@master ~]# echo 'net.ipv4.ip_nonlocal_bind = 1' >>/etc/sysctl.conf
[root@master ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
[root@master ~]# cat /proc/sys/net/ipv4/ip_nonlocal_bind
1

## slave ##
[root@slave ~]# echo 'net.ipv4.ip_nonlocal_bind = 1' >>/etc/sysctl.conf
[root@slave ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
[root@slave ~]# cat /proc/sys/net/ipv4/ip_nonlocal_bind
1

让keepalived通过脚本来监控httpd负载均衡机的状态
## master ##
[root@master ~]# mkdir /scripts
[root@master ~]# cd /scripts/
[root@master scripts]# vim check_m.sh
[root@master scripts]# cat check_m.sh 
if [ $httpd_status -lt 1 ];then
    systemctl stop keepalived
fi
[root@master scripts]# chmod +x check_m.sh
[root@master scripts]# vim notify.sh
[root@master scripts]# cat 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" 593582553@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 '\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  65 10月 22 00:34 check_m.sh
-rwxr-xr-x 1 root root 656 10月 22 00:35 notify.sh

## slave ##
[root@slave ~]# mkdir /scripts
[root@slave ~]# cd /scripts/
[root@slave scripts]# vim notify.sh
[root@slave scripts]# cat 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" 1344933566@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 '\bhttpd\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月 22 00:37 notify.sh

配置keepalived加入监控脚本的配置
## master ##
[root@master scripts]# vim notify.sh
[root@master scripts]# vim /etc/keepalived/keepalived.conf
[root@master scripts]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id lb01    ## 非常重要,标识本机的hostname
}

vrrp_script httpd_check {
    script "/scripts/check_m.sh"  ## 执行的脚本位置
    interval 1   ## 检测时间间隔
    weight -20    ## 如果条件成立则权重减20
}

vrrp_instance VI_1 {
    state MASTER    ## 非常重要,标识主机,备用机为BACKUP
    interface ens33    ## 非常重要,网卡名
    virtual_router_id 52    ## 非常重要,虚拟路由ID号(主备节点要相同)
    priority 100   ## 优先级(0-254),一般主机的大于备机
    advert_int 1   ## 主备信息发送间隔,两个节点必须一致,默认1秒
    authentication {
        auth_type PASS
        auth_pass lll  证匹配,设置认证类型和密码,MASTER和BACKUP必须使用相同的密码才能正常通信
    }
    virtual_ipaddress {  
        192.168.145.250   ##虚拟ip,可以指定多个
    }
    track_script {    ## 检查haproxy健康状况的脚本
        httpd_check
    }
    notify_master "/scripts/notify.sh master 192.168.216.250"
    notify_backup "/scripts/notify.sh backup 192.168.216.250"
}

virtual_server 192.168.145.250 80 {   ####虚拟ip 的详细配置
    delay_loop 6  
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.145.149 80 {  ## ## 本机的真实ip
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.145.150 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

## slave  ## 
[root@slave scripts]# vim /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 ens33
    virtual_router_id 52
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass lll
    }
    virtual_ipaddress {
        192.168.145.250
    }
    notify_master "/scripts/notify.sh master 192.168.216.250"
    notify_backup "/scripts/notify.sh backup 192.168.216.250"
}

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

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

    real_server 192.168.145.150 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@slave scripts]# systemctl restart keepalived

 

 


模拟主服务器宕机 [root@master scripts]# systemctl stop keepalived [root@master scripts]# systemctl stop httpd

 

 

 

标签:httpd,slave,##,keepalived,master,root
来源: https://www.cnblogs.com/Aimmi/p/15438320.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有