通过四层负载LVS+keepalived将请求转发到nginx代理服务器。通过代理服务器访问后端真实应用服务。
拓补图:
准备环境6台机器;
lvs+keepalived---两台
nginx代理 ---两台
tomcat----一台
php ----一台
所有机器关闭防火墙和selinux
所有机器解析如下:
192.168.246.142 lvs-keep-master 192.168.30.129
192.168.246.143 lvs-keep-backup 192.168.30.130
192.168.246.144 nginx-web1 192.168.30.131
192.168.246.145 nginx-web2 192.168.30.132
192.168.246.146 tomcat-server 192.168.30.133
192.168.246.147 php-server 192.168.30.134
步骤一部署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
}
}
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 {
weight 1
inhibit_on_failure
TCP_CHECK {
connect_port 80
connect_timeout 3
}
}
real_server 192.168.246.145 80 {
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.30.131:/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
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.30.16/32
}
}
virtual_server 192.168.30.16 80 {
delay_loop 6
lb_algo rr
lb_kind DR
net_mask 255.255.255.0
protocol TCP
real_server 192.168.30.133 80 {
weight 1
inhibit_on_failure
TCP_CHECK {
connect_port 80
connect_timeout 3
}
}
real_server 192.168.30.134 80 {
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.30.16/32
[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.30.16/32
[root@nginx-web2 ~]# echo "net.ipv4.conf.all.arp_ignore = 1" >> /etc/sysctl.conf
[root@nginx-web2 ~]# sysctl -p
3.配置nginx反向代理
[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.30.133:8080;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ \.php$ {
proxy_pass http://192.168.30.134;
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.con拷贝到nginx-web2上面
[root@nginx-web1 conf.d]# scp proxy.conf 192.168.30.133:/etc/nginx/conf.d/
启动两台nginx
第三步:部署tomcat服务。。。略
启动tomcat浏览器访问:
第四步部署php服务
1.安装php 略
2.配置nginx
配置nginx源与安装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
浏览器访问nginx服务:
开始测试:
通过vip访问:tomcat
通过vip访问php