标签:安装 规划 nss rtu smt ifconfig 连接 The reflect
以用户访问Apache服务为例,用户通过虚拟IP:“192.168.31.10”访问web服务,这个虚拟IP是LVS的VIP,LVS作为负载均衡器有两台,分别是“192.168.31.11”(主LVS)、“192.168.31.12”(备LVS),当LVS主机接到用户的web请求时,将请求轮询转发到后端真实web服务器的“192.168.31.13”和“192.168.31.14”。
节点类型 | IP地址规划 | 主机名 | 地址类型 | 网卡 |
---|---|---|---|---|
LVS-MASTER | 192.168.31.11/24 | study01.linux.com | 真实地址 | eth0 |
LVS-BACKUP | 192.168.31.12/24 | study02.linux.com | 真实地址 | eth0 |
LVS-VIP | 192.168.31.10/32 | - | 虚拟地址 | eth0 |
Apache01 | 192.168.31.13/24 | study03.linux.com | 真实地址 | eth0 |
Apache01 | 192.168.31.10/32 | study03.linux.com | 虚拟地址 | lo:0 |
Apache02 | 192.168.31.14/24 | study04.linux.com | 真实地址 | eth0 |
Apache02 | 192.168.31.10/32 | study04.linux.com | 虚拟地址 | lo:0 |
yum -y install gcc-* glibc-* *c++* openssl-devel #安装开发环境
yum -y install ipvsadm #安装LVS
yum -y install keepalived #安装keepalive
! Configuration File for keepalived
global_defs {
notification_email {
wang_zengyi@126.com #设置接收报警邮件,可以设置多个,每行一个,前提是需要开启本机的sendmail服务
}
notification_email_from 647956023@qq.com #设置邮件的发送邮箱
smtp_server 121.51.19.175 #设置SMTP服务器地址
smtp_connect_timeout 30 #设置连接SMTP服务器的超时时间
router_id LVS_DEVEL #表示运行keepalive服务器的一个标识,发邮件时显示在邮件主题中的信息
}
vrrp_instance VI_1 { #VRPP实例部分
state MASTER #指定VRRP角色
interface eth0 #指定HA监测的网络接口
virtual_router_id 51 #VRRP实例ID,同一组VRRP必须试用同样的ID
priority 100 #VRRP优先级,越大越优先
advert_int 1 #心跳报文通告间隔
authentication {
auth_type PASS #设置验证类型,主要有PASS和AH两种
auth_pass 1111 #设置验证密码,同一组VRRP的主备必须是用相同的密码和类型
}
virtual_ipaddress {
192.168.31.10 #设置VIP
}
}
virtual_server 192.168.31.10 80 { #设置对用户提供服务的虚拟IP和端口
delay_loop 6 #设置运行情况检查,时间是秒
lb_algo rr #设置负载调度算法
lb_kind DR #设置LVS实现负载均衡的机制
nat_mask 255.255.255.0
#persistence_timeout 50 #会话超时时间,单位是秒,超时后会分发到下一个节点
protocol TCP #指定协议类型,有TCP和UDP
real_server 192.168.31.13 80 { #指定后端真实服务器地址和端口
weight 1
TCP_CHECK {
connect_timeout 3 #表示3秒无响应超时
nb_get_retry 3 #表示重试次数
delay_before_retry 3 #表示重试间隔
}
}
real_server 192.168.31.14 80 { #指定后端真实服务器地址和端口
weight 1
TCP_CHECK {
connect_timeout 3 #表示3秒无响应超时
nb_get_retry 3 #表示重试次数
delay_before_retry 3 #表示重试间隔
}
}
}
yum -y install gcc-* glibc-* *c++* openssl-devel #安装开发环境
yum -y install ipvsadm #安装LVS
yum -y install keepalived #安装keepalive
LVS-BACKUP的配置文件和主配置文件只有以下部分不同:
1. 角色改为BACKUP。
2. priority改的要低于主机的优先级。
#搭建Apache基本测试环境
/etc/init.d/httpd restart
chkconfig --level 35 httpd on
echo "this is real-linux01" > /var/www/html/index.html
#以下内容实际环境中可以编写成脚本
ifconfig lo:0 192.168.31.10 netmask 255.255.255.255 up
echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
# LVS主和备都使用如下方法启动
chkconfig --level 35 keepalived on
/etc/init.d/keepalived restart
chkconfig --level 35 ipvsadm on
/etc/init.d/ipvsadm restart
当关闭LVS主的keepalive服务的时候,VIP会立刻飘到备LVS,用户业务不会中断,可以观察"/var/log/messages"的日志;
Mar 16 15:54:42 study02 Keepalived_vrrp[25603]: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 16 15:54:43 study02 Keepalived_vrrp[25603]: VRRP_Instance(VI_1) Entering MASTER STATE
Mar 16 15:54:43 study02 Keepalived_vrrp[25603]: VRRP_Instance(VI_1) setting protocol VIPs.
Mar 16 15:54:43 study02 Keepalived_vrrp[25603]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.31.10
Mar 16 15:54:43 study02 Keepalived_healthcheckers[25602]: Netlink reflector reports IP 192.168.31.10 added
Mar 16 15:54:48 study02 Keepalived_vrrp[25603]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.31.10
Mar 16 15:54:41 study01 Keepalived[25735]: Stopping Keepalived v1.2.13 (03/19,2015)
Mar 16 15:54:41 study01 Keepalived_vrrp[25738]: VRRP_Instance(VI_1) sending 0 priority
Mar 16 15:54:41 study01 Keepalived_vrrp[25738]: VRRP_Instance(VI_1) removing protocol VIPs.
Mar 16 15:54:41 study01 Keepalived_healthcheckers[25737]: Netlink reflector reports IP 192.168.31.10 removed
用户访问192.168.31.10,不停的刷新,会依次看到real-server1和real-server2的内容。
提示:要注释掉会话保持参数,否则看不到现象。
MacBook-Pro:~ momo$ curl 192.168.31.10
this is real-linux01
MacBook-Pro:~ momo$ curl 192.168.31.10
this is real-linux02
MacBook-Pro:~ momo$ curl 192.168.31.10
this is real-linux01
MacBook-Pro:~ momo$ curl 192.168.31.10
this is real-linux02
MacBook-Pro:~ momo$ curl 192.168.31.10
this is real-linux01
MacBook-Pro:~ momo$ curl 192.168.31.10
this is real-linux02
关闭掉real-server1的Apache服务,这个时候用户访问到的就是real-server2的内容,业务不受影响,关闭real-server2,同样的效果,用户业务也不会受到影响。
Mar 16 15:58:28 study01 Keepalived_healthcheckers[26083]: TCP connection to [192.168.31.13]:80 failed !!!
Mar 16 15:58:28 study01 Keepalived_healthcheckers[26083]: Removing service [192.168.31.13]:80 from VS [192.168.31.10]:80
Mar 16 15:58:58 study01 Keepalived_healthcheckers[26083]: TCP connection to [192.168.31.13]:80 success.
Mar 16 15:58:58 study01 Keepalived_healthcheckers[26083]: Adding service [192.168.31.13]:80 to VS [192.168.31.10]:80
标签:安装 规划 nss rtu smt ifconfig 连接 The reflect
原文地址:https://www.cnblogs.com/wangzengyi/p/12504569.html