lvs+keepalived+mariadb集群

1、环境准备
节点1:172.16.2.95
节点2:172.16.2.160
节点3:172.16.2.220
LVS1:172.16.2.67
LVS2:172.16.2.234

2、mariadb集群的安装

https://www.cnblogs.com/linyouyi/p/11515639.html

3、在三台安装mariadb机器操作

echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
ifconfig lo:0 172.16.2.253 broadcast 172.16.2.253 netmask 255.255.255.255 up
#ifconfig lo:0 172.16.2.253 netmask 255.255.255.255 up
route add -host 172.16.2.253 lo:0

3.1、查看虚拟ip

//01
[root@hadoop01 linyouyi]# ip addr show lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet 172.16.2.253/32 brd 172.16.2.253 scope global lo:0
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
[root@hadoop01 linyouyi]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.16.2.1      0.0.0.0         UG    0      0        0 eth0
0.0.0.0         172.16.2.1      0.0.0.0         UG    100    0        0 eth0
169.254.169.254 172.16.2.1      255.255.255.255 UGH   100    0        0 eth0
172.16.2.0      0.0.0.0         255.255.255.0   U     100    0        0 eth0
172.16.2.253    0.0.0.0         255.255.255.255 UH    0      0        0 lo
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
//02
[root@hadoop02 mariadb]# ip addr show lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet 172.16.2.253/32 brd 172.16.2.253 scope global lo:0
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
[root@hadoop02 mariadb]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.16.2.1      0.0.0.0         UG    100    0        0 eth0
169.254.169.254 172.16.2.1      255.255.255.255 UGH   100    0        0 eth0
172.16.2.0      0.0.0.0         255.255.255.0   U     100    0        0 eth0
172.16.2.253    0.0.0.0         255.255.255.255 UH    0      0        0 lo
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
//03
[root@hadoop03 mariadb]# ip addr show lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet 172.16.2.253/32 brd 172.16.2.253 scope global lo:0
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
[root@hadoop03 mariadb]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.16.2.1      0.0.0.0         UG    100    0        0 eth0
169.254.169.254 172.16.2.1      255.255.255.255 UGH   100    0        0 eth0
172.16.2.0      0.0.0.0         255.255.255.0   U     100    0        0 eth0
172.16.2.253    0.0.0.0         255.255.255.255 UH    0      0        0 lo
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0

 

4、LVS1安装keepalived-1.3.5和ipvsadm-1.27

[root@LVS1 ~]# yum -y install keepalived ipvsadm
[root@LVS1 ~]# cd /etc/keepalived
[root@LVS1 keepalived]# cp keepalived.conf keepalived.conf.bak
[root@LVS1 keepalived]# vim keepalived.conf
global_defs {
    notification_email {
        admin@linyouyi.com
    }
    notification_email_from ka@localhost
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id lvs1
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 50
    priority 100
    advert_int 1
    authentication {
        auth_type pass
        auth_pass forlvs
    }
    virtual_ipaddress { 
        172.16.2.253
    }
}

virtual_server 172.16.2.253 3306 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.255.255
    persistence_timeout 600
    protocol TCP
    real_server 172.16.2.95 3306 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
    real_server 172.16.2.220 3306 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
    real_server 172.16.2.160 3306 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}


[root@LVS1 keepalived]# systemctl start keepalived
[root@LVS1 keepalived]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.2.253:3306 rr persistent 600
  -> 172.16.2.95:3306             Route   1      0          0         
  -> 172.16.2.160:3306            Route   1      0          0         
  -> 172.16.2.220:3306            Route   1      0          0 
// 但是其他的机器总是访问不了vip,不知道是云主机不能用keepalived虚拟ip还是啥原因,还没找到

4.1、测试

[root@LVS1 keepalived]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether fa:16:3e:2f:b0:e0 brd ff:ff:ff:ff:ff:ff
    inet 172.16.2.67/24 brd 172.16.2.255 scope global dynamic eth0
       valid_lft 85187sec preferred_lft 85187sec
    inet 172.16.2.253/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::f816:3eff:fe2f:b0e0/64 scope link 
       valid_lft forever preferred_lft forever
//把数据库220的mariadb停掉
[root@LVS1 keepalived]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  cx--0009.novalocal:mysql rr persistent 600
  -> 172.16.2.95:mysql            Route   1      0          0         
  -> 172.16.2.160:mysql           Route   1      0          0

5、LVS1安装keepalived-1.3.5和ipvsadm-1.27

[root@LVS2 keepalived]# vim keepalived.conf
global_defs {
    
    router_id lvs2
}

vrrp_instance VI_1 {
    state BACKUP
    
    priority 99
    
    }
    
}
//配置文件这几项改一下,其余的步骤和LVS1一样

5.1、测试LVS高可用

把LVS1的keepalived停掉,看VIP会不会自动移动到LVS2

[root@LVS1 keepalived]# systemctl stop keepalived

[root@LVS2 ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether fa:16:3e:a3:75:41 brd ff:ff:ff:ff:ff:ff
    inet 172.16.2.234/24 brd 172.16.2.255 scope global dynamic eth0
       valid_lft 62412sec preferred_lft 62412sec
    inet 172.16.2.253/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::f816:3eff:fea3:7541/64 scope link 
       valid_lft forever preferred_lft forever
[root@LVS2 ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  cx--0015.novalocal:mysql rr persistent 600
  -> 172.16.2.95:mysql            Route   1      0          0         
  -> 172.16.2.160:mysql           Route   1      0          0         
  -> 172.16.2.220:mysql           Route   1      0          0 

但是其他的机器总是访问不了vip,不知道是云主机不能用keepalived虚拟ip还是啥原因,还没找到,似乎是云服务器的vip需要申请,跟服务商申请。反正就是云服务器跟虚拟机不一样

https://www.cnblogs.com/galengao/articles/7417520.html

//每台主机查一下arp,发现VIP的HWaddress都是incomplete,也就是无法获取mac硬件地址,所以不知道指向那台机器,因此无法ping通,无法访问
[root@cx--0008 ~]# arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
172.16.2.1               ether   fa:16:3e:3d:4d:a0   C                     eth0
10.42.2.0                        (incomplete)                              flannel.1
172.16.2.95              ether   fa:16:3e:dc:a1:c6   C                     eth0
172.16.2.222                     (incomplete)                              eth0
172.16.2.67              ether   fa:16:3e:2f:b0:e0   C                     eth0
172.17.0.2               ether   02:42:ac:11:00:02   C                     docker0
10.42.1.0                        (incomplete)                              flannel.1
172.17.0.4               ether   02:42:ac:11:00:04   C                     docker0
172.16.2.254             ether   fa:16:3e:de:bd:f9   C                     eth0
172.16.2.234             ether   fa:16:3e:a3:75:41   C                     eth0
172.16.2.253                     (incomplete)                              eth0
172.17.0.3               ether   02:42:ac:11:00:03   C                     docker0
172.16.2.17              ether   fa:16:3e:f3:5e:6f   C                     eth0

//清空arp缓存依然不行
[root@LVS1 keepalived]# arp -n|awk '/^[1-9]/{system("arp -d "$1)}'
SIOCDARP(dontpub): Network is unreachable
SIOCDARP(dontpub): Network is unreachable

 

 

ipvsadm -lnc

posted @ 2019-09-19 23:57  linyouyi  阅读(660)  评论(0编辑  收藏  举报