lvs+keepalived+nginx高可用集群

2021-03-09 16:56:00
admin
原创
1465
摘要:lvs+keepalived+nginx高可用集群

lvs+keepalived+nginx高可用集群

1、首先我找了两台机器

机器的ip是,192.168.56.101和192.168.56.102

设置的虚拟机的ip是:192.168.56.103

101和102分别装上keepalived和nginx

nginx在博客中已经安装过,本文主要安装keepalived

2、安装keepalived

yum -y install ipvsadm

重启命令

systemctl restart keepalived

安装之后,vi /etc/keepalived/keepalived.conf

配置文件如下:

102是备份的,配置文件如下:


! Configuration File for keepalived

global_defs {
   router_id lvs102
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth1
    virtual_router_id 101
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.56.103
    }
}
virtual_server 192.168.56.103 80 {
    delay_loop 6                 
    lb_algo rr                  
    lb_kind DR                   
    #persistence_timeout 50        
    protocol TCP               
    real_server 192.168.56.101 80 {
      weight 1               
      TCP_CHECK {
       connect_timeout 2
       nb_get_retry 2
       connect_port 80
       delay_befor_retry 3
       }
    }
    real_server 192.168.56.102 80 {
      weight 1               
      TCP_CHECK {
       connect_timeout 2
       nb_get_retry 2
       connect_port 80
       delay_befor_retry 3
       }
    }
}


101是主的配置文件如下:


! Configuration File for keepalived

global_defs {
   router_id lvs101
}

vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 101
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.56.103
    }
}
virtual_server 192.168.56.103 80 {
    delay_loop 6                 
    lb_algo rr                  
    lb_kind DR                   
    #persistence_timeout 50        
    protocol TCP               
    real_server 192.168.56.101 80 {
      weight 1             
      TCP_CHECK {
       connect_timeout 2
       nb_get_retry 2
       connect_port 80
       delay_befor_retry 3
      }
    }
    real_server 192.168.56.102 80 {
      weight 1               
      TCP_CHECK {            
       connect_timeout 2  
       nb_get_retry 2
       connect_port 80
       delay_befor_retry 3     
       }
    }

}
3、配置之后分别安装ipvs


yum -y install ipvsadm
然后会出现错误执行下面的就可以了
ipvsadm --save > /etc/sysconfig/ipvsad

删除集群

ipvsadm -C

查看集群

ipvsadm -Ln

重启ipvs

systemctl restart ipvsadm.service

ipvs的状态查看

4、两台机器都要执行如下:

安装ifconfig

yum install net-tools.x86_64

ifconfig lo:0 192.168.56.103 netmask 255.255.255.255 up(如果不执行,会出现能ping不通的情况,也访问不了的情况)

5、然后查看效果

停到nginx或者keepalived会出现不同的效果

7、上面的方式不能修改端口号,如果端口号修改了,就会出现ping通,但是打不开的效果

可以使用下面的方式添加

ipvsadm -a -t 192.168.56.103:80 -r 192.168.56.101:80 -m -w 1
ipvsadm -a -t 192.168.56.103:80 -r 192.168.56.102:80 -m -w 1

    发表评论
    评论通过审核之后才会显示。
    文章分类
    联系方式
    联系人: 郑州-小万
    电话: 13803993919
    Email: 1027060531@qq.com
    QQ: 1027060531
    网址: www.wanhejia.com