实现master/slave的Keepalived单主架构

Posted y_zilong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实现master/slave的Keepalived单主架构相关的知识,希望对你有一定的参考价值。

​
[root@ka1 ~]# yum install -y keepalived
[root@ka1 ~]# systemctl start keepalived.service
[root@ka1 ~]# ps auxf |grep keepalived
root       28262  0.0  0.0  12112   968 pts/0    S+   08:15   0:00  |           \\_ grep --color=auto keepalived

#默认是起不来,因为配置文件没配置

全局配置

启动keepalived独立日志功能

[root@ka1 ~]# vi /etc/sysconfig/keepalived 
KEEPALIVED_OPTIONS="-D -S 6"

[root@ka1 ~]# vi /etc/rsyslog.conf 
local6.*                           /var/log/keepalived.log

[root@ka1 ~]# systemctl restart keepalived.service rsyslog.service
[root@ka1 ~]# tail -f /var/log/keepalived.log 

实现独立子配置文件

[root@ka1 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
     123456@163.com             #发生故障切换时邮件发送的对象,可以按行区分写多个
   }
   notification_email_from 123456@qq.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id ka1.example.com
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

include /etc/keepalived/conf.d/*.conf    #将VRRP相关配置放在子配置文件中
[root@ka1 ~]# 

MASTER配置

[root@ka1 ~]# cat /etc/keepalived/conf.d/m1.conf 
vrrp_instance m1 {
    state MASTER
    interface ens33
    virtual_router_id 66     #每个虚拟路由器必须唯一,同属一个虚拟路由器的多个keepalived节点必须相同
    priority 100            #优先级高
    advert_int 1
    preempt_delay 10       #抢占延迟模式,默认延迟300s
    authentication {
        auth_type PASS      #预共享密钥认证,同一个虚拟路由器的keepalived节点必须一样
        auth_pass 123456
    }
    virtual_ipaddress {
      10.0.0.100/24 dev ens33 label ens33:1
    }
    unicast_src_ip 10.0.0.10      #本机IP
    unicast_peer {                 
      10.0.0.20                #指向对方主机IP,如果有多个keepalived,在加其他节点的IP
    }                                
}
[root@ka1 ~]# 

BACKUP配置

[root@ka2 ~]# cat /etc/keepalived/conf.d/m2.conf 
vrrp_instance m2 {
    state BACKUP
    interface ens33
    virtual_router_id 66
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
      10.0.0.100/24 dev ens33 label ens33:0
    }
    unicast_src_ip 10.0.0.20
    unicast_peer { 
    10.0.0.20
    }
}
[root@ka2 ~]# 

抓包

[root@ka1 ~]# hostname -I
10.0.0.10 10.0.0.100 
[root@ka1 ~]# 
[root@ka2 ~]# hostname -I
10.0.0.20 
[root@ka2 ~]# 

[root@ka1 ~]# tcpdump -i ens33 -nn src host 10.0.0.10 and dst host 10.0.0.20
dropped privs to tcpdump
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
22:44:58.108813 IP 10.0.0.10 > 10.0.0.20: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
22:44:59.109771 IP 10.0.0.10 > 10.0.0.20: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
[root@ka1 ~]#

 

以上是关于实现master/slave的Keepalived单主架构的主要内容,如果未能解决你的问题,请参考以下文章

keepalived+nginx实现高可用and负载均衡集群

haproxy+keepalived+mycat+mysql (读写分离)

Nginx+keepalived双机热备(主主模式)

Nginx+keepalived双机热备(主主模式)

keepalived+nginx双机热备+负载均衡

Linux下部署LVS(DR)+keepalived+Nginx负载均衡