通过四层负载LVS+keepalived将请求转发到nginx代理服务器。通过代理服务器访问后端真实应用服务

通过四层负载LVS+keepalived将请求转发到nginx代理服务器。通过代理服务器访问后端真实应用服务。

Centeos7系统

准备环境6台机器;

lvs+keepalived—两台

nginx代理 —两台

tomcat----一台

php ----一台

所有机器关闭防火墙和selinux

所有机器解析如下:vim /etc/hosts
#在最后添加

192.168.246.142 lvs-keep-master
192.168.246.143 lvs-keep-backup
192.168.246.144 nginx-web1
192.168.246.145 nginx-web2
192.168.246.146 tomcat-server
192.168.246.147 php-server

步骤一部署lvs-keepalived服务

[root@lvs-keep-master ~]# yum -y install ipvsadm keepalived
[root@lvs-keep-master ~]# cd /etc/keepalived/
[root@lvs-keep-master keepalived]# cp keepalived.conf keepalived.conf.bak
[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.246.16/32	#(虚拟IP;vip)
    }
}

virtual_server 192.168.246.16 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    net_mask 255.255.255.0
    protocol TCP

    real_server 192.168.246.144 80 {	#(nginx-web1的IP)
        weight 1
        inhibit_on_failure
        TCP_CHECK {
		connect_port 80
		connect_timeout 3
	}
}

    real_server 192.168.246.145 80 {	#(nginx-web2的IP)
        weight 1
        inhibit_on_failure
        TCP_CHECK {
                connect_port 80
                connect_timeout 3
        }
}
}
[root@lvs-keep-backup ~]# yum -y install ipvsadm keepalived
[root@lvs-keep-backup ~]# cd /etc/keepalived/
[root@lvs-keep-backup keepalived]# cp keepalived.conf keepalived.conf.bak
将master上面的配置文件拷贝backup机器上面
[root@lvs-keep-master keepalived]# scp keepalived.conf 192.168.246.143:/etc/keepalived/
修改backup机器配置文件如下:
[root@lvs-keep-backup keepalived]# vim keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id backup   #修改为backup
}

vrrp_instance VI_1 {
    state BACKUP    #修改为BACKUP
    interface ens33
    nopreempt       #不抢占资源
    virtual_router_id 80
    priority 50		#改成50,
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.246.16/32
    }
}

virtual_server 192.168.246.16 80 {		#(虚拟IP;vip)
    delay_loop 6
    lb_algo rr
    lb_kind DR
    net_mask 255.255.255.0
    protocol TCP

    real_server 192.168.246.144 80 {	#(nginx-web1的IP)
        weight 1
        inhibit_on_failure
        TCP_CHECK {
		connect_port 80
		connect_timeout 3
	}
}

    real_server 192.168.246.145 80 {	#(nginx-web2的IP)
        weight 1
        inhibit_on_failure
        TCP_CHECK {
                connect_port 80
                connect_timeout 3
        }
}
}
两台均启动keepalived
[root@lvs-keep-master ~]# systemctl start keepalived
[root@lvs-keep-backup ~]# systemctl start keepalived
master检查vip是生成:
[root@lvs-keep-master ~]# ip a

第二步部署nginx服务

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
2.给两台机器lo配置vip
[root@nginx-web1 ~]# ip addr add dev lo 192.168.246.16/32		#(虚拟IP;vip)
[root@nginx-web1 ~]# echo "net.ipv4.conf.all.arp_ignore = 1" >> /etc/sysctl.conf
[root@nginx-web1 ~]# sysctl -p
====================================================================================
[root@nginx-web2 ~]# ip addr add dev lo 192.168.246.16/32		#(虚拟IP;vip)
[root@nginx-web2 ~]# echo "net.ipv4.conf.all.arp_ignore = 1" >> /etc/sysctl.conf
[root@nginx-web2 ~]# sysctl -p
3.配置nginx反向代理
[root@nginx-web1 ~]# cd /etc/nginx/conf.d/
[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.246.146:8080;		#(tomcat-server的IP)
        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.246.147:80;		#(PHP-server的IP)
        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;
    }

}
nginx-web2操作:
[root@nginx-web2 ~]# cd /etc/nginx/conf.d/
[root@nginx-web2 conf.d]# mv default.conf default.conf.bak
将nginx-web1上面的proxy.conf拷贝到nginx-web2上面
[root@nginx-web1 conf.d]# scp proxy.conf 192.168.246.145:/etc/nginx/conf.d/		#(nginx-web2 的IP)
启动两台nginx
[root@nginx-web1 ~]# systemctl start nginx
[root@nginx-web2 ~]# systemctl start nginx

第三步:部署tomcat服务

1.安装jdk

上传jdk1.8到服务器。安装jdk
[root@tomcat-server ~]# tar xzf jdk-8u191-linux-x64.tar.gz -C /usr/local/
[root@tomcat-server ~]# cd /usr/local/
[root@tomcat-server local]# mv jdk1.8.0_191/ java
设置环境变量:
[root@tomcat-server local]# vim /etc/profile
export JAVA_HOME=/usr/local/java #指定java安装目录
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH #用于指定java系统查找命令的路径
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar #类的路径,在编译运行java程序时,如果有调用到其他类的时候,在classpath中寻找需要的类。

检测JDK是否安装成功:
[root@tomcat-server local]# source /etc/profile
[root@tomcat-server local]# java -version
java version “1.8.0_191”
Java™ SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot™ 64-Bit Server VM (build 25.191-b12, mixed mode)

2.安装tomcat

[root@tomcat-server ~]# mkdir /data/application -p
[root@tomcat-server ~]# cd /usr/src/
[root@tomcat-server src]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.42/bin/apache-tomcat-8.5.42.tar.gz
[root@tomcat-server src]# tar xzf apache-tomcat-8.5.42.tar.gz -C /data/application/
[root@tomcat-server src]# cd /data/application/
[root@tomcat-server application]# mv apache-tomcat-8.5.42/ tomcat
设置环境变量:

[root@tomcat-server application]# vim /etc/profile
#在最后添加如下
export TOMCAT_HOME=/data/application/tomcat #指定tomcat的安装目录
[root@tomcat-server application]# source /etc/profile
查看tomcat是否安装成功:
[root@tomcat-server tomcat]# /data/application/tomcat/bin/version.sh
Using CATALINA_BASE: /data/application/tomcat
Using CATALINA_HOME: /data/application/tomcat
Using CATALINA_TMPDIR: /data/application/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH:
/data/application/tomcat/bin/bootstrap.jar:/data/application/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/8.5.42
Server built: Jun 4 2019 20:29:04 UTC
Server number: 8.5.42.0
OS Name: Linux
OS Version: 3.10.0-693.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_191-b12
JVM Vendor: Oracle Corporation

3.启动tomcat

启动程序 # /data/application/tomcat/bin/startup.sh #启动
关闭程序 # /data/application/tomcat/bin/shutdown.sh #关闭

启动tomcat,浏览器访问: IP地址:8080

第四步部署php服务

1.安装php 
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
[root@php-server ~]# systemctl start php-fpm
[root@php-server ~]# systemctl enable php-fpm

2.配置nginx
配置nginx源与安装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
[root@php-server yum.repos.d]# systemctl start nginx
[root@php-server yum.repos.d]# systemctl enable nginx

[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;
    }

}
创建index.php文件
[root@php-server conf.d]# cd /usr/share/nginx/html/
[root@php-server html]# vim index.php
<?php
phpinfo();
?>

启动nginx

浏览器访问PHP服务:
192.168.246.147/index.php
开始测试:

通过vip访问:tomcat
192.168.246.16
#(浏览器输入 vip 的地址,访问到的就是tomcat的页面)

通过vip访问:php
192.168.246.16:index.php
#(浏览器输入 vip 的地址,后面加上 index.php ,他就会识别出你的URL,跳转到PHP页面)
到此,OK

你可能感兴趣的:(linux,请求转发)