综合架构-负载均衡

一、了解负载均衡

  负载均衡建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。

二、配置负载均衡

  1,准备两台虚拟机,

    lb01     IP 10.0.0.7

    web01   IP:10.0.0.5  

    web02  IP: 10.0.0.6  

  2,安装相应的服务

    均安装nginx /1.14.2

    设置本地yum源,之后yum安装即可 yum install -y nginx

[root@web01 yum.repos.d]# pwd
/etc/yum.repos.d
[root@web01 yum.repos.d]# cat nginx.repo 
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

三、配置nginx.conf 文件

  web01端配置

    

[root@web01 conf.d]# pwd
/etc/nginx/conf.d
[root@web01 conf.d]# cat 01-www.conf 
server {
   listen   80;
   server_name  www.oldgirl.com;
   root /html/www/;
   location / {
      index index.html;
    }
}
[root@web01 conf.d]# cat /html/www/index.html 
www web01

   web02端配置与01一直,为效果web02的index 内容与01不一致

[root@web02 conf.d]# pwd
/etc/nginx/conf.d
[root@web02 conf.d]# cat 
01-www.conf   02-blog.conf  bak/          
[root@web02 conf.d]# cat 01-www.conf 
server {
   listen   80;
   server_name  www.oldgirl.com;
   root /html/www/;
   location / {
      index index.html;
    }
}
[root@web02 conf.d]# cat /html/www/index.html 
www web02

  lb01端配置

[root@lb01 ~]# cat /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

upstream www {
    server 10.0.0.7;
    server 10.0.0.8;
}

server {
    listen 80;
    server_name www.oldgirl.com;
    location / {
        proxy_pass http://www;
        proxy_set_header Host  $host;
                proxy_set_header X-Forwarded-For $remote_addr;
        }
    }
}

重点部分已标红,upstream www 定义一个池塘www为池塘名 内写web主机地址

location 中配置,proxy_pass 为访问默认loadhost时跳转的内容 http:// 接池塘名 当访问lb01ip是会跳转到池塘内的ip中

四、效果图

[root@lb01 ~]# curl 10.0.0.5
www web02
[root@lb01 ~]# curl 10.0.0.5
www web01
[root@lb01 ~]# curl 10.0.0.5
www web02
[root@lb01 ~]# curl 10.0.0.5
www web01
[root@lb01 ~]# 

访问lb01本身会自动跳转到web01与web02

五、常见错误

[root@lb01 scripts]# curl 10.0.0.5
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.14.2</center>
</body>
</html>

由于防火墙开启关闭即可

systemctl stop firewalld.service 

  

  

猜你喜欢

转载自www.cnblogs.com/qiang-qiang/p/10559772.html