nginx负载均衡高可用


nginx负载均衡配置和nginx负载均衡调度器高可用配置

环境

主机名 IP地址 服务 系统
master 192.168.34.130 keepalived
nginx
centos8
backup 192.168.34.131 keepalived
nginx
centos8
rs1 192.168.34.135 http centos8
rs2 192.168.34.140 nginx centos8

rs1安装http

rs2安装nginx

添加nginx负载均衡配置

master端做负载均衡
//安装所需的包
[root@master ~]# yum -y install epel-release vim wget gcc gcc-c++ keepavlied
源码安装nginx
[root@master ~]# cd /usr/local/nginx/html/
[root@master html]# vim /usr/local/nginx/conf/nginx.conf
    upstream ly.com{					//添加此字段在server外在http内
        server 192.168.34.135;
        server 192.168.34.140;
    }
    server {
        location / {
            root   html;
            index  index.html index.htm;	//添加此行
	    proxy_pass http://ly.com;

slave端做负载均衡
[root@slave ~]# yum -y install epel-release vim wget gcc gcc-c++ keepavlied
源码安装nginx
[root@backup ~]# cd /usr/local/nginx/html/
[root@backup html]# vim /usr/local/nginx/conf/nginx.conf
   upstream ly.com{						//添加此字段在server外在http内
        server 192.168.34.135;
        server 192.168.34.140;
    }
    location / {
        root   html;
        index  index.html index.htm;
        proxy_pass http://ly.com;		//添加此行
        }
[root@backup html]# curl 192.168.34.131
nginx
[root@backup html]# curl 192.168.34.131
http
[root@backup html]# curl 192.168.34.131
nginx
[root@backup html]# curl 192.168.34.131
http

keepalived高可用

master端

[root@master ~l]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id ly1
}

vrrp_instance LY_1 {
    state MASTER
    interface ens160
    virtual_router_id 02
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass liuyang
    }
    virtual_ipaddress {
        192.168.34.248
    }
}
vrrp_script nginx_check {
    script "/scripts/check_n.sh"
    interval 1
    weight -20
}
    track_script {          	//脚本    
        nginx_check
    }
    notify_master "/scripts/notify.sh master 192.168.34.248"			//脚本
virtual_server 192.168.34.248 80{
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.34.130 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.34.131 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

[root@master scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:57:dd:0b brd ff:ff:ff:ff:ff:ff
    inet 192.168.34.130/24 brd 192.168.34.255 scope global dynamic noprefixroute ens160
       valid_lft 1156sec preferred_lft 1156sec
    inet 192.168.34.248/32 scope global ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::8c36:5fa5:1ee:4ec6/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
使用vip进行访问
[root@master ~]# curl 192.168.34.248
http
[root@master ~]# curl 192.168.34.248
nginx
[root@master ~]# curl 192.168.34.248
http
[root@master ~]# curl 192.168.34.248
nginx
编写脚本    上面的配置文件已经将脚本写入了
[root@master scripts]# cat check_n.sh 
#!/bin/bash
nginx_status=$(ps -ef |grep \bnginx\b |grep -v grep |wc -l)
if [ $nginx_status -lt 1 ];then
    systemctl stop keepalived
fi
[root@master scripts]# cat notify.sh 
#!/bin/bash
VIP=$2
case "$1" in
  master)
        nginx_status=$(ps -ef|grep -Ev grep | grep '\bnginx\b'|wc -l)
        if [ $nginx_status -lt 1 ];then
            systemctl start nginx
        fi
  ;;
  backup)
        nginx_status=$(ps -ef|grep -v grep|grep '\bnginx\b'|wc -l)
        if [ $nginx_status -gt 0 ];then
            systemctl stop nginx
将此脚本传到备主机,传输前现在备主机创建此目录
[root@master scripts]# scp /scripts/notify.sh 192.168.34.131:/scripts/

backup

[root@backup ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id ly2
}

vrrp_instance LY_1 {
    state BACKUP
    interface ens160
    virtual_router_id 02
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass liuyang
    }
    virtual_ipaddress {
        192.168.34.248				//设置的vip
    }
}

virtual_server 192.168.34.248 80{
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.34.130 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.34.131 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
notify_master "/scripts/notify.sh master 192.168.34.248"		//添加此行
[root@backup ~]# systemctl  enable --now keepalived
[root@backup ~]# mkdir /scripts						
[root@backup ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:9a:86:3b brd ff:ff:ff:ff:ff:ff
    inet 192.168.34.131/24 brd 192.168.34.255 scope global dynamic noprefixroute ens160
       valid_lft 1120sec preferred_lft 1120sec
    inet 192.168.34.248/32 scope global ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::dec5:1788:109a:67f8/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

测试

[root@master scripts]# systemctl  stop keepalived
[root@master scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:57:dd:0b brd ff:ff:ff:ff:ff:ff
    inet 192.168.34.130/24 brd 192.168.34.255 scope global dynamic noprefixroute ens160
       valid_lft 1104sec preferred_lft 1104sec
    inet6 fe80::8c36:5fa5:1ee:4ec6/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@master scripts]# systemctl  stop nginx


[root@backup ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:9a:86:3b brd ff:ff:ff:ff:ff:ff
    inet 192.168.34.131/24 brd 192.168.34.255 scope global dynamic noprefixroute ens160
       valid_lft 1120sec preferred_lft 1120sec
    inet 192.168.34.248/32 scope global ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::dec5:1788:109a:67f8/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@backup ~]# curl 192.168.34.248
http
[root@backup ~]# curl 192.168.34.248
nginx
[root@backup ~]# curl 192.168.34.248
http
[root@backup ~]# curl 192.168.34.248
nginx

posted @ 2022-10-18 10:04  Tqing  阅读(76)  评论(0编辑  收藏  举报