Nginx负载均衡集群

 集群是什么

 为什么要使用集群

 集群的分类

计算机集群按照功能可以分成如下几类

  1. 负载均衡集群

  2. 高可用集群

  3. 高性能计算集群

  4. 网格计算

常见集群硬,软件设备介绍

互联网企业常用的开源集群软件有:

  1. nginx

  2. lvs

  3. haproxy

  4. keepalived

  5. hearbeat

企业常用的商业集群硬件有:

  1. f5

  2. netscaler

  3. radware

  4. A10

淘宝,京东,新浪等公司用过netscaler等负载均衡产品,以及F5产品。

 

 Nginx负载均衡部署

ngx_http_proxy_module        proxy代理模块,用于把请求抛给后端的服务器节点,或是upstream服务器池
ngx_http_upstream_module    负载均衡模块,实现服务器的负载均衡节点配置,以及健康检查

  

 服务器准备

  1. lb01,主负载均衡器,

  2. lb02, 备负载均衡器

  3. web01

  4. web02

1.安装依赖环境,重要
注意统一更换阿里云yum源!
yum install gcc patch libffi-devel python-devel  zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel openssl openssl-devel wget vim -y



2.编译安装nginx,编译安装能够统一管理目录,便于后期维护
mkdir -p /home/chaoge/tools
wget -P /home/chaoge/tools/ http://nginx.org/download/nginx-1.16.0.tar.gz

3.解压缩安装nginx
cd /home/chaoge/tools/
tar xf nginx-1.16.0.tar.gz
cd nginx-1.16.0
./configure --user=nginx --group=nginx --prefix=/opt/nginx-1.16.0 && make && make install

4.统一配置nginx环境变量
ln -s /opt/nginx-1.16.0/ /opt/nginx
检查软连接
ls -dl /opt/nginx

5.配置nginx环境变量
echo "PATH='/opt/nginx/sbin:/opt/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin'" >> /etc/profile

source /etc/profile

6.检查nginx环境变量
which nginx

  

配置测试nginx服务

# 先做备份
[root@web01 opt]# cp /opt/nginx/conf/nginx.conf{,.bak}

[root@web01 opt]# cat /opt/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

server {
listen 80;
server_name bbs.chaoge.com;
location / {
    root html/bbs;
    index index.html;
}
access_log logs/access_bbs.log main;
}
server {
listen 80;
server_name www.chaoge.com;
location / {
    root html/www;
    index index.html index.htm;
}
access_log logs/access_www.log main;
}


}

【创建站点资源数据】

1.创建数据文件夹
mkdir -p /opt/nginx/html/{www,bbs}

2.创建nginx静态网页文件
[root@web01 opt]# echo "chaoge_www_121" > /opt/nginx/html/www/index.html
[root@web01 opt]# echo "chaoge_bbs_121" > /opt/nginx/html/bbs/index.html

【启动nginx】

[root@web01 opt]# nginx -t
nginx: the configuration file /opt/nginx-1.16.0//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx-1.16.0//conf/nginx.conf test is successful
[root@web01 opt]#
[root@web01 opt]# nginx
[root@web01 opt]# netstat -tunlp|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7981/nginx: master

  

配置lb01

该load balance 功能是由nginx提供,修改nginx.conf如下

1.添加一个负载均衡池参数
# 定义web服务器地址池,也就是121,122两个节点
upstream www_pools {
server 192.168.178.121 weight=1;
server 192.168.178.122 weight=1;
}


2.修改server{}虚拟主机参数
server {
        listen       80;
        server_name  www.chaoge.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
                proxy_pass http://www_pools;
        }

        ...
}

  

【 启动lb01负载均衡器】

[root@lb01 nginx-1.16.0]# nginx -t
nginx: the configuration file /opt/nginx-1.16.0/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx-1.16.0/conf/nginx.conf test is successful

# 如果出现如下错误,表明当nginx还未启动,找不到pid文件
[root@lb01 nginx-1.16.0]# nginx -s reload
nginx: [error] invalid PID number "" in "/opt/nginx-1.16.0/logs/nginx.pid"

# 直接启动nginx即可
[root@lb01 nginx-1.16.0]# nginx
[root@lb01 nginx-1.16.0]#

# 检查负载均衡器
[root@lb01 nginx-1.16.0]# netstat -tunlp|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7500/nginx: master

  

原文地址:https://www.cnblogs.com/abc1234567/p/14219296.html