1. 环境准备

1. VMware;
2. 4台CentOs7虚拟主机:192.168.122.248,192.168.122.68, 192.168.122.110, 192.168.122.167
3. 系统服务:LVS, Keepalived
4. Web服务器:nginx
5. 集群搭建:LVS DR模式

2. 软件安装

在四台虚拟机上,我们以如下方式搭建集群:

192.168.122.248 lvs+keepalived-1
192.168.122.68   lvs+keepalived-2
192.168.122.110 nginx-1
192.168.122.167 nginx-2

这里我们使用192.168.122.248和192.168.122.68两台机器作为lvs+keepalived的工作机器,也就是说这两台机器的作用主要是进行负载均衡和故障检测和下线的;我们使用192.168.122.110和192.168.122.167两台机器作为应用服务器,主要是对外提供服务的。这四台服务器作为整个后端集群服务,并且对外提供的虚拟ip是192.168.122.120。需要说明的是,这里的keepalived所检测的服务是两台lvs服务器,这两台服务器,一台作为master服务器,一台作为backup服务器,两者在负载均衡的配置上是完全一样的。在正常情况下,客户端请求虚拟ip的时候,lvs会将该请求转发到master服务器上,然后master服务器根据配置的负载均衡策略选择一台应用服务器,并且将请求发送给该应用服务器进行处理。如果在某个时刻,lvs的master服务器由于故障宕机了,keepalived就会检测到该故障,并且进行故障下线,然后将backup机器上线用于提供服务,从而实现故障转移的功能。

 

2.1 lvs+keepalived安装

在192.168.122.248和192.168.122.68上安装ipvs和keepalived:

[root@lvskeepalived- ~]# yum install ipvsadm keepalived -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB ::
epel | 5.3 kB ::
extras | 3.4 kB ::
updates | 3.4 kB ::
(/): epel/x86_64/updateinfo | kB ::
(/): epel/x86_64/primary_db | 6.8 MB ::
Package ipvsadm-1.27-.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package keepalived.x86_64 :1.3.-.el7_6. will be installed
--> Processing Dependency: libnetsnmpmibs.so.()(64bit) for package: keepalived-1.3.-.el7_6..x86_64
--> Processing Dependency: libnetsnmpagent.so.()(64bit) for package: keepalived-1.3.-.el7_6..x86_64
--> Processing Dependency: libnetsnmp.so.()(64bit) for package: keepalived-1.3.-.el7_6..x86_64
--> Running transaction check
---> Package net-snmp-agent-libs.x86_64 :5.7.-.el7_6. will be installed
---> Package net-snmp-libs.x86_64 :5.7.-.el7_6. will be installed
--> Finished Dependency Resolution Dependencies Resolved ===================================================================================================================================================================================================
Package Arch Version Repository Size
===================================================================================================================================================================================================
Installing:
keepalived x86_64 1.3.-.el7_6. updates k
Installing for dependencies:
net-snmp-agent-libs x86_64 :5.7.-.el7_6. updates k
net-snmp-libs x86_64 :5.7.-.el7_6. updates k Transaction Summary
===================================================================================================================================================================================================
Install Package (+ Dependent packages) Total download size: 1.7 M
Installed size: 6.0 M
Downloading packages:
(/): net-snmp-libs-5.7.-.el7_6..x86_64.rpm | kB ::
(/): keepalived-1.3.-.el7_6..x86_64.rpm | kB ::
(/): net-snmp-agent-libs-5.7.-.el7_6..x86_64.rpm | kB ::
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 1.1 MB/s | 1.7 MB ::
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : :net-snmp-libs-5.7.-.el7_6..x86_64 /
Installing : :net-snmp-agent-libs-5.7.-.el7_6..x86_64 /
Installing : keepalived-1.3.-.el7_6..x86_64 /
Verifying : :net-snmp-agent-libs-5.7.-.el7_6..x86_64 /
Verifying : keepalived-1.3.-.el7_6..x86_64 /
Verifying : :net-snmp-libs-5.7.-.el7_6..x86_64 / Installed:
keepalived.x86_64 :1.3.-.el7_6. Dependency Installed:
net-snmp-agent-libs.x86_64 :5.7.-.el7_6. net-snmp-libs.x86_64 :5.7.-.el7_6. Complete!

在192.168.122.110和192.168.122.167上安装nginx:

[root@nginx- ~]# yum install nginx -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB ::
epel | 5.3 kB ::
extras | 3.4 kB ::
updates | 3.4 kB ::
(/): epel/x86_64/updateinfo | kB ::
(/): epel/x86_64/primary_db | 6.8 MB ::
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 :1.12.-.el7 will be installed
--> Processing Dependency: nginx-all-modules = :1.12.-.el7 for package: :nginx-1.12.-.el7.x86_64
--> Processing Dependency: nginx-filesystem = :1.12.-.el7 for package: :nginx-1.12.-.el7.x86_64
--> Processing Dependency: nginx-filesystem for package: :nginx-1.12.-.el7.x86_64
--> Processing Dependency: libprofiler.so.()(64bit) for package: :nginx-1.12.-.el7.x86_64
--> Running transaction check
---> Package gperftools-libs.x86_64 :2.6.-.el7 will be installed
---> Package nginx-all-modules.noarch :1.12.-.el7 will be installed
--> Processing Dependency: nginx-mod-http-geoip = :1.12.-.el7 for package: :nginx-all-modules-1.12.-.el7.noarch
--> Processing Dependency: nginx-mod-http-image-filter = :1.12.-.el7 for package: :nginx-all-modules-1.12.-.el7.noarch
--> Processing Dependency: nginx-mod-http-perl = :1.12.-.el7 for package: :nginx-all-modules-1.12.-.el7.noarch
--> Processing Dependency: nginx-mod-http-xslt-filter = :1.12.-.el7 for package: :nginx-all-modules-1.12.-.el7.noarch
--> Processing Dependency: nginx-mod-mail = :1.12.-.el7 for package: :nginx-all-modules-1.12.-.el7.noarch
--> Processing Dependency: nginx-mod-stream = :1.12.-.el7 for package: :nginx-all-modules-1.12.-.el7.noarch
---> Package nginx-filesystem.noarch :1.12.-.el7 will be installed
--> Running transaction check
---> Package nginx-mod-http-geoip.x86_64 :1.12.-.el7 will be installed
---> Package nginx-mod-http-image-filter.x86_64 :1.12.-.el7 will be installed
--> Processing Dependency: gd for package: :nginx-mod-http-image-filter-1.12.-.el7.x86_64
--> Processing Dependency: libgd.so.()(64bit) for package: :nginx-mod-http-image-filter-1.12.-.el7.x86_64
---> Package nginx-mod-http-perl.x86_64 :1.12.-.el7 will be installed
---> Package nginx-mod-http-xslt-filter.x86_64 :1.12.-.el7 will be installed
--> Processing Dependency: libxslt.so.(LIBXML2_1.0.11)(64bit) for package: :nginx-mod-http-xslt-filter-1.12.-.el7.x86_64
--> Processing Dependency: libxslt.so.(LIBXML2_1.0.18)(64bit) for package: :nginx-mod-http-xslt-filter-1.12.-.el7.x86_64
--> Processing Dependency: libexslt.so.()(64bit) for package: :nginx-mod-http-xslt-filter-1.12.-.el7.x86_64
--> Processing Dependency: libxslt.so.()(64bit) for package: :nginx-mod-http-xslt-filter-1.12.-.el7.x86_64
---> Package nginx-mod-mail.x86_64 :1.12.-.el7 will be installed
---> Package nginx-mod-stream.x86_64 :1.12.-.el7 will be installed
--> Running transaction check
---> Package gd.x86_64 :2.0.-.el7 will be installed
--> Processing Dependency: libjpeg.so.(LIBJPEG_6.)(64bit) for package: gd-2.0.-.el7.x86_64
--> Processing Dependency: libjpeg.so.()(64bit) for package: gd-2.0.-.el7.x86_64
--> Processing Dependency: libfontconfig.so.()(64bit) for package: gd-2.0.-.el7.x86_64
--> Processing Dependency: libXpm.so.()(64bit) for package: gd-2.0.-.el7.x86_64
--> Processing Dependency: libX11.so.()(64bit) for package: gd-2.0.-.el7.x86_64
---> Package libxslt.x86_64 :1.1.-.el7 will be installed
--> Running transaction check
---> Package fontconfig.x86_64 :2.13.-4.3.el7 will be installed
--> Processing Dependency: fontpackages-filesystem for package: fontconfig-2.13.-4.3.el7.x86_64
--> Processing Dependency: dejavu-sans-fonts for package: fontconfig-2.13.-4.3.el7.x86_64
---> Package libX11.x86_64 :1.6.-.el7 will be installed
--> Processing Dependency: libX11-common >= 1.6.-.el7 for package: libX11-1.6.-.el7.x86_64
--> Processing Dependency: libxcb.so.()(64bit) for package: libX11-1.6.-.el7.x86_64
---> Package libXpm.x86_64 :3.5.-.el7 will be installed
---> Package libjpeg-turbo.x86_64 :1.2.-.el7 will be installed
--> Running transaction check
---> Package dejavu-sans-fonts.noarch :2.33-.el7 will be installed
--> Processing Dependency: dejavu-fonts-common = 2.33-.el7 for package: dejavu-sans-fonts-2.33-.el7.noarch
---> Package fontpackages-filesystem.noarch :1.44-.el7 will be installed
---> Package libX11-common.noarch :1.6.-.el7 will be installed
---> Package libxcb.x86_64 :1.13-.el7 will be installed
--> Processing Dependency: libXau.so.()(64bit) for package: libxcb-1.13-.el7.x86_64
--> Running transaction check
---> Package dejavu-fonts-common.noarch :2.33-.el7 will be installed
---> Package libXau.x86_64 :1.0.-2.1.el7 will be installed
--> Finished Dependency Resolution Dependencies Resolved ===================================================================================================================================================================================================
Package Arch Version Repository Size
===================================================================================================================================================================================================
Installing:
nginx x86_64 :1.12.-.el7 epel k
Installing for dependencies:
dejavu-fonts-common noarch 2.33-.el7 base k
dejavu-sans-fonts noarch 2.33-.el7 base 1.4 M
fontconfig x86_64 2.13.-4.3.el7 base k
fontpackages-filesystem noarch 1.44-.el7 base 9.9 k
gd x86_64 2.0.-.el7 base k
gperftools-libs x86_64 2.6.-.el7 base k
libX11 x86_64 1.6.-.el7 base k
libX11-common noarch 1.6.-.el7 base k
libXau x86_64 1.0.-2.1.el7 base k
libXpm x86_64 3.5.-.el7 base k
libjpeg-turbo x86_64 1.2.-.el7 base k
libxcb x86_64 1.13-.el7 base k
libxslt x86_64 1.1.-.el7 base k
nginx-all-modules noarch :1.12.-.el7 epel k
nginx-filesystem noarch :1.12.-.el7 epel k
nginx-mod-http-geoip x86_64 :1.12.-.el7 epel k
nginx-mod-http-image-filter x86_64 :1.12.-.el7 epel k
nginx-mod-http-perl x86_64 :1.12.-.el7 epel k
nginx-mod-http-xslt-filter x86_64 :1.12.-.el7 epel k
nginx-mod-mail x86_64 :1.12.-.el7 epel k
nginx-mod-stream x86_64 :1.12.-.el7 epel k Transaction Summary
===================================================================================================================================================================================================
Install Package (+ Dependent packages) Total download size: 4.3 M
Installed size: M
Downloading packages:
(/): dejavu-fonts-common-2.33-.el7.noarch.rpm | kB ::
(/): fontconfig-2.13.-4.3.el7.x86_64.rpm | kB ::
(/): fontpackages-filesystem-1.44-.el7.noarch.rpm | 9.9 kB ::
(/): gperftools-libs-2.6.-.el7.x86_64.rpm | kB ::
(/): libX11-1.6.-.el7.x86_64.rpm | kB ::
(/): gd-2.0.-.el7.x86_64.rpm | kB ::
(/): libX11-common-1.6.-.el7.noarch.rpm | kB ::
(/): libXau-1.0.-2.1.el7.x86_64.rpm | kB ::
(/): libXpm-3.5.-.el7.x86_64.rpm | kB ::
(/): libjpeg-turbo-1.2.-.el7.x86_64.rpm | kB ::
(/): libxslt-1.1.-.el7.x86_64.rpm | kB ::
(/): dejavu-sans-fonts-2.33-.el7.noarch.rpm | 1.4 MB ::
(/): libxcb-1.13-.el7.x86_64.rpm | kB ::
(/): nginx-all-modules-1.12.-.el7.noarch.rpm | kB ::
(/): nginx-1.12.-.el7.x86_64.rpm | kB ::
(/): nginx-filesystem-1.12.-.el7.noarch.rpm | kB ::
(/): nginx-mod-http-geoip-1.12.-.el7.x86_64.rpm | kB ::
(/): nginx-mod-http-image-filter-1.12.-.el7.x86_64.rpm | kB ::
(/): nginx-mod-http-xslt-filter-1.12.-.el7.x86_64.rpm | kB ::
(/): nginx-mod-mail-1.12.-.el7.x86_64.rpm | kB ::
(/): nginx-mod-stream-1.12.-.el7.x86_64.rpm | kB ::
(/): nginx-mod-http-perl-1.12.-.el7.x86_64.rpm | kB ::
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 3.0 MB/s | 4.3 MB ::
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : fontpackages-filesystem-1.44-.el7.noarch /
Installing : dejavu-fonts-common-2.33-.el7.noarch /
Installing : dejavu-sans-fonts-2.33-.el7.noarch /
Installing : fontconfig-2.13.-4.3.el7.x86_64 /
Installing : :nginx-filesystem-1.12.-.el7.noarch /
Installing : libX11-common-1.6.-.el7.noarch /
Installing : gperftools-libs-2.6.-.el7.x86_64 /
Installing : libXau-1.0.-2.1.el7.x86_64 /
Installing : libxcb-1.13-.el7.x86_64 /
Installing : libX11-1.6.-.el7.x86_64 /
Installing : libXpm-3.5.-.el7.x86_64 /
Installing : libxslt-1.1.-.el7.x86_64 /
Installing : libjpeg-turbo-1.2.-.el7.x86_64 /
Installing : gd-2.0.-.el7.x86_64 /
Installing : :nginx-mod-mail-1.12.-.el7.x86_64 /
Installing : :nginx-mod-stream-1.12.-.el7.x86_64 /
Installing : :nginx-mod-http-xslt-filter-1.12.-.el7.x86_64 /
Installing : :nginx-mod-http-image-filter-1.12.-.el7.x86_64 /
Installing : :nginx-mod-http-perl-1.12.-.el7.x86_64 /
Installing : :nginx-mod-http-geoip-1.12.-.el7.x86_64 /
Installing : :nginx-all-modules-1.12.-.el7.noarch /
Installing : :nginx-1.12.-.el7.x86_64 /
Verifying : :nginx-all-modules-1.12.-.el7.noarch /
Verifying : fontconfig-2.13.-4.3.el7.x86_64 /
Verifying : :nginx-1.12.-.el7.x86_64 /
Verifying : :nginx-mod-mail-1.12.-.el7.x86_64 /
Verifying : :nginx-mod-stream-1.12.-.el7.x86_64 /
Verifying : fontpackages-filesystem-1.44-.el7.noarch /
Verifying : libjpeg-turbo-1.2.-.el7.x86_64 /
Verifying : :nginx-mod-http-xslt-filter-1.12.-.el7.x86_64 /
Verifying : :nginx-mod-http-image-filter-1.12.-.el7.x86_64 /
Verifying : dejavu-fonts-common-2.33-.el7.noarch /
Verifying : libxcb-1.13-.el7.x86_64 /
Verifying : libXpm-3.5.-.el7.x86_64 /
Verifying : :nginx-mod-http-perl-1.12.-.el7.x86_64 /
Verifying : libxslt-1.1.-.el7.x86_64 /
Verifying : libX11-1.6.-.el7.x86_64 /
Verifying : dejavu-sans-fonts-2.33-.el7.noarch /
Verifying : gd-2.0.-.el7.x86_64 /
Verifying : :nginx-mod-http-geoip-1.12.-.el7.x86_64 /
Verifying : libXau-1.0.-2.1.el7.x86_64 /
Verifying : gperftools-libs-2.6.-.el7.x86_64 /
Verifying : libX11-common-1.6.-.el7.noarch /
Verifying : :nginx-filesystem-1.12.-.el7.noarch / Installed:
nginx.x86_64 :1.12.-.el7 Dependency Installed:
dejavu-fonts-common.noarch :2.33-.el7 dejavu-sans-fonts.noarch :2.33-.el7 fontconfig.x86_64 :2.13.-4.3.el7 fontpackages-filesystem.noarch :1.44-.el7
gd.x86_64 :2.0.-.el7 gperftools-libs.x86_64 :2.6.-.el7 libX11.x86_64 :1.6.-.el7 libX11-common.noarch :1.6.-.el7
libXau.x86_64 :1.0.-2.1.el7 libXpm.x86_64 :3.5.-.el7 libjpeg-turbo.x86_64 :1.2.-.el7 libxcb.x86_64 :1.13-.el7
libxslt.x86_64 :1.1.-.el7 nginx-all-modules.noarch :1.12.-.el7 nginx-filesystem.noarch :1.12.-.el7 nginx-mod-http-geoip.x86_64 :1.12.-.el7
nginx-mod-http-image-filter.x86_64 :1.12.-.el7 nginx-mod-http-perl.x86_64 :1.12.-.el7 nginx-mod-http-xslt-filter.x86_64 :1.12.-.el7 nginx-mod-mail.x86_64 :1.12.-.el7
nginx-mod-stream.x86_64 :1.12.-.el7 Complete!

需要注意的是,在两台nginx服务器上需要将防火墙关闭,否则lvs+keepalived的两台机器就无法将请求发送到两台nginx服务器上来:

[root@nginx- ~]# systemctl disable firewalld.service

查看两台负载均衡机器是否支持lvs:

# 如果看到如下结果,则说明是支持的
[root@lvskeepalived- ~]# sudo lsmod |grep ip_vs
ip_vs
nf_conntrack ip_vs
libcrc32c xfs,ip_vs,nf_conntrack

如果上述命令没有任何结果,则执行sudo ipvsadm命令启动ipvs之后,再通过上述命令进行查看即可。启动ipvs之后,我们就可以在/etc/keepalived/目录下编辑keepalived.conf文件,我们以192.168.122.248机器作为master机器,master节点配置如下:

# Global Configuration
global_defs {
lvs_id director1 # 指定lvs的id
} # VRRP Configuration
vrrp_instance LVS {
state MASTER # 指定当前节点为master节点
interface eth0 # 这里是网卡的名称,通过ifconfig或者ip addr可以查看
virtual_router_id # 这里指定的是虚拟路由id,master节点和backup节点需要指定一样的
priority # 指定了当前节点的优先级,数值越大优先级越高,master节点要高于backup节点
advert_int # 指定发送VRRP通告的间隔,单位是秒
authentication {
auth_type PASS # 鉴权,默认通过
auth_pass # 鉴权访问密码
} virtual_ipaddress {
192.168.122.120 # 指定了虚拟ip
} } # Virtual Server Configuration - for www server
# 后台真实主机的配置
virtual_server 192.168.122.120 {
delay_loop # 健康检查的时间间隔
lb_algo rr # 负载均衡策略,这里是轮询
lb_kind DR # 调度器类型,这里是DR
persistence_time # 指定了持续将请求打到同一台真实主机的时间长度
protocol TCP # 指定了访问后台真实主机的协议类型 # Real Server configuration
# 指定了真实主机1的ip和端口
real_server 192.168.122.110 {
weight # 指定了当前主机的权重
TCP_CHECK {
connection_timeout # 指定了进行心跳检查的超时时间
nb_get_retry # 指定了心跳超时之后的重复次数
delay_before_retry # 指定了在尝试之前延迟多长时间
}
} # Real Server Configuration
real_server 192.168.122.167 {
weight # 指定了当前主机的权重
TCP_CHECK {
connection_timeout # 指定了进行心跳检查的超时时间
nb_get_retry # 指定了心跳超时之后的重复次数
delay_before_retry # 指定了在尝试之前延迟多长时间
}
}
}

上面是master节点上keepalived的配置,对于backup节点,其配置与master几乎是一致的,只是其state和priority参数不同。如下是backup节点的完整配置:

# Global Configuration
global_defs {
lvs_id director2 # 指定lvs的id
} # VRRP Configuration
vrrp_instance LVS {
state BACKUP # 指定当前节点为master节点
interface eth0 # 这里的ens33是网卡的名称,通过ifconfig或者ip addr可以查看
virtual_router_id # 这里指定的是虚拟路由id,master节点和backup节点需要指定一样的
priority # 指定了当前节点的优先级,数值越大优先级越高,master节点要高于backup节点
advert_int # 指定发送VRRP通告的间隔,单位是秒
authentication {
auth_type PASS # 鉴权,默认通过
auth_pass # 鉴权访问密码
} virtual_ipaddress {
192.168.122.120 # 指定了虚拟ip
} } # Virtual Server Configuration - for www server
# 后台真实主机的配置
virtual_server 192.168.122.120 {
delay_loop # 健康检查的时间间隔
lb_algo rr # 负载均衡策略,这里是轮询
lb_kind DR # 调度器类型,这里是DR
persistence_time # 指定了持续将请求打到同一台真实主机的时间长度
protocol TCP # 指定了访问后台真实主机的协议类型 # Real Server configuration
# 指定了真实主机1的ip和端口
real_server 192.168.122.110 {
weight # 指定了当前主机的权重
TCP_CHECK {
connection_timeout # 指定了进行心跳检查的超时时间
nb_get_retry # 指定了心跳超时之后的重复次数
delay_before_retry # 指定了在尝试之前延迟多长时间
}
} # Real Server Configuration
real_server 192.168.122.167 {
weight # 指定了当前主机的权重
TCP_CHECK {
connection_timeout # 指定了进行心跳检查的超时时间
nb_get_retry # 指定了心跳超时之后的重复次数
delay_before_retry # 指定了在尝试之前延迟多长时间
}
}
}

将master和backup配置成完全一样的原因是,在master宕机时,可以根据backup的配置进行服务的无缝切换。

在lvs+keepalived机器配置完成之后,我们下面配置两台应用服务器的nginx配置。这里我们是将nginx作为应用服务器,在其配置文件中配置返回状态码为200,并且会将当前主机的ip返回,如下是其配置:

worker_processes auto;
# pid /run/nginx.pid; events {
worker_connections ;
} http {
server {
listen ; # 这里是直接返回200状态码和一段文本
location / {
default_type text/html;
return "Hello, Nginx! Server nginx-1\n";
}
}
}
worker_processes auto;
# pid /run/nginx.pid; events {
worker_connections ;
} http {
server {
listen ; # 这里是直接返回200状态码和一段文本
location / {
default_type text/html;
return "Hello, Nginx! Server nginx-2\n";
}
}
}

可以看到,两台机器返回的文本中主机ip是不一样的。nginx配置完成后,可以通过如下命令进行启动:

[root@nginx- nginx]# sudo nginx

在启动nginx之后,我们需要配置虚拟ip,这是因为我们使用的lvs调度器是DR模式,前面我们讲到过,这种模式下,对客户端的响应是真实服务器直接返回给客户端的,而真实服务器需要将响应报文中的源ip修改为虚拟ip,这里配置的虚拟ip就是起这个作用的。我们编辑/etc/init.d/lvsrs文件,写入如下内容:

#!/bin/bash

ifconfig lo: 192.168.122.120 netmask 255.255.255.255 broadcast 192.168.122.120 up
route add -host 192.168.122.120 dev lo: echo "" > /proc/sys/net/ipv4/ip_forward
echo "" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "" > /proc/sys/net/ipv4/conf/all/arp_announce exit
  • lo:表示当前主机真实网卡的名称;
  • 172.16.28.120:表示虚拟ip;

编写完成后运行该脚本文件即可。然后将两台lvs+keepalived机器上的keepalived服务启动起来即可:

[root@lvskeepalived- keepalived]# service keepalived start
Redirecting to /bin/systemctl start keepalived.service
[root@lvskeepalived- keepalived]# service keepalived status
Redirecting to /bin/systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: active (running) since Tue -- :: CST; 10s ago
Process: ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=/SUCCESS)
Main PID: (keepalived)
CGroup: /system.slice/keepalived.service
├─ /usr/sbin/keepalived -D
├─ /usr/sbin/keepalived -D
└─ /usr/sbin/keepalived -D Aug :: lvskeepalived- Keepalived_vrrp[]: Sending gratuitous ARP on eth0 for 192.168.122.120
Aug :: lvskeepalived- Keepalived_vrrp[]: Sending gratuitous ARP on eth0 for 192.168.122.120
Aug :: lvskeepalived- Keepalived_vrrp[]: Sending gratuitous ARP on eth0 for 192.168.122.120
Aug :: lvskeepalived- Keepalived_vrrp[]: Sending gratuitous ARP on eth0 for 192.168.122.120
Aug :: lvskeepalived- Keepalived_vrrp[]: Sending gratuitous ARP on eth0 for 192.168.122.120
Aug :: lvskeepalived- Keepalived_vrrp[]: VRRP_Instance(LVS) Sending/queueing gratuitous ARPs on eth0 for 192.168.122.120
Aug :: lvskeepalived- Keepalived_vrrp[]: Sending gratuitous ARP on eth0 for 192.168.122.120
Aug :: lvskeepalived- Keepalived_vrrp[]: Sending gratuitous ARP on eth0 for 192.168.122.120
Aug :: lvskeepalived- Keepalived_vrrp[]: Sending gratuitous ARP on eth0 for 192.168.122.120
Aug :: lvskeepalived- Keepalived_vrrp[]: Sending gratuitous ARP on eth0 for 192.168.122.120
[root@lvskeepalived- keepalived]#

最后可以通过如下命令查看配置的lvs+keepalived的策略:

[root@lvskeepalived- keepalived]# ipvsadm -ln
IP Virtual Server version 1.2. (size=)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.122.120: rr
-> 192.168.122.110: Route
-> 192.168.122.167: Route

2.2 集群测试

根据上述步骤,我们配置完成了一个lvs+keepalived+nginx的集群。在浏览器中,我们可以访问http://192.168.122.120即可看到如下响应:

Hello, Nginx! Server nginx-

多次刷新浏览器之后,可以看到浏览器中显示的文本切换如下,这是因为lvs的负载均衡策略产生的:

[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
You have new mail in /var/spool/mail/root
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]# curl 192.168.122.120
Hello, Nginx! Server nginx-
[root@test2 ~]#
 

Keepalived+LVS+nginx搭建nginx高可用集群的更多相关文章

  1. 搭建 Kubernetes 高可用集群

    使用 3 台阿里云服务器(k8s-master0, k8s-master1, k8s-master2)作为 master 节点搭建高可用集群,负载均衡用的是阿里云 SLB ,需要注意的是由于阿里云负载 ...

  2. 阿里云搭建k8s高可用集群(1.17.3)

    首先准备5台centos7 ecs实例最低要求2c4G 开启SLB(私网) 这里我们采用堆叠拓扑的方式构建高可用集群,因为k8s 集群etcd采用了raft算法保证集群一致性,所以高可用必须保证至少3 ...

  3. 基于keepalived搭建MySQL高可用集群

    MySQL的高可用方案一般有如下几种: keepalived+双主,MHA,MMM,Heartbeat+DRBD,PXC,Galera Cluster 比较常用的是keepalived+双主,MHA和 ...

  4. 基于 ZooKeeper 搭建 Spark 高可用集群

    一.集群规划 二.前置条件 三.Spark集群搭建         3.1 下载解压         3.2 配置环境变量         3.3 集群配置         3.4 安装包分发 四.启 ...

  5. Spark学习之路(七)—— 基于ZooKeeper搭建Spark高可用集群

    一.集群规划 这里搭建一个3节点的Spark集群,其中三台主机上均部署Worker服务.同时为了保证高可用,除了在hadoop001上部署主Master服务外,还在hadoop002和hadoop00 ...

  6. Spark 系列(七)—— 基于 ZooKeeper 搭建 Spark 高可用集群

    一.集群规划 这里搭建一个 3 节点的 Spark 集群,其中三台主机上均部署 Worker 服务.同时为了保证高可用,除了在 hadoop001 上部署主 Master 服务外,还在 hadoop0 ...

  7. 入门大数据---基于Zookeeper搭建Spark高可用集群

    一.集群规划 这里搭建一个 3 节点的 Spark 集群,其中三台主机上均部署 Worker 服务.同时为了保证高可用,除了在 hadoop001 上部署主 Master 服务外,还在 hadoop0 ...

  8. mysql+mycat搭建稳定高可用集群,负载均衡,主备复制,读写分离

    数据库性能优化普遍采用集群方式,oracle集群软硬件投入昂贵,今天花了一天时间搭建基于mysql的集群环境. 主要思路 简单说,实现mysql主备复制-->利用mycat实现负载均衡. 比较了 ...

  9. CentOS7Linux中服务器LVS负载均衡、高可用集群搭建(NAT、DR);

    目录 集群 声明 集群概念 集群特性 Web服务器并发相应瓶颈 集群的分类 LB实现方法: LVS集群 负载调度器 服务器池 共享存储 LVS负载均衡的三种模式 负载均衡 集群 声明 文档不断更新中. ...

  10. 使用 Load Balancer,Corosync,Pacemaker 搭建 Linux 高可用集群

    由于网络架构的原因,在一般虚拟机或物理环境中常见的用 VIP 来实现双机高可用方案,无法照搬到 Azure 平台.但利用 Azure 平台提供的负载均衡或者内部负载均衡功能,可以达到类似的效果. 本文 ...

随机推荐

  1. zabbix生产环境案例(三)

    生产环境案例(三) 链接:https://pan.baidu.com/s/1q5YwJMTcZLcS5OQ0iOu44A 提取码:8gdi 复制这段内容后打开百度网盘手机App,操作更方便哦 1. Z ...

  2. mongodb的学习 (2)

    1.条件查询 查询姓名为小明的学生                            db.local.find({name:'小明'});; 查询英语成绩大于90分的同学            ...

  3. ansible-继续普通用户权限运行

    ansible 远程以普通用户执行命令   1. ansible 10.0.0.1 -m raw -a "date" -u www 2.在ansible的主机配置文件中指定ssh_ ...

  4. docker部署coredns

    示例: [root@corends coredns]# cd /home/coredns/ [root@corends coredns]# ls -al total drwxr-xr-x root r ...

  5. leetcode-161周赛-5249-移除无效的括号

    题目描述: 自己的提交:O(N) class Solution: def minRemoveToMakeValid(self, s: str) -> str: #from collections ...

  6. C#中ArrayList 、Array与、string、string[]数组的相关转换

    一.ArrayList 与 string.string[]数组的转换 1.ArrayList 转换为 string[] : ArrayList list = new ArrayList(); list ...

  7. 【Dart学习】-- Dart之操作符

    一,概述 dart定义了下表所示的运算符.你可以重写许多这些运算符. 描述 运算符 一元后缀 expr++ expr-- () [] . ?. 一元前缀 -expr !expr ~expr ++exp ...

  8. JAVA并发工具类---------------(Fork/Join)

    Fork/Join 分而治之 将一个大任务分成数个小任务执行,然后将这些小人物执行后的结果进行join汇总: (假设:你要计算1到1000的总和,你可以把它分成1-100,101-200,...... ...

  9. ASP.NET Core学习——4

    静态文件 静态文件(static files),例如HTML.CSS.图片和JavaScript之类的资源会被ASP.NET Core应用直接提供给客户端. 静态文件通常位于web root(< ...

  10. 无法将 Ethernet0 连接到虚拟网络”VMnet0″ 详细信息可以在 vmware.log 文件中找到未能连接虚拟机Ethernet0

    在 vmware“编辑->虚拟网络设置”里面,点“恢复默认”可解决.