Linux Nginx hot standby and load balancing cluster configuration (1 Nginx primary server, a backup server Nginx, 2tomcat server, a client)

Nginx Profile - server features a high-performance, lightweight web services software

High stability

Low system resource consumption

HTTP processing power of concurrent connections high

 

Forward proxy (Proxy)

Proxy server proxy requests for internal network connection to the Internet

If you already have the URL cache server

 

Reverse Proxy (Reverse Proxy)

Proxy Proxy external Internet host connection request to the internal network

No Nginx reverse proxy cache function

 

Load Balancing (LoadBalance)

The traffic distribution to multiple servers for load balancing service

Eliminate single points of failure

Service Performance Tips

Nginx load balancing algorithm

round-robin (polling)

weighted(权重)

least-connected (minimum number of connections)

ip-hash (IP hash)

 

Hot standby function module keepalived

core

Load and parse the start of the main process responsible for the core keepalived, maintenance and global configuration files

check

Responsible for checking the health of the host back-end server pool

vrrp

Realization of VRRP (Virtual Router Redundancy Protocol)

Dual hot standby function to achieve

 

Backup

A primary (master) and more prepared (Backup)

Share the same virtual IP address (VIP) but with different priorities

 

Profile keepalived.conf

The sample file is located in / etc / keepalived / samples / ....

global_defs {// global configuration

     ...

}

vrrp_instance VI_1 {// definition of hot backup instance

     ...

}

 

Environmental Projects

 

Projects topology

 

The basic configuration of the environment Case

1, clone 5 virtual machines (4 RHEL6.5 64 bit server, a client station Windows 7)

2, 5 virtual machines configured the IP address, host name (refer cases to complete the environment)

3, 4 close iptables firewall Linux servers, selinux

4, open the local yum feature four Linux servers

5, testing connectivity between the hosts 5

 

克隆5台虚拟机(4台RHEL6.5 64位服务器、1台Windows 7客户机)

 

配置5台虚拟机的主机名

重启后生效

 

配置IP地址

 

关闭4台Linux服务器的iptables防火墙、selinux

 

开启4台Linux服务器的本地yum功能

 

测试5台主机间的连通性

win7客户端的IP配置

五台主机相互之间都能ping通

 

从物理机复制文件到虚拟机

虚拟机开启服务

物理机访问并上传文件

 

虚拟机获取到

 

其他虚拟机获取文件

 

Nginx服务器的配置(主、备服务器上分别完成)

安装Nginx所需的支持包

 

创建运行Nginx程序的系统用户账户www 并指定其登陆shell为/bin/false

 

解压nginx软件包

 

进入nginx的解压目录

 

配置nginx

 

# --prefix=... 指定nginx的安装路径

# --user=...,--group=... 指定运行nginx程序的用户账户和组账户

# --with-file-aio 启用文件修改支持功能

# --with-http_stub_status_module 启用状态统计功能

# --with-http_gzip_static_module 启用gzip静态压缩功能

# --with-http_flv_module 启用flv模块,提供寻求内存使用基于时间的偏移量文件

# --with-http_ssl_module 启用SSL模块

 

编译nginx 生成二进制安装文件

 

安装nginx

 

测试nginx的配置文件是否正确

 

启动nginx

停止nginx服务(杀死nginx工作进程)

kill $(cat /usr/local/nginx/logs/nginx.pid)

 

要重启nginx服务,按如下操作进行:

kill $(cat /usr/local/nginx/logs/nginx.pid)

/usr/local/nginx/sbin/nginx

 

查看服务进程

 

查看nginx程序的默认端口80

 

测试网页

 

部署节点服务器(在两台Tomcat服务器上分别完成)

卸载Linux系统自带的jdk

 

解压jdk软件包

 

移动解压后的jdk至/usr/local中 并重命名目录名为java

 

修改java的环境变量

 

重新加载环境变量

 

查看jdk版本

 

解压Tomcat软件包

 

移动解压后的tomcat到/usr/local 并重命名目录名为tomcat7

 

启动tomcat服务

查看tomcat的运行端口8080

 

测试默认页面

 

建立测试页

Tomcat1服务器WebSrv1上建立

 

Tomcat2服务器WebSrv2上建立

 

测试index.jsp页面

 

配置负载均衡功能(在主、备Nginx服务器上分别完成)

配置负载均衡功能

 

测试nginx配置文件是否正确

 

重启服务

 

查看nginx服务进程

 

查看nginx程序的默认端口

 

测试Nginx分负载均衡功能

win7客户机上分别访问192.168.113.1和192.168.113.2  能看到两个不同的java页面

 

部署keepalived(主、备Nginx服务器上分别完成)

安装keepalived的支持软件

 

安装群集管理工具ipvsadm

 

解压keepalived软件包

 

配置keepalived

# --prefix=... 指定安装路径

# --with-kernel-dir=... 指定Linux系统内核目录路径

 

编译keepalived

 

安装keepalived软件包

 

#/etc/init.d/keepalived

#chkconfig --add keepalived

#chkconfig keepalived on

 

建立配置文件keepalived.conf

主Nginx服务器

 

备份的Nginx服务器

 

将keepalived服务添加到开启自启动项中

 

设置当前系统中的keepalived服务为开启状态

 

启动keepalived服务

 

查看主Nginx服务器网卡的虚拟IP地址

 

测试双机热备功能

win7上不停ping 192.168.113.254(群集IP)

 

主机正常时

Nginx服务器可以看到群集地址

 

备份Nginx服务器看不到群集地址

 

主设备异常(故障)

在主Nginx服务器上执行

 

在备份Nginx服务器上可以看到群集地址

 

可以看到win7的ping请求超时了三个包后恢复正常

 

修复主设备

在主Nginx服务器上执行

 

在备份Nginx服务器上执行 备份设备交出控制权给主设备

 

win7的包丢了三个

 

win7测试网页 访问群集IP地址

两个窗口都访问192.168.113.254 能看到2个不同的java页面

发布了94 篇原创文章 · 获赞 68 · 访问量 4126

Guess you like

Origin blog.csdn.net/qq_37077262/article/details/104045541