首页 > 系统相关 >nginx负载均衡高可用

nginx负载均衡高可用

时间:2022-10-18 10:11:40浏览次数:66  
标签:forever 负载 nginx 00 192.168 lft 均衡 root

目录


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

标签:forever,负载,nginx,00,192.168,lft,均衡,root
From: https://www.cnblogs.com/TQingS/p/16801662.html

相关文章

  • keepalived实现nginx负载均衡机高可用
    keepalived实现nginx负载均衡机高可用目录keepalived实现nginx负载均衡机高可用配置web界面配置nginx负载均衡配置keepalived高可用编写脚本配置keepalived加入监控脚本的......
  • 使用Etcd+Confd实现Nginx配置文件自动管理
    ​安装etcd#yum安装yuminstalletcd-y#更新https://github.com/coreos/etcd/releases替换掉etcd和etcdctlsed-i's/localhost/0.0.0.0/g'/etc/etcd/etcd.confsystemc......
  • Nginx安装与各类配置集合
    安装vi/etc/yum.repos.d/nginx.repo#Stableversion[nginx]name=nginxrepobaseurl=http://nginx.org/packages/centos/7/$basearch/gpgcheck=0enabled=1#Mainlineversion......
  • Nginx集成LDAP统一认证
    编译安装nginxgitclonehttps://github.com/kvspb/nginx-auth-ldap.gitwgethttp://nginx.org/download/nginx-1.18.0.tar.gzyum-yinstallopenldap-develpcre-develop......
  • LVS负载均衡+高可用集群解决方案 ipvsadm+heartbeat+ldirectord
    ​前言LVS的介绍可以查看本公众号的文章:LVS搭建集群的一些介绍(方案,优化)首先按照本公众号中的前两篇日志:LVSVS-TUNipvsadm+heartbeat-ldirectord搭建linux集群 LVSV......
  • 小破站最好的Nginx保姆级入门教程,全面掌握Nginx核心技术!
    尚硅谷2022版Nginx教程 https://www.bilibili.com/video/BV1Y341137Kq/?spm_id_from=333.337.search-card.all.click&vd_source=4a69745b599dffec877b0fcfe130b092https......
  • 采用Ingress暴露容器化应用(Nginx)
    1.部署容器化应用[root@k8smaster~]#kubectlcreatedeploymentnginx--image=nginxdeployment.apps/nginxcreated2.暴露该服务[root@k8smaster~]#kubectlexpose......
  • 四层负载均衡和七层负载均衡
    四层负载均衡指的是通过ip+端口进行转发;七层负载均衡指的是通过URL、浏览器类别、语言等应用层信息进行转发。七层负载均衡举个例子,如果你的Web服务器分成两组,一组是中......
  • lua_nginx_module 简单解读
       最近写的文章比较少一些,一方面是因为自己最近疏忽了,还有一部分原因是自己在阅读luanginxmodule源码,相关资料较少,自己又并无lua及c基础,需要临时学习的东西较......
  • mac系统下用nginx服务器部署页面
    1.安装nginx(需要先安装Homebrew)。使用命令brewinstallnginx安装nginx。 2.用命令open/usr/local/Cellar/nginx用访达打开安装路径。 3.打开bin文件夹,双击nginx文件,启动......