发表于: 2018-03-28 23:01:07
1 548
今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin)
参考(可能需要科学上网)
Nginx官方文档
我用的Ubuntu 16.04
Ubuntu:Version Codename Supported Platforms14.04 trusty x86_64, i386, aarch64/arm6416.04 xenial x86_64, i386, ppc64el, aarch64/arm6417.04 zesty x86_64, i386
- 1
- 2
- 3
- 4
- 5
记住对应的Codename Xenial
For Debian/Ubuntu, in order to authenticate the nginx repository signature and to eliminate warnings about missing PGP key during installation of the nginx package, it is necessary to add the key used to sign the nginx packages and repository to the apt program keyring. Please download this key from our web site, and add it to the apt program keyring with the following command:
对于Debian/Ubuntu,为了在安装nginx包验证nginx存储库签名以及消除与丢失PGP密钥有关的警告,有必要向apt程序密钥串中添加用以签名nginx包的密钥。
请从我们的网站下载该密钥,并用以下的命令把它添加到apt程序密钥串:
sudo apt-key add nginx_signing.key
- 1
For Ubuntu replace codename with Ubuntu distribution codename, and append the following to the end of the /etc/apt/sources.list file:
对Ubuntu系统,用Ubuntu配给的codename取代下列命令中的codename字段,并附在/etc/apt/source.list文件最后:
deb http://nginx.org/packages/ubuntu/ codename nginx
deb-src http://nginx.org/packages/ubuntu/ codename nginx
- 1
- 2
For Debian/Ubuntu then run the following commands:
最后运行下面的命令完成安装:
apt-get update
apt-get install nginx
然后了解了下Nginx负载均衡如何实现,引用https://www.nginx.cn/doc/
文档中有负载均衡的实例
http {
: upstream myproject {
: server 127.0.0.1:8000 weight=3;
: server 127.0.0.1:8001;
: server 127.0.0.1:8002;
: server 127.0.0.1:8003;
: }
: server {
: listen 80;
: server_name www.domain.com;
: location / {
: proxy_pass http://myproject;
: }
: }
}
把www.domain.com均衡到不同端口,也可以改为不同地址。
至于轮询算法和加权轮询
每个后端peer都有三个权重变量,先解释下它们的含义。
(1) weight
配置文件中指定的该后端的权重,这个值是固定不变的。
(2) effective_weight
后端的有效权重,初始值为weight。
在释放后端时,如果发现和后端的通信过程中发生了错误,就减小effective_weight。
此后有新的请求过来时,在选取后端的过程中,再逐步增加effective_weight,最终又恢复到weight。
之所以增加这个字段,是为了当后端发生错误时,降低其权重。
(3) current_weight
后端目前的权重,一开始为0,之后会动态调整。那么是怎么个动态调整呢?
每次选取后端时,会遍历集群中所有后端,对于每个后端,让它的current_weight增加它的effective_weight,
同时累加所有后端的effective_weight,保存为total。
如果该后端的current_weight是最大的,就选定这个后端,然后把它的current_weight减去total。
如果该后端没有被选定,那么current_weight不用减小。
弄清了三个weight字段的含义后,加权轮询算法可描述为:
1. 对于每个请求,遍历集群中的所有可用后端,对于每个后端peer执行:
peer->current_weight += peer->effecitve_weight。
同时累加所有peer的effective_weight,保存为total。
2. 从集群中选出current_weight最大的peer,作为本次选定的后端。
3. 对于本次选定的后端,执行:peer->current_weight -= total。
上述描述可能不太直观,来看个例子。
现在使用以下的upstream配置块:
upstream backend {
server a weight=4;
server b weight=2;
server c weight=1;
}
按照这个配置,每7个客户端请求中,a会被选中4次、b会被选中2次、c会被选中1次,且分布平滑。
我们来算算看是不是这样子的。
initial current_weight of a, b, c is { 0, 0, 0 }
通过上述过程,可得以下结论:
1. 7个请求中,a、b、c分别被选取了4、2、1次,符合它们的权重值。
2. 7个请求中,a、b、c被选取的顺序为a, b, a, c, a, b, a,分布均匀,权重大的后端a没有被连续选取。
3. 每经过7个请求后,a、b、c的current_weight又回到初始值{ 0, 0, 0 },因此上述流程是不断循环的。
这个平滑的加权轮询算法背后应该有数学论证,这里就不继续研究了
明天计划的事情:(一定要写非常细致的内容)
nginx负载均衡实现两个容器的均衡
遇到的问题:(遇到什么困难,怎么解决的)
轮询算法有点不懂
收获:(通过今天的学习,学到了什么知识)
nginx的基本知识
评论