keepalived详细安装教程

1.通过源码包的方式进行安装

[root@master src]# pwd
/usr/local/src
[root@master src]# wget http://www.keepalived.org/software/keepalived-2.0.7.tar.gz
[root@master src]# tar xvf keepalived-2.0.7.tar.gz
[root@master src]# cd keepalived-2.0.7
[root@master keepalived-2.0.7]# ./configure --prefix=/usr/local/keepalived
[root@master keepalived-2.0.7]# make && make install



**2.初始化启动**
*# keepalived启动脚本变量引用文件,默认文件路径是/etc/sysconfig/,也可以不做软链接,直接修改启动脚本中文件路径即可(安装目录下)*

```powershell
[root@localhost /]# cp /usr/local/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/keepalived 
 

# 将keepalived主程序加入到环境变量(安装目录下)

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

# keepalived启动脚本(源码目录下),放到/etc/init.d/目录下就可以使用service命令便捷调用

[root@localhost /]# cp /usr/local/src/keepalived-2.0.7/keepalived/etc/init.d/keepalived  /etc/init.d/keepalived

# 将配置文件放到默认路径下

[root@localhost /]# mkdir /etc/keepalived
[root@localhost /]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

#添加keepalived到开机启动
chkconfig keepalived on
#启动keepalived服务
service keepalived start [stop restart]
查看服务状态

检查脚本

#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
    /usr/local/webserver/nginx/sbin/nginx
    sleep 2
    counter=$(ps -C nginx --no-heading|wc -l)
    if [ "${counter}" = "0" ]; then
        /etc/init.d/keepalived stop
    fi
fi
[root@pekglb05 keepalived]# more keepalived.conf
global_defs {
router_id LVS_DEVEL
}

vrrp_script check_run {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 2
fall 3
}

vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 60
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.212.148
}
track_script {
check_run
}
notify_backup "service nginx restart"
notify_fault "service nginx stop"
}


[root@pekglb06 ~]# more /etc/keepalived/keepalived.conf 
global_defs {
router_id LVS_DEVEL
}

vrrp_script check_run {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 2
}

vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 60
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.212.148
}
track_script {
check_run
}
notify_backup "service nginx restart"
notify_fault "service nginx stop"
}

你可能感兴趣的:(系统部署,linux,负载均衡,nginx,运维)