1.keepalived二进制安装【Ubuntu20.04】
(1).官网下载二进制源码包
官网:
https://keepalived.org/download.html
下载二进制包:
wget https://keepalived.org/software/keepalived-2.2.7.tar.gz
亦可通过window本地下载并上传
(2).解压文件
[root@ubuntu2004 ~]#tar xf keepalived-2.2.7.tar.gz -C /usr/local/src
(3).安装编译软件
如下有部分是操作系统自带的,并不是所有的都需要
[root@ubuntu2004 ~]#apt-get -y install libssl-dev libpopt-dev daemon build-essential libssl-dev
(4).编译、安装
[root@ubuntu2004 ~]#mkdir -p /usr/local/keepalived
[root@ubuntu2004 keepalived-2.2.7]#./configure --prefix=/usr/local/keepalived --disable-fwmark
[root@ubuntu2004 keepalived-2.2.7]#make && make install
注意:
--disable-fwmark 可用于禁用iptables规则,可访止VIP无法访问
无此选项默认会启用 ipatbles规则
(5).服务管理
[root@ubuntu2004 ~]#vim /etc/profile.d/keepalived.sh
#!/bin/bash
#set keepalived environment
export KEEPALIVED_HOME=/usr/local/keepalived
export PATH=$PATH:$KEEPALIVED_HOME/sbin
[root@ubuntu2004 ~]#source /etc/profile.d/keepalived.sh
[root@ubuntu2004 ~]#chmod +x /etc/profile.d/keepalived.sh
[root@ubuntu2004 ~]#export PATH=$PATH:$KEEPALIVED_HOME/sbin
[root@ubuntu2004 ~]#export KEEPALIVED_HOME=/usr/local/keepalived
[root@ubuntu2004 ~]#source /etc/profile.d/keepalived.sh
(6).目录配置
[root@ubuntu2004 ~]#mkdir /etc/keepalived #配置文件存放目录
[root@ubuntu2004 ~]#cp /usr/local/keepalived/etc/keepalived/keepalived.conf.sample /etc/keepalived/ #拷贝模板文件
[root@ubuntu2004 ~]#ln -s /usr/local/keepalived/etc/sysconfig//keepalived /etc/default/keealived #链接启动参数文件
[root@ubuntu2004 ~]#ln -s /usr/local/keepalived/sbin/keepalived* /usr/sbin/keepalived #链接启动参数文件
(7).配置service文件
[root@ubuntu2004 ~]#cat /lib/systemd/system/keepalived.service
[Unit]
Description=Keepalive Daemon (LVS and VRRP)
After=network-online.target
Wants=network-online.target
# Only start if there is a configuration file
ConditionFileNotEmpty=/etc/keepalived/keepalived.conf
[Service]
Type=simple
# Read configuration variable file if it is present
EnvironmentFile=-/etc/default/keepalived
ExecStart=/usr/sbin/keepalived --dont-fork $DAEMON_ARGS
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
(8).修改配置文件
- 主
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.2.32.206/24
}
}
}
- 备
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.2.32.206/24
}
}
}
(9).启动服务
[root@ubuntu2004 ~]#systemctl daemon-reload
[root@ubuntu2004 ~]#systemctl enable --now keepalived.service
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /lib/systemd/system/keepalived.service.
[root@ubuntu2004 ~]#systemctl status keepalived.service
● keepalived.service - Keepalive Daemon (LVS and VRRP)
Loaded: loaded (/lib/systemd/system/keepalived.service; enabled; vendor preset: enab>
Active: active (running) since Tue 2022-03-15 13:22:16 CST; 9s ago
Process: 73238 ExecStart=/data/apps/keepalived/sbin/keepalived $KEEPALIVED_OPITONS (c>
Main PID: 73255 (keepalived)
Tasks: 3 (limit: 1037)
Memory: 3.6M
CGroup: /system.slice/keepalived.service
├─73255 /data/apps/keepalived/sbin/keepalived
├─73256 /data/apps/keepalived/sbin/keepalived
└─73257 /data/apps/keepalived/sbin/keepalived
2.keepalived yum安装【Centos】
(1).通过yum安装
yum install -y keepalived
yum install –y ipvsadm
(2).修改配置文件
- 主
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.2.32.206/24
}
}
}
- 备
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.2.32.206/24
}
}
}