zoukankan      html  css  js  c++  java
  • nginx部署基于http负载均衡器

    nginx跨多个应用程序实例的负载平衡是一种用于优化资源利用率,最大化吞吐量,减少延迟和确保容错配置的常用技术。

    环境介绍

    配置nginx负载均衡器因会用到多台服务器来进行,所以下面我会用到docker,具体docker的使用请移步docker实战

    • 系统环境:

       root@ubuntu:~# lsb_release  -a			#查看系统版本
       No LSB modules are available.
       Distributor ID:	Ubuntu
       Description:	Ubuntu 19.10
       Release:	19.10
       Codename:	eoan
       root@ubuntu:~# uname -a			#查看系统是多少位
       Linux ubuntu 5.3.0-18-generic #19-Ubuntu SMP Tue Oct 8 20:14:06 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
      
    • docker版本和相关操作系统版本

       docker版本:
       root@ubuntu:~# docker --version			#查看docker版本
       Docker version 19.03.3, build a872fc2f86
      
       操作系统版本:
       [root@57b669db1de1 /]# cat /etc/redhat-release 		#查看系统版本
       CentOS Linux release 8.0.1905 (Core) 
       [root@57b669db1de1 /]# uname -a
       Linux 57b669db1de1 5.3.0-18-generic #19-Ubuntu SMP Tue Oct 8 20:14:06 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
      
    • 软件版本:

       nginx版本:
       root@ubuntu:~# nginx  -v		查看nginx版本
       nginx version: nginx/1.16.1 (Ubuntu)
      
       docker中nginx版本:
       [root@57b669db1de1 /]# nginx -v		#查看nginx版本
       nginx version: nginx/1.14.1
      

    安装nginx

    具体nginx的安装请参考nginx安装部署

    • 安装依赖软件

       更新软件包:
       root@ubuntu:~# apt-get update
       
       安装依赖软件:
       root@ubuntu:~# apt -y install openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev make gcc
      
    • 编译安装nginx

       下载nginx:
       root@ubuntu:~# wget http:#nginx.org/download/nginx-1.17.6.tar.gz
      
       解压:
       root@ubuntu:/opt# tar zxf nginx-1.17.6.tar.gz 
       root@ubuntu:/opt# cd nginx-1.17.6/
       root@ubuntu:/opt/nginx-1.17.6# ls		#列出目录
       auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
       root@ubuntu:/opt/nginx-1.17.6# 
      
       配置:
       root@ubuntu:/opt/nginx-1.17.6# ./configure --prefix=/usr/local/nginx
       编译以及部署:
       root@ubuntu:/opt/nginx-1.17.6# make && make install
       root@ubuntu:/opt/nginx-1.17.6# cd /usr/local/nginx/
       root@ubuntu:/usr/local/nginx# ls
       conf  html  logs  sbin
      
       启动:
       root@ubuntu:/usr/local/nginx# ln sbin/nginx /usr/local/sbin/		#创建nginx软连接,设置为命令
       root@ubuntu:/usr/local/nginx# nginx		#启动nginx
       root@ubuntu:/usr/local/nginx# netstat -anpl | grep nginx		#查看nginx端口是否开启
       root@ubuntu:/usr/local/nginx# lsof -i:80			#查看80端口是否开启
      

    docker安装nginx

    • 创建容器

       下载镜像:
       root@ubuntu:~# docker pull registry.cn-beijing.aliyuncs.com/blxt/centos
      
       创建容器并启动:
       root@ubuntu:~# 
       root@ubuntu:~# docker run -itd --name nginx1 --privileged  registry.cn-beijing.aliyuncs.com/blxt/centos /sbin/init
       root@ubuntu:~# docker ps -a		#查看容器是否创建并启动
       CONTAINER ID        IMAGE                                          COMMAND             CREATED             STATUS              			PORTS               NAME
       6801d55682a3        registry.cn-beijing.aliyuncs.com/blxt/centos   "/sbin/init"        5 minutes ago       Up 5 	minutes                            nginx1
       
       相同方法创建第二个容器:
       root@ubuntu:~# docker run -itd --name nginx2 --privileged  registry.cn-beijing.aliyuncs.com/blxt/centos /sbin/init
       root@ubuntu:~# docker ps -a	
       root@ubuntu:~# 
       root@ubuntu:~# docker ps -a
       CONTAINER ID        IMAGE                                          COMMAND             CREATED             STATUS              			PORTS               NAMES
       1d31d3fc0ec1        registry.cn-beijing.aliyuncs.com/blxt/centos   "/sbin/init"        4 minutes ago       Up 4 	minutes                            nginx2
       6801d55682a3        registry.cn-beijing.aliyuncs.com/blxt/centos   "/sbin/init"        5 minutes ago       Up 5 	minutes                            nginx1
      
      
       连接到容器:
       root@ubuntu:~# docker exec -it nginx1 /bin/bash
       root@ubuntu:~# docker exec -it nginx2 /bin/bash 	
       [root@6801d55682a3 /]# hostname nginx1			#更改主机名称
       [root@6801d55682a3 /]# bash			#使更改的名称生效
       [root@1d31d3fc0ec1 /]# hostname nginx2
       [root@1d31d3fc0ec1 /]# bash
      
    • 安装nginx

       更新软件包:
       [root@nginx1 /]# yum clean all		#清空缓存
       [root@nginx1 /]# yum makecache		#更新软件包
      
       安装nginx:
       [root@nginx1 /]# yum -y install nginx
       [root@nginx1 /]# rpm -qa | grep nginx		#查看是否已经安装
      
       启动nginx:
       [root@nginx1 /]# systemctl start nginx
       [root@nginx1 /]# netstat -anpl | grep nginx		#查看nginxnn端口是否开启
       [root@nginx1 /]# lsof -i:80		#查看80端口是否开启
       安装第二个nginx使用相同方法即可!此处省略!!!
      
       访问nginx:
       [root@nginx1 /]# curl 127.0.0.1
      
    • 修改网页内容

       查看网页存在目录:
       [root@nginx1 nginx]# more nginx.conf		#查看配置文件
       在配置文件中找到下面root行,后面目录即网页文件存放目录
       root         /usr/share/nginx/html;
       [root@nginx1 html]# cd /usr/share/nginx/html/
       [root@nginx1 html]# ls		#列出目录内容
       404.html  50x.html  index.html	nginx-logo.png	poweredby.png
       [root@nginx1 html]# echo nginx1 > index.html		#清空nginx主页文件内容,并重新写入内容为nginx1
       nginx2如同,注意不要设置一样的主页内容
      
       访问nginx:
       [root@nginx1 html]# curl  127.0.0.1
       nginx1
       [root@nginx2 ~]# curl 127.0.0.1
       nginx2
      
    • 修改配置文件

       nginx1修改内容如下:
        server {
       listen       80;
       server_name  www.nginx1.com;			//设置域名
      
       #charset koi8-r;
      
       #access_log  logs/host.access.log  main;
      
       location / {
           root   html;
           index  index.html index.htm;
       	}
       }
       
       nginx2修改内容如下:
        server {
       listen       80;
       server_name  www.nginx2.com;			//设置域名
      
       #charset koi8-r;
      
       #access_log  logs/host.access.log  main;
      
       location / {
           root   html;
           index  index.html index.htm;
       	}
       }
      

    配置http负载均衡器

    要开始使用NGINX Plus或NGINX开源对一组服务器的HTTP流量进行负载均衡,使用upstream指令定义该组,该指令放置在http上下文中,proxy_pass指令用来转发请求到后端一般指定在loction上下文中
    基本配置方法:

    http {
        upstream nginx {			#test为组名
            server www.nginx1.com ;		#server用来指定后端服务器的访问地址,一般指定域名、ip、端口,域名和ip二选一,端口可忽略
            server www.nginx2.com weight=5;		#weight指定权重值
            server www.nginx3.com down;			#down用来停止对此服务器的转发
            ......省略其他配置
        }
        server{
    		location / {
    			proxy_pass http://nginx;		#http://nginx为转发那个组的后端服务器
    			......省略其他配置
    		}
    		......省略其他配置
    	}
    }
    

    nginx做负载均衡官方提供了4种方法,下面逐一介绍这四种方法:

    • Round Robin

    请求在服务器之间平均分配,同时考虑了服务器权重。默认情况下使用此方法(没有启用它的指令)
    默认配置就是Round Robin,配置方法:

    http {
       upstream nginx {			#test为组名
           server www.nginx1.com ;		#server用来指定后端服务器的访问地址
           server www.nginx2.com ;		#weight指定权重值
           ......省略其他配置
       }
       server{
       	location / {
       		proxy_pass http://nginx;		#http://nginx为转发那个组的后端服务器
       		......省略其他配置
       	}
       	......省略其他配置
       }
    }
    
    • 最少连接

    将活动连接最少的请求发送到服务器,也是在考虑到服务器的权重问题才设置的这种方法
    配置方法:

    http {
       upstream nginx {			#test为组名
       	   less_conn ; 
           server www.nginx1.com ;		#server用来指定后端服务器的访问地址
           server www.nginx2.com ;		#weight指定权重值
           ......省略其他配置
       }
       server{
       	location / {
       		proxy_pass http://nginx;		#http:#nginx为转发那个组的后端服务器
       		......省略其他配置
       	}
       	......省略其他配置
       }
    }
    
    • ip_hash

    从客户端IP地址确定向其发送请求的服务器。在这种情况下,可以使用IPv4地址的前三个八位位组或整个IPv6地址来计算哈希值。该方法保证了来自同一地址的请求将到达同一服务器,除非它不可用。
    配置方法:

    http {
       upstream nginx {			#test为组名
       	   ip_hash ; 
           server www.nginx1.com ;		#server用来指定后端服务器的访问地址
           server www.nginx2.com ;		#weight指定权重值
           ......省略其他配置
       }
       server{
       	location / {
       		proxy_pass http://nginx;		#http:#nginx为转发那个组的后端服务器
       		......省略其他配置
       	}
       	......省略其他配置
       }
    }
    
    • hash

    向其发送请求的服务器是根据用户定义的键确定的,该键可以是文本字符串,变量或组合。
    配置方法:

    http {
      upstream nginx {			#test为组名
      	  hash $request_uri consistent;
          server www.nginx1.com ;		#server用来指定后端服务器的访问地址
          server www.nginx2.com ;		#weight指定权重值
          ......省略其他配置
      }
      server{
      	location / {
      		proxy_pass http://nginx;		#http://nginx为转发那个组的后端服务器
      		......省略其他配置
      	}
      	......省略其他配置
      }
    }
  • 相关阅读:
    UVA 11488 Hyper Prefix Sets (字典树)
    UVALive 3295 Counting Triangles
    POJ 2752 Seek the Name, Seek the Fame (KMP)
    UVA 11584 Partitioning by Palindromes (字符串区间dp)
    UVA 11100 The Trip, 2007 (贪心)
    JXNU暑期选拔赛
    计蒜客---N的-2进制表示
    计蒜客---线段的总长
    计蒜客---最大质因数
    JustOj 2009: P1016 (dp)
  • 原文地址:https://www.cnblogs.com/blxt/p/12047531.html
Copyright © 2011-2022 走看看