LVM+keepalive

一.Keepalived工作原理

  • Keepalived 是一个基于VRRP协议来实现的LVS服务高可用方案,可以解决静态路由出现的单点故障问题。

  • 在一个LVS服务集群中通常有主服务器(MASTER)和备份服务器(BACKUP)两种角色的服务器,但是对外表现为一个虚拟IP(VIP),主服务器会发送VRRP通告信息给备份服务器,当备份服务器收不到VRRP消息的时候,即主服务器异常的时候,备份服务器就会接管虚拟IP,继续提供服务,从而保证了高可用性。

二.Keepalived体系主要模块及作用

  • core模块:为keepalived的核心,负责主进程的启动、维护及全局配置文件的加载和解析。
  • vrrp模块:是来实现VRRP协议的。(调度器之间的健康检查和主备切换)
  • check模块:负责健康检查,常见的方式有端口检查及URL检查。(节点服务器的健康检查)

三.LVS+Keepalived 高可用群集

  • 实验前准备
主DR 服务器:192.168.247.131
备DR 服务器:192.168.247.133
Web 服务器1:192.168.247.135
Web 服务器2:192.168.247.136
vip:192.168.247.188
客户端:192.168.247.200

1.主192.168.247.131

[root@lion ~]# systemctl stop firewalld.service
[root@lion ~]# setenforce 0
[root@lion ~]# mount /dev/sr0 /mnt
mount: /dev/sr0 写保护,将以只读方式挂载
[root@lion ~]# yum -y install ipvsadm keepalived
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
local                                                                                                                                  | 3.6 kB  00:00:00
软件包 ipvsadm-1.27-7.el7.x86_64 已安装并且是最新版本
正在解决依赖关系
--> 正在检查事务
---> 软件包 keepalived.x86_64.0.1.3.5-6.el7 将被 安装
--> 正在处理依赖关系 libnetsnmpmibs.so.31()(64bit),它被软件包 keepalived-1.3.5-6.el7.x86_64 需要
--> 正在处理依赖关系 libnetsnmpagent.so.31()(64bit),它被软件包 keepalived-1.3.5-6.el7.x86_64 需要
--> 正在检查事务
---> 软件包 net-snmp-agent-libs.x86_64.1.5.7.2-37.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

==============================================================================================================================================================
 Package                                      架构                            版本                                       源                              大小
==============================================================================================================================================================
正在安装:
 keepalived                                   x86_64                          1.3.5-6.el7                                local                          329 k
为依赖而安装:
 net-snmp-agent-libs                          x86_64                          1:5.7.2-37.el7                             local                          705 k

事务概要
==============================================================================================================================================================
安装  1 软件包 (+1 依赖软件包)

总下载量:1.0 M
安装大小:3.0 M
Downloading packages:
--------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                          7.1 MB/s | 1.0 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : 1:net-snmp-agent-libs-5.7.2-37.el7.x86_64                                                                                                 1/2
  正在安装    : keepalived-1.3.5-6.el7.x86_64                                                                                                             2/2
  验证中      : keepalived-1.3.5-6.el7.x86_64                                                                                                             1/2
  验证中      : 1:net-snmp-agent-libs-5.7.2-37.el7.x86_64                                                                                                 2/2

已安装:
  keepalived.x86_64 0:1.3.5-6.el7

作为依赖被安装:
  net-snmp-agent-libs.x86_64 1:5.7.2-37.el7

完毕!
[root@lion ~]# modprobe ip_vs
[root@lion ~]# cat /proc/net/ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@lion ~]# cd /etc/keepalived/
[root@lion keepalived]# cp keepalived.conf keepalived.conf.bak
[root@lion keepalived]# vim keepalived.conf
......
global_defs {                       #定义全局参数
--10行--修改,邮件服务指向本地
    smtp_server 127.0.0.1
--12行--修改,指定服务器(路由器)的名称,主备服务器名称须不同,主为LVS_01,备为LVS_02
    router_id LVS_01
}
 
vrrp_instance VI_1 {                #定义VRRP热备实例参数
--20行--修改,指定热备状态,主为MASTER,备为BACKUP
    state MASTER
--21行--修改,指定承载vip地址的物理接口
    interface ens32
--22行--修改,指定虚拟路由器的ID号,每个热备组保持一致 
    virtual_router_id 10
--23行--修改,指定优先级,数值越大优先级越高,主为100,备为99
    priority 100
    advert_int 1                    #通告间隔秒数(心跳频率)
    authentication {                #定义认证信息,每个热备组保持一致
        auth_type PASS              #认证类型
--27行--修改,指定验证密码,主备服务器保持一致
        auth_pass 123456
    }
    virtual_ipaddress {             #指定群集vip地址
        192.168.247.200
    }
}
--36行--修改,指定虚拟服务器地址(VIP)、端口,定义虚拟服务器和Web服务器池参数
virtual_server 192.168.247.200 80 {
    delay_loop 6                    #健康检查的间隔时间(秒)
    lb_algo rr                      #指定调度算法,轮询(rr)
--39行--修改,指定群集工作模式,直接路由(DR)
    lb_kind DR
    persistence_timeout 50          #连接保持时间(秒)
    protocol TCP                    #应用服务采用的是 TCP协议
--43行--修改,指定第一个Web节点的地址、端口
    real_server 192.168.247.135 80 {
        weight 1                    #节点的权重
--45行--删除,添加以下健康检查方式        
        TCP_CHECK {
            connect_port 80         #添加检查的目标端口
            connect_timeout 3       #添加连接超时(秒)
            nb_get_retry 3          #添加重试次数
            delay_before_retry 3    #添加重试间隔
        }
    }
 
    real_server 192.168.247.136 80 {     #添加第二个 Web节点的地址、端口
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
##删除后面多余的配置##
}
[root@lion keepalived]# systemctl start keepalived
[root@lion keepalived]# ip addr show dev ens32
2: ens32:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:d8:04:9b brd ff:ff:ff:ff:ff:ff
    inet 192.168.247.131/24 brd 192.168.247.255 scope global noprefixroute ens32
       valid_lft forever preferred_lft forever
    inet 192.168.247.188/32 brd 192.168.247.188 scope global noprefixroute ens32:0
       valid_lft forever preferred_lft forever
    inet6 fe80::9f88:edcf:867c:4272/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
[root@lion keepalived]# ipvsadm-save > /etc/sysconfig/ipvsadm
[root@lion keepalived]# systemctl start ipvsadm
[root@lion keepalived]# ipvsadm -C
[root@lion keepalived]# ipvsadm -A -t 192.168.247.200:80 -s rr
[root@lion keepalived]# ipvsadm -a -t 192.168.247.200:80 -r 192.168.247.135:80 -g
[root@lion keepalived]# ipvsadm -a -t 192.168.247.200:80 -r 192.168.247.136:80 -g
[root@lion keepalived]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.247.200:http rr
  -> 192.168.247.135:http         Route   1      0          0
  -> 192.168.247.136:http         Route   1      0          0
[root@lion keepalived]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.247.200:80 rr
  -> 192.168.247.135:80           Route   1      0          0
  -> 192.168.247.136:80           Route   1      0          0
[root@lion keepalived]# ipvsadm-save > /etc/sysconfig/ipvsadm
[root@lion keepalived]# vim /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens32.send_redirects = 0
[root@lion keepalived]# sysctl -p
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens32.send_redirects = 0
[root@lion keepalived]#  scp keepalived.conf [email protected]:`pwd`
[email protected]'s password:
keepalived.conf                                                                                                             100% 1306     2.2MB/s   00:00

2.备192.168.247.133

[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# mount /dev/sr0 /mnt
mount: /dev/sr0 写保护,将以只读方式挂载
[root@localhost ~]# yum -y install ipvsadm keepalived
已加载插件:fastestmirror, langpacks
base                                                                                                                                   | 3.6 kB  00:00:00
extras                                                                                                                                 | 2.9 kB  00:00:00
nginx-stable                                                                                                                           | 2.9 kB  00:00:00
updates                                                                                                                                | 2.9 kB  00:00:00
Loading mirror speeds from cached hostfile
 * base: mirrors.ustc.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.ustc.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 ipvsadm.x86_64.0.1.27-8.el7 将被 安装
---> 软件包 keepalived.x86_64.0.1.3.5-19.el7 将被 安装
--> 正在处理依赖关系 ipset-libs >= 7.1,它被软件包 keepalived-1.3.5-19.el7.x86_64 需要
--> 正在处理依赖关系 libnetsnmpmibs.so.31()(64bit),它被软件包 keepalived-1.3.5-19.el7.x86_64 需要
--> 正在处理依赖关系 libnetsnmpagent.so.31()(64bit),它被软件包 keepalived-1.3.5-19.el7.x86_64 需要
--> 正在检查事务
---> 软件包 ipset-libs.x86_64.0.6.29-1.el7 将被 升级
--> 正在处理依赖关系 ipset-libs(x86-64) = 6.29-1.el7,它被软件包 ipset-6.29-1.el7.x86_64 需要
--> 正在处理依赖关系 libipset.so.3()(64bit),它被软件包 ipset-6.29-1.el7.x86_64 需要
--> 正在处理依赖关系 libipset.so.3(LIBIPSET_1.0)(64bit),它被软件包 ipset-6.29-1.el7.x86_64 需要
--> 正在处理依赖关系 libipset.so.3(LIBIPSET_2.0)(64bit),它被软件包 ipset-6.29-1.el7.x86_64 需要
--> 正在处理依赖关系 libipset.so.3(LIBIPSET_3.0)(64bit),它被软件包 ipset-6.29-1.el7.x86_64 需要
---> 软件包 ipset-libs.x86_64.0.7.1-1.el7 将被 更新
---> 软件包 net-snmp-agent-libs.x86_64.1.5.7.2-49.el7_9.2 将被 安装
--> 正在处理依赖关系 net-snmp-libs = 1:5.7.2-49.el7_9.2,它被软件包 1:net-snmp-agent-libs-5.7.2-49.el7_9.2.x86_64 需要
--> 正在检查事务
---> 软件包 ipset.x86_64.0.6.29-1.el7 将被 升级
---> 软件包 ipset.x86_64.0.7.1-1.el7 将被 更新
---> 软件包 net-snmp-libs.x86_64.1.5.7.2-28.el7 将被 升级
---> 软件包 net-snmp-libs.x86_64.1.5.7.2-49.el7_9.2 将被 更新
--> 解决依赖关系完成

依赖关系解决

==============================================================================================================================================================
 Package                                     架构                           版本                                        源                               大小
==============================================================================================================================================================
正在安装:
 ipvsadm                                     x86_64                         1.27-8.el7                                  base                             45 k
 keepalived                                  x86_64                         1.3.5-19.el7                                base                            332 k
为依赖而安装:
 net-snmp-agent-libs                         x86_64                         1:5.7.2-49.el7_9.2                          updates                         707 k
为依赖而更新:
 ipset                                       x86_64                         7.1-1.el7                                   base                             39 k
 ipset-libs                                  x86_64                         7.1-1.el7                                   base                             64 k
 net-snmp-libs                               x86_64                         1:5.7.2-49.el7_9.2                          updates                         752 k

事务概要
==============================================================================================================================================================
安装  2 软件包 (+1 依赖软件包)
升级           ( 3 依赖软件包)

总计:1.9 M
总下载量:1.1 M
Downloading packages:
警告:/var/cache/yum/x86_64/7/base/packages/ipvsadm-1.27-8.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
ipvsadm-1.27-8.el7.x86_64.rpm 的公钥尚未安装
(1/3): ipvsadm-1.27-8.el7.x86_64.rpm                                                                                                   |  45 kB  00:00:00
(2/3): keepalived-1.3.5-19.el7.x86_64.rpm                                                                                              | 332 kB  00:00:00
net-snmp-agent-libs-5.7.2-49.el7_9.2.x86_64.rpm 的公钥尚未安装
(3/3): net-snmp-agent-libs-5.7.2-49.el7_9.2.x86_64.rpm                                                                                 | 707 kB  00:00:00
--------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                          3.6 MB/s | 1.1 MB  00:00:00
从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 检索密钥
导入 GPG key 0xF4A80EB5:
 用户ID     : "CentOS-7 Key (CentOS 7 Official Signing Key) "
 指纹       : 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 软件包     : centos-release-7-4.1708.el7.centos.x86_64 (@anaconda)
 来自       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在更新    : ipset-libs-7.1-1.el7.x86_64                                                                                                               1/9
  正在更新    : 1:net-snmp-libs-5.7.2-49.el7_9.2.x86_64                                                                                                   2/9
  正在安装    : 1:net-snmp-agent-libs-5.7.2-49.el7_9.2.x86_64                                                                                             3/9
  正在安装    : keepalived-1.3.5-19.el7.x86_64                                                                                                            4/9
  正在更新    : ipset-7.1-1.el7.x86_64                                                                                                                    5/9
  正在安装    : ipvsadm-1.27-8.el7.x86_64                                                                                                                 6/9
  清理        : ipset-6.29-1.el7.x86_64                                                                                                                   7/9
  清理        : ipset-libs-6.29-1.el7.x86_64                                                                                                              8/9
  清理        : 1:net-snmp-libs-5.7.2-28.el7.x86_64                                                                                                       9/9
  验证中      : 1:net-snmp-libs-5.7.2-49.el7_9.2.x86_64                                                                                                   1/9
  验证中      : keepalived-1.3.5-19.el7.x86_64                                                                                                            2/9
  验证中      : ipset-7.1-1.el7.x86_64                                                                                                                    3/9
  验证中      : ipset-libs-7.1-1.el7.x86_64                                                                                                               4/9
  验证中      : ipvsadm-1.27-8.el7.x86_64                                                                                                                 5/9
  验证中      : 1:net-snmp-agent-libs-5.7.2-49.el7_9.2.x86_64                                                                                             6/9
  验证中      : ipset-libs-6.29-1.el7.x86_64                                                                                                              7/9
  验证中      : ipset-6.29-1.el7.x86_64                                                                                                                   8/9
  验证中      : 1:net-snmp-libs-5.7.2-28.el7.x86_64                                                                                                       9/9

已安装:
  ipvsadm.x86_64 0:1.27-8.el7                                                 keepalived.x86_64 0:1.3.5-19.el7

作为依赖被安装:
  net-snmp-agent-libs.x86_64 1:5.7.2-49.el7_9.2

作为依赖被升级:
  ipset.x86_64 0:7.1-1.el7                     ipset-libs.x86_64 0:7.1-1.el7                     net-snmp-libs.x86_64 1:5.7.2-49.el7_9.2

完毕!
[root@localhost ~]# modprobe ip_vs
[root@localhost ~]# cat /proc/net/ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port Forward Weight ActiveConn InActConn
[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# ls
keepalived.conf
[root@localhost keepalived]# vim keepalived.conf
......
global_defs {                       #定义全局参数
--10行--修改,邮件服务指向本地
    smtp_server 127.0.0.1
--12行--修改,指定服务器(路由器)的名称,主备服务器名称须不同,主为LVS_01,备为LVS_02
    router_id LVS_02
}
 
vrrp_instance VI_1 {                #定义VRRP热备实例参数
--20行--修改,指定热备状态,主为MASTER,备为BACKUP
    state BACKUP
--21行--修改,指定承载vip地址的物理接口
    interface ens32
--22行--修改,指定虚拟路由器的ID号,每个热备组保持一致 
    virtual_router_id 10
--23行--修改,指定优先级,数值越大优先级越高,主为100,备为90
    priority 90
    advert_int 1                    #通告间隔秒数(心跳频率)
    authentication {                #定义认证信息,每个热备组保持一致
        auth_type PASS              #认证类型
--27行--修改,指定验证密码,主备服务器保持一致
        auth_pass 123456
    }
    virtual_ipaddress {             #指定群集vip地址
        192.168.247.200
    }
}
--36行--修改,指定虚拟服务器地址(VIP)、端口,定义虚拟服务器和Web服务器池参数
virtual_server 192.168.247.200 80 {
    delay_loop 6                    #健康检查的间隔时间(秒)
    lb_algo rr                      #指定调度算法,轮询(rr)
--39行--修改,指定群集工作模式,直接路由(DR)
    lb_kind DR
    persistence_timeout 50          #连接保持时间(秒)
    protocol TCP                    #应用服务采用的是 TCP协议
--43行--修改,指定第一个Web节点的地址、端口
    real_server 192.168.247.135 80 {
        weight 1                    #节点的权重
--45行--删除,添加以下健康检查方式        
        TCP_CHECK {
            connect_port 80         #添加检查的目标端口
            connect_timeout 3       #添加连接超时(秒)
            nb_get_retry 3          #添加重试次数
            delay_before_retry 3    #添加重试间隔
        }
    }
 
    real_server 192.168.247.136 80 {     #添加第二个 Web节点的地址、端口
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
##删除后面多余的配置##
}
[root@localhost keepalived]# systemctl start keepalived
[root@localhost keepalived]# ip addr show dev ens32
2: ens32:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:8d:8a:35 brd ff:ff:ff:ff:ff:ff
    inet 192.168.247.133/24 brd 192.168.247.255 scope global ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::b7de:ddd:163c:8ed6/64 scope link
       valid_lft forever preferred_lft forever
[root@localhost keepalived]# ipvsadm-save > /etc/sysconfig/ipvsadm
[root@localhost keepalived]# systemctl start ipvsadm
[root@localhost keepalived]# ipvsadm -C
[root@localhost keepalived]# ipvsadm -A -t 192.168.247.200:80 -s rr
[root@localhost keepalived]# ipvsadm -a -t 192.168.247.200:80 -r 192.168.247.135:80 -g
[root@localhost keepalived]#
[root@localhost keepalived]# ipvsadm -a -t 192.168.247.200:80 -r 192.168.247.136:80 -g
[root@localhost keepalived]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.247.200:http rr
  -> 192.168.247.135:http         Route   1      0          0
  -> 192.168.247.136:http         Route   1      0          0
[root@localhost keepalived]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.247.200:80 rr
  -> 192.168.247.135:80           Route   1      0          0
  -> 192.168.247.136:80           Route   1      0          0
[root@localhost keepalived]# ipvsadm-save > /etc/sysconfig/ipvsadm
[root@localhost keepalived]# vim /etc/sysctl.conf
[root@localhost keepalived]# sysctl -p
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens32.send_redirects = 0

3.配置节点服务器192.168.247.135

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install httpd
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.ustc.edu.cn
 * extras: ftp.sjtu.edu.cn
 * updates: mirrors.ustc.edu.cn
base                                                                                                                                   | 3.6 kB  00:00:00
extras                                                                                                                                 | 2.9 kB  00:00:00
updates                                                                                                                                | 2.9 kB  00:00:00
(1/4): base/7/x86_64/group_gz                                                                                                          | 153 kB  00:00:00
(2/4): extras/7/x86_64/primary_db                                                                                                      | 249 kB  00:00:00
(3/4): base/7/x86_64/primary_db                                                                                                        | 6.1 MB  00:00:04
(4/4): updates/7/x86_64/primary_db                                                                                                     |  21 MB  00:00:13
正在解决依赖关系
--> 正在检查事务
---> 软件包 httpd.x86_64.0.2.4.6-99.el7.centos.1 将被 安装
--> 正在处理依赖关系 httpd-tools = 2.4.6-99.el7.centos.1,它被软件包 httpd-2.4.6-99.el7.centos.1.x86_64 需要
--> 正在处理依赖关系 /etc/mime.types,它被软件包 httpd-2.4.6-99.el7.centos.1.x86_64 需要
--> 正在检查事务
---> 软件包 httpd-tools.x86_64.0.2.4.6-99.el7.centos.1 将被 安装
---> 软件包 mailcap.noarch.0.2.1.41-2.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

==============================================================================================================================================================
 Package                              架构                            版本                                             源                                大小
==============================================================================================================================================================
正在安装:
 httpd                                x86_64                          2.4.6-99.el7.centos.1                            updates                          2.7 M
为依赖而安装:
 httpd-tools                          x86_64                          2.4.6-99.el7.centos.1                            updates                           94 k
 mailcap                              noarch                          2.1.41-2.el7                                     base                              31 k

事务概要
==============================================================================================================================================================
安装  1 软件包 (+2 依赖软件包)

总下载量:2.8 M
安装大小:9.6 M
Downloading packages:
警告:/var/cache/yum/x86_64/7/updates/packages/httpd-tools-2.4.6-99.el7.centos.1.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
httpd-tools-2.4.6-99.el7.centos.1.x86_64.rpm 的公钥尚未安装
(1/3): httpd-tools-2.4.6-99.el7.centos.1.x86_64.rpm                                                                                    |  94 kB  00:00:00
mailcap-2.1.41-2.el7.noarch.rpm 的公钥尚未安装
(2/3): mailcap-2.1.41-2.el7.noarch.rpm                                                                                                 |  31 kB  00:00:00
(3/3): httpd-2.4.6-99.el7.centos.1.x86_64.rpm                                                                                          | 2.7 MB  00:00:06
--------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                          432 kB/s | 2.8 MB  00:00:06
从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 检索密钥
导入 GPG key 0xF4A80EB5:
 用户ID     : "CentOS-7 Key (CentOS 7 Official Signing Key) "
 指纹       : 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 软件包     : centos-release-7-9.2009.0.el7.centos.x86_64 (@anaconda)
 来自       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : httpd-tools-2.4.6-99.el7.centos.1.x86_64                                                                                                  1/3
  正在安装    : mailcap-2.1.41-2.el7.noarch                                                                                                               2/3
  正在安装    : httpd-2.4.6-99.el7.centos.1.x86_64                                                                                                        3/3
  验证中      : httpd-2.4.6-99.el7.centos.1.x86_64                                                                                                        1/3
  验证中      : mailcap-2.1.41-2.el7.noarch                                                                                                               2/3
  验证中      : httpd-tools-2.4.6-99.el7.centos.1.x86_64                                                                                                  3/3

已安装:
  httpd.x86_64 0:2.4.6-99.el7.centos.1

作为依赖被安装:
  httpd-tools.x86_64 0:2.4.6-99.el7.centos.1                                           mailcap.noarch 0:2.1.41-2.el7

完毕!
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.247.200
NETMASK=255.255.255.255
[root@localhost ~]# service network restart
Restarting network (via systemctl):                        [  确定  ]
[root@localhost ~]# ifup lo:0
[root@localhost ~]# ifconfig lo:0
lo:0: flags=73  mtu 65536
        inet 192.168.247.200  netmask 255.255.255.255
        loop  txqueuelen 1000  (Local Loopback)

[root@localhost ~]# route add -host 192.168.247.200 dev lo:0
[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@localhost ~]# sysctl -p
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@localhost ~]# echo "xiaowu like sleep" > /var/www/html/index.html
[root@localhost ~]# cat /var/www/html/index.html
xiaowu like sleep

4.配置节点服务器2192.168.247.136

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install httpd
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.ustc.edu.cn
 * extras: ftp.sjtu.edu.cn
 * updates: mirrors.ustc.edu.cn
base                                                                                                                                   | 3.6 kB  00:00:00
extras                                                                                                                                 | 2.9 kB  00:00:00
nginx-stable                                                                                                                           | 2.9 kB  00:00:00
updates                                                                                                                                | 2.9 kB  00:00:00
正在解决依赖关系
--> 正在检查事务
---> 软件包 httpd.x86_64.0.2.4.6-99.el7.centos.1 将被 安装
--> 正在处理依赖关系 httpd-tools = 2.4.6-99.el7.centos.1,它被软件包 httpd-2.4.6-99.el7.centos.1.x86_64 需要
--> 正在处理依赖关系 /etc/mime.types,它被软件包 httpd-2.4.6-99.el7.centos.1.x86_64 需要
--> 正在检查事务
---> 软件包 httpd-tools.x86_64.0.2.4.6-99.el7.centos.1 将被 安装
---> 软件包 mailcap.noarch.0.2.1.41-2.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

==============================================================================================================================================================
 Package                              架构                            版本                                             源                                大小
==============================================================================================================================================================
正在安装:
 httpd                                x86_64                          2.4.6-99.el7.centos.1                            updates                          2.7 M
为依赖而安装:
 httpd-tools                          x86_64                          2.4.6-99.el7.centos.1                            updates                           94 k
 mailcap                              noarch                          2.1.41-2.el7                                     base                              31 k

事务概要
==============================================================================================================================================================
安装  1 软件包 (+2 依赖软件包)

总下载量:2.8 M
安装大小:9.6 M
Downloading packages:
(1/3): mailcap-2.1.41-2.el7.noarch.rpm                                                                                                 |  31 kB  00:00:00
(2/3): httpd-tools-2.4.6-99.el7.centos.1.x86_64.rpm                                                                                    |  94 kB  00:00:00
(3/3): httpd-2.4.6-99.el7.centos.1.x86_64.rpm                                                                                          | 2.7 MB  00:00:00
--------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                          4.2 MB/s | 2.8 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告:RPM 数据库已被非 yum 程序修改。
  正在安装    : httpd-tools-2.4.6-99.el7.centos.1.x86_64                                                                                                  1/3
  正在安装    : mailcap-2.1.41-2.el7.noarch                                                                                                               2/3
  正在安装    : httpd-2.4.6-99.el7.centos.1.x86_64                                                                                                        3/3
  验证中      : httpd-2.4.6-99.el7.centos.1.x86_64                                                                                                        1/3
  验证中      : mailcap-2.1.41-2.el7.noarch                                                                                                               2/3
  验证中      : httpd-tools-2.4.6-99.el7.centos.1.x86_64                                                                                                  3/3

已安装:
  httpd.x86_64 0:2.4.6-99.el7.centos.1

作为依赖被安装:
  httpd-tools.x86_64 0:2.4.6-99.el7.centos.1                                           mailcap.noarch 0:2.1.41-2.el7

完毕!
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# echo "xiaowu like food" > /var/www/html/index.html
[root@localhost ~]# cat /var/www/html/index.html
xiaowu like food

5.测试验证

  • 在客户端访问 http://192.168.247.188/
  • 再在主服务器关闭 keepalived 服务后再测试:systemctl stop keepalived

6.Keepalived相关问题

  • Keepalived通过什么判断哪台主机为主服务器,通过什么方式配置浮动IP?
    答案:
    Keepalived首先做初始化先检查state状态,master为主服务器,backup为备服务器。
    然后再对比所有服务器的priority,谁的优先级高谁是最终的主服务器。
    优先级高的服务器会通过ip命令为自己的电脑配置一个提前定义好的浮动IP地址。

  • keepalived的抢占与非抢占模式:
    抢占模式即MASTER从故障中恢复后,会将VIP从BACKUP节点中抢占过来。非抢占模式即MASTER恢复后不抢占BACKUP升级为MASTER后的VIP
    非抢占式俩节点state必须为bakcup,且必须配置nopreempt。
    注意:这样配置后,我们要注意启动服务的顺序,优先启动的获取master权限,与优先级没有关系了。

你可能感兴趣的:(NGINX,服务器,运维,linux)