软件负载均衡

软件负载均衡成本几乎为零,基本都是开源软件。例如:LVS、HAProxy、Nginx等。

该机群包含一台Nginx服务器,两台Web服务器(node2和node3)

软件负载均衡之--httpd

修改nginx.conf文件

upstream rss{
	server 192.168.48.102;
	server 192.168.48.103;
}
  server {
  	listen	80;
  	server_name	www.123.com;
  	access_log	logs/main.log main;
  	location / {
  		root /mnt;
  		autoindex on;
  	}
  	location /toms {
  		proxy_pass http://rss/;
  	}
  }
[root@nginx conf] systemctl restart nginx.service  # 重启nginx服务

在node2和node3上:

安装httpd应用:yum install httpd -y

进入/var/www/html 各创建一个index.html

分别写入from 192.168.48.102 ..以及from 192.168.48.103 方便测试的时候区分。

启动httpd服务: systemctl start httpd.service

请求测试:http://www.123.com/toms,发现已经实现了负载均衡。(中小企业一般使用该方式)

软件负载均衡之--通过在Nginx服务器上配置hosts本地域名解析:
  1. node2和node3上:

    安装httpd应用:yum install httpd -y

    进入/var/www/html 各创建一个index.html

    分别写入from 192.168.48.102 ..以及from 192.168.48.103 方便测试的时候区分。

    启动httpd服务: systemctl start httpd.service

  2. 配置nginx服务器端 /etc/hosts文件

[root@nginx conf]# vim /etc/hosts
192.168.48.101 node1
192.168.48.102 node2  xxx   # xxx静态映射
192.168.48.103 node3  xxx   # xxx静态映射
192.168.48.104 node4
  1. 配置nginx.conf文件中的location
[root@nginx conf]# pwd
/opt/nginx/conf
[root@nginx conf]#vim nginx.conf
location /cats {  #软件负载均衡之--测试通过在Nginx服务器上配置hosts本地域名解析
            proxy_pass http://xxx/;
        }
[root@nginx conf] systemctl restart nginx.service  # 重启nginx服务
  1. 测试:http://www.123.com/cats

一样能够实现负载均衡的目的(大型企业使用该方式,一般还会搭建一个DNS域名解析服务器,只需要修改DNS域名解析服务器就ok了)

Nginx会用到域名解析,如果一个域名解析出多个ip地址,会在这些ip地址之间做负载均衡