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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


linux下开机自启: 在/etc/init.d目录下新建文件elasticsearch 并敲入shell脚本: 注意, 前两行必须填写,且要注释掉。 第一行为shell前行代码,目的告诉系统使用shell。 第二行分别代表运行级别、启动优先权、关闭优先权,且后面添加开机服务会用到。 shell脚本
1、因为在centos7中/etc/rc.d/rc.local的权限被降低了,所以需要赋予其可执行权 chmod +x /etc/rc.d/rc.local 2、赋予脚本可执行权限假设/usr/local/script/autostart.sh是你的脚本路径,给予执行权限 chmod +x /usr
最简单的查看方法可以使用ls -ll、ls-lh命令进行查看,当使用ls -ll,会显示成字节大小,而ls- lh会以KB、MB等为单位进行显示,这样比较直观一些。 通过命令du -h –max-depth=1 *,可以查看当前目录下各文件、文件夹的大小,这个比较实用。 查询当前目录总大小可以使用d
ASP.NET Core应用程序发布linux在shell中运行是正常的。可一但shell关闭网站也就关闭了,所以要配置守护进程, 用的是Supervisor,本文主要记录配置的过程和过程遇到的问题 安装Supervisor&#160;1 yum install python-setuptools
设置时区(CentOS 7) 先执行命令timedatectl status|grep &#39;Time zone&#39;查看当前时区,如果不是时区(Asia/Shanghai),则需要先设置为中国时区,否则时区不同会存在时差。 #已经是Asia/Shanghai,则无需设置 [root@xia
vim&#160;/etc/sysconfig/network-scripts/ifcfg-eth0 BOOTPROTO=&quot;static&quot; ONBOOT=yes IPADDR=192.168.8.106 NETMASK=255.255.252.0 GATEWAY=192.168.
一、安装gcc依赖 由于 redis 是用 C 语言开发,安装之前必先确认是否安装 gcc 环境(gcc -v),如果没有安装,执行以下命令进行安装 [root@localhost local]# yum install -y gcc 二、下载并解压安装包 [root@localhost local
第一步 On CentOS/RHEL 6.*: $ sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el6/x86_64/nux-dextop-release-0-2.el6.nux.noarch.rpm On CentOS/RHEL 7: $
/// &lt;summary&gt; /// 取小写文件名后缀 /// &lt;/summary&gt; /// &lt;param name=&quot;name&quot;&gt;文件名&lt;/param&gt; /// &lt;returns&gt;返回小写后缀,不带“.”&lt;/ret
which nohup .bash_profile中并source加载 如果没有就安装吧 yum provides */nohup nohup npm run start &amp; nohup ./kibana &amp;
1.1 MySQL安装 1.1.1 下载wget命令 yum -y install wget 1.1.2 在线下载mysql安装包 wget https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm 1.1.3 安装My
重启 reboot shutdown -r now init 6 关闭 init 0 shutdown -h now shutdown -h 20:25 #8点25关机查看内存 free CPU利用率 top 日期 date 设置时间 date 033017002015 #月日时间年 日历 cal
1、firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status firewalld 开机禁用 : systemctl disable firewalld 开机启用 :
1 下载并安装MySQL官方的&#160;Yum Repository wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm 使用上面的命令就直接下载了安装用的Yum Repository,大概
CentOS6.x CentOS6中转用Upstrat代替以前的init.d/rcX.d的线性启动方式。 一、相关命令 通过initctl help可以查看相关命令 [root@localhost ~]# initctl help Job commands: start Start job. sto
1、使用命令:df -lk 找到已满磁盘 2、使用命令:du --max-depth=1 -h 查找大文件,删除
ifconfig:查看网卡信息 网卡配置文件位置: /etc/sysconfig/network-scripts/文件夹 nmtui:配置网卡 netstat -tlunp:查看端口信息 端口信息存储位置: /etc/services文件 route:查看路由信息 wget:下载网路文件,例如 wg
ps -ef:查看所有进程,&#160;ps -ef |grap firewalld 查看与firewalld相关的进程 which :查看进程:which firewalld kill 进程id:杀掉进程 kill 640,强制杀:kill -9 640 man:查看帮助,例如 man ps 查看
useradd:添加用户 useradd abc,默认添加一个abc组 vipw:查看系统中用户 groupadd:添加组groupadd ccna vigr:查看系统中的组 gpasswd:将用户abc添加到ccna组 gpasswd -a abc ccna groups abc:查看用户abc属