nginx负载均衡案例和高可用keepalived

一、案例1:根据用户的客户端转发请求

image

1.1 环境准备

服务器名称 内网IP 外网IP
lb01 172.16.1.5 10.0.0.5 负载均衡
web01 172.16.1.7 10.0.0.7 存放PC端的页面
web02 172.16.1.8 10.0.0.8 存放移动端的页面

1.2 创建环境

\\web01
echo this is PC website>/app/www/lidao.html

\\web02
echo this is Mobile website>/app/www/lidao.html

1.3 lb01命令行测试

[root@lb01 ~]# curl 10.0.0.[7-8]/lidao.html

[1/2]: 10.0.0.7/lidao.html --> 
--_curl_--10.0.0.7/lidao.html
this is PC website

[2/2]: 10.0.0.8/lidao.html --> 
--_curl_--10.0.0.8/lidao.html
this is Mobile website
[root@lb01 ~]# 

1.4 在lb01判断客户端类型,

[root@lb01 /etc/nginx]# cat nginx.conf
……
    upstream default_pools{
        server 10.0.0.7:80 weight=1 max_fails=1 fail_timeout=10s;
    }
    upstream mobile_pools{
        server 10.0.0.8:80 weight=1 max_fails=1 fail_timeout=10s;
    }
    server{ 
        listen 80;
        server_name www.oldboy.com;
        location / {
            if ($http_user_agent ~* "Android|IOS") { 
                proxy_pass http://mobile_pools;
            }
        proxy_pass http://default_pools;
        proxy_set_header Host $host;
        proxy_set_header X_Forwarded-For $remote_addr;
        }
    }
……
[root@lb01 /etc/nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 /etc/nginx]# systemctl reload nginx

1>在命令行进行测试

[root@lb01 /etc/nginx]# curl 10.0.0.5/lidao.html
this is PC website
[root@lb01 /etc/nginx]# curl -A ios 10.0.0.5/lidao.html
this is Mobile website
[root@lb01 /etc/nginx]# 

2>在火狐浏览器进行
首先,下载安装firefox,安装 user agent switch 插件

image

其次,打开火狐浏览器进行测试

image

二、案例2:动静态分离

根据用户的uri进行转发location

image
image

2.1 环境准备

服务器名称 内网IP 外网IP
lb01 172.16.1.5 10.0.0.5 负载均衡
web01 172.16.1.7 10.0.0.7 模拟存放上传/upload
web02 172.16.1.8 10.0.0.8 模拟存放静态网页static
web03 172.16.1.9 10.0.0.9 模拟存放动态网页(默认)

2.2 创建环境

\\\\web01 
[root@web01 ~]# mkdir -p  /app/www/upload/
[root@web01 ~]# echo  this is upload  >/app/www/upload/guoav.html
[root@web01 ~]# 

\\\\web02  
[root@web02 ~]# mkdir -p  /app/www/static/
[root@web02 ~]# echo  this is static  >/app/www/static/guoav.html 
[root@web02 ~]# 

\\\\web03  
[root@web03 ~]# mkdir -p  /app/www/
[root@web03 ~]# echo  this is default  >/app/www/guoav.html 
[root@web03 ~]#

检查模拟环境是否ok

[root@web01 ~]# curl 10.0.0.7/upload/guoav.html
this is upload
[root@web01 ~]# curl 10.0.0.8/static/guoav.html
this is static
[root@web01 ~]# curl 10.0.0.9/guoav.html
this is default
[root@web01 ~]# 

2.3 配置lb01

[root@lb01 /etc/nginx]# cat nginx.conf
…… 
    upstream upload{
        server 10.0.0.7:80 weight=1 max_fails=1 fail_timeout=10s;
    }
    upstream static{
        server 10.0.0.8:80 weight=1 max_fails=1 fail_timeout=10s;
    }
    upstream default{
        server 10.0.0.9:80 weight=1 max_fails=1 fail_timeout=10s;
    }
    server{ 
        listen 80;
        server_name www.oldboy.com;
        location /upload {
            proxy_pass http://upload;
        proxy_set_header Host $host;
        proxy_set_header X_Forwarded-For $remote_addr;
        }
        location /static {
            proxy_pass http://static;
        proxy_set_header Host $host;
        proxy_set_header X_Forwarded-For $remote_addr;
        }
        location / {
            proxy_pass http://default;
        proxy_set_header Host $host;
        proxy_set_header X_Forwarded-For $remote_addr;
        }
    }
……
[root@lb01 /etc/nginx]# 
[root@lb01 /etc/nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 /etc/nginx]# systemctl reload nginx 

2.4 浏览器测试

image

根据用户请求文件类型进行转发

三、负载均衡的排查思路

四、负载均衡的轮询算法

默认轮询(rr)
加权轮询(wrr):weight
最小连接数(least conn):根据后端服务器连接数分配任务
ip_hash:只要客户端ip地址相同就会被转发到同一台机器上※※

image

五、会话保持

cookie和session

1>cookie和session共同点:

存放用户修改
key value类型,变量和变量内容

2>cookie和session区别:

cookie:
存放在浏览器里面
存放简单的信息或存放钥匙
开发设置的
相应的时候服务器给你设置

session:
存放在服务器中--redis中
存放敏感信息
锁头

image

六、高可用keepalived

image
image

Keepalived服务的工作原理※※※

1> Keepalived高可用对之间是通过VRRP进行通信的,VRRP是通过竞选机制来确定主备的,主的优先高于备,因此,工作时主会优先获得所有的资源,备节点处于等待状态,当主挂了的时候,备节点就会接管主节点的资源,然后顶替主节点对外提供服务。
2> 在Keepalived服务队之间,只有作为主的服务器会一直发送VRRP广播包,告诉备他还活着,此时备不会抢占主,当主不可用时,即备监听不到主发送的广播包时,就回启动相关服务接管资源,保证业务的连续性。
3> 接管速度最快可以小于1秒。

6.1 环境准备

服务器名称 内网IP 外网IP
lb01 172.16.1.5 10.0.0.5
lb02 172.16.1.6 10.0.0.6
web01 172.16.1.7 10.0.0.7
web02 172.16.1.8 10.0.0.8

1>每台机器安装好nginx
2>在lb01和lb02安装keepalived----yum install -y keepalived
3>启动服务,并设置开机自启动
  启动服务:systemctl start keepalived
  开机自启动:systemctl enable keepalived
4>检查服务:rpm -qa keepalived

6.2 配置文件

分为三个部分:
    GLOBAL CONFIGURATION:全局定义部分
    VRRPD CONFIGURATION:vrrp实例(rsync模块)
    LVS CONFIGURATION:通过keepalived配置文件控制lvs

6.3 keepalived配置文件详解

https://www.processon.com/view/link/5d034b4ee4b08ceab31b8e11

image

6.4 配置keepalived

对lb01(主)和lb02(备)进行以下配置
注:配置时请注意lbo1主负载均衡与lb02备负载均衡有些不同

[root@lb01 /etc/keepalived]# cp keepalived.conf{,.bak}
[root@lb01 /etc/keepalived]# ll
total 8
-rw-r--r-- 1 root root 3598 Jan  6 16:47 keepalived.conf
-rw-r--r-- 1 root root 3598 Jun 14 15:56 keepalived.conf.bak
[root@lb01 /etc/keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
    router_id lb01
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
     10.0.0.3/24 dev eth0 label eth0:1  
    }
}
[root@lb01 /etc/keepalived]# systemctl restart keepalived.service

image
image

6.5 命令行测试

image

6.7 抓包测试

image
image

你可能感兴趣的:(nginx负载均衡案例和高可用keepalived)