【keepalived】CentOS7.0下安装教程

安装前所需环境

keepalived安装之前,需要环境:wget、gcc、pcre、openssl、zlib、popt-devel
1,wget:用于从外网上下载插件

wget安装命令:yum -y install wget 

2,gcc:GCC(GNU Compiler Collection,GNU编译器套件),是由 GNU 开发的编程语言编译器。GCC原本作为GNU操作系统的官方编译器,现已被大多数类Unix操作系统(如Linux、BSD、Mac OS X等)采纳为标准的编译器

gcc安装命令:yum install gcc-c++

3,pcre:PCRE(Perl Compatible Regular Expressions)安装 ,它是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库

pcre安装命令:yum install -y pcre pcre-devel

4,openssl:它是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用; nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库

openssl安装命令:yum install -y openssl openssl-devel 

5,zlib:它提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库

zlib安装命令:yum install -y zlib zlib-devel

6,popt-devel 用于从外网上下载插件

popt-devel安装命令: yum install –y popt-devel 
keepalived的安装及配置

(1) 进入一个目录(随便一个目录即可,用来存放下载的Keepalived的包,在此以/root目录为例)命令

cd /root 

(2) 下载keepalived安装包 ,本文用例keepalived1.3.4(目前最新版本可通过官网http://www.keepalived.org/softwar)

命令: wget http://www.keepalived.org/software/keepalived-1.3.4.tar.gz 

(3)解压keepalived-1.3.4.tar.gz

命令: tar -zxvf keepalived-1.3.4.tar.gz 

(4)进入刚解压的目录

 命令:cd keepalived-1.3.4 

(5)进行相关配置(后半部分指定安装目录)

命令:  ./configure -prefix=/usr/local/keepalived-1.3.4 

(6)编译和安装

命令:make && make install 

(7)建立服务启动脚本,以便使用service命令控制,将路径为root/keepalived-1.3.4/keepalived/etc/init.d的文件keepalived拷贝到/etc/init.d下,具体命令:

cp /root/keepalived-1.3.4/keepalived/etc/init.d/keepalived /etc/init.d/keepalived 

(8)由于安装使用非默认路径(本教程中使用的/usr/local/keepalived-1.3.4)故需修改相关路径,保证keepalived能正常启动

文件 /etc/init.d/keepalived 大约15行位置将
. /etc/sysconfig/keepalived 修改为
. /usr/local/keepalived-1.3.4/etc/sysconfig/keepalived

这里写图片描述
(9)配置环境变量(将keepavlied主程序所在路径导入到环境变量PATH中)

命令: vi /etc/profile
加入相关内容
export PATH=/usr/local/keepalived-1.3.4/sbin:$PATH
(其中# add keepalived path为注释) 

这里写图片描述
(10)用命令:source /etc/profile 使修改的环境变量生效
用命令: export 进行检查:
这里写图片描述
(11)建立可执行文件链接 命令:

ln -s /usr/local/keepalived-1.3.4/sbin/keepalived /usr/sbin/ 

(12)修改keepalived配置文件命令:

vi /usr/local/keepalived-1.3.4/etc/keepalived/keepalived.conf 

将配置文件中网卡修改为自己的网卡
(13)编写配置文件中涉及到的监控Nginx脚本;(本教程以目录是在/usr/local/keepalived-1.3.4 下创建nginx_check.sh为例)
①创建文件命令:

vi /usr/local/keepalived-1.3.4/nginx_check.sh 

②编写内容并保存:

nxPidNum=`ps -C nginx --no-header |wc -l`
keepalivedPidNum=`ps -C keepalived --no-header |wc -l`             
if [ $nxPidNum -eq 0 ];then                             
    /usr/local/nginx-1.10.3/sbin/nginx  
    sleep 3  
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then  
       killall keepalived                     
    elif [$keepalivedPidNum -eq 0 ];then
       service keepalived start
    fi  
elif [ $keepalivedPidNum -eq 0 ];then
    service keepalived start
fi

③进入到相应目录下将监控Nginx编写的脚本提升权限为可执行文件,本例以/usr/local/keepalived-1.3.4 目录下的nginx_check.sh为例;命令:

 chmod +x nginx_check.sh

检查文件是否变颜色,变颜色则说明为可执行文件
这里写图片描述
(14)修改相应文件,设置正确启动参数命令:

 vi /usr/local/keepalived-1.3.4/etc/sysconfig/keepalived 
  修改的路径为安装路径下的配置文件修改内容:
    KEEPALIVED_OPTIONS="-D -f /usr/local/keepalived-1.3.4/etc/keepalived/keepalived.conf"

(15)将修改好的keepalived拷贝到/etc/etc/sysconfig 目录下 命令:

cp /usr/local/keepalived-1.3.4/etc/sysconfig/keepalived /etc/sysconfig/ 

(16)测试:
①启动 命令:

service keepalived start 

②查看keepalived运行状态,命令:

service keepalived status 

提示:在配置keepalived.conf时,需要特别注意配置文件的语法格式,因为keepalived在启动时并不检测配置文件的正确性,即使没有配置文件,Keepalived也照样能够启动,所以一定要保证配置文件正确。只要下面出现提示信息为running证明成功
这里写图片描述

posted @ 2018-07-02 07:57  Developer_lulu  阅读(296)  评论(1编辑  收藏  举报