LVS:(linux virtual server)是一款网络模型四层的负载软件,即IP+端口的转发模式,
LVS:提供三种负载方式:
VS/NAT:网络地址转化
VS/TUN:端
VS/DR:直接轮询模式
10种调度算法:常用的由一下几种:
rr:轮叫调度,不管每个服务器的实际情况
wrr :加权轮叫调度。
lc:最少链接调度
wlc:加权最少链接调度
环境如下:
操作系统:centos7
Lvs主调度器:192.168.65.128
备调度器:192.168.65.129
VIP:192.168.65.200
Real server1:192.168.65.150
Real server2:192.168.65.151
系统结构图如下:
LVS主备上面,配置linux系统内核参数开启内核的路由模式
vim /etc/sysctl.conf
net.ipv4.ip.forward = 1
立即生效
```
# sysctl -p
```
所有服务器先暂时关闭iptables服务,避免影响服务搭建。
在主备服务器上安装LVS
选择yum或源码包的安装方式
```
#yum install ipvsadm
#wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
#tar –zxvf ipvsadm-1.26.tar.gz
#make && make install
```
对主备配置LVS,将请求分别轮询到150和151
```
#ipvsadm –A 192.168.65.200:80 –s wrr 这里是加权轮询
#ipvsadm –a 192.168.65.200:80 –r 192.168.65.150:80 –g –w 5
# ipvsadm –a 192.168.65.200:80 –r 192.168.65.150:80 –g –w 5
```
为了使配置文件一直生效可以添加进配置文件中
```
Vim /etc/sysconfig/ipvsadm
ipvsadm –A 192.168.65.200:80 –s wrr 这里是加权轮询
ipvsadm –a 192.168.65.200:80 –r 192.168.65.150:80 –g –w 5
ipvsadm –a 192.168.65.200:80 –r 192.168.65.150:80 –g –w 5
#-g表示直接路由,-w表示权重的值
```
在主备服务器安装keepalived
```
#wget http://www.keepalived.org/software/keepalived-1.2.4.tar.gz
#tar –zxvf keepalived-1.2.4.tar.gz
#./configure –prefix=/usr/local/keepalived
#make && make install
```
将keepalived做成开启启动服务
```
#cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
#cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
#cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
#cp /usr/local/sbin/keepalived /usr/sbin/
```
在主备配置keepalived.conf 主LVS上面的keepalived的配置文件
```
! Configuration File for keepalived
global_defs {
notification_email {
}
router_id LVS_MASTER //备LVS上面LVS_BACKUP
}
vrrp_instance VI_1 {
state MASTER //备LVS设置BACKUP
interface eno16777736 //centos6设置为eth0
virtual_router_id 51
priority 150 //备LVS设置100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.65.200
}
}
vrrp_server 192.168.65.200 80 {
delay_loop 3
lb_algo wrr
lb_kind DR
persistence_timeout 30
protocol TCP
real_server 192.168.65.200 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.65.200 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
```
安装nginx 下载需要的nginx的源码包
```
#wget http://nginx.org/download/nginx-xx.xx.tar.gz
#tar –zxvf nginx1.9.4.tar.gz
#./configure –prefix=/usr/local/nginx
#make && make install
```
配置real server服务器,两台nginx的服务器都一样,禁止arp相应的请求
```
Vim /etc/sysctl.conf
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce =2
```
两台real server 配置虚拟网卡 新建ifcfg-lo:0
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
```
DEVICE=lo:0
BOOTPROTO=static
IPADDR=192.168.65.200
NETMASK=255.255.255.255
ONBOOT=yes
```
使配置生效:sysctl –p
为了区分两台real server服务器对nginx做如下配置
修改Real server1 和real server2
```
Vim nginx/html/index.html
<h1>Welcome to nginx!192.168.65.150</h1>
<h1>Welcome to nginx!192.168.65.151</h1>
```
分别启动服务
主备LVS : service ipvsadm start
Service keepalived start
Real server: service nginx start
测试在浏览器输入:192.168.65.200
任意一台服务器宕机,查看服务是否可用