赞
踩
目录
步骤1在LVS1和LVS2虚拟机上安装keepalived和ipvs
Keepalived起初是为LVS设计的,专门用来监控集群系统中各个服务节点的状态,后来有加入VRRP的功能,VRRP是Virtual Router Redundancy protocol(虚拟路由器冗余协议)的缩写VRRP出现的目的就是为了解决静态路由器出现的单点故障问题,它能偶保证网络的不间断、稳定的运行。所有,keepalived一方面具有LVS cluster nodes healthchecks功能,另一方面也具有LVS directors faiover功能。
keepalived除了能够管理LVS软件外,还可以作为其他服务的高可用解决方案软件。采用nginx+keepalived,它是一个高性能的服务器高可用或者热备解决方案,Keepalived主要来防止服务器单点故障的发生问题,可以通过其与Nginx的配合实现Web服务器端的高可用。使用keepalived可以保证nginx的高可用,他能监控nginx的健康状态,当nginx出现宕机时自动主备切换。
服务器名称 | IP | 用途 |
---|---|---|
Nginx_Master | 192.168.186.100 | 提供负载均衡 |
Nginx_Backup | 192.168.186.101 | 提供负载均衡 |
LVS-DR-VIP | 192.168.186.102 | 网站的VIP地址 |
Web1服务器 | 192.168.186.103 | 提供Web服务 |
Web2服务器 | 192.168.186.104 | 提供Web服务 |
- 提供httpd服务
- #yum install httpd -y //安装httpd服务
- [root@web1 ~]# echo "this is first web" > /var/www/html/index.html
- [root@web1 ~]# systemctl start httpd
- [root@web1 ~]# systemctl enable httpd
- [root@web2 ~]# echo "this is second web" > /var/www/html/index.html
- [root@web2 ~]# systemctl start httpd
- [root@web2 ~]# systemctl enable httpd
两台nginx做同样配置
- 1、安装nginx
- [root@Nginx_Backup ~]# yum install nginx -y
- 2、配置nginx反向代理
- [root@Nginx_Master ~]# cd /etc/nginx/conf.d/
- [root@Nginx_Master conf.d]# vim web.conf
-
- upstream websrvs{
- server 192.168.186.103:80 weight=1;
- server 192.168.186.104:80 weight=1;
- }
-
- server{
- location / {
- proxy_pass http://websrvs;
- index index.html;
- }
- }
- 1、安装keepalived
- [root@Nginx_Master conf.d]# yum install keepalived -y
-
- 2、开发检测nginx存活的shell脚本
- [root@Nginx_Master conf.d]# vim /etc/keepalived/check_nginx.sh
- #!/bin/bash
- #代码一定注意空格,逻辑就是:如果nginx进程不存在则启动nginx,如果nginx无法启动则kill掉keepalived所有进程
- A=`ps -C nginx --no-header |wc -l`
- if [ $A -eq 0 ];then
- systemctl start nginx
- sleep 3
- if [ `ps -C nginx --no-header |wc -l` -eq 0 ]
- then
- systemctl stop keepalived
- fi
- fi
-
- 3、配置keepalived
- 1、安装keepalived
- [root@Nginx_Backup ~]# yum install keepalived -y
-
- 2、开发检测nginx存活的shell脚本
- [root@Nginx_Backup ~]# vim /etc/keepalived/check_nginx.sh
- #!/bin/bash
- #代码一定注意空格,逻辑就是:如果nginx进程不存在则启动nginx,如果nginx无法启动则kill掉keepalived所有进程
- A=`ps -C nginx --no-header |wc -l`
- if [ $A -eq 0 ];then
- systemctl start nginx
- sleep 3
- if [ `ps -C nginx --no-header |wc -l` -eq 0 ]
- then
- systemctl stop keepalived
- fi
- fi
- 3、配置keepalived
- state BACKUP //从服务器
- priority 150 //优先级
- [root@Nginx_Master ~]# systemctl restart nginx keepalived
- [root@Nginx_Backup ~]# systemctl restart nginx keepalived
-
- Master,Backup都正常,只有Master对外提供服务
- [root@Nginx_Master ~]# systemctl restart nginx keepalived
- [root@Nginx_Master ~]# ip a | grep "192.168.186.102"
- inet 192.168.186.102/32 scope global ens33
Master宕机,Backup接替Master对外提供服务
- #模拟Master的keepalived服务器停止
- [root@Nginx_Master ~]# systemctl stop keepalived
- #此时VIP在Backup上
- [root@Nginx_Backup ~]# ip a | grep "192.168.186.102"
- inet 192.168.186.102/32 scope global ens33
Master恢复正常,Master继续提供服务,Backup停止服务
- #模拟Master的keepalived服务恢复正常
- [root@Nginx_Master ~]# systemctl restart keepalived
- #此时VIP在Master上
- [root@Nginx_Master ~]# ip a | grep "192.168.186.102"
- inet 192.168.186.102/32 scope global ens33
Master上的nginx服务停止,尝试重新启动nginx
- [root@Nginx_Master ~]# systemctl stop nginx
- [root@Nginx_Master ~]# systemctl status nginx
- nginx启动正常,keepalived不做切换
本实验将使用 Keepalived 为 LVS 提供高可用配置,同时 LVS 为后端的 Nginx1 和 Nginx2 提供负载均衡,最终通过 Keepalived+LVS 实现 Nginx 集群。
LVS1 和 LVS2 通过 keepalived 组成高可用集群,同时向后端的 Nginx1 和 Nginx2 提供负载均衡。
服务器名称 | IP |
---|---|
192.168.186.100 | LVS1 |
192.168.186.101 | Nginx1 |
192.168.186.102 | Nginx2 |
192.168.186.103 | LVS2 |
192.168.186.104 | Client |
使用以下命令在LVS1和LVS2虚拟机上安装keepalived和ipvs
[root@LVS2 ~]# yum install -y keepalived ipvsadm
将LVS1的配置keepalived配置文件修改为以下内容:
- [root@LVS1 ~]# cat /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived
-
- global_defs {
- router_id LVS1 # 设置lvs的id,在一个网络应该是唯一的
- }
-
- vrrp_instance Nginx {
- state MASTER # 指定keepalived的角色,MASTER为主,BACKUP为备
- interface ens33 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡)
- mcast_src_ip 20.0.0.1
- virtual_router_id 51 # 虚拟路由编号,主从要一直
- priority 255 # 优先级,数值越大,获取处理请求的优先级越高
- advert_int 1 # 检查间隔,默认为1s(vrrp组播周期秒数)
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.186.20/24 # 定义虚拟ip(VIP),可多设,每行一个
- }
- }
-
- virtual_server 192.168.186.20 80 {
- delay_loop 6 # 设置健康检查时间,单位为秒
- lb_algo rr # 设置负载调度的算法为rr
- lb_kind DR # 设置lvs实现负载的机制,有NAT、TUN、DR三个模式
- persistence_timeout 50 # 同一IP 50秒内的请求都发到同个real server
- protocol TCP
-
- real_server 192.168.186.101 80 { # 指定real server1的ip地址
- weight 1 # 配置节点权值,数值越大权重越高
- TCP_CHECK {
- connect_timeout 3
- retry 3
- delay_before_retry 3
- }
- }
-
- real_server 192.168.186.102 80 {
- weight 2
- TCP_CHECK {
- connect_timeout 3
- retry 3
- delay_before_retry 3
- }
- }
- }
将 LVS2 的配置 keepalived 配置文件修改为以下内容:
- [root@LVS2 ~]# cat /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived
-
- global_defs {
- router_id LVS2
- }
-
- vrrp_instance VI_1 {
- state BACKUP
- interface ens33
- mcasr_src_ip 20.0.0.2
- virtual_router_id 51
- priority 100
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.186.20/24
- }
- }
-
- virtual_server 192.168.186.20 80 {
- delay_loop 6
- lb_algo rr
- lb_kind DR
- persistence_timeout 50
- protocol TCP
-
- real_server 192.168.186.101 80 {
- weight 1
- TCP_CHECK {
- connect_timeout 3
- retry 3
- delay_before_retry 3
- }
- }
-
- real_server 192.168.186.102 80 {
- weight 2
- TCP_CHECK {
- connect_timeout 3
- retry 3
- delay_before_retry 3
- }
- }
配置完成两台 LVS 服务器后,,具体如下:
- [root@LVS1 ~]# systemctl restart keepalived.service
- [root@LVS2 ~]# systemctl status keepalived.service
- [root@LVS1 ~]# systemctl restart keepalived
- [root@LVS1 ~]# 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.186.20:80 rr
- -> 192.168.186.101:80 Route 1 0 0
- -> 192.168.186.102:80 Route 2 0 0
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。