zoukankan      html  css  js  c++  java
  • Keeplive+LVS部署实例

    Keeplived部署LVM

    实验环境

    主机IP 环境角色
    192.168.197.147 HTTP后端服务器RS1
    192.168.197.150 HTTP后端服务器RS2
    192.168.197.154 LVM调度器DR1 Keepalived主服务
    192.168.197.155 LVM调度器DR2 Keepalived备服务

    步骤

    • RS安装HTTP服务,并测试访问
    [root@localhost ~]# yum install -y httpd
    
    [root@localhost ~]# vim /etc/httpd/conf/httpd.conf
    ......
    #
    ServerName localhost:80  取消注释并设置为localhost
    
    
    
    ## 开启服务并设置开机启用
    [root@localhost ~]# systemctl enable --now httpd
    Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
    [root@localhost ~]# ss -antl
    State         Recv-Q        Send-Q                 Local Address:Port                 Peer Address:Port        
    LISTEN        0             128                          0.0.0.0:22                        0.0.0.0:*           
    LISTEN        0             128                                *:80                              *:*           
    LISTEN        0             128                             [::]:22                           [::]:*           
    
    
    ## 添加主页文件并访问
    [root@localhost ~]# cd /var/www/html/
    [root@localhost html]# ls
    [root@localhost html]# touch index.html
    [root@localhost html]# vim index.html 
    
    • DR1配置LVM
    [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
    TYPE=Ethernet
    BOOTPROTO=static
    NAME=ens33
    DEVICE=ens33
    ONBOOT=yes
    IPADDR0=192.168.197.154                 IP0为DIP
    NETMASK0=255.255.255.0
    GATEWAY=192.168.197.2
    IPADDR1=192.168.197.250                 IP1为VIP
    NETMASK1=255.255.255.0
    DNS1=114.114.114.114
    ~ 
    # 重启网卡
    [root@localhost html]# systemctl restart NetworkManager ; ifdown ens33 ; ifup ens33
    
    [root@localhost ~]# ip a
    ......
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
        link/ether 00:0c:29:4e:98:e3 brd ff:ff:ff:ff:ff:ff
        inet 192.168.197.154/24 brd 192.168.197.255 scope global noprefixroute ens33
           valid_lft forever preferred_lft forever
        inet 192.168.197.250/24 brd 192.168.197.255 scope global secondary noprefixroute ens33
           valid_lft forever preferred_lft forever
        inet6 fe80::20c:29ff:fe4e:98e3/64 scope link 
           valid_lft forever preferred_lft forever
    
    • RS1与RS2配置IP
    [root@localhost html]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 
    TYPE=Ethernet
    BOOTPROTO=static
    NAME=ens33
    DEVICE=ens33
    ONBOOT=yes
    IPADDR=192.168.197.147
    GATEWAY=192.168.197.154                  网关地址指向DR的DIP
    NETMASK=255.255.255.0
    
    # 重启网卡
    [root@localhost html]# systemctl restart NetworkManager ; ifdown ens33 ; ifup ens33
    [root@localhost html]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         192.168.197.154 0.0.0.0         UG    100    0        0 ens33
    192.168.197.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
    
    • DR1上添加IP转发功能
    [root@localhost html]# vim /etc/sysctl.conf
    ......
    net.ipv4.ip_forward = 1
    
    ## 重读配置文件
    [root@localhost html]# sysctl -p
    net.ipv4.ip_forward = 1
    
    • 添加LVS规则
    [root@localhost yum.repos.d]# yum install -y ipvsadm
    
    [root@localhost yum.repos.d]# ipvsadm -A -t 192.168.197.250:80 -s rr
    [root@localhost yum.repos.d]# 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.197.250:80 rr
    
    ## 添加RS1与RS2到规则中
    [root@localhost yum.repos.d]# ipvsadm -a -t 192.168.197.250:80 -r 192.168.197.147:80 -m
    [root@localhost yum.repos.d]# ipvsadm -a -t 192.168.197.250:80 -r 192.168.197.150:80 -m
    [root@localhost yum.repos.d]# 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.197.250:80 rr
      -> 192.168.197.147:80           Masq    1      0          0         
      -> 192.168.197.150:80           Masq    1      0          0  
    
    ## 保存配置到配置文件中
    
    [root@localhost yum.repos.d]# ipvsadm -Sn > /etc/sysconfig/ipvsadm
    [root@localhost yum.repos.d]# cat /etc/sysconfig/ipvsadm
    -A -t 192.168.197.250:80 -s rr
    -a -t 192.168.197.250:80 -r 192.168.197.147:80 -m -w 1
    -a -t 192.168.197.250:80 -r 192.168.197.150:80 -m -w 1
    
    
    • 测试负载均衡
    [root@localhost yum.repos.d]# curl 192.168.197.250
    Hello WORLD1
    [root@localhost yum.repos.d]# curl 192.168.197.250
    HELLO WORLD2
    [root@localhost yum.repos.d]# curl 192.168.197.250
    Hello WORLD1
    
    • DR1上Keeplived安装并配置
    yum -y install keepalived
    
    ## 配置keepalived主配置文件
    
    [root@localhost keepalived]# vim keepalived.conf
    ! Configuration File for keepalived
    
    global_defs {
       router_id lb01
    }
    
    vrrp_instance VI_1 {
        state MASTER
        interface ens33
        virtual_router_id 51
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass wangqing
        }
        virtual_ipaddress {
            192.168.197.250
        }
    }
    
    virtual_server 172.16.12.250 80 {
        delay_loop 6
        lb_algo rr
        lb_kind DR
        persistence_timeout 50
        protocol TCP
    
        real_server 192.168.197.147 80 {
            weight 1
            TCP_CHECK {
                connect_port 80
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
        }
    
        real_server 192.168.197.150 80 {
            weight 1
            TCP_CHECK {
                connect_port 80
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
        }
    }
    
    • Keeplived备服务器配置
    
    # 配置VIP
    [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
    TYPE=Ethernet
    BOOTPROTO=static
    NAME=ens33
    DEVICE=ens33
    ONBOOT=yes
    IPADDR0=192.168.197.155
    NETMASK0=255.255.255.0
    GATEWAY=192.168.197.2
    IPADDR1=192.168.197.250
    NETMASK1=255.255.255.0
    DNS1=114.114.114.114
    
    # 重启网卡
    [root@localhost ~]# systemctl restart NetworkManager ; ifdown ens33 ; ifup ens33
    Connection 'ens33' successfully deactivated
    ......
    
    [root@localhost ~]# ip a
    ......
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
        link/ether 00:0c:29:06:50:c1 brd ff:ff:ff:ff:ff:ff
        inet 192.168.197.155/24 brd 192.168.197.255 scope global noprefixroute ens33
           valid_lft forever preferred_lft forever
        inet 192.168.197.250/24 brd 192.168.197.255 scope global secondary noprefixroute ens33
           valid_lft forever preferred_lft forever
        inet6 fe80::20c:29ff:fe06:50c1/64 scope link 
           valid_lft forever preferred_lft forever
    
    # 打开流量转发
    [root@localhost ~]# vim /etc/sysctl.conf
    [root@localhost ~]# sysctl -p
    net.ipv4.ip_forward = 1
    
    • DR2上配置LVM负载均衡
    [root@localhost yum.repos.d]# yum install -y ipvsadm
    
    [root@localhost yum.repos.d]# ipvsadm -A -t 192.168.197.250:80 -s rr
    [root@localhost yum.repos.d]# 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.197.250:80 rr
    
    
    [root@localhost yum.repos.d]# ipvsadm -a -t 192.168.197.250:80 -r 192.168.197.147:80 -m
    [root@localhost yum.repos.d]# ipvsadm -a -t 192.168.197.250:80 -r 192.168.197.150:80 -m
    
    
    [root@localhost yum.repos.d]# 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.197.250:80 rr
      -> 192.168.197.147:80           Masq    1      0          0         
      -> 192.168.197.150:80           Masq    1      0          0 
    
    • DR2上配置Keepalived高可用
    [root@localhost keepalived]# vim /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived
    
    global_defs {
       router_id lb02     路由标识位,每个keepalived都不同
    }
    
    vrrp_instance VI_1 {
        state BACKUP   
        interface ens33
        virtual_router_id 51
        priority 90     级别,级别等级高的为主服务器,反之为备服务器
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 123456            设置密码,每个Keepalive保持一致
        }
        virtual_ipaddress {
            192.168.197.250           定位VIP
        }
    }
    
    virtual_server 192.168.197.250 80 {
        delay_loop 6
        lb_algo rr
        lb_kind DR
        persistence_timeout 50
        protocol TCP
    
        real_server 192.168.197.147 80 {                 第一台RS配置
            weight 1
            TCP_CHECK {
                connect_port 80
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
        }
    
        real_server 192.168.197.150 80 {                第二台RS配置
            weight 1
            TCP_CHECK {
                connect_port 80
                connect_timeout 3
                nb_get_retry 3
                delay_before_retry 3
            }
        }
    }
    
    • 查看Keepalive主备ip
    
    ## 主服务器上查看发现已存在VIP
    [root@localhost keepalived]# ip a
    ......
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
        link/ether 00:0c:29:4e:98:e3 brd ff:ff:ff:ff:ff:ff
        inet 192.168.197.154/24 brd 192.168.197.255 scope global noprefixroute ens33
           valid_lft forever preferred_lft forever
        inet 192.168.197.250/32 scope global ens33
           valid_lft forever preferred_lft forever
        inet6 fe80::20c:29ff:fe4e:98e3/64 scope link 
           valid_lft forever preferred_lft forever
    
    ## 备服务器上查看 没有VIP
    [root@localhost keepalived]# ip a
    ......
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
        link/ether 00:0c:29:06:50:c1 brd ff:ff:ff:ff:ff:ff
        inet 192.168.197.155/24 brd 192.168.197.255 scope global noprefixroute ens33
           valid_lft forever preferred_lft forever
        inet6 fe80::20c:29ff:fe06:50c1/64 scope link 
           valid_lft forever preferred_lft forever
    
    • 测试LVM负载均衡功能
    ## 主服务器上成功访问RS
    [root@localhost keepalived]# curl 192.168.197.250
    Hello WORLD1
    [root@localhost keepalived]# curl 192.168.197.250
    HELLO WORLD2
    [root@localhost keepalived]# curl 192.168.197.250
    Hello WORLD1
    [root@localhost keepalived]# curl 192.168.197.250
    HELLO WORLD2
    [root@localhost keepalived]# curl 192.168.197.250
    Hello WORLD1
    [root@localhost keepalived]# curl 192.168.197.250
    HELLO WORLD2
    
    • 关闭RS1上的Keepalive服务,再次查看备服务器上的IP
    
    [root@localhost keepalived]# systemctl stop keepalived
    
    
    # DR1上查看ip,vip已经没有了
    [root@localhost keepalived]# ip a
    ......
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
        link/ether 00:0c:29:4e:98:e3 brd ff:ff:ff:ff:ff:ff
        inet 192.168.197.154/24 brd 192.168.124.255 scope global noprefixroute ens33
           valid_lft forever preferred_lft forever
        inet6 fe80::20c:29ff:fe4e:98e3/64 scope link 
           valid_lft forever preferred_lft forever
    
    
    # DR2上查看Ip,有2个Ip
    [root@localhost ~]# ip a
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
        link/ether 00:0c:29:06:50:c1 brd ff:ff:ff:ff:ff:ff
        inet 192.168.197.155/24 brd 192.168.124.255 scope global noprefixroute ens33
           valid_lft forever preferred_lft forever
        inet 192.168.197.250/24 scope global secondary ens33
           valid_lft forever preferred_lft forever
        inet6 fe80::20c:29ff:fe06:50c1/64 scope link 
           valid_lft forever preferred_lft forever
    
    • DR2上访问后端RS
    [root@localhost ~]# curl 192.168.197.250
    HELLO WORLD2
    [root@localhost ~]# curl 192.168.197.250
    Hello WORLD1
    [root@localhost ~]# curl 192.168.197.250
    HELLO WORLD2
    [root@localhost ~]# curl 192.168.197.250
    Hello WORLD1
    [root@localhost ~]# curl 192.168.124.250
    HELLO WORLD2
    
    .....
    
  • 相关阅读:
    利用python做矩阵的简单运算(行列式、特征值、特征向量等的求解)
    numpy.linalg.svd函数
    梯度裁剪(Clipping Gradient):torch.nn.utils.clip_grad_norm
    tf.matmul()报错expected scalar type Float but found Double
    1283 最小周长
    1182 完美字符串
    1091 线段的重叠
    1090 3个数和为0
    1087 1 10 100 1000
    1083 矩阵取数问题
  • 原文地址:https://www.cnblogs.com/sawyer95/p/14076941.html
Copyright © 2011-2022 走看看