欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

centos 安装keepalived-1.3.5并配置nginx 博客分类: keepalived keepalivednginx主从 

程序员文章站 2024-02-28 21:36:40
...

 

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

 

官网源码包下载地址(2017-07-31 Keepalived最新版keepalived-1.3.5.tar.gz):http://www.keepalived.org/download.html

 

[root@iZ256w2hluuZ local]# tar -zxvf /tmp/keepalived-1.3.5.tar.gz 

[root@iZ256w2hluuZ local]# cd keepalived-1.3.5/

[root@iZ256w2hluuZ keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived

 

 

 

1. 系统出现警告信息“*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.”,具体日志如下:

 

Keepalived configuration

------------------------

Keepalived version       : 1.3.5

Compiler                 : gcc

Preprocessor flags       : 

Compiler flags           : -Wall -Wunused -Wstrict-prototypes -Wextra -g -O2

Linker flags             : 

Extra Lib                :  -lcrypto  -lssl 

Use IPVS Framework       : Yes

IPVS use libnl           : No

IPVS syncd attributes    : No

IPVS 64 bit stats        : No

fwmark socket support    : Yes

Use VRRP Framework       : Yes

Use VRRP VMAC            : Yes

Use VRRP authentication  : Yes

With ip rules/routes     : Yes

SNMP vrrp support        : No

SNMP checker support     : No

SNMP RFCv2 support       : No

SNMP RFCv3 support       : No

DBUS support             : No

SHA1 support             : No

Use Debug flags          : No

Stacktrace support       : No

Memory alloc check       : No

libnl version            : None

Use IPv4 devconf         : No

Use libiptc              : No

Use libipset             : No

init type                : upstart

Build genhash            : Yes

Build documentation      : No

 

*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.

 

 

 

解决方案一:(在线安装)

 

执行yum命令yum -y install libnl libnl-devel解决上述警告问题

 

[root@iZ256w2hluuZ keepalived-1.3.5]# yum -y install libnl libnl-devel

 

 

 

解决方案二:(离线安装)

 

离线安装libnl-devel包,从网上下载libnl-devel-1.1.4-2.el6.x86_64.rpm包,执行rpm -ivh libnl-devel-1.1.4-2.el6.x86_64.rpm命令。

 

[root@iZ256w2hluuZ other-packages]# wget http://mirror.centos.org/centos/6/os/x86_64/Packages/libnl-devel-1.1.4-2.el6.x86_64.rpm

####执行wget命令是从另外一台可以连接外网的服务器上下载libnl-devel-1.1.4-2.el6.x86_64.rpm包的命令,可以忽略,只是为大家提供个下载libnl-devel地址####

 

[root@iZ256w2hluuZ other-packages]# rpm -ivh libnl-devel-1.1.4-2.el6.x86_64.rpm 

Preparing...                ########################################### [100%]

        package libnl-devel-1.1.4-2.el6.x86_64 is already installed

 

 

 

安装完成以后,重新执行configure命令

 

[root@iZ256w2hluuZ keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived

 

 

 

2. 系统出现错误信息“configure: error: libnfnetlink headers missing”,具体日志如下:

 

checking for nl_socket_modify_cb in -lnl... yes

checking for linux/rtnetlink.h... yes

checking libnfnetlink/libnfnetlink.h usability... no

checking libnfnetlink/libnfnetlink.h presence... no

checking for libnfnetlink/libnfnetlink.h... no

configure: error: libnfnetlink headers missing

 

 

 

解决方案一:(在线安装)

 

执行yum命令yum install -y libnfnetlink-devel解决上述错误问题

 

[root@iZ256w2hluuZ keepalived-1.3.5]# yum install -y libnfnetlink-devel

 

 

 

解决方案二:(离线安装)

 

Linux执行wget下载rpm包(离线安装libnfnetlink包时所需rpm包,执行wget下载libnfnetlink rpm包命令,此步可以忽略,只是为大家提供个下载libnfnetlink地址),具体命令如下:

 

wget ftp://mirror.switch.ch/mirror/centos/6/os/x86_64/Packages/libnfnetlink-1.0.0-1.el6.x86_64.rpm

wget ftp://mirror.switch.ch/mirror/centos/6/os/x86_64/Packages/libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm

 

执行rpm -ivh libnfnetlink-1.0.0-1.el6.x86_64.rpm命令安装libnfnetlink-1.0.0-1.el6.x86_64.rpm包,具体如下:

 

[root@iZ256w2hluuZ other-packages]# rpm -ivh libnfnetlink-1.0.0-1.el6.x86_64.rpm 

Preparing...                ########################################### [100%]

   1:libnfnetlink           ########################################### [100%]

 

执行rpm -ivh libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm命令安装libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm包,具体如下:

 

[root@iZ256w2hluuZ other-packages]# rpm -ivh libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm

Preparing...                ########################################### [100%]

   1:libnfnetlink-devel     ########################################### [100%]

 

 

 

安装完成以后,重新执行configure命令

 

[root@iZ256w2hluuZ keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived

 

 

 

3. 编译keepalived不会出现上述警告和其他错误,执行make && make install命令进行安装:

 

[root@iZ256w2hluuZ keepalived-1.3.5]# make && make install

 

至此Keepalived编译并安装结束,在下一篇文章中会详解介绍Nginx搭配Keepalived的详细配置和使用方法,欢迎大家收藏本站素文宅博客。

 

启动命令:

 

[root@iZ256w2hluuZ keepalived-1.3.5]# ./sbin/keepalived -f /usr/local/keepalived/etc/keepalived.conf

 

 

 

制作快捷启动,在源文件的目录./keepalived-1.3.5/keepalived/etc/init.d下有两个个快捷启动文件和生成/usr/local/keepalived目录下一个配置文件需要复制,具体执行如下命令:

 

[root@iZ256w2hluuZ keepalived-1.3.5]#  cp ./keepalived/etc/init.d/keepalived /etc/init.d/

[root@iZ256w2hluuZ keepalived-1.3.5]# mkdir /etc/keepalived

[root@iZ256w2hluuZ keepalived-1.3.5]#  cp  /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

[root@iZ256w2hluuZ keepalived-1.3.5]#  cp ./keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived

[root@yoodb /]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

 

这样就可以执行service keepalived [start | stop | reload | restart ]命令。

 

启动keepalived服务报错

 

[html] view plain copy

 

1.  # service keepalived start  

 

错误如下

 

Job for keepalived.service failed because a configured resource limit was exceeded. See "systemctl status keepalived.service" and "journalctl -xe" for details.

 

Job for keepalived.service failed because a configured resource limit was exceeded. See "systemctl status keepalived.service" and "journalctl -xe" for

 

 details.

 


查看错误

 

1.  # systemctl status keepalived.service  

 

我们可以看到日志,是写日志出了问题

 

1.  [root@zk-02 sbin]# systemctl status keepalived.service  

2.  ● keepalived.service - LVS and VRRP High Availability Monitor  

3.     Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)  

4.     Active: failed (Result: resources) since Fri 2017-08-04 15:32:31 CST; 4min 59s ago  

5.    Process: 16764 ExecStart=/usr/local/program/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exitedstatus=0/SUCCESS)  

6.    

7.  Aug 04 15:32:25 zk-02 Keepalived_healthcheckers[16768]: Activating healthchecker for service [10.10.10.2]:1358  

8.  Aug 04 15:32:25 zk-02 Keepalived_healthcheckers[16768]: Activating healthchecker for service [10.10.10.3]:1358  

9.  Aug 04 15:32:25 zk-02 Keepalived_healthcheckers[16768]: Activating healthchecker for service [10.10.10.3]:1358  

10.Aug 04 15:32:25 zk-02 Keepalived_vrrp[16769]: (VI_1): No VIP specified; at least one is required  

11.Aug 04 15:32:26 zk-02 Keepalived[16766]: Keepalived_vrrp exited with permanent error CONFIG. Terminating  

12.Aug 04 15:32:26 zk-02 Keepalived[16766]: Stopping  

13.Aug 04 15:32:31 zk-02 systemd[1]: keepalived.service never wrote its PID file. Failing.    ##写日志出了问题  

14.Aug 04 15:32:31 zk-02 systemd[1]: Failed to start LVS and VRRP High Availability Monitor.  

15.Aug 04 15:32:31 zk-02 systemd[1]: Unit keepalived.service entered failed state.  

16.Aug 04 15:32:31 zk-02 systemd[1]: keepalived.service failed.  

17.[root@zk-02 sbin]# vi /var/run/keepalived.pid  

18.[root@zk-02 sbin]# vim /lib/systemd/system/keepalived.service  

19.[root@zk-02 sbin]# vim /lib/systemd/system/keepalived.service  

 

 

 

查看keepalived.service

 

[html] view plain copy

 

1.  # vi /lib/systemd/system/keepalived.service  

 

内容如下

 

[html] view plain copy

 

1.  [Unit]  

2.  Description=LVS and VRRP High Availability Monitor  

3.  After=syslog.target network-online.target  

4.    

5.  [Service]  

6.  Type=forking  

7.  PIDFile=/usr/local/program/keepalived/var/run/keepalived.pid      ##发现这个目录不存在,无法写入keepalived.pid  

8.  KillMode=process  

9.  EnvironmentFile=-/usr/local/program/keepalived/etc/sysconfig/keepalived  

10.ExecStart=/usr/local/program/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS  

11.ExecReload=/bin/kill -HUP $MAINPID  

12.  

13.[Install]  

14.WantedBy=multi-user.target  

 


问题见

 

[html] view plain copy

 

1.  PIDFile=/usr/local/program/keepalived/var/run/keepalived.pid      ##发现这个目录不存在,无法写入keepalived.pid  

 

修改成

 

[html] view plain copy

 

1.  PIDFile=/var/run/keepalived.pid    

 

 

 

运行命令

 

[html] view plain copy

 

1.  # systemctl daemon-reload    #重新载入 systemd,扫描新的或有变动的单元  

 

在运行就Ok

 

[html] view plain copy

 

1.  service keepalived start  

2.  Starting keepalived (via systemctl):                       [  OK  ]