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
# 采用systemd的方式添加到系统服务,执行如下命令,执行下面一整陀:
cd /etc/systemd/system vim keepalived.service [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
wq!保存,完成后执行:
sudo systemctl daemon-reload sudo systemctl enable keepalived sudo systemctl start keepalived sudo systemctl stop keepalived
keepalived默认配置文件参考:
! Configuration File for keepalived global_defs { router_id 123 } vrrp_script check_apiserver { script "/etc/keepalived/check-apiserver.sh" #健康监测脚本路径 interval 3 weight -2 } vrrp_instance VI_1 { state MASTER mcast_src_ip 192.168.10.130 #自己的ip,也可不设置 interface ens33 # 监听的网卡 virtual_router_id 51 # 虚拟路由编号,同一实例可以一致,但是其权重一定不能一致 priority 100 # 权重,两个节点不能一样 advert_int 1 # 用于设定MASTER与BACKUP主机之间同步检查的时间间隔,单位秒 authentication { # 用于设定节点间通信验证码类型和密码 ,主要类型有PASS和AH两种,在一个vrrp_instance下,MASTER和BACKUP必须使用相同的密码才能正常通信; auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.10.138 # 虚拟IP地址 } track_script { check_apiserver # 健康检查配置(调用上面的 check_apiserver ) } }
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/m0_46900715/article/details/130746498