linux虚拟服务器
LVS实际上相当于基于IP地址的虚拟化应用
为基于IP地址和内容请求分发的负载均衡提出了一种高效的解决办法
专为LVS和HA设计的一款健康检查工具
支持故障自动切换
支持节点健康状态检查
环境配置:
主机 | 操作系统 | IP地址 | 软件/安装包工具 |
---|---|---|---|
主负载调度器(Director1) | centos7 | ens33:192.168.133.10 | ipvsadm、keepalived |
备负载调度器(Director2) | centos7 | ens33:192.168.133.20 | ipvsadm、keepalived |
web节点服务器1 | centos7 | ens33:192.168.133.30 lo:0(VIP):192.168.133.100 | rpcbind、nfs-utils、httpd |
web节点服务器2 | centos7 | ens33:192.168.133.40 lo:0(VIP):192.168.133.100 | rpcbind、nfs-utils、httpd |
NFS服务器 | centos7 | 192.168.133.50 | rpcbind、nfs-utils |
客户机 | win10 | 192.168.133.60 |
NFS服务器:192.168.133.50
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
yum -y install nfs-utils rpcbind
systemctl start rpcbind.service
systemctl start nfs.service
systemctl enable nfs.service
systemctl enable rpcbind.service
mkdir /opt/test1
mkdir /opt/test2
chmod 777 /opt/test1
chmod 777 /opt/test2
vim /etc/exports
/opt/test1 192.168.163.0/24(rw,sync)
/opt/test2 192.168.163.0/24(rw,sync)
exportfs -rv
web节点服务器1:192.168.133.30 lo:0(VIP):192.168.133.100
web节点服务器1:192.168.133.40 lo:0(VIP):192.168.133.100
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.133.100
NETMASK=255.255.255.255
ONBOOT=yes
ifup lo:0
ifconfig lo:0
#设置临时的路由,重启失效
route add -host 192.168.133.100 dev lo:0
#开机自动添加路由
vim /etc/rc.local
/sbin/route add -host 192.168.133.100 dev lo:0
chmod +x /etc/rc.d/rc.local
vim /etc/sysctl.conf
......
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
sysctl -p
yum install -y nfs-utils rpcbind httpd
systemctl start rpcbind
systemctl enable rpcbind
systemctl start httpd.service
systemctl enable httpd.service
showmount -e 192.168.133.50
mount.nfs 192.168.133.50:/opt/test1 /var/www/html
echo 'this is test1 web!' > /var/www/html/index.html
showmount -e 192.168.133.50
mount.nfs 192.168.133.50:/opt/test2 /var/www/html
echo 'this is test2 web!' > /var/www/html/index.html
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
modprobe ip_vs
cat /proc/net/ip_vs
yum -y install ipvsadm
vim /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
sysctl -p
ipvsadm-save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm.service
ipvsadm -C
#这里-t指定的虚拟地址为本机ens33网卡ip
ipvsadm -A -t 192.168.133.10:80 -s rr
ipvsadm -a -t 192.168.133.10:80 -r 192.168.133.30:80 -g
ipvsadm -a -t 192.168.133.10:80 -r 192.168.133.40:80 -g
ipvsadm
ipvsadm -ln
ipvsadm-save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm.service
ipvsadm -C
ipvsadm -A -t 192.168.133.20:80 -s rr
ipvsadm -a -t 192.168.133.20:80 -r 192.168.163.30:80 -g
ipvsadm -a -t 192.168.133.20:80 -r 192.168.163.40:80 -g
ipvsadm -ln
yum -y install keepalived
cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak
vim keepalived.conf
......
#定义全局参数
global_defs {
#10行修改,邮件服务指向本地
smtp_server 127.0.0.1
#12行修改,指定服务器(路由器)的名称,主备服务器名称须不同,主为LVS_01,备为LVS_02
router_id LVS_01
#14行修改;加注释;vrrp_strict:严格遵守VRRP协议。下列情况将会阻止启动Keepalived:1. 没有VIP地址。2. 单播邻居。3. 在VRRP版本2中有IPv6地址。
#vrrp_strict
}
#定义VRRP热备实例参数
vrrp_instance VI_1 {
#20行修改;指定热备状态,主为MASTER,备为BACKUP
state MASTER
#21行修改;指定承载vip地址的物理接口
interface ens33
#22行修改;指定虚拟路由器的ID号,每个热备组保持一致
virtual_router_id 10
#23行修改;指定优先级,数值越大优先级越高,主为100,备为90
priority 100
#通告间隔秒数(心跳频率)
advert_int 1
#定义认证信息,每个热备组保持一致
authentication {
#认证类型
auth_type PASS
#27行修改,指定验证密码,主备服务器保持一致
auth_pass 123456
}
#指定群集vip地址
virtual_ipaddress {
192.168.133.100
}
}
#34行修改,指定虚拟服务器地址(VIP)、端口,定义虚拟服务器和Web服务器池参数
virtual_server 192.168.133.100 80 {
#健康检查的间隔时间(秒)
delay_loop 6
#指定调度算法,轮询(rr)
lb_algo rr
#37行修改,指定群集工作模式,直接路由(DR)
lb_kind DR
#连接保持时间(秒)
persistence_timeout 50
#应用服务采用的是TCP协议
protocol TCP
#41行修改,指定第一个Web节点的地址、端口
real_server 192.168.133.30 80 {
#节点的权重
weight 1
#43行删除,添加以下健康检查方式
TCP_CHECK {
#添加检查的目标端口
connect_port 80
#添加连接超时(秒)
connect_timeout 3
#添加重试次数
nb_get_retry 3
#添加重试间隔
delay_before_retry 4
}
}
#添加第二个 Web节点的地址、端口
real_server 192.168.133.40 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
#删除后面多余的配置
systemctl start keepalived
#查看虚拟网卡vip
ip addr show dev ens33
访问192.168.133.100,隔一段时间进行刷新
轮询显示
lvs:linux虚拟服务器
keepalived:为lvs和HA健康检查工具
支持故障自动切换
健康状态检查