首页 > 其他 > 详细

20.keepalived负载均衡--高可用

时间:2019-08-02 23:25:45      阅读:131      评论:0      收藏:0      [点我收藏+]
1.1keepalived服务概念说明
keepalived软件能干什么?
Keepalived软件起初是专为LVS负载均衡软件设计的,
用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能

Keepalived软件主要是通过VRRP协议实现高可用功能的。
VRRP是Virtual Router Redundancy Protocol(虚拟路由器冗余协议)的缩写,
VRRP出现的目的就是为了解决静态路由单点故障问题的,它能够保证当个别节点宕机时,
整个网络可以不间断地运行

keepalived软件工作原理?(重点)
原理
1)VRRP协议,全称Virtual Router Redundancy Protocol,中文名为虚拟路由冗余协议,
VRRP的出现是为了解决静态路由的单点故障。
2)VRRP是用过IP多播的方式(默认多播地址(224.0.0.18))实现高可用对之间通信的。
3)工作时主节点发包,备节点接包,当备节点接收不到主节点发的数据包的时候,
就启动接管程序接管主节点的资源。备节点可以有多个,通过优先级竞选,
但一般Keepalived系统运维工作中都是一对。

keepalived软件主要功能?
①. 管理LVS负载均衡软件
②. 实现对LVS集群节点健康检查功能
③. 作为系统网络服务的高可用功能

1.2部署keepalived高可用服务:

1)确认反向代理服务是否工作正常
第一个里程:在lb01/lb02上测试web服务器是否可以正常
lb01/lb02上nginx配置

[root@lb01 scripts]# cat /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream oldboy {
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80;
}
server {
listen 80;
server_name www.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 80;
server_name bbs.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
7.8.9服务器上nginx配置

[root@web01 ~]# cat /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org;
root html/www;
index index.html index.htm;
}
server {
listen 80;
server_name bbs.etiantian.org;
root html/bbs;
index index.html index.htm;
}

}
验证负载均衡功能是否正常

curl -H host:www.etiantian.org 10.0.0.7/oldboy.html
curl -H host:www.etiantian.org 10.0.0.8/oldboy.html
curl -H host:www.etiantian.org 10.0.0.9/oldboy.html
curl -H host:bbs.etiantian.org 10.0.0.7/oldboy.html
curl -H host:bbs.etiantian.org 10.0.0.8/oldboy.html
curl -H host:bbs.etiantian.org 10.0.0.9/oldboy.html

第二个里程:在浏览器上测试访问lb01/lb02
解析hosts文件,将域名解析为10.0.0.5,进行测试访问
解析hosts文件,将域名解析为10.0.0.6,进行测试访问
scp -rp /application/nginx/conf/nginx.conf 10.0.0.6:/application/nginx/conf/ ---测试前同步lb01和lb02配置文件

2)安装部署高可用keepalived服务
第一个里程:安装keepalived服务软件
yum install -y keepalived

第二个里程:编写keepalived配置文件
vim /etc/keepalived/keepalived.conf
man keepalived.conf --- 配置文件说明信息
配置文件结构:
GLOBAL CONFIGURATION --- 全局配置()
VRRPD CONFIGURATION --- vrrp配置()
LVS CONFIGURATION --- LVS服务相关配置
advert_int 1 --- 通告的间隔时间,即主端每隔多久向从端发送心跳信息
lb01主负载均衡器配置
global_defs {
router_id lb01
}

vrrp_instance gorup01 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
/etc/init.d/keepalived reload

lb02配置信息
global_defs {
router_id lb02
}

vrrp_instance group01 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
/etc/init.d/keepalived reload
3)进行测试访问
本地电脑中的hosts
10.0.0.3 www.etiantian.org
10.0.0.3 bbs.etiantian.org
访问http://www.etiantian.org/仍然可以实现负载均衡

1.3 部署keepalived高可用服务问题

同时在keepalived高可用集群中,出现了两个虚拟IP地址信息,这种情况就称为脑裂

脑裂情况出现原因:
1. 心跳线出现问题
   网卡配置有问题
   交换设备有问题
   线缆连接有问题
2. 有防火墙软件阻止问题
3. virtual_router_id配置数值不正确
总之:只要备服务器收不到组播包,就会成为主,而主资源没有释放,就会出现脑裂

利用shell脚本实现监控管理:
备用设备有VIP就是表示不正常
01. 真正实现主备切换
02. 出现脑裂情况了

[root@lb01 keepalived]# ip a|grep 10.0.0.3
inet 10.0.0.3/24 scope global secondary eth0:1

[root@lb01 keepalived]# ip a|grep -c 10.0.0.3
1

#!/bin/bash
check_info=$(ip a|grep -c 10.0.0.3)
if [ $check_info -ne 0 ]
then
echo "keepalived server error!!!"
fi

1.4 实现nginx反向代理监控虚拟IP地址

如果不配置监听,那么即可以通过NGINX所在的服务器上的其他网卡访问,
也可以通过虚拟Ip访问,这样是不安全的,所以要配置Nginx只监听虚拟Ip,下面是配置
1)编写nginx反向代理配置
server {
listen 10.0.0.3:80;
server_name www.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 10.0.0.3:80;
server_name bbs.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
/application/nginx/sbin/nginx -s stop
/application/nginx/sbin/nginx
netstat -lntup|grep nginx
tcp 0 0 10.0.0.3:80 0.0.0.0:* LISTEN 53334/nginx

实现监听本地网卡上没有的IP地址
echo ‘net.ipv4.ip_nonlocal_bind = 1‘ >>/etc/sysctl.conf
sysctl -p

1.5将keepalived服务和反向代理nginx服务建立联系

nginx反向代理服务停止,keepalived服务也停止
1)编写脚本
#!/bin/bash
web_info=$(ps -ef|grep [n]ginx|wc -l)
if [ $web_info -lt 2 ]
then
/etc/init.d/keepalived stop
fi

2)运行脚本,实现监控nginx服务
编辑keepalived服务配置文件
vrrp_script check_web {
#定义一个监控脚本,脚本必须有执行权限
script "/server/scripts/check_web.sh"
#指定脚本间隔时间
interval 2
#脚本执行完成,让优先级值和权重值进行运算,从而实现主备切换  
weight 2
}

track_script {
check_web
}
[root@lb01 scripts]# cat /etc/keepalived/keepalived.conf    
global_defs {
router_id lb01
}
vrrp_script check_web {
#定义一个监控脚本,脚本必须有执行权限
script "/server/scripts/check_web.sh"
#指定脚本间隔时间
interval 2
#脚本执行完成,让优先级值和权重值进行运算,从而实现主备切换  
weight 2
}

vrrp_instance gorup01 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
track_script {
check_web
}
}
chmod +x check_web.sh --- 修改脚本可执行权限
停止nginx服务,可发现keepalived服务也被关闭,虚拟ip去了另一台服务器上

1.6实现高可用集群架构中双主配置(互为主备配置)

lb01

[root@lb01 scripts]#cat /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream oldboy {
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80;
}
server {
listen 10.0.0.3:80;
server_name www.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 10.0.0.4:80;
server_name bbs.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}

[root@lb01 scripts]#cat /etc/keepalived/keepalived.conf
global_defs {
router_id lb01
}
vrrp_instance gorup01 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
vrrp_instance gorup02 {
state BACKUP
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:1
}
}

lb02

[root@lb02 scripts]#cat /etc/keepalived/keepalived.conf
global_defs {
router_id lb02
}
vrrp_instance gorup01 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
vrrp_instance gorup02 {
state MASTER
interface eth0
virtual_router_id 52
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:1
}
}

[root@lb02 scripts]# cat /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream oldboy {
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80;
}
server {
listen 10.0.0.3:80;
server_name www.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 10.0.0.4:80;
server_name bbs.etiantian.org;
root html;
index index.html index.htm;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}

本地hosts
10.0.0.3 www.etiantian.org
10.0.0.4 bbs.etiantian.org

20.keepalived负载均衡--高可用

原文:https://blog.51cto.com/10983441/2426100

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!