keepalived01-安装keepalived

  • https://www.keepalived.org
  • https://github.com/acassen/keepalived

1、安装keepalived(yum)

  • keepalived主网站不提供任何Linux发行版的软件包,它只提供源代码和代码库。
  • 大多数Linux发行版都提供keepalived包作为主线包,包维护人员在维护和报告有关它的问题方面做得很好。

1.1、部署环境

  • 软件版本
    • keepalived:
  • 系统环境
    • 10.1.1.11:CentOSLinuxrelease7.7.1908(Core)
    • 10.1.1.12:CentOSLinuxrelease7.7.1908(Core)

1.2、安装前的准备

1、时间同步

#安装ntpdate
yum install ntpdate -y
#同步时间
ntpdate ntp1.aliyun.com

2、关闭防火墙

#查看防护墙状态
systemctl status firewalld.service
#关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service

3、禁用selinux

#查看selinux的状态
getenforce
#临时有效
setenforce 0
#永久有效。改系统文件/etc/sysconfig/selinux,然后重启机器
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

4、开启网路接口的多播功能

#开启用于集群服务的网络接口的多播功能(MULTICAST状态)
ip link set multicast on dev ens33

1.3、安装keepalived

1、安装keepalived

  • CentoS 6.4版本之后,keepalived服务由base仓库提供。
yum install keepalived

2、查看keepalived服务的主要文件

]# rpm -ql keepalived 
/etc/keepalived/keepalived.conf               //主配置文件
/etc/sysconfig/keepalived                     //Unit File的环境文件
/usr/lib/systemd/system/keepalived.service    //Unit File
/usr/sbin/keepalived                          //主程序文件
......

3、修改unit文件

  • 默认会自动生成unit文件
    • 需要修改自动生成的unit文件,否则不能使用systemctl stop keepalived.service命令停止服务
]# vim /usr/lib/systemd/system/keepalived.service
[Unit]
Description=LVS and VRRP High Availability Monitor
After=network-online.target syslog.target
Wants=network-online.target
 
[Service]
Type=forking
EnvironmentFile=-/etc/sysconfig/keepalived
ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS
Restart=on-failure
RestartSec=5
LimitNOFILE=65536
 
[Install]
WantedBy=multi-user.target
  • 重读系统服务配置,使其生效
]# systemctl daemon-reload

4、keepalived配置文件

  • 修改配置文件/etc/keepalived/keepalived.conf
    • 这里仅仅实现了vip飘逸(通过启停keepalived)
  • 主备模型之主节点(10.1.1.11)
]# vim /etc/keepalived/keepalived.conf
global_defs {
    notification_email {
        root@localhost
    }
    notification_email_from keepalived@hengha11
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
 
    router_id hengha11
    vrrp_mcast_group4 224.1.1.1
}
 
vrrp_instance VR_1 {
    state MASTER
    priority 100
    interface ens33
    virtual_router_id 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass hh101
    }
    virtual_ipaddress {
        10.1.1.99/24 dev ens33 label ens33:1
    }
}
  • 主备模型之备节点(10.1.1.12)
]# vim /etc/keepalived/keepalived.conf
global_defs {
    notification_email {
        root@localhost
    }
    notification_email_from keepalived@hengha11
    smtp_server 127.0.0.1
    smtp_connect_timeout 30

    router_id hengha12    #!
    vrrp_mcast_group4 224.1.1.1
}

vrrp_instance VR_1 {
    state BACKUP          #!
    priority 95           #!
    interface ens33
    virtual_router_id 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass hh101
    }
    virtual_ipaddress {
        10.1.1.99/24 dev ens33 label ens33:1
    }
}

5、启动keepalived

systemctl enable keepalived.service
systemctl start keepalived.service

systemctl status keepalived.service

systemctl stop keepalived.service

2、安装keepalived(源码编译)

2.1、部署环境

  • 软件版本
    • keepalived:2.1.5
  • 系统环境
    • 10.1.1.11:CentOSLinuxrelease7.7.1908(Core)
    • 10.1.1.12:CentOSLinuxrelease7.7.1908(Core)

2.2、安装前的准备

1、时间同步

#安装ntpdate
yum install ntpdate -y
#同步时间
ntpdate ntp1.aliyun.com

2、关闭防火墙

#查看防护墙状态
systemctl status firewalld.service
#关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service

3、禁用selinux

#查看selinux的状态
getenforce
#临时有效
setenforce 0
#永久有效。改系统文件/etc/sysconfig/selinux,然后重启机器
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

4、开启网路接口的多播功能

#开启用于集群服务的网络接口的多播功能(MULTICAST状态)
ip link set multicast on dev ens33

2.3、安装keepalived

1、安装依赖

]# yum install gcc openssl-devel libnl libnl3-devel -y

2、安装keepalived

]# wget https://www.keepalived.org/software/keepalived-2.1.5.tar.gz --no-check-certificate
]# tar zvfx keepalived-2.1.5.tar.gz

]# cd keepalived-2.1.5/
]# mkdir -pv /apps/keepalived-2.1.5

]# ./configure --prefix=/apps/keepalived-2.1.5
]# make && make install
  • 问题:在执行make的时候由很多警告信息(不知道是否有影响)
]# make
Making all in lib
make[1]: 进入目录“/root/keepalived-2.1.5/lib”
make  all-am
make[2]: 进入目录“/root/keepalived-2.1.5/lib”
  CC       memory.o
  CC       utils.o
utils.c:1091:1: 警告:function might be candidate for attribute ‘pure’ [-Wsuggest-attribute=pure]
 memcmp_constant_time(const void *s1, const void *s2, size_t n)
 ^
  CC       notify.o
  CC       timer.o
  CC       scheduler.o
  CC       vector.o
  CC       html.o
  CC       parser.o
  CC       signals.o
  CC       logger.o
  CC       list_head.o
  CC       rbtree.o
  CC       process.o
  CC       json_writer.o
  CC       rttables.o
rttables.c:60:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTN_LOCAL, "local", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:61:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTN_NAT, "nat", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:62:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTN_BROADCAST, "broadcast", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:63:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTN_BROADCAST, "brd", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:64:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTN_ANYCAST, "anycast", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:65:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTN_MULTICAST, "multicast", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:66:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTN_PROHIBIT, "prohibit", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:67:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTN_UNREACHABLE, "unreachable", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:68:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTN_BLACKHOLE, "blackhole", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:69:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTN_XRESOLVE, "xresolve", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:70:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTN_UNICAST, "unicast", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:71:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTN_THROW, "throw", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:72:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { 0, NULL, {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:76:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTPROT_UNSPEC, "none", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:77:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTPROT_REDIRECT, "redirect", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:78:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTPROT_KERNEL, "kernel", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:79:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTPROT_BOOT, "boot", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:80:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTPROT_STATIC, "static", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:82:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTPROT_GATED, "gated", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:83:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTPROT_RA, "ra", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:84:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTPROT_MRT, "mrt", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:85:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTPROT_ZEBRA, "zebra", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:86:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTPROT_BIRD, "bird", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:90:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTPROT_DNROUTED, "dnrouted", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:91:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTPROT_XORP, "xorp", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:92:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTPROT_NTK, "ntk", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:93:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RTPROT_DHCP, "dhcp", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:94:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { 0, NULL, {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:98:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RT_TABLE_DEFAULT, "default", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:99:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RT_TABLE_MAIN, "main", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:100:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RT_TABLE_LOCAL, "local", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:101:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { 0, NULL, {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:106:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RT_SCOPE_UNIVERSE, "global", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:107:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RT_SCOPE_NOWHERE, "nowhere", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:108:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RT_SCOPE_HOST, "host", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:109:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RT_SCOPE_LINK, "link", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:110:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { RT_SCOPE_SITE, "site", {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
rttables.c:111:2: 警告:missing initializer for field ‘next’ of ‘list_head_t’ [-Wmissing-field-initializers]
  { 0, NULL, {}},
  ^
In file included from rttables.c:31:0:
list_head.h:49:20: 附注:‘next’在此声明
  struct list_head *next, *prev;
                    ^
  AR       liblib.a
make[2]: 离开目录“/root/keepalived-2.1.5/lib”
make[1]: 离开目录“/root/keepalived-2.1.5/lib”
Making all in keepalived
make[1]: 进入目录“/root/keepalived-2.1.5/keepalived”
Making all in core
make[2]: 进入目录“/root/keepalived-2.1.5/keepalived/core”
  CC       main.o
main.c: 在函数‘sigend’中:
main.c:952:34: 警告:assuming signed overflow does not occur when simplifying conditional to constant [-Wstrict-overflow]
   for (i = 0; i < NUM_CHILD_TERM && wait_count; i++) {
                                  ^
main.c:990:6: 警告:assuming signed overflow does not occur when simplifying conditional to constant [-Wstrict-overflow]
   if (wait_count) {
      ^
main.c:917:8: 警告:assuming signed overflow does not occur when simplifying conditional to constant [-Wstrict-overflow]
  while (wait_count) {
        ^
  CC       daemon.o
  CC       pidfile.o
  CC       layer4.o
  CC       smtp.o
  CC       global_data.o
  CC       global_parser.o
  CC       keepalived_netlink.o
  CC       namespaces.o
In file included from /usr/include/libnl3/netlink/socket.h:16:0,
                 from namespaces.c:171:
/usr/include/libnl3/netlink/handlers.h:51:15: 警告:‘struct nlmsgerr’在形参表内部声明 [默认启用]
        struct nlmsgerr *nlerr, void *arg);
               ^
/usr/include/libnl3/netlink/handlers.h:51:15: 警告:它的作用域仅限于此定义或声明,这可能并不是您想要的 [默认启用]
/usr/include/libnl3/netlink/handlers.h:51:15: 警告:‘struct sockaddr_nl’在形参表内部声明 [默认启用]
/usr/include/libnl3/netlink/handlers.h:137:18: 警告:‘struct sockaddr_nl’在形参表内部声明 [默认启用]
           struct ucred **));
                  ^
  CC       track_process.o
  CC       reload_monitor.o
  AR       libcore.a
make[2]: 离开目录“/root/keepalived-2.1.5/keepalived/core”
Making all in vrrp
make[2]: 进入目录“/root/keepalived-2.1.5/keepalived/vrrp”
  CC       vrrp_daemon.o
  CC       vrrp_print.o
  CC       vrrp_data.o
  CC       vrrp_parser.o
  CC       vrrp.o
  CC       vrrp_notify.o
  CC       vrrp_scheduler.o
vrrp_scheduler.c: 在函数‘try_up_instance’中:
vrrp_scheduler.c:612:2: 警告:missing initializer for field ‘ifa’ of ‘ip_address_t’ [-Wmissing-field-initializers]
  ip_address_t ip_addr = {};
  ^
In file included from ../../keepalived/include/vrrp_arp.h:33:0,
                 from vrrp_scheduler.c:43:
../../keepalived/include/vrrp_ipaddress.h:43:19: 附注:‘ifa’在此声明
  struct ifaddrmsg ifa;
                   ^
vrrp_scheduler.c: 在函数‘vrrp_dispatcher_read’中:
vrrp_scheduler.c:945:6: 警告:提领类型双关的指针将破坏强重叠规则 [-Wstrict-aliasing]
      vrrp->rx_ttl_hop_limit = *(unsigned int *)CMSG_DATA(cmsg);
      ^
In file included from /usr/include/netinet/ip.h:24:0,
                 from vrrp_scheduler.c:26:
vrrp_scheduler.c:951:60: 警告:提领类型双关的指针将破坏强重叠规则 [-Wstrict-aliasing]
      vrrp->multicast_pkt = IN6_IS_ADDR_MULTICAST(&((struct in6_pktinfo *)CMSG_DATA(cmsg))->ipi6_addr);
                                                            ^
  CC       vrrp_sync.o
  CC       vrrp_arp.o
  CC       vrrp_if.o
vrrp_if.c: 在函数‘set_default_garp_delay’中:
vrrp_if.c:434:2: 警告:missing initializer for field ‘garp_interval’ of ‘garp_delay_t’ [-Wmissing-field-initializers]
  garp_delay_t default_delay = {};
  ^
In file included from ../../keepalived/include/global_data.h:52:0,
                 from vrrp_if.c:54:
../../keepalived/include/vrrp_if.h:74:13: 附注:‘garp_interval’在此声明
  timeval_t  garp_interval;  /* Delay between sending gratuitous ARP messages on an interface */
             ^
  CC       vrrp_track.o
  CC       vrrp_ipaddress.o
vrrp_ipaddress.c: 在函数‘clear_diff_static_addresses’中:
vrrp_ipaddress.c:768:2: 警告:missing initializer for field ‘family’ of ‘vrrp_t’ [-Wmissing-field-initializers]
  vrrp_t old = {};
  ^
In file included from ../../keepalived/include/vrrp_ipaddress.h:35:0,
                 from vrrp_ipaddress.c:31:
../../keepalived/include/vrrp.h:213:15: 附注:‘family’在此声明
  sa_family_t  family;   /* AF_INET|AF_INET6 */
               ^
vrrp_ipaddress.c:769:2: 警告:missing initializer for field ‘family’ of ‘vrrp_t’ [-Wmissing-field-initializers]
  vrrp_t new = {};
  ^
In file included from ../../keepalived/include/vrrp_ipaddress.h:35:0,
                 from vrrp_ipaddress.c:31:
../../keepalived/include/vrrp.h:213:15: 附注:‘family’在此声明
  sa_family_t  family;   /* AF_INET|AF_INET6 */
               ^
  CC       vrrp_ndisc.o
  CC       vrrp_if_config.o
  CC       vrrp_static_track.o
  CC       vrrp_vmac.o
  CC       vrrp_ipsecah.o
  CC       vrrp_iproute.o
  CC       vrrp_iprule.o
  CC       vrrp_ip_rule_route_parser.o
  AR       libvrrp.a
make[2]: 离开目录“/root/keepalived-2.1.5/keepalived/vrrp”
Making all in check
make[2]: 进入目录“/root/keepalived-2.1.5/keepalived/check”
  CC       check_daemon.o
  CC       check_data.o
  CC       check_parser.o
  CC       check_api.o
  CC       check_tcp.o
  CC       check_http.o
  CC       check_ssl.o
  CC       check_smtp.o
  CC       check_misc.o
  CC       check_dns.o
  CC       check_print.o
  CC       ipwrapper.o
  CC       ipvswrapper.o
  CC       libipvs.o
  CC       check_udp.o
  CC       check_ping.o
  CC       check_file.o
  AR       libcheck.a
make[2]: 离开目录“/root/keepalived-2.1.5/keepalived/check”
Making all in trackers
make[2]: 进入目录“/root/keepalived-2.1.5/keepalived/trackers”
  CC       track_file.o
  AR       libtracker.a
make[2]: 离开目录“/root/keepalived-2.1.5/keepalived/trackers”
Making all in etc
make[2]: 进入目录“/root/keepalived-2.1.5/keepalived/etc”
Making all in init
make[3]: 进入目录“/root/keepalived-2.1.5/keepalived/etc/init”
make[3]: 对“all”无需做任何事。
make[3]: 离开目录“/root/keepalived-2.1.5/keepalived/etc/init”
Making all in init.d
make[3]: 进入目录“/root/keepalived-2.1.5/keepalived/etc/init.d”
make[3]: 对“all”无需做任何事。
make[3]: 离开目录“/root/keepalived-2.1.5/keepalived/etc/init.d”
make[3]: 进入目录“/root/keepalived-2.1.5/keepalived/etc”
make[3]: 对“all-am”无需做任何事。
make[3]: 离开目录“/root/keepalived-2.1.5/keepalived/etc”
make[2]: 离开目录“/root/keepalived-2.1.5/keepalived/etc”
make[2]: 进入目录“/root/keepalived-2.1.5/keepalived”
  CC       main.o
  CCLD     keepalived
  EDIT     keepalived.service
make[2]: 离开目录“/root/keepalived-2.1.5/keepalived”
make[1]: 离开目录“/root/keepalived-2.1.5/keepalived”
Making all in doc
make[1]: 进入目录“/root/keepalived-2.1.5/doc”
Making all in man/man8
make[2]: 进入目录“/root/keepalived-2.1.5/doc/man/man8”
  EDIT     keepalived.8
make[2]: 离开目录“/root/keepalived-2.1.5/doc/man/man8”
make[2]: 进入目录“/root/keepalived-2.1.5/doc”
make[2]: 对“all-am”无需做任何事。
make[2]: 离开目录“/root/keepalived-2.1.5/doc”
make[1]: 离开目录“/root/keepalived-2.1.5/doc”
Making all in genhash
make[1]: 进入目录“/root/keepalived-2.1.5/genhash”
  CC       main.o
main.c: 在函数‘usage’中:
main.c:99:3: 警告:字符串长‘1003’比‘509’(ISO C90 被要求支持的最大长度) 还要长 [-Woverlength-strings]
   "   --timeout         -t       Timeout in seconds\n");
   ^
  CC       sock.o
  CC       layer4.o
  CC       http.o
  CC       ssl.o
  CCLD     genhash
make[1]: 离开目录“/root/keepalived-2.1.5/genhash”
Making all in bin_install
make[1]: 进入目录“/root/keepalived-2.1.5/bin_install”
make[1]: 离开目录“/root/keepalived-2.1.5/bin_install”
make[1]: 进入目录“/root/keepalived-2.1.5”
  EDIT     README
make[1]: 离开目录“/root/keepalived-2.1.5
View Code

3、修改unit文件

  • 创建软链
]# cd /apps/
]# ln -s keepalived-2.1.5 keepalived
  • 默认会自动生成unit文件
    • 需要修改自动生成的unit文件,否则不能使用systemctl stop keepalived.service命令停止服务
    • keepalived默认读取的配置文件是/etc/keepalived/keepalived.conf,使用-f指定要使用的配置文件。
]# vim /usr/lib/systemd/system/keepalived.service 
[Unit]
Description=LVS and VRRP High Availability Monitor
After=network-online.target syslog.target
Wants=network-online.target
 
[Service]
Type=forking
EnvironmentFile=-/apps/keepalived/etc/sysconfig/keepalived
ExecStart=/apps/keepalived/sbin/keepalived -f /apps/keepalived/etc/keepalived/keepalived.conf $KEEPALIVED_OPTIONS
Restart=on-failure
RestartSec=5
LimitNOFILE=65536
 
[Install]
WantedBy=multi-user.target
  • 重读系统服务配置,使其生效
]# systemctl daemon-reload

4、keepalived配置文件

  • 修改配置文件/apps/keepalived/etc/keepalived/keepalived.conf
    • 这里仅仅实现了vip飘逸(通过启停keepalived)
  • 主备模型之主节点(10.1.1.11)
global_defs {
    notification_email {
        root@localhost
    }
    notification_email_from keepalived@hengha11
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
 
    router_id hengha11
    vrrp_mcast_group4 224.1.1.1
}
 
vrrp_instance VR_1 {
    state MASTER
    priority 100
    interface ens33
    virtual_router_id 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass hh101
    }
    virtual_ipaddress {
        10.1.1.99/24 dev ens33 label ens33:1
    }
}
View Code
  • 主备模型之备节点(10.1.1.12)
global_defs {
    notification_email {
        root@localhost
    }
    notification_email_from keepalived@hengha11
    smtp_server 127.0.0.1
    smtp_connect_timeout 30

    router_id hengha12
    vrrp_mcast_group4 224.1.1.1
}

vrrp_instance VR_1 {
    state BACKUP
    priority 95
    interface ens33
    virtual_router_id 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass hh101
    }
    virtual_ipaddress {
        10.1.1.99/24 dev ens33 label ens33:1
    }
}
View Code

5、启动keepalived

systemctl enable keepalived.service
systemctl start keepalived.service

systemctl status keepalived.service

systemctl stop keepalived.service

3、配置keepalive的日志

  • keepalived默认将日志输出到/var/log/messages。
  • 注意,keepalived 2.1.5修改日志路径的实验失败了(不知道是否与版本有关),但使用yum安装的进行实验是成功的。

1、修改EnvironmentFile文件

  • yum安装:/etc/sysconfig/keepalived
  • 编译安装:$keepalived_home/etc/sysconfig/keepalived
]# vim /etc/sysconfig/keepalived
#KEEPALIVED_OPTIONS="-D"
KEEPALIVED_OPTIONS="-D -S 0"

2、修改rsyslog.conf文件

  • local后的数字要和-S的值一样
]# vim /etc/rsyslog.conf
local0.* /var/log/keepalived.log    #添加

3、重启服务

]# systemctl restart rsyslog.service
]# systemctl restart keepalived.service

1

#                                                                                                                        #
posted @ 2022-07-27 01:34  麦恒  阅读(38)  评论(0编辑  收藏  举报