Keepalived安装与配置

Keepalived是一个免费开源的,用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层、第4层和第7层交换机的功能。主要提供loadbalancing(负载均衡)和 high-availability(高可用)功能,负载均衡实现需要依赖Linux的虚拟服务内核模块(ipvs),而高可用是通过VRRP协议实现多台机器之间的故障转移服务。

Keepalived体系结构


上图是Keepalived的功能体系结构,大致分两层:用户空间(user space)和内核空间(kernel space)。
内核空间:主要包括IPVS(IP虚拟服务器,用于实现网络服务的负载均衡)和NETLINK(提供高级路由及其他相关的网络功能)两个部份。
用户空间

WatchDog:负载监控checkers和VRRP进程的状况

VRRP Stack:负载负载均衡器之间的失败切换FailOver,如果只用一个负载均稀器,则VRRP不是必须的。

Checkers:负责真实服务器的健康检查healthchecking,是keepalived最主要的功能。换言之,可以没有VRRP Stack,但健康检查healthchecking是一定要有的。

IPVS wrapper:用户发送设定的规则到内核ipvs代码

Netlink Reflector:用来设定vrrp的vip地址等。

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"

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/67aa3d6377bea01b3989314c3760827a.html