上一章节博主为大家介绍了目前大型互联网项目的系统架构体系,相信大家应该注意到其中很重要的一块知识nginx技术,在本节博主将为大家分享nginx的相关技术以及配置过程。
一、nginx相关概念
(1.1)反向代理
反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。
(1.2)负载均衡
负载均衡,英文名称为Load Balance,是指建立在现有网络结构之上,并提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。其原理就是数据流量分摊到多个服务器上执行,减轻每台服务器的压力,多台服务器共同完成工作任务,从而提高了数据的吞吐量。
二、安装nginx
(2.1)下载nginx
(2.2)上传并解压nginx,tar -zxvf nginx-1.14.0.tar.gz -C /usr/local/src
(2.3)进入到nginx源码目录:cd /usr/local/src/nginx-1.14.0/
(2.4)检查安装环境,并指定将来要安装的路径: ./configure --prefix=/usr/local/nginx
缺包报错 ./configure: error: C compiler cc is not found
(2.5)由于上一步骤提示缺少C编译器,先安装c编译器;yum -y install gcc
(2.6)继续2.4步骤中的检查安装环境./configure --prefix=/usr/local/nginx
缺包报错./configure: error: the HTTP rewrite module requires the PCRE library.
(2.7)由于上一步骤提示缺少PCRE library,先安装RCRE library;
yum list|grep pcre
yum -y install pcre-devel
(2.7)继续2.4步骤中的检查安装环境./configure --prefix=/usr/local/nginx
缺包报错./configure: error: the HTTP gzip module requires the zlib library.
(2.8)由于上一步骤提示缺少zlib library,先安装zlib library;
yum list|grep zlib
yum -y install zlib-devel
(2.9)继续2.4步骤中的检查安装环境./configure --prefix=/usr/local/nginx,成功
(2.10)执行安装nginx命令:make && make install
(2.11)安装完成后,测试检查nginx是否正常
./usr/loca/nginx/sbin/nginx
(2.12)查看端口是否有ngnix进程监听
netstat -ntlp | grep 80
(2.13)使用浏览器访问,发现打不开nginx默认页面
(2.14)检查linux防火墙发现只接受端口22的数据包
(2.15)添加允许端口80的数据访问,执行:
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -I OUTPUT -p tcp --sport 80 -j ACCEPT
(2.16)浏览器访问,nginx已经可以正常访问
(2.17)配置nginx反向代理
(2.18)启动tomcat,并重启nginx,浏览器访问;./sbin/nginx
(2.19)nginx补充配置
#动态资源 index.jsp
location ~ .*\.(jsp|do|action)$ {
proxy_pass http://centos-aaron-04:8080;
}
#静态资源
location ~ .*\.(html|js|css|gif|jpg|jpeg|png)$ {
expires 3d;
}
(2.20)nginx负载均衡配置
在http这个节下面配置一个叫upstream的,后面的名字可以随意取,但是要和location下的proxy_pass http://后的保持一致。
http {
#是在http里面的, 已有http, 不是在server里,在server外面
upstream tomcats {
server 192.168.29.132:8080 weight=1;#weight表示多少个
server 192.168.29.133:8080 weight=1;
server 192.168.29.134:8080 weight=1;
}
#卸载server里
location ~ .*\.(jsp|do|action) {
proxy_pass http://tomcats; #tomcats是后面的tomcat服务器组的逻辑组号
}
}
(2.21)重新加载nginx配置文件./sbin/nginx -s reload;并浏览器访问服务
三、利用keepalived实现高可靠(HA)
(3.1)高可用概念
HA(High Available), 高可用性集群,是保证业务连续性的有效解决方案,一般有两个或两个以上的节点,且分为活动节点及备用节点。
(3.2)高可靠软件keepalived
keepalive是一款可以实现高可靠的软件,通常部署在2台服务器上,分为一主一备。Keepalived可以对本机上的进程进行检测,一旦Master检测出某个进程出现问题,将自己切换成Backup状态,然后通知另外一个节点切换成Master状态。
(3.3)keepalived安装,下载keepalived,官网地址:http://www.keepalived.org/download.html
(3.4)上传keepalived压缩包到服务器
(3.5)将keepalived解压到/usr/local/src目录下:tar -zxvf keepalived-1.4.5.tar.gz -C /usr/local/src
(3.6)进入到/usr/local/src/keepalived-1.4.5/目录:cd /usr/local/src/keepalived-1.4.5/
(3.7)开始configure编译检查:./configure --prefix=/usr/local/keepalived
缺包报错configure: error:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!
(3.8)由于上一步骤提示缺少OpenSSL包,先安装OpenSSL
yum list|grep openssl
yum install -y openssl openssl-devel
(3.9)继续执行3.7步骤中的编译检查:./configure --prefix=/usr/local/keepalived,编译检查通过
(3.10)编译并安装:make && make install
安装过程中有很多警告信息:分析后是因为没安装lvs引起.
(3.11)安装LVS
yum install ipvsadm
(3.12)将keepalived添加到系统服务中
拷贝执行文件
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
将init.d文件拷贝到etc下,加入开机启动项
cp /usr/local/src/keepalived-1.4.5/keepalived/etc/init.d/keepalived /etc/init.d/keepalived
将keepalived文件拷贝到etc下
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
创建keepalived文件夹
mkdir -p /etc/keepalived
将keepalived配置文件拷贝到etc下
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
添加可执行权限
chmod +x /etc/init.d/keepalived
##以上所有命令一次性执行:
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/src/keepalived-1.4.5/keepalived/etc/init.d/keepalived /etc/init.d/keepalived
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir -p /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
chmod +x /etc/init.d/keepalived
chkconfig --add keepalived
chkconfig keepalived on
添加keepalived到开机启动
chkconfig --add keepalived
chkconfig keepalived on
(3.13)配置keepalived虚拟IP;修改配置文件: /etc/keepalived/keepalived.conf
#MASTER节点
global_defs {
}
vrrp_instance VI_1 {
state MASTER #指定A节点为主节点 备用节点上设置为BACKUP即可
interface eth0 #绑定虚拟IP的网络接口
virtual_router_id 51 #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
priority 100 #主节点的优先级(1-254之间),备用节点必须比主节点优先级低
advert_int 1 #组播信息发送间隔,两个节点设置必须一样
authentication { #设置验证信息,两个节点必须一致
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #指定虚拟IP, 两个节点设置必须一样
192.168.29.190/24 #如果两个nginx的ip分别是192.168.29.135,,...139,则此处的虚拟ip跟它俩同一个网段即可
}
}
#BACKUP节点
global_defs {
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.29.190/24
}
}
(3.14)分别启动两台机器上的keepalived;命令:service keepalived start
(3.15)测试:
杀掉master上的keepalived进程,你会发现,在slave机器上的eth0网卡多了一个ip地址
查看ip地址的命令: ip addr
(3.16).配置keepalived心跳检查
原理:Keepalived并不跟nginx耦合,它俩完全不相关的两个软件但是keepalived提供一个机制:让用户自定义一个shell脚本去检测用户自己的程序,返回状态给keepalived就可以了.
#MASTER节点
global_defs {
}
vrrp_script chk_health {
script "[[ `ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1"
interval 1 #每隔1秒执行上述的脚本,去检查用户的程序ngnix
weight -2
}
vrrp_instance VI_1 {
state MASTER #指定A节点为主节点 备用节点上设置为BACKUP即可
interface eth0 #绑定虚拟IP的网络接口
virtual_router_id 51 #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
priority 100 #主节点的优先级(1-254之间),备用节点必须比主节点优先级低
advert_int 1 #组播信息发送间隔,两个节点设置必须一样
authentication { #设置验证信息,两个节点必须一致
auth_type PASS
auth_pass 1111
}
track_script {
chk_health
}
virtual_ipaddress { #指定虚拟IP, 两个节点设置必须一样
192.168.29.190/24 #如果两个nginx的ip分别是192.168.29.135,,...139,则此处的虚拟ip跟它俩同一个网段即可
}
notify_master "/usr/local/keepalived/sbin/notify.sh master"
notify_backup "/usr/local/keepalived/sbin/notify.sh backup"
notify_fault "/usr/local/keepalived/sbin/notify.sh fault"
}
#BACKUP节点
global_defs {
}
vrrp_script chk_health {
script "[[ `ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1"
interval 1 #每隔1秒执行上述的脚本,去检查用户的程序ngnix
weight -2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_health
}
virtual_ipaddress {
192.168.29.190/24
}
notify_master "/usr/local/keepalived/sbin/notify.sh master"
notify_backup "/usr/local/keepalived/sbin/notify.sh backup"
notify_fault "/usr/local/keepalived/sbin/notify.sh fault"
}
(3.17)添加切换通知脚本:vi /usr/local/keepalived/sbin/notify.sh
#!/bin/bash
case "$1" in
master)
/usr/local/nginx/sbin/nginx
exit 0
;;
backup)
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx
exit 0
;;
fault)
/usr/local/nginx/sbin/nginx -s stop
exit 0
;;
*)
echo 'Usage: notify.sh {master|backup|fault}'
exit 1
;;
esac
(3.18)添加执行权限:chmod +x /usr/local/keepalived/sbin/notify.sh
(3.19)启动两台nginx、tomcat、keepalived测试
在mater活着的时候,浏览器访问一次,master上nginx日志多一条访问记录;
关闭master后,浏览器访问一次,backup上的nginx多一条访问日志
以上是nginx安装搭建以及keeperalived配置主备nginx服务器的全过程;总结:可以将nginx设置为开机启动,这样通知脚本可以使用nginx服务来启动。最后,如果大家对nginx高可用负载均衡