Centos7+Nginx+Keepalived实现WEB(Apache)服务的高可用&负载均衡

Centos7+Nginx+Keepalived实现WEB(Apache)服务的高可用&负载均衡

今天是2017年的第一天,昨天也就是2016年的最后一天,我尝试部署了Centos7+Nginx+Keepalived实现WEB服务的高可用负载均衡服务,终于在2017年的第一天前完成了,所以在此分享给有需要的朋友;说到负载均衡,其实在linux下有很多服务可以实现,比如Nginx、haproxy、lvs等服务,当前我们在前面的文章有介绍过了,但是对于高可用服务,我们在linux下最常见也是应用最多的是Keepalived,对于这些综合服务对于一个管理员来说是必须要会的,所以今天我们主要介绍的是Centos7+Nginx+Keepalived实现WEB(Apache)服务的高可用负载均衡。具体见下:


环境介绍:

Hostname:A-S

IP:192.168.5.21

Role:Apache Web Service

Hostname:B-S

IP:192.168.5.22

Role:Apache Web Service

Hostname:BB-S

IP:192.168.5.53

Role:Nginx+Keepalived

Hostname:BB-S

IP:192.168.5.54

Role:Nginx+Keepalived

Vitual IP:192.168.5.88

我们需要首先安装两台Apache Web服务

因为我们准备使用yum安装服务,所以需要定义阿里云仓库

cd/etc/yum.repo
vimepel.repo
添加以下内容
[epel]name=aliyunepelbaseurl=http://mirrors.aliyun.com/epel/7Server/x86_64/
gpgcheck=0

Image(28)

Image(29)

首先在192.168.5.21上安装apache

yumisntall-yhttpd

Image(30)

然后定义显示页面

vim/var/www/html/index.html
</html>
<!DOCTYPEhtml>
<html>
<head>
<title>WelcometoApache</title>
<style>
body{
35em;
margin:0auto;
font-family:Tahoma,Verdana,Arial,sans-serif;
}
</style>
<styletype="text/css">
h1{color:red}
h2{color:blue}
h3{color:green}
h4{color:yellow}
}
</style>
</head><bodybgcolor='#46A3FF'>
<h1>WelcometoA-SApache</h1>
<h2>HostName:A-S</h2>
<h3>IP:192.168.5.21</h3>
<h4>Service:Apache</h4>
<inputtype=buttonvalue="Refresh"onclick="window.location.href('http://192.168.5.21')">
</body>
</html>

Image(31)

Systemctlstarthttpd

clip_image002

然后添加认的防火墙端口8o

Firewall-cmd--zone=public--add-port='80/tcp'--permanent

clip_image004

或者vim/etc/firewalld/zone/public.xml
添加一下格式
<portportocal='tcp'port='80'>

我们测试访问

Image(33)

我们按照同上的方法在第二台服务器上进行配置,安装跳过

第二台主机的配置:192.168.5.22 主机名 B-S

安装好httpd后,我们将a-s上的index拷贝到b-s服务器上

scpindex.htmlroot@192.168.5.22:/var/www/html/

然后修改index.html文件

</html>
<!DOCTYPEhtml>
<html>
<head>
<title>WelcometoApache</title>
<style>
body{
35em;
margin:0auto;
font-family:Tahoma,sans-serif;
}
</style>
<styletype="text/css">
h1{color:red}
h2{color:blue}
h3{color:green}
h4{color:yellow}
}
</style>
</head><bodybgcolor='#CA8EFF'>
<h1>WelcometoB-SApache</h1>
<h2>HostName:B-S</h2>
<h3>IP:192.168.5.22</h3>
<h4>Service:Apache</h4>
<inputtype=buttonvalue="Refresh"onclick="window.location.href('http://192.168.5.22')">
</body>
</html>

Image(34)

测试访问

Image(35)

接下来我们开始准备在两台前端服务器192.168.5.53、192.168.5.54上安装Nginx和keepalived;我们将keepalived和Nginx安装在同一台机器上

我们首先在主备服务器上安装Nginx

我们首先要定于安装源, 才可以使用yum安装,我们在主备服务器上都配置

cd/etc/yum.repo
vimepel.repo
添加以下内容
[epel]name=aliyunepelbaseurl=http://mirrors.aliyun.com/epel/7Server/x86_64/
gpgcheck=0

Image(36)

定义好后,我们将定义的安装源给另外一台备服务器也拷贝一份

scp/etc/yum.repo/epel.reporoot@192.168.5.54/etc/yum.repo/

Image(37)

指定源后,我们就可以开始安装Nginx了,首先在主服务器上

yuminstall-yNginx

Image(38)

安装后,我们首先查看认的Nginx配置

vim/etc/Nginx/Nginx.conf
#Formoreinformationonconfiguration,see:
#*OfficialEnglishDocumentation:http://Nginx.org/en/docs/
#*OfficialRussianDocumentation:http://Nginx.org/ru/docs/
userNginx;
worker_processesauto;
error_log/var/log/Nginx/error.log;
pid/run/Nginx.pid;
#Loaddynamicmodules.See/usr/share/Nginx/README.dynamic.
include/usr/share/Nginx/modules/*.conf;
events{
worker_connections1024;
}
http{
log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'
'$status$body_bytes_sent"$http_referer"'
'"$http_user_agent""$http_x_forwarded_for"';
access_log/var/log/Nginx/access.logmain;
sendfileon;
tcp_nopushon;
tcp_nodelayon;
keepalive_timeout65;
types_hash_max_size2048;
include/etc/Nginx/mime.types;
default_typeapplication/octet-stream;
#Loadmodularconfigurationfilesfromthe/etc/Nginx/conf.ddirectory.
#Seehttp://Nginx.org/en/docs/ngx_core_module.html#include
#formoreinformation.
include/etc/Nginx/conf.d/*.conf;
server{
listen80default_server;
listen[::]:80default_server;
server_name_;
root/usr/share/Nginx/html;
#Loadconfigurationfilesforthedefaultserverblock.
include/etc/Nginx/default.d/*.conf;
location/{
}
error_page404/404.html;
location=/40x.html{
}
error_page500502503504/50x.html;
location=/50x.html{
}
}
#SettingsforaTLSenabledserver.
#
#server{
#listen443sslhttp2default_server;
#listen[::]:443sslhttp2default_server;
#server_name_;
#root/usr/share/Nginx/html;
#
#ssl_certificate"/etc/pki/Nginx/server.crt";
#ssl_certificate_key"/etc/pki/Nginx/private/server.key";
#ssl_session_cacheshared:SSL:1m;
#ssl_session_timeout10m;
#ssl_ciphersHIGH:!aNULL:!MD5;
#ssl_prefer_server_cipherson;
#
##Loadconfigurationfilesforthedefaultserverblock.
#include/etc/Nginx/default.d/*.conf;
#
#location/{
#}
#
#error_page404/404.html;
#location=/40x.html{
#}
#
#error_page500502503504/50x.html;
#location=/50x.html{
#}
#}
}

Image(39)

认配置我们备份一份

cp/etc/Nginx/Nginx.conf/etc/Nginx/Nginx.conf.bak

Image(40)

接下来我们要配置Nginx实现后端服务器的web的负载均衡;

在此我们使用的是Nginx负载均衡的认方式-轮询

我们需要在http区域里面添加负载配置

upstreamreal_server_pool{
#ip_hash;
server192.168.5.21:80weight=1max_fails=2fail_timeout=30s;
server192.168.5.22:80weight=1max_fails=2fail_timeout=30s;
}
#添加一组真实的服务器地址池
#供proxy_pass和fastcgi_pass指令中使用的代理服务器
#后台如果有动态应用的时候,ip_hash指令可以通过hash算法
#将客户端请求定位到同一台后端服务器上,解决session共享,#但建议用动态应用做session共享
#server用于指定一个后端服务器的名称和参数
#weight代表权,重认为1,权重越高被分配的客户端越多
#max_fails指定时间内对后端请求失败的次数
#fail_timeout达到max_fails指定的失败次数后暂停的时间
#down参数用来标记为离线,不参与负载均衡.在ip_hash下使用
#backup仅仅在非backup服务器宕机或繁忙的时候使用
server{
listen192.168.5.53:80;#监听ip改为本地ip
server_namelocalhost;
#charsetkoi8-r;
#access_loglogs/host.access.logmain;
location/{
#roothtml;
#indexindex.htmlindex.htm;
proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header;
proxy_passhttp://real_server_pool;
proxy_set_headerHost$host;
proxy_set_headerX-Forwarded-For$remote_addr;
}

Image(41)

添加后的

#Formoreinformationonconfiguration,see:
#*OfficialEnglishDocumentation:http://Nginx.org/en/docs/
#*OfficialRussianDocumentation:http://Nginx.org/ru/docs/
userNginx;
worker_processesauto;
error_log/var/log/Nginx/error.log;
pid/run/Nginx.pid;
#Loaddynamicmodules.See/usr/share/Nginx/README.dynamic.
include/usr/share/Nginx/modules/*.conf;
events{
worker_connections1024;
}
http{
log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'
'$status$body_bytes_sent"$http_referer"'
'"$http_user_agent""$http_x_forwarded_for"';
access_log/var/log/Nginx/access.logmain;
sendfileon;
tcp_nopushon;
tcp_nodelayon;
keepalive_timeout65;
types_hash_max_size2048;
include/etc/Nginx/mime.types;
default_typeapplication/octet-stream;
upstreamreal_server_pool{
#ip_hash;
server192.168.5.21:80weight=1max_fails=2fail_timeout=30s;
server192.168.5.22:80weight=1max_fails=2fail_timeout=30s;
}
#添加一组真实的服务器地址池
#供proxy_pass和fastcgi_pass指令中使用的代理服务器
#后台如果有动态应用的时候,不参与负载均衡.在ip_hash下使用
#backup仅仅在非backup服务器宕机或繁忙的时候使用
#Loadmodularconfigurationfilesfromthe/etc/Nginx/conf.ddirectory.
#Seehttp://Nginx.org/en/docs/ngx_core_module.html#include
#formoreinformation.
include/etc/Nginx/conf.d/*.conf;
server{
#listen80default_server;
#listen[::]:80default_server;
listen192.168.5.53:80;#监听ip改为本地ip
server_namelocalhost;
#server_name_;
root/usr/share/Nginx/html;
#Loadconfigurationfilesforthedefaultserverblock.
include/etc/Nginx/default.d/*.conf;
#增加的
location/{
#roothtml;
#indexindex.htmlindex.htm;
proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header;
proxy_passhttp://real_server_pool;
proxy_set_headerHost$host;
proxy_set_headerX-Forwarded-For$remote_addr;
}
error_page404/404.html;
location=/40x.html{
}
error_page500502503504/50x.html;
location=/50x.html{
}
}
#SettingsforaTLSenabledserver.
#
#server{
#listen443sslhttp2default_server;
#listen[::]:443sslhttp2default_server;
#server_name_;
#root/usr/share/Nginx/html;
#
#ssl_certificate"/etc/pki/Nginx/server.crt";
#ssl_certificate_key"/etc/pki/Nginx/private/server.key";
#ssl_session_cacheshared:SSL:1m;
#ssl_session_timeout10m;
#ssl_ciphersHIGH:!aNULL:!MD5;
#ssl_prefer_server_cipherson;
#
##Loadconfigurationfilesforthedefaultserverblock.
#include/etc/Nginx/default.d/*.conf;
#
#location/{
#}
#
#error_page404/404.html;
#location=/40x.html{
#}
#
#error_page500502503504/50x.html;
#location=/50x.html{
#}
#}
}

接着我们重启Nginx

systemctlenableNginx
systemctlstartNginx

Image(42)

接下来我们尝试访问一下,因为使用的是轮询,所以访问Nginx跳转到不同的两个页面

Image(43)

Image(44)

我们在安装keepalived前,先配置路由转发

vim/etc/sysctl.conf
net.ipv4.ip_forward=1#此参数改为1

Image(26)

sysctl-p#使修改生效

Image(27)

接着我们在主服务器上安装keepalived

yuminstall-ykeepalived

Image(45)

安装完成后,我们备份一下keepalived的配置文件

cp/etc/keepalived/keepalived.conf/etc/keepalived/keepalived.conf.bak

Image(46)

接下来我们查看一下认的keepalived认配置

cat/etc/keepalived/keepalived.conf
!ConfigurationFileforkeepalived
global_defs{
notification_email{
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_fromAlexandre.Cassen@firewall.loc
smtp_server192.168.200.1
smtp_connect_timeout30
router_idLVS_DEVEL
}
vrrp_instanceVI_1{
stateMASTER
interfaceeth0
virtual_router_id51
priority100
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
virtual_ipaddress{
192.168.200.16
192.168.200.17
192.168.200.18
}
}
virtual_server192.168.200.100443{
delay_loop6
lb_algorr
lb_kindNAT
nat_mask255.255.255.0
persistence_timeout50
protocolTCP
real_server192.168.201.100443{
weight1
SSL_GET{
url{
path/
digestff20ad2481f97b1754ef3e12ecd3a9cc
}
url{
path/mrtg/
digest9b3a0c85a887a256d6939da88aabd8cd
}
connect_timeout3
nb_get_retry3
delay_before_retry3
}
}
}
virtual_server10.10.10.21358{
delay_loop6
lb_algorr
lb_kindNAT
persistence_timeout50
protocolTCP
sorry_server192.168.200.2001358
real_server192.168.200.21358{
weight1
HTTP_GET{
url{
path/testurl/test.jsp
digest640205b7b0fc66c1ea91c463fac6334d
}
url{
path/testurl2/test.jsp
digest640205b7b0fc66c1ea91c463fac6334d
}
url{
path/testurl3/test.jsp
digest640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout3
nb_get_retry3
delay_before_retry3
}
}
real_server192.168.200.31358{
weight1
HTTP_GET{
url{
path/testurl/test.jsp
digest640205b7b0fc66c1ea91c463fac6334c
}
url{
path/testurl2/test.jsp
digest640205b7b0fc66c1ea91c463fac6334c
}
connect_timeout3
nb_get_retry3
delay_before_retry3
}
}
}
virtual_server10.10.10.31358{
delay_loop3
lb_algorr
lb_kindNAT
nat_mask255.255.255.0
persistence_timeout50
protocolTCP
real_server192.168.200.41358{
weight1
HTTP_GET{
url{
path/testurl/test.jsp
digest640205b7b0fc66c1ea91c463fac6334d
}
url{
path/testurl2/test.jsp
digest640205b7b0fc66c1ea91c463fac6334d
}
url{
path/testurl3/test.jsp
digest640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout3
nb_get_retry3
delay_before_retry3
}
}
real_server192.168.200.51358{
weight1
HTTP_GET{
url{
path/testurl/test.jsp
digest640205b7b0fc66c1ea91c463fac6334d
}
url{
path/testurl2/test.jsp
digest640205b7b0fc66c1ea91c463fac6334d
}
url{
path/testurl3/test.jsp
digest640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout3
nb_get_retry3
delay_before_retry3
}
}
}

接下来我们修改认的keepalived配置

echo>/etc/keepalived/keepalived.conf

Image(47)

我们清空认配置后,添加以下内容

vim/etc/keepalived/keepalived.conf
global_defs{
notification_email{
gavin@ixmsoft.com#定义通知邮箱,有多个可以换行添加
}
notification_email_fromadmin@ixmsoft.com#定义发送邮件的邮箱
smtp_servermail.ixmsoft.com#定义发件服务器
smtp_connect_timeout30#定义连接smtp服务器超时时间
router_idLVS_DEVEL
}
vrrp_instanceVI_1{
stateMASTER#标示主备,备机上改为BACKUP
interfaceens160#HA监测的端口
virtual_router_id51#主备的virtual_router_id的值必须相同
priority100#优先级,通常主要比备稍大
advert_int1#VRRPMulticast广播周期秒数
authentication{#定义认证
auth_typePASS#认证方式
auth_pass1111#认证口令字
}
virtual_ipaddress{#定义vip
192.168.5.88#多个可换行添加,一行一个
}
}
virtual_server192.168.5.8880{
delay_loop6#每隔6秒查询realserver状态
lb_algorr
lb_kindNAT
nat_mask255.255.255.0
persistence_timeout50#同一IP的连接50秒内被分配到同一台realserver
protocolTCP#用TCP监测realserver的状态
real_server192.168.5.5380{#需要指向Nginx负载的地址
weight3#权重
TCP_CHECK{
connect_timeout10#10秒无响应超时
nb_get_retry3
delay_before_retry3
connect_port80
}
}
real_server192.168.5.5480{#需要指向Nginx负载的地址
weight3
TCP_CHECK{
connect_timeout3
delay_before_retry3
connect_port80
}
}
}

我们介绍一下主要参数

global_defs
notification_email:keepalived在发生诸如切换操作时需要发送email通知地址,后面的smtp_server相比也都知道是邮件服务器地址。也可以通过其它方式报警,毕竟邮件不是实时通知的。
router_id:机器标识,通常可设为hostname。故障发生时,邮件通知会用到
vrrp_instance
state:指定instance(Initial)的初始状态,就是说在配置好后,这台服务器的初始状态就是这里指定的,但这里指定的不算,还是得要通过竞选通过优先级来确定。如果这里设置为MASTER,但如若他的优先级不及另外一台,那么这台在发送通告时,会发送自己的优先级,另外一台发现优先级不如自己的高,那么他会就回抢占为MASTER
interface:实例绑定的网卡,因为在配置虚拟IP的时候必须是在已有的网卡上添加的
mcast_src_ip:发送多播数据包时的源IP地址,这里注意了,这里实际上就是在那个地址上发送VRRP通告,这个非常重要,一定要选择稳定的网卡端口来发送,这里相当于heartbeat的心跳端口,如果没有设置那么就用认的绑定的网卡的IP,也就是interface指定的IP地址
virtual_router_id:这里设置VRID,这里非常重要,相同的VRID为一个组,他将决定多播的MAC地址
priority:设置本节点的优先级,优先级高的为master
advert_int:检查间隔,认为1秒。这就是VRRP的定时器,MASTER每隔这样一个时间间隔,就会发送一个advertisement报文以通知组内其他路由器自己工作正常
authentication:定义认证方式和密码,主从必须一样
virtual_ipaddress:这里设置的就是VIP,也就是虚拟IP地址,他随着state的变化而增加删除,当state为master的时候就添加,当state为backup的时候删除,这里主要是有优先级来决定的,和state设置的值没有多大关系,这里可以设置多个IP地址
track_script:引用VRRP脚本,即在vrrp_script部分指定的名字。定期运行它们来改变优先级,并最终引发主备切换。
vrrp_script
告诉keepalived在什么情况下切换,所以尤为重要。可以有多个vrrp_script
script:自己写的检测脚本。也可以是一行命令如killall-0Nginx
interval2:每2s检测一次
weight-5:检测失败(脚本返回非0)则优先级-5
fall2:检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)
rise1:检测1次成功就算成功。但不修改优先级

Image(48)

保存退出后,我们启动keepalived

systemctlstartkeepalived
systemctlenablekeepalived

Image(49)

然后我们测试ping一下vistual ip

注:如果ping不通vistual ip的话,有可能是keepalived.conf文件的中的网卡配置,centos6的网卡是eth0,而centos7的是 ens160

Image(50)

ping 192.168.5.88

Image(51)

接下来我们配置第二台备服务器,然后首先安装及配置Nginx

yuminstall-yNginx

Image(52)

然后备份备服务器的Nginx.conf文件

cp/etc/Nginx/Nginx.conf/etc/Nginx/Nginx.conf.bak

Image(53)

然后我们从主服务器下的Nginx.conf配置拷贝到备服务器Nginx目录下,然后替换认的Nginx.conf文件

scp/etc/Nginx/Nginx.confroot@192.168.5.54:/etc/Nginx/

Image(54)

然后我们修改备服务器的监听地址,需要指向备服务器的本地地址---> listen 192.168.5.54:80; # 监听ip改为本地ip

#Formoreinformationonconfiguration,see:
#*OfficialEnglishDocumentation:http://Nginx.org/en/docs/
#Formoreinformationonconfiguration,不参与负载均衡.在ip_hash下使用
#backup仅仅在非backup服务器宕机或繁忙的时候使用
#Loadmodularconfigurationfilesfromthe/etc/Nginx/conf.ddirectory.
#Seehttp://Nginx.org/en/docs/ngx_core_module.html#include
#formoreinformation.
include/etc/Nginx/conf.d/*.conf;
server{
#listen80default_server;
#listen[::]:80default_server;
listen192.168.5.54:80;#监听ip改为本地ip
server_namelocalhost;
#server_name_;
root/usr/share/Nginx/html;
#Loadconfigurationfilesforthedefaultserverblock.
include/etc/Nginx/default.d/*.conf;
location/{
#roothtml;
#indexindex.htmlindex.htm;
proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header;
proxy_passhttp://real_server_pool;
proxy_set_headerHost$host;
proxy_set_headerX-Forwarded-For$remote_addr;
}
error_page404/404.html;
location=/40x.html{
}
error_page500502503504/50x.html;
location=/50x.html{
}
}
#SettingsforaTLSenabledserver.
#
#server{
#listen443sslhttp2default_server;
#listen[::]:443sslhttp2default_server;
#server_name_;
#root/usr/share/Nginx/html;
#
#ssl_certificate"/etc/pki/Nginx/server.crt";
#ssl_certificate_key"/etc/pki/Nginx/private/server.key";
#ssl_session_cacheshared:SSL:1m;
#ssl_session_timeout10m;
#ssl_ciphersHIGH:!aNULL:!MD5;
#ssl_prefer_server_cipherson;
#
##Loadconfigurationfilesforthedefaultserverblock.
#include/etc/Nginx/default.d/*.conf;
#
#location/{
#}
#
#error_page404/404.html;
#location=/40x.html{
#}
#
#error_page500502503504/50x.html;
#location=/50x.html{
#}
#}
}

其实我们也可以只修改添加内容

upstreamreal_server_pool{
#ip_hash;
server192.168.5.21:80weight=1max_fails=2fail_timeout=30s;
server192.168.5.22:80weight=1max_fails=2fail_timeout=30s;
}
#添加一组真实的服务器地址池
#供proxy_pass和fastcgi_pass指令中使用的代理服务器
#后台如果有动态应用的时候,不参与负载均衡.在ip_hash下使用
#backup仅仅在非backup服务器宕机或繁忙的时候使用
server{
listen192.168.5.54:80;#监听ip改为本地ip
server_namelocalhost;
#charsetkoi8-r;
#access_loglogs/host.access.logmain;
location/{
#roothtml;
#indexindex.htmlindex.htm;
proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header;
proxy_passhttp://real_server_pool;
proxy_set_headerHost$host;
proxy_set_headerX-Forwarded-For$remote_addr;
}

Image(55)

然后我们启动备服务器的Nginx服务

systemctlstartNginx
systemctlenableNginx

Image(56)

我们在安装keepalived前,先配置路由转发

vim/etc/sysctl.conf
net.ipv4.ip_forward=1#此参数改为1

Image(26)[1]

sysctl-p#使修改生效

Image(57)

然后安装keepalived

Image(58)

然后我们备份认keepalived配置

cp/etc/keepalived/keepalived.conf/etc/keepalived/keepalived.conf.bak

Image(59)

然后修改拷贝的数据文件

scp/etc/keepalived/keepalived.confroot@192.168.5.54:/etc/keepalived/

Image(60)

配置备用调度器的keepalived,只需要将state MASTER 改为state BACKUP,降低priority 100 的值:

vim/etc/keepalived/keepalived.conf
global_defs{
notification_email{
gavin@ixmsoft.com#定义通知邮箱,有多个可以换行添加
}
notification_email_fromadmin@ixmsoft.com#定义发送邮件的邮箱
smtp_servermail.ixmsoft.com#定义发件服务器
smtp_connect_timeout30#定义连接smtp服务器超时时间
router_idLVS_DEVEL
}
vrrp_instanceVI_1{
stateBACKUP#标示主备,备机上改为BACKUP
interfaceens160#HA监测的端口
virtual_router_id51#主备的virtual_router_id的值必须相同
priority50#优先级,一行一个
}
}
virtual_server192.168.5.8880{
delay_loop6#每隔6秒查询realserver状态
lb_algorr
lb_kindNAT
nat_mask255.255.255.0
persistence_timeout50#同一IP的连接50秒内被分配到同一台realserver
protocolTCP#用TCP监测realserver的状态
real_server192.168.5.5380{#需要指向Nginx负载的地址
weight3#权重
TCP_CHECK{
connect_timeout10#10秒无响应超时
nb_get_retry3
delay_before_retry3
connect_port80
}
}
real_server192.168.5.5480{#需要指向Nginx负载的地址
weight3
TCP_CHECK{
connect_timeout3
delay_before_retry3
connect_port80
}
}
}

我们介绍一下主要参数

global_defs
notification_email:keepalived在发生诸如切换操作时需要发送email通知地址,后面的smtp_server相比也都知道是邮件服务器地址。也可以通过其它方式报警,毕竟邮件不是实时通知的。
router_id:机器标识,通常可设为hostname。故障发生时,邮件通知会用到
vrrp_instance
state:指定instance(Initial)的初始状态,就是说在配置好后,这台服务器的初始状态就是这里指定的,但这里指定的不算,还是得要通过竞选通过优先级来确定。如果这里设置为MASTER,但如若他的优先级不及另外一台,那么这台在发送通告时,会发送自己的优先级,另外一台发现优先级不如自己的高,那么他会就回抢占为MASTER
interface:实例绑定的网卡,因为在配置虚拟IP的时候必须是在已有的网卡上添加的
mcast_src_ip:发送多播数据包时的源IP地址,这里注意了,这里实际上就是在那个地址上发送VRRP通告,这个非常重要,一定要选择稳定的网卡端口来发送,这里相当于heartbeat的心跳端口,如果没有设置那么就用认的绑定的网卡的IP,也就是interface指定的IP地址
virtual_router_id:这里设置VRID,这里非常重要,相同的VRID为一个组,他将决定多播的MAC地址
priority:设置本节点的优先级,优先级高的为master
advert_int:检查间隔,认为1秒。这就是VRRP的定时器,MASTER每隔这样一个时间间隔,就会发送一个advertisement报文以通知组内其他路由器自己工作正常
authentication:定义认证方式和密码,主从必须一样
virtual_ipaddress:这里设置的就是VIP,也就是虚拟IP地址,他随着state的变化而增加删除,当state为master的时候就添加,当state为backup的时候删除,这里主要是有优先级来决定的,和state设置的值没有多大关系,这里可以设置多个IP地址
track_script:引用VRRP脚本,即在vrrp_script部分指定的名字。定期运行它们来改变优先级,并最终引发主备切换。
vrrp_script
告诉keepalived在什么情况下切换,所以尤为重要。可以有多个vrrp_script
script:自己写的检测脚本。也可以是一行命令如killall-0Nginx
interval2:每2s检测一次
weight-5:检测失败(脚本返回非0)则优先级-5
fall2:检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)
rise1:检测1次成功就算成功。但不修改优先级

Image(61)

记得修改防火墙配置哦

firewall-cmd--add-port='tcp/80'--permanent

然后我们启动 keepalived服务

systemctlstartkeepalived
systemcltlenablekeepalived

Image(62)

我们也测试ping一下vistual ip:

ping 192.168.5.88

Image(63)

接下来我们要实现realserver服务器配置

我们需要在两台web(http)192.168.5.21,192.168.5.22,需要在这两台服务器上配置虚拟VIP,所以在服务器上执行以下脚本

首先在192.168.5.21服务器上新建脚本文件

vimreal_server
然后添加一下脚本内容:
#!/bin/bash
#chkconfig:23458535
#Description:Startrealserverwithhostboot
VIP=192.168.5.88
functionstart(){
ifconfiglo:0$VIPnetmask255.255.255.255broadcast$VIP
echo1>/proc/sys/net/ipv4/conf/lo/arp_ignore
echo2>/proc/sys/net/ipv4/conf/lo/arp_announce
echo1>/proc/sys/net/ipv4/conf/all/arp_ignore
echo2>/proc/sys/net/ipv4/conf/all/arp_announce
echo“RealServer$(uname-n)started”
}
functionstop(){
ifconfiglo:0down
ifconfiglo:0$VIPnetmask255.255.255.255broadcast$VIP
echo0>/proc/sys/net/ipv4/conf/lo/arp_ignore
echo0>/proc/sys/net/ipv4/conf/lo/arp_announce
echo0>/proc/sys/net/ipv4/conf/all/arp_ignore
echo0>/proc/sys/net/ipv4/conf/all/arp_announce
echo“RealServer$(uname-n)stopped”
}
case$1in
start)
start
;;
stop)
stop
;;
*)
echo“Usage:$0{start|stop}”
exit1
esac

Image(64)

保存退出

然后赋予执行权限

chmoea+xrealserver

Image(65)

然后将脚本拷贝到第二台web服务器上:192.168.5.22

scp/DATA/real_serverroot@192.168.5.22/DATA/

Image(66)

我们在第二台服务器上查看

Image(67)

最后我们说一下如何将realserver的脚本添加到随系统启动

我们将使用chkconfig --add 来管理服务器的添加、顺序

cprealserver/etc/init.d/将脚本拷贝到指定目录
chkconfig--addrealserver添加realserver脚本到自动启动
chkconfig--list查看自动启动服务

Image(68)

chkconfig--list查看自动启动服务

Image(69)

通过以上配置后,我们就可以通过服务进行操作了

/etc/init.d/realserverstop
/etc/init.d/realserverstart

Image(24)

chkconfigrealserveron设置为自动启动

Image(25)

然后我们在第二台web服务器上做同样的操作

cprealserver/etc/init.d/将脚本拷贝到指定目录
chkconfig--addrealserver添加realserver脚本到自动启动
chkconfig--list查看自动启动服务

Image(70)

chkconfig--list查看自动启动服务

Image(71)

通过以上配置后,我们就可以通过服务进行操作了

/etc/init.d/realserverstop
/etc/init.d/realserverstart

Image(72)

chkconfigrealserveron设置为自动启动

Image(73)

接下来我们就是测试了,我们在两台realserver上执行查看ip状态

a-s
ipash

Image(74)

b-s
ipash

Image(75)

最后我们在keelalived服务器上查看ip状态,从状态上看,我们的keepalived是监听在备服务器上的,如果将备服务器keepalibed停止后,会切换到主服务器上的

aa-s主服务器
ipash

Image(76)

备服务器:

bb-s备服务器
ipash

Image(77)

最后我们就是测试了

我们访问vistual ip进行测试

Image(78)

Image(79)

我们查看log

tail-f/var/log/message

Image(80)

我们停止主服务器的keepalived服务

Image(81)

查看备服务器的log

Image(82)

Image(83)

Image(79)[1]

最后我们说一下keepalived的log;Keepalived认所有的日志都是写入到/var/log/message下的,由于message的日志太多了,而Keepalived的日志又很难分离出来,所以本文提供了一个调整Keepalived日志输出路径的方法
具体操作步骤如下:
一、修改 /etc/sysconfig/keepalived

认配置

vim/etc/sysconfig/keepalived

Image(84)

vim/etc/sysconfig/keepalived
把KEEPALIVED_OPTIONS="-D"修改为KEEPALIVED_OPTIONS="-D-d-S0"
#其中-S指定syslog的facility

Image(85)


接下来设置syslog
vim/etc/rsyslog.conf
注:Cnetos6的路劲是/etc/syslog.conf
Centos7的路劲是/etc/rsyslog.conf
添加以下配置
#keepalived-S0
local0.*/var/log/keepalived.log

Image(86)

保存退出后,重启服务

systemctlrestartrsyslog
systemctlrestartkeepalived

Image(87)

接下来我们查看/var/log路劲是否生成了keepalived.log文件

Image(88)

tail-f/var/log/keepalived.conf

Image(89)

相关文章

Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native