◆案例1◆ 编译安装Keepalived
1.安装依赖
yum install -y gcc kernel kernel-devel openssl openssl-devel popt popt-devel
2.编译Keepalived
wget http://www.keepalived.org/software/keepalived-1.4.3.tar.gz tar -xzvf keepalived-1.4.3.tar.gz cd keepalived-1.4.3/ ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-358.el6.x86_64 #RHEL6中的编译参数 ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/3.10.0-123.el7.x86_64/net/ #RHEL7中的编译参数 make && make install
◆案例2◆ 使用Keepalived构建LVS高可用集群
=====================================================================================
实验环境
[类型] [IP地址] [VIP/IO]
LVS1_Master IP:192.168.22.191 VIP:192.168.22.195
LVS2_Slaves IP:192.168.22.192 VIP:192.168.22.195
RealServer_1 IP:192.168.22.193
RealServer_2 IP:192.168.22.194
=====================================================================================
◆配置Keepalived主节点◆
1.修改主配置文件,其他可删除,只写入以下内容,修改以下标★处
vim /etc/keepalived/keepalived.conf 1 ! Configuration File for keepalived 2 3 global_defs { ★ router_id kp_master #指定本机keepalaved名字(主从不能重复) 5 } 6 7 vrrp_instance VI_1 { ★ state MASTER #声明成主服务器(MASTER)/声明成从服务器(SLAVE) ★ interface eth0 #定义相应网卡接口名称 ★ virtual_router_id 100 #虚拟路由ID(主从应同步) ★ priority 100 #Keepalaved主从服务器优先级(主服务器必须大于从服务器) 12 advert_int 1 #检查间隔,默认1秒 13 authentication { #定义主从验证 14 auth_type PASS #设置验证方式(PASS或HA) 15 auth_pass 1111 #验证密码 16 } 17 virtual_ipaddress { #指定负载调度器(指定VIP的地址) ★ 192.168.22.195 19 } 20 } 21 ★ virtual_server 192.168.22.195 80 { #虚拟主机区域(指定VIP地址) 23 delay_loop 6 #服务器轮询间隔时间 24 lb_algo rr #指定rr轮询算法 ★ lb_kind DR #指定DR模式 ★ net_mask 255.255.255.0 #指定子网掩码 27 persistence_timeout 50 #会话保持时间 28 protocol TCP #指定数据转发协议 29 ★ real_server 192.168.22.193 80 { #RealServer1池,如有多台复制此区域 31 weight 1 #设置服务器权重 ★ TCP_CHECK { #对后端真实服务器TCP健康检查 33 connect_timeout 3 #链接超时时间 34 retry 3 #重试次数 35 delay_before_retry 3 #重试时间间隔 36 } 37 } 38 ★ real_server 192.168.22.194 80 { #RealServer2池,如有多台复制此区域 40 weight 1 #设置服务器权重 ★ TCP_CHECK { #对后端真实服务器TCP健康检查 42 connect_timeout 3 #连接超时时间 43 retry 3 #重试次数 44 delay_before_retry 3 #重试时间间隔 45 } 46 } 47 }
2.启动keepalived
chkconfig --add keepalived chkconfig keepalived on ./keepalived -f /usr/local/keepalived/etc/keepalived.conf systemctl restart keepalived
◆配置Keepalived备节点◆
1.修改主配置文件,其他可删除,只写入以下内容,修改以下标★处
vim /etc/keepalived/keepalived.conf 1 ! Configuration File for keepalived 2 3 global_defs { ★ router_id kp_slave #指定本机keepalaved名字(主从不能重复) 5 } 6 7 vrrp_instance VI_1 { ★ state SLAVE #声明成主服务器(MASTER)/声明成从服务器(SLAVE) ★ interface eth0 #定义相应网卡接口名称 ★ virtual_router_id 100 #虚拟路由ID(主从应同步) ★ priority 50 #Keepalaved主从服务器优先级(主服务器必须大于从服务器) 12 advert_int 1 #检查间隔,默认1秒 13 authentication { #定义主从验证 14 auth_type PASS #设置验证方式(PASS或HA) 15 auth_pass 1111 #验证密码 16 } 17 virtual_ipaddress { #指定负载调度器(指定VIP的地址) ★ 192.168.22.195 19 } 20 } 21 ★ virtual_server 192.168.22.195 80 { #虚拟主机区域(指定VIP地址) 23 delay_loop 6 #服务器轮询间隔时间 24 lb_algo rr #指定rr轮询算法 ★ lb_kind DR #指定DR模式 ★ net_mask 255.255.255.0 #指定子网掩码 27 persistence_timeout 50 #会话保持时间 28 protocol TCP #指定数据转发协议 29 ★ real_server 192.168.22.193 80 { #RealServer1池,如有多台复制此区域 31 weight 1 #设置服务器权重 ★ TCP_CHECK { #对后端真实服务器TCP健康检查 33 connect_timeout 3 #链接超时时间 34 retry 3 #重试次数 35 delay_before_retry 3 #重试时间间隔 36 } 37 } 38 ★ real_server 192.168.22.194 80 { #RealServer2池,如有多台复制此区域 40 weight 1 #设置服务器权重 ★ TCP_CHECK { #对后端真实服务器TCP健康检查 42 connect_timeout 3 #连接超时时间 43 retry 3 #重试次数 44 delay_before_retry 3 #重试时间间隔 45 } 46 } 47 }
2.修改内核参数.防止相同网络地址广播冲突
vim /etc/sysctl.conf net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 net.ipv4.conf.eth0.send_redirects = 0 sysctl -p
3.启动keepalived 添加自启动菜单以及设置开机自启
chkconfig --add keepalived chkconfig keepalived on ./keepalived -f /usr/local/keepalived/etc/keepalived.conf systemctl restart keepalived