环境:本地虚拟机中安装两台centos7
在centos7中安装keepalived
安装命令
yum install -y keepalived
Loading mirror speeds from cached hostfile
There are no enabled repos.
Run "yum repolist all" to see the repos you have.
To enable Red Hat Subscription Management repositories:
subscription-manager repos --enable <repo>
To enable custom repositories:
yum-config-manager --enable <repo>
提示错误
解决方案
这是由于yum没有配置源的问题引起的
删除原有的文件重新配置yum的源即可
删除
rm -f /etc/yum.repos.d/CentOS-Base.repo
配置成阿里的源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
清除缓存
yum clean all
可能会出现的问题
make: *** 没有指明目标并且找不到 makefile。 停止。
yum install gcc
如果没有问题查看是否安装成功
keepalived -v
修改配置文件
/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id kepp130
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens33 这是网卡
virtual_router_id 100 这个id必须 相同
mcast_src_ip 192.168.220.130 当前id地址
priority 100
advert_int 1
authentication {
auth_type PASS 这里要和备用服务器相同
auth_pass 1111 这里也要和备用服务器项目
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.220.168 这个是虚拟ip 随便填都可以
}
}
备
cript chk_nginx {
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 100
mcast_src_ip 192.168.220.130
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.220.2
}
}
重启keepalived
systemctl restart keepalived
修改虚拟机名称
vim /etc/hosts
#!/bin/bash
A=`ps -C nginx -no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi