在 RHEL/CentOS 系统上使用 YUM history 命令回滚升级操作

为服务器打补丁是 Linux 系统管理员的一项重要任务,为的是让系统更加稳定,性能更加优化。厂商经常会发布一些安全/高危的补丁包,相关软件需要升级以防范潜在的安全风险。

Yum (Yellowdog Update Modified) 是 CentOS 和 RedHat 系统上用的 RPM 包管理工具,yum history 命令允许系统管理员将系统回滚到上一个状态,但由于某些限制,回滚不是在所有情况下都能成功,有时 yum 命令可能什么都不做,有时可能会删掉一些其他的包。

我建议你在升级之前还是要做一个完整的系统备份,而 yum history 并不能用来替代系统备份的。系统备份能让你将系统还原到任意时候的节点状态。

某些情况下,安装的应用程序在升级了补丁之后不能正常工作或者出现一些错误(可能是由于库不兼容或者软件包升级导致的),那该怎么办呢?

与应用开发团队沟通,并找出导致库和软件包的问题所在,然后使用 yum history 命令进行回滚。

注意:

  • 它不支持回滚 selinux,selinux-policy-*,kernel,glibc (以及依赖 glibc 的包,比如 gcc)。
  • 不建议将系统降级到更低的版本(比如 CentOS 6.9 降到 CentOS 6.8),这会导致系统处于不稳定的状态
    让我们先来看看系统上有哪些包可以升级,然后挑选出一些包来做实验。
#yum update
Loaded plugins: fastestmirror, security
Setting up UpdateProcess
Loading mirror speeds from cached hostfile
epel/metalink |12 kB 00:00
* epel: mirror.csclub.uwaterloo.ca
base |3.7 kB 00:00
dockerrepo |2.9 kB 00:00
draios |2.9 kB 00:00
draios/primary_db |13 kB 00:00
epel |4.3 kB 00:00
epel/primary_db |5.9 MB 00:00
extras |3.4 kB 00:00
updates |3.4 kB 00:00
updates/primary_db |2.5 MB 00:00
ResolvingDependencies
-->Running transaction check
--->Packagegit.x86_64 0:1.7.1-8.el6 will be updated
--->Packagegit.x86_64 0:1.7.1-9.el6_9 will be an update
--->Package httpd.x86_64 0:2.2.15-60.el6.centos.4 will be updated
--->Package httpd.x86_64 0:2.2.15-60.el6.centos.5 will be an update
--->Package httpd-tools.x86_64 0:2.2.15-60.el6.centos.4 will be updated
--->Package httpd-tools.x86_64 0:2.2.15-60.el6.centos.5 will be an update
--->Package perl-Git.noarch 0:1.7.1-8.el6 will be updated
--->Package perl-Git.noarch 0:1.7.1-9.el6_9 will be an update
-->FinishedDependencyResolution
DependenciesResolved
=================================================================================================
PackageArchVersionRepositorySize
=================================================================================================
Updating:
git x86_64 1.7.1-9.el6_9 updates 4.6 M
httpd x86_64 2.2.15-60.el6.centos.5 updates 836 k
httpd-tools x86_64 2.2.15-60.el6.centos.5 updates 80 k
perl-Git noarch 1.7.1-9.el6_9 updates 29 k
TransactionSummary
=================================================================================================
Upgrade4Package(s)
Total download size:5.5 M
Isthis ok [y/N]: n

你会发现 git 包可以被升级,那我们就用它来实验吧。运行下面命令获得软件包的版本信息(当前安装的版本和可以升级的版本)。

#yumlistgit
Loaded plugins: fastestmirror, security
Setting up UpdateProcess
Loading mirror speeds from cached hostfile
* epel: mirror.csclub.uwaterloo.ca
InstalledPackages
git.x86_64 1.7.1-8.el6@base
AvailablePackages
git.x86_64 1.7.1-9.el6_9 updates

运行下面命令来将 git 从 1.7.1-8 升级到 1.7.1-9。

#yum update git
Loaded plugins: fastestmirror, presto
Setting up UpdateProcess
Loading mirror speeds from cached hostfile
* base: repos.lax.quadranet.com
* epel: Fedora.mirrors.pair.com
* extras: mirrors.seas.harvard.edu
* updates: mirror.sesp.northwestern.edu
ResolvingDependencies
-->Running transaction check
--->Packagegit.x86_64 0:1.7.1-8.el6 will be updated
-->ProcessingDependency:git=1.7.1-8.el6forpackage: perl-Git-1.7.1-8.el6.noarch
--->Packagegit.x86_64 0:1.7.1-9.el6_9 will be an update
-->Running transaction check
--->Package perl-Git.noarch 0:1.7.1-8.el6 will be updated
--->Package perl-Git.noarch 0:1.7.1-9.el6_9 will be an update
-->FinishedDependencyResolution
DependenciesResolved
=================================================================================================
PackageArchVersionRepositorySize
=================================================================================================
Updating:
git x86_64 1.7.1-9.el6_9 updates 4.6 M
Updatingfor dependencies:
perl-Git noarch 1.7.1-9.el6_9 updates 29 k
TransactionSummary
=================================================================================================
Upgrade2Package(s)
Total download size:4.6 M
Isthis ok [y/N]: y
DownloadingPackages:
Setting up and reading Presto delta metadata
Processing delta metadata
Package(s) data still to download:4.6 M
(1/2):git-1.7.1-9.el6_9.x86_64.rpm |4.6 MB 00:00
(2/2): perl-Git-1.7.1-9.el6_9.noarch.rpm |29 kB 00:00
-------------------------------------------------------------------------------------------------
Total5.8 MB/s |4.6 MB 00:00
Running rpm_check_debug
RunningTransactionTest
TransactionTestSucceeded
RunningTransaction
Updating: perl-Git-1.7.1-9.el6_9.noarch1/4
Updating:git-1.7.1-9.el6_9.x86_64 2/4
Cleanup: perl-Git-1.7.1-8.el6.noarch3/4
Cleanup:git-1.7.1-8.el6.x86_64 4/4
Verifying:git-1.7.1-9.el6_9.x86_64 1/4
Verifying: perl-Git-1.7.1-9.el6_9.noarch2/4
Verifying:git-1.7.1-8.el6.x86_64 3/4
Verifying: perl-Git-1.7.1-8.el6.noarch4/4
Updated:
git.x86_64 0:1.7.1-9.el6_9
DependencyUpdated:
perl-Git.noarch 0:1.7.1-9.el6_9
Complete!

验证升级后的 git 版本.

#yumlistgit
InstalledPackages
git.x86_64 1.7.1-9.el6_9@updates
或
# rpm -q git
git-1.7.1-9.el6_9.x86_64

现在我们成功升级这个软件包,可以对它进行回滚了。步骤如下。

使用 YUM history 命令回滚升级操作

首先,使用下面命令获取 yum 操作的 id。下面的输出很清晰地列出了所有需要的信息,例如操作 id、谁做的这个操作(用户名)、操作日期和时间、操作的动作(安装还是升级)、操作影响的包数量。

#yum history
或
#yum history list all
Loaded plugins: fastestmirror, presto
ID |Login user |Dateandtime|Action(s)|Altered
-------------------------------------------------------------------------------
13| root |2017-08-1813:30|Update|2
12| root |2017-08-1007:46|Install|1
11| root |2017-07-2817:10| E, I, U |28 EE
10| root |2017-04-2109:16| E, I, U |162 EE
9| root |2017-02-0917:09| E, I, U |20 EE
8| root |2017-02-0210:45|Install|1
7| root |2016-12-1506:48|Update|1
6| root |2016-12-1506:43|Install|1
5| root |2016-12-0210:28| E, I, U |23 EE
4| root |2016-10-2805:37| E, I, U |13 EE
3| root |2016-10-1812:53|Install|1
2| root |2016-09-3010:28| E, I, U |31 EE
1| root |2016-07-2611:40| E, I, U |160 EE

上面命令显示有两个包受到了影响,因为 git 还升级了它的依赖包 perl-Git。 运行下面命令来查看关于操作的详细信息。

#yum history info13
Loaded plugins: fastestmirror, presto
Transaction ID :13
Begintime:FriAug1813:30:522017
Begin rpmdb :420:f5c5f9184f44cf317de64d3a35199e894ad71188
Endtime:13:30:542017(2 seconds)
End rpmdb :420:d04a95c25d4526ef87598f0dcaec66d3f99b98d4
User: root
Return-Code:Success
CommandLine: update git
Transaction performed with:
Installed rpm-4.8.0-55.el6.x86_64 @base
Installedyum-3.2.29-81.el6.centos.noarch @base
Installedyum-plugin-fastestmirror-1.1.30-40.el6.noarch@base
Installedyum-presto-0.6.2-1.el6.noarch@anaconda-CentOS-201207061011.x86_64/6.3
PackagesAltered:
Updatedgit-1.7.1-8.el6.x86_64 @base
Update1.7.1-9.el6_9.x86_64 @updates
Updated perl-Git-1.7.1-8.el6.noarch@base
Update1.7.1-9.el6_9.noarch@updates
history info

运行下面命令来回滚 git 包到上一个版本。

#yum history undo 13
Loaded plugins: fastestmirror, presto
Undoing transaction 53,fromFriAug1813:30:522017
Updatedgit-1.7.1-8.el6.x86_64 @base
Update1.7.1-9.el6_9.x86_64 @updates
Updated perl-Git-1.7.1-8.el6.noarch@base
Update1.7.1-9.el6_9.noarch@updates
Loading mirror speeds from cached hostfile
* base: repos.lax.quadranet.com
* epel: fedora.mirrors.pair.com
* extras: repo1.dal.innoscale.net
* updates: mirror.vtti.vt.edu
ResolvingDependencies
-->Running transaction check
--->Packagegit.x86_64 0:1.7.1-8.el6 will be a downgrade
--->Packagegit.x86_64 0:1.7.1-9.el6_9 will be erased
--->Package perl-Git.noarch 0:1.7.1-8.el6 will be a downgrade
--->Package perl-Git.noarch 0:1.7.1-9.el6_9 will be erased
-->FinishedDependencyResolution
DependenciesResolved
=================================================================================================
PackageArchVersionRepositorySize
=================================================================================================
Downgrading:
git x86_64 1.7.1-8.el6 base 4.6 M
perl-Git noarch 1.7.1-8.el6 base 29 k
TransactionSummary
=================================================================================================
Downgrade2Package(s)
Total download size:4.6 M
Isthis ok [y/N]: y
DownloadingPackages:
Setting up and reading Presto delta metadata
Processing delta metadata
Package(s) data still to download:4.6 M
(1/2):git-1.7.1-8.el6.x86_64.rpm |4.6 MB 00:00
(2/2): perl-Git-1.7.1-8.el6.noarch.rpm |29 kB 00:00
-------------------------------------------------------------------------------------------------
Total3.4 MB/s |4.6 MB 00:01
Running rpm_check_debug
RunningTransactionTest
TransactionTestSucceeded
RunningTransaction
Installing: perl-Git-1.7.1-8.el6.noarch1/4
Installing:git-1.7.1-8.el6.x86_64 2/4
Cleanup: perl-Git-1.7.1-9.el6_9.noarch3/4
Cleanup:git-1.7.1-9.el6_9.x86_64 4/4
Verifying:git-1.7.1-8.el6.x86_64 1/4
Verifying: perl-Git-1.7.1-8.el6.noarch2/4
Verifying:git-1.7.1-9.el6_9.x86_64 3/4
Verifying: perl-Git-1.7.1-9.el6_9.noarch4/4
Removed:
git.x86_64 0:1.7.1-9.el6_9 perl-Git.noarch 0:1.7.1-9.el6_9
Installed:
git.x86_64 0:1.7.1-8.el6 perl-Git.noarch 0:1.7.1-8.el6
Complete!

回滚后,使用下面命令来检查降级包的版本。

#yumlistgit
或
# rpm -q git
git-1.7.1-8.el6.x86_64

使用YUM downgrade 命令回滚升级

此外,我们也可以使用 YUM downgrade 命令回滚升级。

#yum downgrade git-1.7.1-8.el6 perl-Git-1.7.1-8.el6
Loaded plugins: search-disabled-repos, security, ulninfo
Setting up DowngradeProcess
ResolvingDependencies
-->Running transaction check
--->Packagegit.x86_64 0:1.7.1-8.el6 will be a downgrade
--->Packagegit.x86_64 0:1.7.1-9.el6_9 will be erased
--->Package perl-Git.noarch 0:1.7.1-8.el6 will be a downgrade
--->Package perl-Git.noarch 0:1.7.1-9.el6_9 will be erased
-->FinishedDependencyResolution
DependenciesResolved
=================================================================================================
PackageArchVersionRepositorySize
=================================================================================================
Downgrading:
git x86_64 1.7.1-8.el6 base 4.6 M
perl-Git noarch 1.7.1-8.el6 base 29 k
TransactionSummary
=================================================================================================
Downgrade2Package(s)
Total download size:4.6 M
Isthis ok [y/N]: y
DownloadingPackages:
(1/2):git-1.7.1-8.el6.x86_64.rpm |4.6 MB 00:00
(2/2): perl-Git-1.7.1-8.el6.noarch.rpm |28 kB 00:00
-------------------------------------------------------------------------------------------------
Total3.7 MB/s |4.6 MB 00:01
Running rpm_check_debug
RunningTransactionTest
TransactionTestSucceeded
RunningTransaction
Installing: perl-Git-1.7.1-8.el6.noarch1/4
Installing:git-1.7.1-8.el6.x86_64 2/4
Cleanup: perl-Git-1.7.1-9.el6_9.noarch3/4
Cleanup:git-1.7.1-9.el6_9.x86_64 4/4
Verifying:git-1.7.1-8.el6.x86_64 1/4
Verifying: perl-Git-1.7.1-8.el6.noarch2/4
Verifying:git-1.7.1-9.el6_9.x86_64 3/4
Verifying: perl-Git-1.7.1-9.el6_9.noarch4/4
Removed:
git.x86_64 0:1.7.1-9.el6_9 perl-Git.noarch 0:1.7.1-9.el6_9
Installed:
git.x86_64 0:1.7.1-8.el6 perl-Git.noarch 0:1.7.1-8.el6
Complete!

注意: 你也需要降级依赖包,否则它会删掉当前版本的依赖包而不是对依赖包做降级,因为 downgrade 命令无法处理依赖关系。

至于 Fedora 用户

命令是一样的,只需要将包管理器名称从 yum 改成 dnf 就行了。

# dnf listgit
# dnf history
# dnf history info
# dnf history undo
# dnf listgit
# dnf downgrade git-1.7.1-8.el6 perl-Git-1.7.1-8.el6

RHEL6 搭建LVS/NAT 负载均衡集群 案例

实验拓扑图:

未分类

操作流程:

Director Server : 192.168.4.50 pc50
安装并启用ipvsadm
创建虚拟服务器
向虚拟服务器上加入节点

Real Server : 192.168.4.51 pc51 192.168.4.52 pc52
配置WEB 服务器

Clinet : 192.168.2.253 pc253
连接虚拟服务器测试

具体步骤:

环境准备:

配置yum源

# service iptables stop            //关闭防火墙
# chkconfig iptables off            //关闭开机自启
# setenforce 0                            //设置SELinux 为宽松模式

网站服务器 pc51 / pc52 :

# yum -y install httpd
[root@pc51 ~]# echo '192.168.4.51' > /var/www/html/test.html
[root@pc52 ~]# echo "192.168.4.52" > /var/www/html/test.html
# service httpd start
# chkconfig httpd on
# yum -y install elinks
[root@pc51 ~]# elinks --dump http://localhost/test.html
  192.168.4.51
[root@pc52 ~]# elinks --dump http://localhost/test.html
  192.168.4.52

配置分发器 pc50:

# mount /dev/cdrom /mnt/
//安装 ipvsadm   rpm 包在光盘挂载文件下的LoadBalancer目录下
#cd /mnt/LoadBalancer/
#yum -y install ipvsadm-1.26-4.el6.x86_64.rpm 
//开启内核的路由转发功能
# sed -i '7s/0/1/' /etc/sysctl.conf
# sed -n '7p' /etc/sysctl.conf 
net.ipv4.ip_forward = 1

网站服务器 pc51 / pc52 :

指定网关地址 192.168.4.50

# route -n//查看路由
# route add default gw 192.168.4.50//临时配置网关 网卡重启后生效
//永久配置网关
# vim /etc/sysconfig/network-scripts/ifcfg-eth0
# sed -n '7p' /etc/sysconfig/network-scripts/ifcfg-eth0
GATEWAY=192.168.4.50
# ifdown eth0 ; ifup eth0      //重新加载网卡

客户端 192.168.2.253 配置

指定网关地址 192.168.2.50 :

# vim /etc/sysconfig/network-scripts/ifcfg-eth1
# sed -n '7p' /etc/sysconfig/network-scripts/ifcfg-eth1
GATEWAY=192.168.2.50
# ifdown eth1 ; ifup eth1
# ping -c 2 192.168.4.51
PING 192.168.4.51 (192.168.4.51) 56(84) bytes of data.
64 bytes from 192.168.4.51: icmp_seq=1 ttl=63 time=0.322 ms
64 bytes from 192.168.4.51: icmp_seq=2 ttl=63 time=0.503 ms

--- 192.168.4.51 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.322/0.412/0.503/0.092 ms
# yum -y install elinks

配置分发器 pc50 :

# yum -y install ipvsadm-1.26-4.el6.x86_64.rpm
# rpm -q ipvsadm
ipvsadm-1.26-4.el6.x86_64

添加虚拟服务

        # ipvsadm -L  //查看 IPVS
        IP Virtual Server version 1.2.1 (size=4096)
        Prot LocalAddress:Port Scheduler Flags
          -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
        # ipvsadm -A -t 192.168.2.50:80 -s rr//添加虚拟服务 调度算法为Round Robin
        # ipvsadm -L
        IP Virtual Server version 1.2.1 (size=4096)
        Prot LocalAddress:Port Scheduler Flags
          -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
        TCP  192.168.2.50:http rr
        # ipvsadm -Ln   //- n  数字显示
        IP Virtual Server version 1.2.1 (size=4096)
        Prot LocalAddress:Port Scheduler Flags
          -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
        TCP  192.168.2.50:80 rr
                //向虚拟服务器中加入节点
        # ipvsadm -a -t 192.168.2.50:80 -r 192.168.4.51:80 -m
        # ipvsadm -a -t 192.168.2.50:80 -r 192.168.4.52:80 -m
        # 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.2.50:80 rr
          -> 192.168.4.51:80              Masq    1      0          0         
          -> 192.168.4.52:80              Masq    1      0          0   
        # /etc/init.d/ipvsadm save//使配置永久生效
        ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm:      [确定]
        # cat /etc/sysconfig/ipvsadm
        -A -t 192.168.2.50:80 -s rr
        -a -t 192.168.2.50:80 -r 192.168.4.51:80 -m -w 1
        -a -t 192.168.2.50:80 -r 192.168.4.52:80 -m -w 1

客户端测试

        # elinks --dump http://192.168.2.50/test.html
           192.168.4.51
        # elinks --dump http://192.168.2.50/test.html
           192.168.4.52
        //客户端 轮询到不同的后端真实服务器        

        [root@pc50 ~]# ipvsadm -Ln --stats
        IP Virtual Server version 1.2.1 (size=4096)
        Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
          -> RemoteAddress:Port
        TCP  192.168.2.50:80                 2       10       10      846     1098
          -> 192.168.4.51:80                     1        5        5        423      549
          -> 192.168.4.52:80                     1        5        5        423      549

        模拟pc51 web服务故障:
        [root@pc51 ~]# service httpd stop
        [root@pc50 ~]# ipvsadm -Z

         //客户端测试
        # elinks --dump http://192.168.2.50/test.html
           192.168.4.52
        # elinks --dump http://192.168.2.50/test.html
           192.168.4.52

        [root@pc50 ~]# ipvsadm -Ln --stats
        IP Virtual Server version 1.2.1 (size=4096)
        Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
          -> RemoteAddress:Port
        TCP  192.168.2.50:80                 3       11       11      906     1138
          -> 192.168.4.51:80                     1        1        1         60       40
          -> 192.168.4.52:80                     2       10       10      846     1098

会发现 LVS/NAT 单点故障时 并不能健康性检查

可以 编写一个脚本 监测两台Real Server 的服务 是否正常 如果监测到故障 将对应的服务在调度服务器 停掉

使用周期性计划任务 定时运行监测脚本 到达 健康检查的目的

RHEL6 搭建 keepalived + lvs/DR 集群

使用Keepalived为LVS调度器提供高可用功能,防止调度器单点故障,为用户提供Web服务:

  • LVS1调度器真实IP地址为192.168.4.50
  • LVS2调度器真实IP地址为192.168.4.55
  • 服务器VIP地址设置为192.168.4.252
  • 真实Web服务器地址分别为192.168.4.51、192.168.4.52

实验拓扑图:

未分类

实验步骤:

实验准备:

配置yum源

# service iptables stop            //关闭防火墙
# chkconfig iptables off            //关闭开机自启
# setenforce 0                            //设置SELinux 为宽松模式 

配置WEB服务器 pc51 / pc52

#yum -y install httpd  
#service httpd start 
#chkconfig httpd on
[root@pc51 ~] #echo " 192.168.4.51  " > /var/www/html/test.html
[root@pc52 ~] #echo " 192.168.4.52 " > /var/www/html/test.html

本次实验有些步骤就不详细介绍了,具体有关 keepalived 和 ipvsadm 的相关配置 可以参考

keepalived 配置高可用集群 : http://blog.51cto.com/13558754/2060950

ipvsadm 配置LVS/DR 负载均衡集群:http://blog.51cto.com/13558754/2060405

1、在web服务上 配置 VIP地址 pc51 / pc52

# ifconfig lo:1 192.168.4.252/32    //只拥有ip 就可以
# ifconfig lo:1
lo:1      Link encap:Local Loopback  
         inet addr:192.168.4.252  Mask:0.0.0.0
         UP LOOPBACK RUNNING  MTU:65536  Metric:1
# cd /proc/sys/net/ipv4/conf/
# echo 1 > lo/arp_ignore
# echo 2 > lo/arp_announce 
# echo 1 > all/arp_ignore 
# echo 2 > all/arp_announce 

2、配置分发器 50(主) 55(备) 分别安装keepalived软件 装包 ipvsadm

# rpm -q ipvsadm keepalived
ipvsadm-1.26-4.el6.x86_64
keepalived-1.2.13-5.el6_6.x86_64

3、修改配置文件

[root@pc50 ~]# vim /etc/keepalived/keepalived.conf 
 vrrp_instance VI_1 {
      state MASTER                 // 描述信息  MASTER为主服务器
      interface eth0                 // 定义网络接口
      virtual_router_id 51           //主 备VRID号必须一致 
      priority 150                   //服务器优先级
      advert_int 1
      authentication {
          auth_type PASS           //验证方式
          auth_pass 1111           //验证密码     主  备服务器密码必须一致 
      }
      virtual_ipaddress {
          192.168.4.252            //VIP地址
      }   
  }

  virtual_server 192.168.4.252 80 {        //配置 VIP为192.168.0.252  80 端口
      delay_loop 6
      lb_algo rr                            //设置LVS调度算法为RR
      lb_kind DR                            //设置LVS的模式为DR
      nat_mask 255.255.255.0
      persistence_timeout 50
      protocol TCP
      connect_timeout 3
      nb_get_retry 3
      delay_before_retry 3

     real_server 192.168.4.51 80 {
          weight 1                        //设置权重为1
     }   
     real_server 192.168.4.52 80 {
          weight 1                        //设置权重为1
      }   

  }

使用第一个虚拟服务的模版

其余的都删除

主机55

[root@pc55 ~]# vim /etc/keepalived/keepalived.conf 
  vrrp_instance VI_1 {
      state BACKUP                // 描述信息 BACKUP为备用服务器
      interface eth0
      virtual_router_id 51
      priority 100
      advert_int 1
      authentication {
          auth_type PASS
          auth_pass 1111
      }
      virtual_ipaddress {
          192.168.4.252
      }
  }

  virtual_server 192.168.4.252 80 {
      delay_loop 6
      lb_algo rr
      lb_kind DR
      nat_mask 255.255.255.0
      persistence_timeout 50
      protocol TCP
      connect_timeout 3
      nb_get_retry 3
      delay_before_retry 3

      real_server 192.168.4.51 80 {
          weight 1
      }
      real_server 192.168.4.52 80 {
          weight 1
      }
  }

4、启动服务

    # service keepalived start
    [root@pc50 ~]# 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.4.252:80 rr persistent 50
      -> 192.168.4.51:80              Route   1      0          0         
      -> 192.168.4.52:80              Route   1      0          0     
    [root@pc50 ~]# ip addr show | grep 192.168.4
        inet 192.168.4.50/24 brd 192.168.4.255 scope global eth0
        inet 192.168.4.252/32 scope global eth0

    [root@pc55 ~]# ipvsadm -Ln --stats
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
      -> RemoteAddress:Port
    TCP  192.168.4.252:80               0        0        0        0        0
      -> 192.168.4.51:80                     0        0        0        0        0
      -> 192.168.4.52:80                     0        0        0        0        0
    [root@pc55 ~]# ip addr show | grep 192.168.4
        inet 192.168.4.55/24 brd 192.168.4.255 scope global eth0

5、客户端访问

# elinks --dump 192.168.4.252
   192.168.4.52
[root@room1pc32 桌面]# elinks --dump 192.168.4.252
   192.168.4.51
[root@room1pc32 桌面]# elinks --dump 192.168.4.252
   192.168.4.52
[root@room1pc32 桌面]# elinks --dump 192.168.4.252
   192.168.4.51
[root@room1pc32 桌面]# elinks --dump 192.168.4.252
   192.168.4.52

# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  192.168.4.252:80               5       25        0     2075        0
  -> 192.168.4.51:80                     2       10        0      830        0
  -> 192.168.4.52:80                     3       15        0     1245        0

[root@pc55 ~]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  192.168.4.252:80               0        0        0        0        0
  -> 192.168.4.51:80                     0        0        0        0        0
  -> 192.168.4.52:80                     0        0        0        0        0

模拟50 故障 验证Keepalived 高可用

[root@pc50 ~]#  service keepalived stop

[root@pc50 ~]# ip addr show | grep 192.168.4
    inet 192.168.4.50/24 brd 192.168.4.255 scope global eth0

[root@pc55 ~]# ip addr show | grep 192.168.4
    inet 192.168.4.55/24 brd 192.168.4.255 scope global eth0
    inet 192.168.4.252/32 scope global eth0

客户端访问

# elinks --dump 192.168.4.252
   192.168.4.52
# elinks --dump 192.168.4.252
   192.168.4.51
# elinks --dump 192.168.4.252
   192.168.4.52
# elinks --dump 192.168.4.252
   192.168.4.51
# elinks --dump 192.168.4.252
   192.168.4.52

# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  192.168.4.252:80               5       25        0     2075        0
  -> 192.168.4.51:80                     2       10        0      830        0
  -> 192.168.4.52:80                     3       15        0     1245        0

将安装了CentOS/RHEL 6/7的机器转变成路由器

在本文中,我们将学习通过使用 NAT 技术将安装有 RHEL/CentOS 6 & 7 的机器转变成路由器来用。 我们都知道,路由器是一个工作在第三层的网络设备,用于将两个或多个网络连接在一起,即,将局域网连接上广域网上或者局域网直接互联。 路由器非常昂贵,尤其对于小型组织来说更是如此,这可能是我们关注路由器的一个原因。 与其使用专用硬件,不如让我们用 Linux 机器转换成路由器来用。
RHEL/CentOS 6 和 7 上的操作过程我们都会讲。但在开始之前, 让我们先看看需要准备那些东西。

前期条件

1、一台装有 RHEL/CentOS 6 或 7 的机器

2、两块分别配有本地 IP 和外网 IP 的网卡

我们需要为两个网卡都分配 IP 地址,一个本地网络的 IP(由我们的网络管理员提供),另一个是互联网 IP(由 ISP 提供)。 像这样:

Ifcfg-en0s3 192.168.1.1 (LAN IP address)
Ifcfg-en0s5 10.1.1.1  (WAN IP address)

注意 不同 Linux 发行版的网卡名是不一样的。

现在准备工作完成了,可以进行配置了。

步骤 1 启用 IP 转发

第一步,我们启用 IP 转发。 这一步在 RHEL/CentOS 6 和 7 上是相同的。 运行

$ sysctl -w net.ipv4.ip_forward=1

但是这样会在系统重启后恢复。要让重启后依然生效需要打开

$ vi /etc/sysctl.conf

然后输入下面内容,

net.ipv4.ip_forward = 1

保存并退出。现在系统就启用 IP 转发了。

步骤 2 配置 IPtables/Firewalld 的规则

下一步我们需要启动 IPtables/firewalld 服务并配置 NAT 规则,

$ systemctl start firewalld (For Centos/RHEL 7)
$ service iptables start  (For Centos/RHEL 6)

然后运行下面命令来配置防火墙的 NAT 规则:

CentOS/RHEL 6
$ iptables -t nat -A POSTROUTING -o XXXX -j MASQUERADE
$ service iptables restart 
CentOS/RHEL 7
$ firewall-cmd  -permanent -direct -passthrough ipv4 -t nat -I POSTROUTING -o XXXX -j MASQUERADE -s 192.168.1.0/24
$ systemctl restart firewalld

这里,XXXX 是配置有外网 IP 的那个网卡名称。 这就将 Linux 机器配置成了路由器了, 下面我们就可以配置客户端然后测试路由器了。

步骤 3 配置客户端

要测试路由器,我们需要在客户端的网关设置成内网 IP, 本例中就是 192.168.1.1。 因此不管客户机是 Windows 还是 Linux, 请先确保网关是 192.168.1.1。 完成后, 打开终端或命令行并 ping 一个网站来测试客户端是否能访问互联网了:

$ ping google.com

我们也可以通过网络浏览器访问网站的方式来检查。