首先,在搞keepalived的时候,一般都是一主一备,所以需要2台设备。这两台设备,要一致,我之前是用了一台centos,一台ubuntu,然后,搞完以后发现,不管是主还是备都持有vip,后来又搞了一台centos,一台ubuntu然后再试,发现就一切正常了。所以,一定是准备2台一致的设备。
另外,再说一下,ubuntu和centos在搭建keepalived的过程,是不太一样的,总的来说,centos要好用很多,Ubuntu就显得很麻烦,问题多多....所以,如果是自己学习搭建试试手,建议还是直接centos,会很简单...
由于,我是在虚拟机上搞了4台设备,等于2个操作系统都搞了一便,因此这里记录一下搭建的过程吧。
1、下载keepalived包:
执行命令:
wget http://www.keepalived.org/software/keepalived-1.2.18.tar.gz
这是一个不是很新的版本。centos上用就可以了。由于最后Ubuntu上出了好些问题,主要是搭完后keepalived启动之后,就停不了的问题,后来在网上找了一些帖子,他们都是用的keepalived的最新版本,我当时以为是版本问题,所以,最后在ubuntu搭建的时候选择的是最新版的keepalived(2.2.7)。地址如下:
https://www.keepalived.org/software/keepalived-2.2.7.tar.gz
当然也可以用命令直接下载,将上面wget地址的最后版本号改掉即可。
2、安装keepalived之前,需要先确保openssl等一些插件处于已安装状态。而且,ubuntu和centos所需插件名称还不一样,具体来说:
ubuntu需下载:
sudo apt-get install openssl
sudo apt-get install libssl-dev
centos需下载:
yum install -y openssl openssl-devel
3、安装
Ubuntu:
# 按照如下命令执行完
tar -xvf keepalived-2.2.7.tar.gz
cd keepalived-2.2.7/
./configure -prefix=/usr/local/keepalived --sysconf=/etc
make
sudo make install
然后添加软连接:
sudo ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
sudo ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/default/keepalived
# 先换个目录
cd /usr/local/keepalived/sbin
# 采用systemd的方式添加到系统服务,执行如下命令,执行下面一整陀:
(
cat <<EOF
[Unit]
Description=Keepalive Daemon (LVS and VRRP)
After=syslog.target network-online.target
Wants=network-online.target
# Only start if there is a configuration file
ConditionFileNotEmpty=/etc/keepalived/keepalived.conf
[Service]
Type=forking
KillMode=process
# Read configuration variable file if it is present
EnvironmentFile=-/etc/default/keepalived
ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
EOF
) > keepalived.service
完成后执行:
sudo cp -a keepalived.service /lib/systemd/system/keepalived.service
sudo ln -s /lib/systemd/system/keepalived.service /etc/systemd/system/multi-user.target.wants/keepalived.se
然后,就可以正常执行keepalived的启动和停止动作了。
sudo service keepalived start
sudo service keepalived stop
关键是真的停掉了,可以用 ps -ef | grep keepalive 查看结果。
不像网上一些帖子,到最后是真的听不掉.....一查就看到真没停。
欧克,然后就可以在主备节点通过停止服务的方式看到vip在不同的设备上来回切换了。
对了,这样安装完成后,keepalived的配置文件,直接就是在/etc/keepalived下,只是它会有个后缀,自己改吧改吧,重新弄一个配置文件依旧放到 /etc/keepalived下即可。
我这有个极简的,就是测测看一切正常不的:
! Configuration File for keepalived
global_defs {
router_id 123
}
vrrp_instance VI_1 {
state MASTER
mcast_src_ip 192.168.10.130
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.138
}
}
对配置文件讲解的帖子很多,我这就不再介绍了:
keepalived原理简介_keepalived工作原理_fedorafrog的博客-CSDN博客
等等吧...
此处关于Ubuntu的配置有参考帖子:
(192条消息) ubuntu安装keepalived_九九九_无可救药的博客-CSDN博客
=======================================================================
ok,接下来看看centos的安装:
执行命令:
tar -xvf keepalived-1.2.18.tar.gz
cd keepalived-1.2.18/
./configure --prefix=/usr/local/keepalived
make
sudo make intsall
再搞一些软连接:
sudo cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
sudo cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
sudo cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig
sudo ln -s /usr/local/keepalived/sbin/keepalived /sbin/
# 设置开机自启动,也可以不设置
chkconfig keepalived on
#这一步待定,去看一下,如果存在这个软连接,就不用搞了
sudo cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
然后,就完成了....这里注意,关于centos的流程目前适用 keepalived-1.2.18 版本,个人不确定是否适合最新版本。
一样的启动停止命令:
service keepalived start
service keepalived stop
不过这个没有加 sudo
centos的执行结果,也是比较明确的....