keepalived实现nginx负载均衡高可用_咖啡调调。
keepalived实现nginx负载均衡高可用...
keepalived实现nginx负载均衡高可用
文章目录
keepalived实现nginx负载均衡高可用@[toc]一、什么是keepalived二、keepalived如何实现故障切换三、keepalived重要功能四、keepalived实现nginx负载均衡高可用部署1、keepalived安装2、在主备机上分别安装nginx3、keepalived配置4、查看VIP在哪里5、让keepalived监控nginx负载均衡机6、配置keepalived加入监控脚本的配置7、检验
一、什么是keepalived
keepalived是云计算平台工作管理中维持云计算平台高用于的一种服务于软文,其功用内似于heartbeat,用作放置单点发动机故障。二、keepalived如何实现故障切换
keepalived 保障事业时,主master接点会持续不断赶来紧急接点上传心跳数据,问他backup接点自个儿还好好活着。当主接点發生故障问题时,就未能上传心跳了,于似乎会调节个人的打压源程序,打压主接点的ip信息和保障。三、keepalived重要功能
keepalived 有两个极为重要的功能表,各自是: 管理LVS负载均衡软件实现LVS集群节点的健康检查作为系统网络服务的高可用性(failover)四、keepalived实现nginx负载均衡高可用部署
环境说明:
主机名 | IP地址 | 系统 |
---|---|---|
master | 192.168.183.135 | centos8 |
slave | 192.168.183.136 | centos8 |
1、keepalived安装
选配主keepalived//修改主机名
[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# bash
//关闭防火墙和selinux,确保selinux状态为disabled
[root@master ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@master ~]# setenforce 0
[root@master ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@master ~]# getenforce
Permissive
[root@master ~]# reboot
[root@master ~]# getenforce
Disabled
//配置yum源
[root@master ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo //mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@master ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@master ~]# yum -y install epel-release vim wget gcc gcc-c++
//安装keepalived
[root@master ~]# yum -y install keepalived
用同一的形式在备保障器上连接keepalived
//修改主机名
[root@localhost ~]# hostnamectl set-hostname slave
[root@localhost ~]# bash
//关闭防火墙和selinux,确保selinux状态为disabled
[root@slave ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@slave ~]# setenforce 0
[root@slave ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@slave ~]# reboot
[root@slave ~]# getenforce
Disabled
//配置yum源
[root@slave ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo //mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@slave ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@slave ~]# yum -y install epel-release vim wget gcc gcc-c++
//安装keepalived
[root@slave ~]# yum -y install keepalived
2、在主备机上分别安装nginx
在master上安装使用nginx[root@master ~]# yum -y install nginx
[root@master ~]# cd /usr/share/nginx/html/
[root@master html]# ls
404.html 50x.html index.html nginx-logo.png poweredby.png
[root@master html]# echo 'master' > index.html
[root@master html]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@master html]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[root@master html]# curl 192.168.183.135
master
在slave上连接nginx
[root@slave ~]# yum -y install nginx
[root@slave ~]# cd /usr/share/nginx/html/
[root@slave html]# ls
404.html 50x.html index.html nginx-logo.png poweredby.png
[root@slave html]# echo 'slave' > index.html
[root@slave html]# systemctl start nginx
//注意备服务器的nginx服务不要加入开机自启
在浏览器上访问试试,确保master上的nginx服务能够正常访问
3、keepalived配置
设备主keepalived[root@master ~]# cd /etc/keepalived/
[root@master keepalived]# mv keepalived.conf{,-bak}
[root@master keepalived]# ls
keepalived.conf-bak
[root@master keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.183.250
}
}
virtual_server 192.168.183.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.183.135 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.183.136 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@master ~]# systemctl enable --now keepalived
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
设备备keepalived
[root@slave ~]# cd /etc/keepalived/
[root@slave keepalived]# mv keepalived.conf{,-bak}
[root@slave keepalived]# ls
keepalived.conf-bak
[root@slave keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02 //修改路由id
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90 //修改优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.183.250
}
}
virtual_server 192.168.183.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.183.135 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.183.136 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@slave ~]# systemctl enable --now keepalived
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
4、查看VIP在哪里
在master上察看[root@master ~]# 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:7f:37:b0 brd ff:ff:ff:ff:ff:ff
inet 192.168.183.135/24 brd 192.168.183.255 scope global dynamic noprefixroute ens33
valid_lft 1737sec preferred_lft 1737sec
inet 192.168.183.250/32 scope global ens33 //可以看到此处有VIP
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe7f:37b0/64 scope link noprefixroute
valid_lft forever preferred_lft forever
在slave上查看手机
[root@slave ~]# 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:07:de:9b brd ff:ff:ff:ff:ff:ff
inet 192.168.183.136/24 brd 192.168.183.255 scope global dynamic noprefixroute ens33
valid_lft 1779sec preferred_lft 1779sec
inet6 fe80::20c:29ff:fe07:de9b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
5、让keepalived监控nginx负载均衡机
keepalived完成js来管控nginx装载动态平衡机的感觉 在master上编写软件按键小精灵[root@master ~]# mkdir /scripts
[root@master ~]# cd /scripts/
[root@master scripts]# vim check_n.sh
#!/bin/bash
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -lt 1 ];then
systemctl stop keepalived
fi
[root@master scripts]# chmod +x check_n.sh
[root@master scripts]# ll
total 4
-rwxr-xr-x 1 root root 142 Oct 8 20:00 check_n.sh
[root@master scripts]# vim notify.sh
#!/bin/bash
VIP=$2
case "$1" in
master)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -lt 1 ];then
systemctl start nginx
fi
;;
backup)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -gt 0 ];then
systemctl stop nginx
fi
;;
*)
echo "Usage:$0 master|backup VIP"
;;
esac
[root@master scripts]# chmod +x notify.sh
[root@master scripts]# ll
total 8
-rwxr-xr-x 1 root root 142 Oct 8 20:00 check_n.sh
-rwxr-xr-x 1 root root 432 Oct 8 20:06 notify.sh
在slave上调用按键小精灵
[root@slave ~]# mkdir /scripts
[root@slave ~]# cd /scripts/
[root@slave scripts]# vim notify.sh
#!/bin/bash
VIP=$2
case "$1" in
master)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -lt 1 ];then
systemctl start nginx
fi
;;
backup)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -gt 0 ];then
systemctl stop nginx
fi
;;
*)
echo "Usage:$0 master|backup VIP"
;;
esac
[root@slave scripts]# chmod +x notify.sh
[root@slave scripts]# ll
total 8
-rwxr-xr-x 1 root root 142 Oct 8 20:10 check_n.sh
-rwxr-xr-x 1 root root 432 Oct 8 20:08 notify.sh
6、配置keepalived加入监控脚本的配置
配值主keepalived[root@master ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_script nginx_check {
script "/scripts/check_n.sh"
interval 1
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.183.250
}
track_script {
nginx_check
}
notify_master "/scripts/notify.sh master 192.168.183.250"
notify_backup "/scripts/notify.sh backup 192.168.183.250"
}
virtual_server 192.168.183.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.183.135 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.183.136 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@master ~]# systemctl restart keepalived
配制备keepalived
slave无需检测nginx是否正常,当升级为MASTER时启动nginx,当降级为SLAVE时关闭
[root@slave ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.183.250
}
notify_master "/scripts/notify.sh master 192.168.183.250"
notify_backup "/scripts/notify.sh backup 192.168.183.250"
}
virtual_server 192.168.183.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.183.135 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.183.136 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@slave ~]# systemctl restart keepalived
7、检验
在master上停掉nginx保障培训,keepalived保障培训也会停掉,VIP会转到slave上,slave上的nginx保障培训默许是不会有启用的,当master停掉时候,slave上的nginx和keepalived都开来了 在master上止住nginx提供服务[root@master ~]# systemctl stop nginx
[root@master ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor >
Active: inactive (dead) since Sat 2022-10-08 20:41:31 CST; 3min 49s ago
Process: 251802 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exit>
Main PID: 251804 (code=exited, status=0/SUCCESS)
在slave上看是不是也又VIP,nginx和keepalived是不是也开启
[root@slave ~]# 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:07:de:9b brd ff:ff:ff:ff:ff:ff
inet 192.168.183.136/24 brd 192.168.183.255 scope global dynamic noprefixroute ens33
valid_lft 972sec preferred_lft 972sec
inet 192.168.183.250/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe07:de9b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@slave ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 [::]:80 [::]:*
[root@slave ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor pres>
Active: active (running) since Sat 2022-10-08 20:41:28 CST; 4min 33s ago
Process: 235224 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 235221 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCES>
Process: 235218 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, stat>
Main PID: 235225 (nginx)
Tasks: 5 (limit: 12199)
Memory: 7.8M
[root@slave ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor >
Active: active (running) since Sat 2022-10-08 20:41:17 CST; 5min ago
Process: 234743 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exit>
Main PID: 234744 (keepalived)
Tasks: 3 (limit: 12199)
Memory: 2.2M
皇冠新体育APP相关的文章
- AI&BlockChain:“知名博主独家讲授”人工智能创新应用竞赛【精选实战作品】之《基于计算机视觉、自然语言处理、区块链和爬虫技术的智能会议系统》软件系统案例的界面简介、功能介绍分享之二、
- python写的2048游戏,源代码,pygame_weixin_65922074
- uniapp 地图组件(map)的使用总结_海海呐_uniapp地图
- 【用vue开发微信小程序】(uni-app)_Kevin_卓_小程序开发使用vue
- Verilog中case,casez,casex语句的用法_孤独的单刀_casez和casex
- 皇冠新体育APP:微信小程序的反编译_一指流沙q_微信小程序最新反编译
- 皇冠新体育APP:自动化测试??unittest框架_鸢也_unittest框架
- 皇冠新体育APP:FPGA基础知识----FPGA 简介_原来如此呀_常用fpga
- logback-spring.xml配置文件标签(超详解)_皮卡丘的情绪
- 皇冠新体育APP:基于微信小程序+SSM+Vue+Node实现智慧旅游商城系统_编程千纸鹤
- IC学习笔记9??多比特信号的跨时钟域处理方法之“MUX/DMUX同步器”_海纳百川13_多bit信号跨时钟域处理
- 数字IC笔试题2_BarFin_无复位寄存器ppa更优
- Linux系统网卡配置详细教程!_小东西的东西
- 皇冠新体育APP:【小程序】常见系统API | 页面分享 | 位置信息 | 本地存储_林有酒
- 皇冠新体育APP:微信小程序的页面布局(1)_tundra38_微信小程序 页面布局
- 同步FIFO的两种Verilog设计方法(计数器法、高位扩展法)_孤独的单刀_fifo计数