## node01 $ curl 192.168.198.131:8080/test ... this is : node01/192.168.198.131 ## node02 $ curl 192.168.198.130:8080/test ... this is : node02/192.168.198.130 ## node03 $ curl 192.168.198.132:8080/test ... this is : node03/192.168.198.132
Nginx负载均衡
Round Robin(轮询)
请求在服务器之间均匀分布,可以设置服务器权重。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
$ vim /etc/nginx/conf/demo.conf upstream backend { server 192.168.198.131:8080; server 192.168.198.132:8080; server 192.168.198.130:8080; }
server { listen 80; server_name 192.168.198.131; location / { proxy_pass http://backend; } } $ service nginx restart
测试下
1 2 3 4 5 6 7 8 9 10 11 12
$ curl 192.168.198.131/test ... this is : node01/192.168.198.131 # node01 $ curl 192.168.198.131/test ... this is : node03/192.168.198.132 # node03 $ curl 192.168.198.131/test ... this is : node03/192.168.198.130 # node02 $ curl 192.168.198.131/test ... this is : node01/192.168.198.131 # node01
$ vim /etc/nginx/conf/demo.conf upstream backend { least_conn; server 192.168.198.131:8080; server 192.168.198.132:8080; server 192.168.198.130:8080; } $ service nginx restart
$ vim /etc/nginx/conf/demo.conf upstream backend { ip_hash; server 192.168.198.131:8080; server 192.168.198.132:8080; server 192.168.198.130:8080; } $ service nginx restart
测试下
1 2 3 4 5 6 7 8 9
$ curl 192.168.198.131/test ... this is : node01/192.168.198.131 $ curl 192.168.198.131/test ... this is : node01/192.168.198.131 $ curl 192.168.198.131/test ... this is : node01/192.168.198.131
$ curl 192.168.198.131/test ... this is : node03/192.168.198.132 $ curl 192.168.198.131/test ... this is : node03/192.168.198.132 $ curl 192.168.198.131/test ... this is : node03/192.168.198.132
$ vim /etc/nginx/conf/demo.conf upstream backend { hash$remote_addr consistent; server 192.168.198.131:8080; server 192.168.198.132:8080; server 192.168.198.130:8080; } $ service nginx restart
测试下
1 2 3 4 5 6 7 8 9
$ curl 192.168.198.131/test ... this is : node02/192.168.198.130 $ curl 192.168.198.131/test ... this is : node02/192.168.198.130 $ curl 192.168.198.131/test ... this is : node02/192.168.198.130
$ vim /etc/nginx/conf/demo.conf upstream backend { random; server 192.168.198.131:8080; server 192.168.198.132:8080; server 192.168.198.130:8080; } $ service nginx restart
测试下
1 2 3 4 5 6 7 8 9 10 11 12
$ curl 192.168.198.131/test ... this is : node03/192.168.198.132 $ curl 192.168.198.131/test ... this is : node01/192.168.198.131 $ curl 192.168.198.131/test ... this is : node02/192.168.198.130 $ curl 192.168.198.131/test ... this is : node01/192.168.198.130
可以看到,请求是被随机分配到三台服务器的。
Weights
除了设置负载均衡算法,我们还可以为服务器设置权重,权重默认值是1
1 2 3 4 5 6 7
$ vim /etc/nginx/conf/demo.conf upstream backend { server 192.168.198.131:8080 weight=5; server 192.168.198.132:8080 weight=10; server 192.168.198.130:8080; } $ service nginx restart
测试下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
$ curl 192.168.198.131/test ... this is : node03/192.168.198.132 $ curl 192.168.198.131/test ... this is : node01/192.168.198.131 $ curl 192.168.198.131/test ... this is : node03/192.168.198.132 $ curl 192.168.198.131/test ... this is : node03/192.168.198.132 $ curl 192.168.198.131/test ... this is : node01/192.168.198.131