IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> keepalived -> 正文阅读

[系统运维]keepalived


Keepalived一个基于VRRP 协议来实现的 LVS 服务高可用方案,可以利用其来解决单点故障。一个LVS服务会有2台服务器运行Keepalived,一台为主服务器(MASTER),一台为备份服务器(BACKUP),但是对外表现为一个虚拟IP,主服务器会发送特定的消息给备份服务器,当备份服务器收不到这个消息的时候,即主服务器宕机的时候, 备份服务器就会接管虚拟IP,继续提供服务,从而保证了高可用性

Keepalived 提供了很好的高可用性保障服务,它可以检查服务器的状态,如果有服务器出现问题,Keepalived 会将其从系统中移除,并且同时使用备份服务器代替该服务器的工作,当这台服务器可以正常工作后,Keepalived 再将其放入服务器群中,这个过程是 Keepalived 自动完成的,不需要人工干涉,我们只需要修复出现问题的服务器即可。

keepalived实现httpd负载均衡机高可用

环境

系统信息主机名IP
centos8master192.168.172.142
centos8slave192.168.172.143

keepalived安装

配置主keepalived

//关闭防火墙及selinux
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config


//配置网络源
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
--2021-10-21 18:34:24--  https://mirrors.aliyun.com/repo/Centos-8.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 45.253.17.214, 45.253.17.217, 103.43.210.117, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|45.253.17.214|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2595 (2.5K) [application/octet-stream]
Saving to: '/etc/yum.repos.d/CentOS-Base.repo'

/etc/yum.repos 100%[====>]   2.53K  --.-KB/s    in 0s      

2021-10-21 18:34:25 (49.1 MB/s) - '/etc/yum.repos.d/CentOS-Base.repo' saved [2595/2595]

[root@localhost ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@localhost ~]# yum -y install epel-release vim wget gcc gcc-c++


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

//查看安装生成的文件
[root@localhost ~]# 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
/usr/share/doc/keepalived/TODO
/usr/share/doc/keepalived/keepalived.conf.HTTP_GET.port
/usr/share/doc/keepalived/keepalived.conf.IPv6
/usr/share/doc/keepalived/keepalived.conf.PING_CHECK
/usr/share/doc/keepalived/keepalived.conf.SMTP_CHECK
/usr/share/doc/keepalived/keepalived.conf.SSL_GET
/usr/share/doc/keepalived/keepalived.conf.SYNOPSIS
/usr/share/doc/keepalived/keepalived.conf.UDP_CHECK
/usr/share/doc/keepalived/keepalived.conf.conditional_conf
/usr/share/doc/keepalived/keepalived.conf.fwmark
/usr/share/doc/keepalived/keepalived.conf.inhibit
/usr/share/doc/keepalived/keepalived.conf.misc_check
/usr/share/doc/keepalived/keepalived.conf.misc_check_arg
/usr/share/doc/keepalived/keepalived.conf.quorum
/usr/share/doc/keepalived/keepalived.conf.sample
/usr/share/doc/keepalived/keepalived.conf.status_code
/usr/share/doc/keepalived/keepalived.conf.track_interface
/usr/share/doc/keepalived/keepalived.conf.virtual_server_group
/usr/share/doc/keepalived/keepalived.conf.virtualhost
/usr/share/doc/keepalived/keepalived.conf.vrrp
/usr/share/doc/keepalived/keepalived.conf.vrrp.localcheck
/usr/share/doc/keepalived/keepalived.conf.vrrp.lvs_syncd
/usr/share/doc/keepalived/keepalived.conf.vrrp.routes
/usr/share/doc/keepalived/keepalived.conf.vrrp.rules
/usr/share/doc/keepalived/keepalived.conf.vrrp.scripts
/usr/share/doc/keepalived/keepalived.conf.vrrp.static_ipaddress
/usr/share/doc/keepalived/keepalived.conf.vrrp.sync
/usr/share/man/man1/genhash.1.gz
/usr/share/man/man5/keepalived.conf.5.gz
/usr/share/man/man8/keepalived.8.gz
/usr/share/snmp/mibs/KEEPALIVED-MIB.txt
/usr/share/snmp/mibs/VRRP-MIB.txt
/usr/share/snmp/mibs/VRRPv3-MIB.txt

备服务器上安装keepalived

//关闭防火墙与SELINUX
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config

//配置网络源
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
--2021-10-21 18:34:34--  https://mirrors.aliyun.com/repo/Centos-8.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 103.43.210.116, 45.253.17.216, 45.253.17.217, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|103.43.210.116|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2595 (2.5K) [application/octet-stream]
Saving to: '/etc/yum.repos.d/CentOS-Base.repo'

/etc/yum.repos 100%[====>]   2.53K  --.-KB/s    in 0s      

2021-10-21 18:34:36 (55.9 MB/s) - '/etc/yum.repos.d/CentOS-Base.repo' saved [2595/2595]

[root@localhost ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@localhost ~]# yum -y install epel-release vim wget gcc gcc-c++


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

在主备机上分别安装httpd

在master上安装nginx

在master上安装httpd
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# cd /usr/share/httpd/noindex/
[root@localhost noindex]# ls
index.html
[root@localhost noindex]# echo 'web1' > index.html 
[root@localhost noindex]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

访问
请添加图片描述
在slave上安装nginx

[root@localhost ~]# yum -y install httpd
[root@localhost noindex]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

访问
请添加图片描述

keepalived配置

配置主keepalived

[root@localhost ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
  router_id lb01
}

vrrp_instance VI_1 {
   state MASTER
   interface ens160
   virtual_router_id 51
   priority 100
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass xux
   }
   virtual_ipaddress {
       192.168.172.250
   }
}

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

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

   real_server 192.168.172.143 80 {
       weight 1
       TCP_CHECK {
           connect_port 80
           connect_timeout 3
           nb_get_retry 3
           delay_before_retry 3
       }
   }
}
[root@localhost ~]# systemctl enable keepalived
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.

配置备keepalived

[root@localhost ~]# 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 xux
    }
    virtual_ipaddress {
        192.168.172.250
    }
}

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

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

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

查看VIP在哪里

在MASTER上查看

[root@localhost ~]# 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 fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:f9:d6:50 brd ff:ff:ff:ff:ff:ff
    inet 192.168.172.142/24 brd 192.168.172.255 scope global noprefixroute ens160
       valid_lft forever preferred_lft forever
    inet 192.168.172.250/32 scope global ens160
       valid_lft forever preferred_lft forever

在SLAVE上查看
[root@localhost ~]# 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 fq_codel state UP group default qlen 1000
link/ether 00:0c:29:db:3c:14 brd ff:ff:ff:ff:ff:ff
inet 192.168.172.143/24 brd 192.168.172.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever

## 修改内核参数,开启监听VIP功能
此步可做可不做,该功能可用于仅监听VIP的时候

在master上修改内核参数
```shell
[root@localhost ~]# echo 'net.ipv4.ip_nonlocal_bind = 1' >>/etc/sysctl.conf
[root@localhost ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
[root@localhost ~]# cat /proc/sys/net/ipv4/ip_nonlocal_bind
1

在slave上修改内核参数

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

让keepalived监控httpd负载均衡机

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

[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# vim check_h.sh
#!/bin/bash
httpd_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bhttpd\b'|wc -l)
if [ $httpd_status -lt 1 ];then
     systemctl stop keepalived
fi
[root@localhost scripts]# chmod +x check_h.sh
[root@localhost scripts]# ll
total 4
-rwxr-xr-x. 1 root root 140 Oct 21 19:20 check_h.sh
[root@localhost scripts]# vim 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" 2296243938@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@localhost scripts]# chmod +x notify.sh

在slave上编写脚本

[root@localhost ~]# cat /scripts/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" 2296243938@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@localhost scripts]# cat check_h.sh 
#!/bin/bash
httpd_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bhttpd\b'|wc -l)
if [ $httpd_status -lt 1 ];then
             systemctl stop keepalived
fi
[root@localhost scripts]# chmod +x notify.sh
[root@localhost scripts]# chmod +x check_h.sh 

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

配置主keepalived

[root@localhost ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id lb01
}

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

vrrp_instance VI_1 {
    state MASTER
    interface ens160
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass xux
    }
    virtual_ipaddress {
        192.168.172.250
    }
    track_script {
        httpd_check
    }
    notify_master "/scripts/notify.sh master 192.168.172.250"
    notify_backup "/scripts/notify.sh backup 192.168.172.250"
}

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

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

    real_server 192.168.172.143 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@localhost ~]# systemctl restart keepalived

配置备keepalived
backup无需检测nginx是否正常,当升级为MASTER时启动nginx,当降级为BACKUP时关闭

[root@localhost ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id lb02
}

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

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

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

    real_server 192.168.172.143 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@localhost ~]# systemctl restart keepalived

测试

模拟主服务器宕机

[root@192 ~]# systemctl stop  keepalived
[root@192 ~]# systemctl stop  httpd

请添加图片描述

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-10-22 11:20:10  更:2021-10-22 11:20:35 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 21:02:51-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码