1.自行到http://www.keepalived.org/下载对应的keepalived版本
2.安装必要的库 libnfnetlink-devel zlib zlib-devel gcc gcc-c++ openssl openssl-devel openssh
yum install -y libnfnetlink-devel zlib zlib-devel gcc gcc-c++ openssl openssl-devel openssh #cengos自带的有yum,其他linux版本如suse请用zypper安装
3.上传keepalived-2.0.6.tar.gz到/home下,解压缩到/usr/local/下
tar -xvf /home/keepalived-2.0.6.tar.gz -C /usr/local/keepalived
4.cd到/usr/local/keepalived/下。执行./configure
cd /usr/local/keepalived/
./configure #如果依赖库安装的没问题的话,这一步不会有报错,下面是结果
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
Use BFD Framework : No
SNMP vrrp support : No
SNMP checker support : No
SNMP RFCv2 support : No
SNMP RFCv3 support : No
DBUS support : No
SHA1 support : No
Use Json output : No
libnl version : None
Use IPv4 devconf : No
Use libiptc : No
Use libipset : No
init type : systemd
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.
#看到这个就算是依赖包安装的没问题
5.开始编译和安装
make && make install
#########以下是安装成功截图
make[1]: 进入目录“/usr/local/keepalived”
make[2]: 进入目录“/usr/local/keepalived”
make[2]: 对“install-exec-am”无需做任何事。
/usr/bin/mkdir -p '/usr/local/share/doc/keepalived'
/usr/bin/install -c -m 644 README '/usr/local/share/doc/keepalived'
make[2]: 离开目录“/usr/local/keepalived”
make[1]: 离开目录“/usr/local/keepalived”
6.将keepalved加进系统服务并且在/etc/sysconfig/写入配置文件以及将keepalived命令加进/usr/bin 和 /usr/sbin
cd /usr/local/keepalived/keepalived/etc/init.d/
cp keepalived /etc/init.d/keepalived #最好查看下keeplaived是否有执行权限
mkdir /etc/keepalived/
cd /usr/local/keepalived/keepalived/etc/keepalived/
cp keepalived.conf /etc/keepalived/
cd /usr/local/keepalived/keepalived/etc/sysconfig/
cp keepalived /etc/sysconfig/keepalived
cp /usr/local/keepalived/sbin/keepalived /usr/bin #方便以后查看keepalived的版本,用keepalived -v可以查看
cp /usr/local/keepalived/sbin/keepalived /usr/sbin
vi /etc/keepalived/keepalived.conf #以下是配置的内容,不可直接复制
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
#vrrp_strict #这个最好注释掉,否则可能造成物理机的浏览器无法访问应用
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface ens33 #这个改成自己的网卡名字,因人而异不同,用ip a这个命令可以查看
virtual_router_id 51 #这个主备两台机器要写成一样的,我的是主服务器是51,所以副服务器也是51
priority 100 #这个数值要比备机服务器的keepalived的大就行,不过最好也要在100左右
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.66.130 #这个是虚拟ip,虚拟ip的作用就是暴露在外面可供外界访问,比如你访问的百度的ip可能并不是真的后台服务器的ip,只是做了一个映射这样的东西(这是我个人理解的,大神勿喷),所以访问这个虚拟ip相当于访问真实服务器的ip。
}
}
7.启动keepalived,并查看是否启动配置成功。
service keepalived start
ip a #查看网卡可以看出是否启动成功,下面是启动成功的网卡显示
ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:19:cc:b4 brd ff:ff:ff:ff:ff:ff
inet 192.168.66.132/24 brd 192.168.66.255 scope global noprefixroute dynamic ens33
valid_lft 1543sec preferred_lft 1543sec
inet 192.168.66.130/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::2d6c:8ffb:2943:38aa/64 scope link noprefixroute
valid_lft forever preferred_lft forever
8.将keepalived加进开机启动
systemctl enable keepalived.service