目录
前言
LVS 是 Linux Virtual Server
的简称,也就是 Linux 虚拟服务器,是一个由章文嵩博士发起的自由软件项目,它的官方站点是www.linuxvirtualserver.org。现在LVS已经是Linux标准内核的一部分,因此性能较高。通过LVS提供的负载均衡技术和Linux操作系统实现一个高性能、高可用的服务器群集,它具有良好可靠性、可扩展性和可操作性,从而以低廉的成本实现最优的服务性能。
一、环境规划
主机 | 功能 |
---|---|
192.168.139.151 | LVS-master + keepalived |
192.168.139.152 | LVS-backup + keepalived |
192.168.139.153 | Nginx代理服务器1 |
192.168.139.154 | Nginx代理服务器2 |
192.168.139.155 | Tomcat后端应用服务 |
192.168.139.156 | PHP后端应用服务 |
二、环境架构
三、环境部署
关闭防火墙及selinux。
3.1 部署LVS-keepalived服务
3.1.1 LVS-master端
(1)安装keepalived
yum -y install ipvsadm keepalived
(2)修改keepalived配置文件
[root@lvs-keep-master keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
router_id master
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 80
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.139.160/32
}
}
virtual_server 192.168.139.160 80 {
delay_loop 6
lb_algo rr
lb_kind DR
net_mask 255.255.255.0
protocol TCP
real_server 192.168.139.151 80 {
weight 1
inhibit_on_failure
TCP_CHECK {
connect_port 80
connect_timeout 3
}
}
real_server 192.168.139.152 80 {
weight 1
inhibit_on_failure
TCP_CHECK {
connect_port 80
connect_timeout 3
}
}
}
3.1.2 LVS-backup端
(1)安装keepalived
yum -y install ipvsadm keepalived
(2)修改keepalived配置文件
[root@lvs-keep-backup keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
router_id backup
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
nopreempt
virtual_router_id 80
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.139.160/32
}
}
virtual_server 192.168.139.160 80 {
delay_loop 6
lb_algo rr
lb_kind DR
net_mask 255.255.255.0
protocol TCP
real_server 192.168.139.151 80 {
weight 1
inhibit_on_failure
TCP_CHECK {
connect_port 80
connect_timeout 3
}
}
real_server 192.168.139.152 80 {
weight 1
inhibit_on_failure
TCP_CHECK {
connect_port 80
connect_timeout 3
}
}
}
3.1.3 启动keepalived
[root@lvs-keep-master ~]# systemctl start keepalived
[root@lvs-keep-backup ~]# systemctl start keepalived
3.2 部署Nginx服务
3.2.1 两台机器部署nginx服务
[root@nginx-web1 ~]# cd /etc/yum.repos.d/
[root@nginx-web1 yum.repos.d]# vim nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
[root@nginx-web1 yum.repos.d]# yum install yum-utils -y
[root@nginx-web1 yum.repos.d]# yum install nginx -y
3.2.2 两台机器lo均配置vip
【机器1】
[root@nginx-web1 ~]# ip addr add dev lo 192.168.139.160/32
[root@nginx-web1 ~]# echo "net.ipv4.conf.all.arp_ignore = 1" >> /etc/sysctl.conf
[root@nginx-web1 ~]# sysctl -p
【机器2】
[root@nginx-web2 ~]# ip addr add dev lo 192.168.139.160/32
[root@nginx-web2 ~]# echo "net.ipv4.conf.all.arp_ignore = 1" >> /etc/sysctl.conf
[root@nginx-web2 ~]# sysctl -p
3.2.3 两台机器均配置nginx反向代理
【机器1】
[root@nginx-web1 conf.d]# cp default.conf default.conf.bak
[root@nginx-web1 conf.d]# mv default.conf proxy.conf
[root@nginx-web1 conf.d]# vim proxy.conf
server {
listen 80;
server_name localhost;
charset koi8-r;
access_log /var/log/nginx/host.access.log main;
location / {
proxy_pass http://192.168.139.155:8080;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /index.php {
proxy_pass http://192.168.139.156:80;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
【机器2】
[root@nginx-web2 conf.d]# cp default.conf default.conf.bak
[root@nginx-web2 conf.d]# mv default.conf proxy.conf
[root@nginx-web2 conf.d]# vim proxy.conf
server {
listen 80;
server_name localhost;
charset koi8-r;
access_log /var/log/nginx/host.access.log main;
location / {
proxy_pass http://192.168.139.155:8080;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /index.php {
proxy_pass http://192.168.139.156:80;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
3.3 部署Tomcat后端应用服务
(1)安装JDK
-
上传jdk到服务器中,安装jdk。
[root@tomcat-server ~ ]# mkdir /application [root@tomcat-server ~ ]# tar xzf jdk-8u60-linux-x64.tar.gz -C /application/ [root@tomcat-server ~ ]# mv /application/jdk1.8.0_60 /application/jdk
-
设置环境变量
[root@tomcat-server ~ ]# vim /etc/profile JAVA_HOME=/usr/local/java PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar export JAVA_HOME PATH CLASSPATH
-
使环境变量生效
[root@tomcat-server ~ ]# source /etc/profile
-
测试jdk是否安装成功
[root@tomcat-server ~ ]# java -version java version "1.8.0_60" Java(TM) SE Runtime Environment (build 1.8.0_60-b27) Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
(2)安装 Tomcat
-
将tomcat安装包上传到服务器中
[root@tomcat-server ~ ]# tar xzf apache-tomcat-8.5.42.tar.gz -C /application/ [root@tomcat-server ~ ]# mv /application/apache-tomcat-8.5.42 /application/tomcat
-
设置并使环境变量生效
[root@tomcat-server ~ ]# echo 'export TOMCAT_HOME=/application/tomcat'>>/etc/profile [root@tomcat-server ~ ]# source /etc/profile
-
启动tomcat
[root@tomcat-server ~ ]# /application/tomcat/bin/startup.sh
3.4 部署PHP后端应用服务
(1)yum 安装php7.1
[root@php-server ~ ]# rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
[root@php-server ~ ]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@php-server ~ ]# yum install php71w-xsl php71w php71w-ldap php71w-cli php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysql php71w-mbstring php71w-bcmath php71w-mcrypt -y
[root@php-server ~ ]# yum install -y php71w-fpm
(2)启动php
[root@php-server ~ ]# systemctl start php-fpm
[root@php-server ~ ]# systemctl enable php-fpm
(3)安装Nginx服务
[root@php-server ~ ]# cd /etc/yum.repos.d/
[root@php-server yum.repos.d]# vim nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
[root@php-server yum.repos.d]# yum install yum-utils -y
[root@php-server yum.repos.d]# yum install nginx -y
(4)配置Nginx连接php
[root@php-server ~ ]# cd /etc/nginx/conf.d/
[root@php-server conf.d]# cp default.conf default.conf.bak
[root@php-server conf.d]# mv default.conf server.conf
[root@php-server conf.d]# vim server.conf
server {
listen 80;
server_name localhost;
charset koi8-r;
access_log /var/log/nginx/host.access.log main;
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
(5)创建index.php文件
[root@php-server conf.d]# cd /usr/share/nginx/html/
[root@php-server html]# vim index.php
<?php
phpinfo();
?>
(6)启动Nginx
[root@php-server conf.d]# systemctl start nginx
[root@php-server conf.d]# systemctl enable nginx
四、环境测试
客户端client通过vip进行访问(在实际生产中该VIP就是我们对外提供访问服务的实际公网IP,做解析之后,客户端就可以通过域名来访问了。这里仅做测试,通过的是IP来访问)。
4.1 访问Tomcat
4.2 访问PHP
总结
- LVS有四种工作模式
(1)NAT模式;
(2)直接路由(Direct routing)模式;
(3)IP隧道(Tunnel)模式;
(4)FULL-NAT模式。 - LVS支持10种调度算法
- 静态算法
(1)RR:轮叫调度(Round Robin)、轮询;
(2)WRR:加权轮询(Weight RR);
(3) DH:目标地址散列调度(Destination Hash );
(4) SH:源地址 hash(Source Hash) - 动态算法
(5) LC:最少链接(Least Connections);
(6)WLC:加权最少连接(Weighted Least Connections),为默认采用;
(7)SED:最短期望延迟调度(Shortest Expected Delay );
(8)NQ:永不排队/最少队列调度(Never Queue Scheduling NQ);
(9) LBLC:基于局部性的最少链接(locality-Based Least Connections);
(10)LBLCR:带复制的基于局部性最少连接(Locality-Based Least Connections with Replication)。
- 静态算法
- LVS可实现四层负载均衡,可做负载均衡集群、结合keepalived实现高可用等。