您好,登录后才能下订单哦!
一、简介
Keepalived是一个免费开源的,用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层、第4层和第7层交换机的功能。主要提供loadbalancing(负载均衡)和 high-availability(高可用)功能,负载均衡实现需要依赖Linux的虚拟服务内核模块(ipvs),而高可用是通过VRRP协议实现多台机器之间的故障转移服务。
上图是Keepalived的功能体系结构,大致分两层:用户空间(user space)和内核空间(kernel space)。
内核空间:主要包括IPVS(IP虚拟服务器,用于实现网络服务的负载均衡)和NETLINK(提供高级路由及其他相关的网络功能)两个部份。
用户空间:
Keepalived的所有功能是配置keepalived.conf文件来实现的。
二、安装与配置
从官网下载keepalived的最新版本,本文以当前最新版本1.2.24为例。
2.1 安装
shell> cd /usr/local/src shell> wget http://www.keepalived.org/software/keepalived-1.2.24.tar.gz shell> tar -zxvf keepalived-1.2.24.tar.gz shell> cd keepalived-1.2.24 shell> ./configure --prefix=/usr/local/keepalived shell> make && make install
2.2 配置
安装完成后,进入安装目录的etc目录下,将keepalived相应的配置文件拷贝到系统相应的目录当中。keepalived启动时会从/etc/keepalived目录下查找keepalived.conf配置文件,如果没有找到则使用默认的配置。/etc/keepalived目录安装时默认是没有安装的,需要手动创建。配置文件目录结构如下所示:
shell> tree -l /usr/local/keepalived/etc -- keepalived | |-- keepalived.conf | `-- samples | |-- keepalived.conf.status_code | |-- keepalived.conf.track_interface | |-- keepalived.conf.vrrp | |-- 。。。 |-- rc.d | `-- init.d | `-- keepalived `-- sysconfig `-- keepalived
分别对应系统目录(忽略samples目录):
/etc/keepalived/keepalived.conf /etc/rc.d/init.d/keepalived /etc/sysconfig/keepalived
将配置文件拷贝到系统对应的目录下:
shell> mkdir /etc/keepalived shell> cp /usr/local/keepalived/etc/keepalived.conf /etc/keepalived/keepalived.conf shell> cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/keepalived shell> cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
设置keepalived服务开机启动:
shell> chkconfig keepalived on shell> service keepalived start #启动服务 shell> service keepalived stop #停止服务 shell> service keepalived restart #重启服务
另外需要注意的一点是,keepalived启动时不会检查配置文件的语法是否正确,所以我们在编写配置文件时要特别小心,别写错了,否则会出现一些意想不到的现象。
使用service keepalived start
命令启动服务时,默认会将/etc/sysconfig/keepalived
文件中KEEPALIVED_OPTIONS
参数作为keepalived
服务启动时的参数,并从/etc/keepalived/
目录下加载keepalived.conf配置文件,或用-f参数指定配置文件的位置。
# Options for keepalived. See `keepalived --help' output and keepalived(8) and # keepalived.conf(5) man pages for a list of all options. Here are the most # common ones : # # --vrrp -P Only run with VRRP subsystem. # --check -C Only run with Health-checker subsystem. # --dont-release-vrrp -V Dont remove VRRP VIPs & VROUTEs on daemon stop. # --dont-release-ipvs -I Dont remove IPVS topology on daemon stop. # --dump-conf -d Dump the configuration data. # --log-detail -D Detailed log messages.日志默认输出在/var/log/message文件中 # --log-facility -S 0-7 Set local syslog facility (default=LOG_DAEMON) # KEEPALIVED_OPTIONS="-D"
运行keepalived --help
可以查看启动时的可选参数,这些可选参数都可以配置在/etc/sysconfig/keepalived
文件中的KEEPALIVED_OPTIONS
选项中,作为服务启动时的参数。
keepalived正常运行后,会启动3个进程,其中一个是父进程,负责监控其子进程。一个是vrrp子进程,另外一个是checkers子进程。
shell> ps -ef | grep keepalived root 831 1 0 11:22 ? 00:00:00 keepalived -D root 840 831 0 11:22 ? 00:00:00 keepalived -D root 841 831 0 11:22 ? 00:00:00 keepalived -D
到此keepalived就安装完成了。
三、keepalived.conf配置文件说明
keepalived服务安装完成之后,后面的主要工作就是在keepalived.conf文件中配置HA和负载均衡。一个功能比较完整的常用的keepalived配置文件,主要包含三块:全局定义块、VRRP实例定义块和虚拟服务器定义块。全局定义块是必须的,如果keepalived只用来做ha,虚拟服务器是可选的。下面是一个功能比较完整的配置文件模板:
#全局定义块 global_defs { # 邮件通知配置 notification_email { email1 email2 } notification_email_from email smtp_server host smtp_connect_timeout num lvs_id string router_id string ## 标识本节点的字条串,通常为hostname } #VRRP 实例定义块 vrrp_sync_group string { group { string string } } vrrp_instance string { state MASTER|BACKUP virtual_router_id num interface string mcast_src_ip @IP priority num advert_int num nopreempt smtp_alert lvs_sync_daemon_interface string authentication { auth_type PASS|AH auth_pass string } virtual_ipaddress { # Block limited to 20 IP addresses @IP @IP @IP } } #虚拟服务器定义块 virtual_server (@IP PORT)|(fwmark num) { delay_loop num lb_algo rr|wrr|lc|wlc|sh|dh|lblc lb_kind NAT|DR|TUN persistence_timeout num protocol TCP|UDP real_server @IP PORT { weight num notify_down /path/script.sh TCP_CHECK { connect_port num connect_timeout num } } real_server @IP PORT { weight num MISC_CHECK { misc_path /path_to_script/script.sh(or misc_path “/path_to_script/script.sh <arg_list>”) } } real_server @IP PORT { weight num HTTP_GET|SSL_GET { url { # You can add multiple url block path alphanum digest alphanum } connect_port num connect_timeout num nb_get_retry num delay_before_retry num } } }
全局定义块
1、email通知(notification_email、smtp_server、smtp_connect_timeout):用于服务有故障时发送邮件报警,可选项,不建议用。需要系统开启sendmail服务,建议用第三独立监控服务,如用nagios全面监控代替。
2、lvs_id:lvs负载均衡器标识,在一个网络内,它的值应该是唯一的。
3、router_id:用户标识本节点的名称,通常为hostname
4、花括号{}:用来分隔定义块,必须成对出现。如果写漏了,keepalived运行时不会得到预期的结果。由于定义块存在嵌套关系,因此很容易遗漏结尾处的花括号,这点需要特别注意。
VRRP实例定义块
虚拟服务器virtual_server定义块
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。