1. 下载keepalived安装包:
https://www.keepalived.org/download.html
2. 上传keepliaved安装包至linux,切换到上传目录:
3. 解压:
tar -zxvf keepalived-2.0.18.tar.gz
4. 进入解压目录
cd keepalived-2.0.18
5. 配置安装路径:
# 指定安装在/data/keepalived 路径下 --prefix=/data/keepalived #指定keepalived的conf配置文件路径 --sysconf=/data/keepalived
6. 编译安装:
make && make install
7. 启动停止
#进入keepalived安装目录 cd /data/keepalived #启动 ./sbin/keepalived #查找keepalived进程号 ps -ef|grep keepalived #停止 kill -9 keepalived进程号
8. 开机启动:
# 编辑启动文件 vi /etc/rc.d/rc.local #添加keepalived开机启动配置项: ./data/keepalived/sbin/keepalived
keepalived.conf说明:
! Configuration File for keepalived
#全局默认参数 global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL }
#虚拟路由冗余协议,主备模式 vrrp_instance VI_1 { state MASTER #MASTER | BACKUP 两种 interface eth0 #网卡信息 virtual_router_id 51 #路由ID,可能有多个keepalived集群,同一个集群中的id设置为一样 priority 100 #权重 主机数值设置为最大,其他备机比主机小即可 advert_int 1 #检查间隔,默认为1s(vrrp组播周期秒数) authentication { # 设置验证类型和密码,主备必须使用相同的密码才能正常通信 auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.168.100/24 dev eth0 label eth0:3 #虚拟IP配置 /24即为 255.255.255.0
} }
#虚拟IP配置 virtual_server 192.168.168.100 80 { delay_loop 6 #服务轮训时间间隔 lb_algo rr #lvs调度算法,轮训 lb_kind DR #DR模型,可选三种:NAT|DR|TUN nat_mask 255.255.255.0 #掩码 persistence_timeout 0 #测试时,改为0,单位:秒 protocol TCP #健康检查用TCP|UDP
# 真实服务器IP配置 real_server 192.168.168.129 80 { weight 1 HTTP_GET { 真实服务器http健康检查,返回200才正常 url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.168.130 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }