负载均衡与高可用综合实验

一、集群是什么?

是有一组独立的计算机系统构成的一个松耦合的多处理系统,作为一个整体向用户提供一组网络资源,这些单个的计算机就是集群的节点。

二、集群类型

Load Balance cluster(负载均衡集群)

把负载压力根据某种算法合理分配到集群中每一台计算机上,减轻主服务器压力,降低对主服务器的软件硬件要求。

High Availability cluster(高可用集群)

当主服务器故障时,备份服务器能够自动接管主服务器的工作,并且及时切换过去。

HIgh Performance Computing clustering(高性能计算集群)

充分利用每一台计算机的资源,实现复杂运算的并行处理。

负载均衡集群

LB集群的主要功能就是解决如何在RS前添加一台主机作为调度器,从而将客户端请求按照某种算法调度给后主机。

实现方式

硬件调度:F5 A10 Array Radware

软件调度:Nginx LVS HAproxy

软件调度按照工作在OSI协议栈的哪一层又可分为:

传输层:LVS HAproxy(mode tcp)

应用层:HAproxy(mode http)Nginx

应用类型

HTTP重定向负载均衡

反向代理负载均衡

DNS域名解析负载均衡

调度算法简介

轮询(roundrobin-rr),按照客户端请求顺序把客户端的请求逐一分配到不同后端节点服务器。

加权轮询,在轮询算法的基础上加权重,权重和用户访问成正比,权重越大,二逼转发的请求越多。

最少连接数,将请求分发给后端节点服务器连接数最少的机器。

最快响应,根据后端节点服务器的响应时间来分配请求,响应时间短的优先分配。。

Hash法,对客户端IP或者访问的URL进行hash运算。

Nginx反向代理实现负载均衡

网络拓扑

基础配置

配置router

echo "net.ipv4.ip_forward=1" >> /etc/sysctl.d/99-sysctl.conf sysctl -p
net.ipv4.ip_forward = 1#开启伪装!!!!!
firewall-cmd --add-masquerade

网关指向路由器

nmcli connection modify ens160 ipv4.gateway 10.1.1.20

配置web

dnf -y install nginx;echo "welcome to $(hostname)" > /usr/share/nginx/html/index.html;systemctl enable nginx --now#client访问
curl 10.1.8.11
welcome to web1.robinkool.cloud
curl 10.1.8.12
welcome to web2.robinkool.cloud
curl 10.1.8.13
welcome to web3.robinkool.cloud

配置lb

dnf -y install nginx
vim /etc/nginx/nginx.conf
#分别在nginx主配置文件中http代码块中增加upstream web {server 10.1.8.11:80;server 10.1.8.12:80;server 10.1.8.13:80;}location / {proxy_pass http://web;}systemctl start nginx.service #client1测试
for i in {1..90};do curl -s 10.1.8.10 ;done|sort|uniq -c30 welcome to web3.robinkool.cloud30 welcome to web1.robinkool.cloud30 welcome to web2.robinkool.cloud#client2测试
for i in {1..90};do curl -s 10.1.8.10 ;done|sort|uniq -cwelcome to web1.robinkool.cloudwelcome to web2.robinkool.cloudwelcome to web3.robinkool.cloud

负载均衡-算法

轮询(round-robin)

默认的调度算法,按照客户端请求顺序逐一分配到不同后端的节点服务器,如果后端节点服务器宕机,宕机的服务器会被自动从节点服务器池中剔除,新的请求分配给正常服务器。

upstream web {server 10.1.8.11:80;server 10.1.8.12:80;server 10.1.8.13:80;
}#测试
for i in {1..90};do curl -s 10.1.8.10 ;done|sort|uniq -c30 welcome to web3.robinkool.cloud30 welcome to web1.robinkool.cloud30 welcome to web2.robinkool.cloud

在rr轮询算法基础上加权重,权重值越大,被转发的请求越多,可根据服务器性能和配置指定权重大小。

upstream web {server 10.1.8.11:80 weight=10;server 10.1.8.12:80 weight=20;server 10.1.8.13:80 weight=30;
}#测试
for i in {1..90};do curl -s 10.1.8.10 ;done|sort|uniq -c15 welcome to web3.robinkool.cloud30 welcome to web1.robinkool.cloud45 welcome to web2.robinkool.cloud
ip哈希(ip_hash)

每个请求按客户端ip的hash结果分配。当新的请求到达时,先将其客户端ip通过哈希算法计算出一个值,在随后的客户端请求中,客户ip的哈希值只要相同 ,就会被分配到同一台服务器。

upstream web {ip_hashserver 10.1.8.11:80;server 10.1.8.12:80;server 10.1.8.13:80;
}#测试
for i in {1..90};do curl -s 10.1.8.10 ;done|sort|uniq -c90 welcome to web3.robinkool.cloud
通用哈希(generic Hash)

请求发送到的服务器有用户定义的键确定,该键可以是文本字符串、变量或者组合。

upstream web {hash $request_url;server 10.1.8.11:80;server 10.1.8.12:80;server 10.1.8.13:80;
}#测试
for i in {1..90};do curl -s 10.1.8.10 ;done|sort|uniq -c90 welcome to web3.robinkool.cloud
最少连接数(least_conn)

讲请求发给后端节点服务器链接最少的机器

upstream web {least_conn;server 10.1.8.11:80;server 10.1.8.12:80;server 10.1.8.13:80;
}#测试
for i in {1..90};do curl -s 10.1.8.10 ;done|sort|uniq -c30 welcome to web3.robinkool.cloud30 welcome to web1.robinkool.cloud30 welcome to web2.robinkool.cloud
#least_conn模式支持权重

HAproxy实现负载均衡

HAproxy是一款提供高可用性、负载均衡以及基于TCP(四层)和HTTP(七层)应用的代理软件,支持虚拟主机。

调度算法

HAproxy有8中负载均衡算法(load balance),分别如下:

round-robin,动态加权轮询,支持权重,

statuc-rr,静态轮询,不支持权重,

leastconn,最小连接数优先处理,

source,源地址哈希算法,

uri,根据uri做哈希算法,

url_param,根据请求的URI参数做哈希,

rdp-cookie(name),根据cookie(name)来锁定并哈希每一次TCP请求。

HAproxy实践

通过HAproxy实现4层和7层负载均衡

基础配置

网络拓扑

网关和路由配置同Nginx
安装HAproxy
#停止nginx服务,避免端口占用
systemctl disable nginx --now
dnf -y install haproxy
http模式(七层)
dnf -y install nginx;echo "welcome to $(hostname)" > /usr/share/nginx/html/index.html;systemctl enable nginx --now#client访问
curl 10.1.8.11
welcome to web1.robinkool.cloud
curl 10.1.8.12
welcome to web2.robinkool.cloud
curl 10.1.8.13
welcome to web3.robinkool.cloud
配置haproxy
#先备份haproxy配置文件
cp /etc/haproxy/haproxy.cfg{,.bak}#修改haproxy配置文件,最后添加
################## web ####################
frontend front_webbind *:80default_backend  back_web #默认后端
backend back_webbalance     roundrobin #rr轮询server  web1 10.1.8.11:80 checkserver  web2 10.1.8.12:80 checkserver  web3 10.1.8.13:80 checksystemctl enable haproxy.service --now
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.#client[1-2]测试
[root@client1 Zc ~ 15:11:09]# for i in {1..90};do curl -s 10.1.8.10 ;done|sort|uniq -c30 welcome to web1.robinkool.cloud30 welcome to web2.robinkool.cloud30 welcome to web3.robinkool.cloud
[root@client2 Zc ~ 15:14:36]# for i in {1..90};do curl -s 10.1.8.10 ;done|sort|uniq -c30 welcome to web1.robinkool.cloud30 welcome to web2.robinkool.cloud30 welcome to web3.robinkool.cloud#使用haproxy中acl代码块实现类似nginx反向代理(通过正则表达式匹配将流量分发到不同后端)
################## web ####################
frontend front_webbind *:80default_backend  back_web #默认后端acl test url_reg -i \.txt$ # 定义ACL规则:匹配.txt结尾的URL(不区分大小写)use_backend test if test # 如果匹配ACL规则"test",则使用名为"test"的后端
backend back_webbalance     roundrobin #rr轮询server  web1 10.1.8.11:80 checkserver  web2 10.1.8.12:80 checkserver  web3 10.1.8.13:80 checkbackend testbalance     roundrobin #rr轮询server  web1 10.1.8.11:81 checkserver  web2 10.1.8.12:81 checkserver  web3 10.1.8.13:81 check#测试环境准备
mkdir /test
echo "hello txt from $(hostname -s)" > /test/index.txt
echo "hello html from $(hostname -s)" > /test/index.html
#准备虚拟主机配置文件
vim /etc/nginx/conf.d/vhost-test.conf
server { listen 81;root   /test;
}
systemctl restart nginx#测试
[root@client1 Zc ~ 16:25:53]# curl 10.1.8.11
welcome to web1.robinkool.cloud
[root@client1 Zc ~ 16:26:21]# curl 10.1.8.11:81
hello html from web1
[root@client1 Zc ~ 16:26:29]# curl 10.1.8.11:81/index.txt
hello txt from web1
[root@client1 Zc ~ 16:27:32]# curl 10.1.8.10/index.txt
hello txt from web3
[root@client1 Zc ~ 16:30:31]# curl 10.1.8.10/index.txt
hello txt from web2
[root@client1 Zc ~ 16:30:38]# curl 10.1.8.10/index.txt
hello txt from web1
tcp模式(四层)

配置ssh

配置haproxy

vim /etc/haproxy/haproxy.cfg
################## ssh ####################
listen sshbind *:1022mode tcpbalance     roundrobinserver  web1 10.1.8.11:22 checkserver  web2 10.1.8.12:22 checkserver  web3 10.1.8.13:22 checksystemctl restart haproxy#测试
[root@client2 Zc ~ 16:06:28]# for i in {1..90};do ssh root@10.1.8.10 -p 1022 hostname 2>/dev/null ;done |sort|uniq -c30 web1.robinkool.cloud30 web2.robinkool.cloud30 web3.robinkool.cloud
#如果在balance位置将rr改为source 那么在使用ssh登录时就会固定一个地址。

配置说明

haproxy配置文件有两部分组成,全局设定和对代理的设定,

其中全局设定(global settings):主要用于定义haproxy进程管理安全性能及相关参数

代理设定(proxies):分为4段:

defaluts:为其他配置提供默认参数,默认配置参数可由下一个defaults重新设定。

fronted:定义一系列监听的套接字,这些套接字可接受客户端请求并与之建立连接。

backend:定义后端服务器,前端代理服务器将会把客户端的请求调度至这些服务器。

listen:定义监听套接字和后端服务器,类似将fronted和backen段放在一起,通常配置TCP流量,也就是四层代理。

LVS

LVS介绍

Linux虚拟服务器(LVS,Linux Virtual Server),使用负载均衡技术将多台服务器组成一个虚拟服务器。

LVS术语

调度器:负载均衡器,Director,Virtual Server(VS)

后端服务器:真实服务器,Real Server(RS),Backend Server

调度器一般配两个ip地址:

VIP:向外提供服务的IP地址

DIP:与后端RS通信的IP地址

RIP:RS的IP地址

CIP:Client的IP地址

LVS由ipvsadm和ipvs组成:

ipvsadm:用户空间命令行工具,用于在Director上定义集群服务和添加集群上的RS

ipvs:工作与内核上netfilter中INPUT钩子上的程序代码

工作原理

客户端将流量发送给LB,LB将流量发送给服务端,服务端在返回流量的时候有两种情况,一种是直接发送给客户端,另一种则是通过LB发送给客户端。相较于Nginx和HAproxy是在流量返回时通过LB将流量返回给客户端,并且可以通过LB的缓存,在下次访问时直接返回请求。

工作模式

NAT模式

通过将请求报文的目标地址和目标端口修改为某RS的IP和PORT来实现报文转发。

工作原理

客户端将流量发送给Director,Director将流量再转发给RS,RS将流量发回给Director,再由Director将流量发给客户端。所以RS的网关是指向Director的。

网络拓扑

nmcli connection modify ens160 ipv4.gateway 10.1.8.10;nmcli connection up ens160 

nmcli connection modify ens160 ipv4.gateway 10.1.1.10;nmcli connection up ens160 

#NAT模式下,Director充当路由器,所以要开启路由转发功能
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf sysctl -p
net.ipv4.ip_forward = 1#安装ipvsamd
dnf -y install ipvsadm
#创建服务启动文件,如果没有该文件启动服务会报错
touch /etc/sysconfig/ipvsadm
systemctl start ipvsadm

ipvsadm -A -t 10.1.1.10:80 -s rr #-A:添加一个新的虚拟服务 -t 10.1.1.10:80:指定虚拟服务的地址和端口(TCP协议)-s rr:指定调度算法为轮询(Round Robin)
ipvsadm -a -t 10.1.1.10:80 -r 10.1.8.11 -m #-a:向虚拟服务添加一个真实服务器 -t 10.1.1.10:80:指定要添加到的虚拟服务 -r 10.1.8.11:指定真实服务器地址 -m:使用 NAT(Masquerading)转发模式
ipvsadm -a -t 10.1.1.10:80 -r 10.1.8.12 -m
ipvsadm -a -t 10.1.1.10:80 -r 10.1.8.13 -m
ipvsadm-save -n > /etc/sysconfig/ipvsadm #生成内容保存到文件中,重启服务加载该配置
ipvsadm -Ln

#多次访问验证
for i in {1..90};do curl -s 10.1.1.10 ;done|sort|uniq -c30 welcome to web1.robinkool.cloud30 welcome to web2.robinkool.cloud30 welcome to web3.robinkool.cloud

ipvsadm -E -t 10.1.1.10:80 -s wrr #-E编辑 w-weight
#修改权重
ipvsadm -e -t 10.1.1.10:80 -r 10.1.8.12 -m -w 2
ipvsadm -e -t 10.1.1.10:80 -r 10.1.8.13 -m -w 3#再次查看
ipvsadm -Ln

for i in {1..90};do curl -s 10.1.1.10 ;done|sort|uniq -c15 welcome to web1.robinkool.cloud30 welcome to web2.robinkool.cloud45 welcome to web3.robinkool.cloud

DR模式

通过为请求报文重新封装一个MAC首部进行报文转发,新MAC首部的源MAC是DIP所在网卡的MAC,目标MAC为某RS位在接口的MAC;整个过程源的IP首部不会发生变化(源IP为CIP,目标IP始终为VIP)

工作原理

网络拓扑

该模式下Director只有一块网卡

nmcli device disconnect ens192 
成功断开设备 "ens192"。
nmcli connection modify ens160 ipv4.gateway 10.1.8.20
nmcli connection up ens160 
nmcli connection modify ens160 ipv4.gateway 10.1.1.20
nmcli connection up ens160 
配置router
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.d/99-sysctl.conf sysctl -p
net.ipv4.ip_forward = 1#开启伪装!!!!!
firewall-cmd --add-masquerade
配置LVS-RS
nmcli connection add type dummy ifname dummy con-name dummy ipv4.addresses 10.1.8.100/32 ipv4.method manual 
连接 "dummy" (156d22e0-3f26-44a2-9260-56afa56ebfc9) 已成功添加。
nmcli connection up dummy 
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/5)#web[1-3]配置arp参数,关闭arp对dummy网卡的解析
cat >> /etc/sysctl.conf << EOF
> net.ipv4.conf.all.arp_ignore = 1
> net.ipv4.conf.all.arp_announce = 2
> net.ipv4.conf.dummy.arp_ignore = 1
> net.ipv4.conf.dummy.arp_announce = 2
> EOF
sysctl -p

配置LVS-DS
#添加虚拟网卡
nmcli connection add type dummy ifname dummy con-name dummy ipv4.addresses 10.1.8.100/32 ipv4.method manual 
nmcli connection up dummy#清空ipvsadm规则
ipvsadm -C
ipvsadm -Ln
ipvsadm -A -t 10.1.1.10:80 -s rr
ipvsadm -a -t 10.1.1.10:80 -r 10.1.8.11
ipvsadm -a -t 10.1.1.10:80 -r 10.1.8.12 
ipvsadm -a -t 10.1.1.10:80 -r 10.1.8.13 
ipvsadm-save -n > /etc/sysconfig/ipvsadm 
ipvsadm -Ln

for i in {1..90};do curl -s 10.1.8.100 ;done|sort|uniq -c 30 welcome to web1.robinkool.cloud30 welcome to web2.robinkool.cloud30 welcome to web3.robinkool.cloud

NAT 模式拓扑
客户端 → LVS(VIP:10.1.1.10) → NAT 转换 → RS(10.1.8.11/12)↑_________________________↓  # 响应流量也经过 LVS
DR 模式拓扑
客户端 → LVS(VIP:10.1.1.10) → MAC 重写 → RS(10.1.1.11/12)↓______________________________↑  # 响应直接返回客户端

配置关键区别

NAT 模式配置
# 添加虚拟服务(VIP)
ipvsadm -A -t 10.1.1.10:80 -s rr# 添加真实服务器(RS),指定 NAT 模式(-m)
ipvsadm -a -t 10.1.1.10:80 -r 10.1.8.11 -m
ipvsadm -a -t 10.1.1.10:80 -r 10.1.8.12 -m

RS 要求

  • 使用私有 IP(如 10.1.8.11

  • 默认网关必须指向 LVS 的内网 IP(如 10.1.8.10


DR 模式配置
# 添加虚拟服务(VIP)
ipvsadm -A -t 10.1.1.10:80 -s rr# 添加真实服务器(RS),指定 DR 模式(-g)
ipvsadm -a -t 10.1.1.10:80 -r 10.1.1.11 -g
ipvsadm -a -t 10.1.1.10:80 -r 10.1.1.12 -g

RS 要求

  1. 需要配置虚拟网卡:
nmcli connection add type dummy ifname dummy con-name dummy ipv4.address 10.1.8.100/32 ipv4.method manual
  1. 禁止 RS 响应 VIP 的 ARP 请求:
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
  1. RS 的网关指向真实路由器(非 LVS)。

Keepalived

keepalived是一个用c语言编写的路由软件,目标是为了Linux系统和基于Linux的基础设施的负载均衡和高可用性提供简单而健壮的设施。

Keepalived+LVS(DR)+Apache+NFS

web[1-3]添加vmnet2网卡,且网关指向router10.1.8.20

ha1和ha2的网关指向router10.1.8.20

dnf -y install httpd;echo "welcome to $(hostname)" > /var/www/html/index.html;systemctl enable httpd --now
dnf -y install nfs-utils
id apache
mount 10.1.2.100:/data/www /var/www/html
df -h
echo "10.1.2.100:/data/www /var/www/html nfs defaults 0 0" >> /etc/fstab
systemctl daemon-reload
mount -a
dnf -y install httpd;echo "welcome to $(hostname)" > /var/www/html/index.html;systemctl enable httpd --now
dnf -y install nfs-utils
mount 10.1.2.100:/data/www /var/www/html/
dnf -y install httpd;echo "welcome to $(hostname)" > /var/www/html/index.html;systemctl enable httpd --now
dnf -y install nfs-utils 
mount 10.1.2.100:/data/www /var/www/html/
dnf -y install nfs-utils
mkdir -p /data/www
chown 48:48 /data/www
echo "/data/www 10.1.2.0/24(rw)" > /etc/exports #将准备好的路径已读写方式共享给2.0网段的主机
systemctl enable nfs-server.service --now
systemctl status nfs-server.service
echo "im nfs" > /data/www/index.html
echo "10.1.2.11 web1.robinkool.cloud" >> /etc/hosts
echo "10.1.2.12 web2.robinkool.cloud" >> /etc/hosts
echo "10.1.2.13 web3.robinkool.cloud" >> /etc/hostscurl http://web2.robinkool.cloud
curl http://web3.robinkool.cloud
curl http://web1.robinkool.cloud

配置LVS-RS(Real Server)
nmcli connection add type dummy ifname dummy con-name dummy ipv4.method manual ipv4.addresses 10.1.8.100/32
nmcli connection up dummycat >> /etc/sysctl.conf << EOF
> net.ipv4.conf.all.arp_ignore = 1
> net.ipv4.conf.all.arp_announce = 2
> net.ipv4.conf.dummy.arp_ignore = 1
> net.ipv4.conf.dummy.arp_announce = 2
> EOF
sysctl -p

配置HA和LVS-DS(Director Server)
dnf -y install keepalived ipvsadm
cp /etc/keepalived/keepalived.conf{,.ori}vim /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {router_id ha1
}vrrp_instance nginx {state  MASTERinterface ens160virtual_router_id 51priority 150advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {10.1.8.100/24}
}virtual_server 10.1.8.100 80 {delay_loop 6lb_algo rrlb_kind DRprotocol TCPreal_server 10.1.8.11 80 {weight 1TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}real_server 10.1.8.12 80 {weight 2TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}real_server 10.1.8.13 80 {weight 2TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}      
}systemctl enable keepalived.service --now
dnf -y install keepalived ipvsadm
cp /etc/keepalived/keepalived.conf{,.ori}vim /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {router_id ha2
}vrrp_instance apache {state BACKUPinterface ens160virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {10.1.8.100/24}
}virtual_server 10.1.8.100 80 {delay_loop 6lb_algo rrlb_kind DRprotocol TCPreal_server 10.1.8.11 80 {weight 1TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}real_server 10.1.8.12 80 {weight 2TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}real_server 10.1.8.13 80 {weight 2TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}      
}systemctl enable keepalived --now
ipvsadm -Ln
#使用keepalived后再使用ipvsadm命令查看发现已经分配完毕

功能性测试

#轮询测试前在web[1-3]中取消/var/www/html的挂载
while true ;do curl -s http://10.1.8.100;sleep 1;done

当模式设置为rr、dr时,访问测试一直显示同一个地址的测试页面是因为配置文件中的会话保持代码没有注释掉。

高可用性测试

init 0

客户端访问不受影响

负载均衡测试

umount /var/www/html

systemctl stop httpd

Keepalived+LVS+Mariadb

Mariadb复制原理

把一个服务器上执行过的sql语句在别的服务器上重复执行一遍,这样只要两个数据库的初态是一样的,那么就能一直同步,这种复制和重复都是mysql自动实现的。

实验环境

dnf -y install mariadb-server
vim /etc/my.cnf.d/mariadb-server.cnf #在[mysqld]代码块中添加如下代码
server-id=1/2
log_bin=mysql-bin
relay_log=mysql-relay-bin
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
systemctl enable mariadb --now

mysql_secure_installation
#设置密码为redhat

Mariadb主从设置
mysql -uroot -predhat
grant replication slave,replication client on *.* to 'repl'@'10.1.8.12' identified by 'redhat';
flush privileges;
show master status\G; #查询主库状态

mysql -uroot -predhat
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 18
Server version: 10.3.39-MariaDB-log MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> change master to master_host='10.1.8.11',-> master_user='repl',-> master_password='redhat',-> master_port=3306,-> master_log_file='mysql-bin.000002',-> master_log_pos=1998,-> master_connect_retry=30;
Query OK, 0 rows affected (0.007 sec)

show slave status\G;

验证:主库db1中创建新库test,在从库db2中查看会同步,但是在db2中删除test库,db1中不受影响

所以此时在db2中创建从库db1

grant replication slave,replication client on *.* to 'repl'@'10.1.8.11' identified by 'redhat';
Query OK, 0 rows affected (0.000 sec)
flush privileges;
Query OK, 0 rows affected (0.000 sec)show master status\G;
*************************** 1. row ***************************File: mysql-bin.000003Position: 805Binlog_Do_DB: 
Binlog_Ignore_DB: information_schema,performance_schema
1 row in set (0.000 sec)ERROR: No query specified

change master to master_host='10.1.8.12',->     master_user='repl',->     master_password='redhat',->     master_port=3306,->     master_log_file='mysql-bin.000003',->     master_log_pos=805,->     master_connect_retry=30;
start slave;
show slave status\G;

配置LVS-RS
#增加虚拟网卡
nmcli connection add type dummy ifname dummy con-name dummy ipv4.method manual ipv4.addresses 10.1.8.100/32
nmcli connection up dummy#配置arp参数,关闭arp对dummy网卡的解析
cat >> /etc/sysctl.conf << EOF
> net.ipv4.conf.all.arp_ignore = 1
> net.ipv4.conf.all.arp_announce = 2
> net.ipv4.conf.dummy.arp_ignore = 1
> net.ipv4.conf.dummy.arp_announce = 2
> EOF
sysctl -p
配置HA和LVS-DS
dnf -y install keepalived ipvsadm
cp /etc/keepalived/keepalived.conf{,.ori}vim /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {router_id ha1
}vrrp_instance nginx {state MASTERinterface ens160virtual_router_id 51priority 110advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {10.1.8.100/24}
}virtual_server 10.1.8.100 3306 {delay_loop 6lb_algo rrlb_kind DRprotocol TCPreal_server 10.1.8.11 3306 {weight 1TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}real_server 10.1.8.12 3306 {weight 2TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}     
}systemctl enable keepalived.service --now
dnf -y install keepalived ipvsadm
cp /etc/keepalived/keepalived.conf{,.ori}vim /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {router_id ha2
}vrrp_instance nginx {state BACKUPinterface ens160virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {10.1.8.100/24}
}virtual_server 10.1.8.100 3306 {delay_loop 6lb_algo rrlb_kind DRprotocol TCPreal_server 10.1.8.11 3306 {weight 1TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}real_server 10.1.8.12 3306 {weight 2TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}  
}systemctl enable keepalived.service --now
测试
mysql -uroot -predhat
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 51
Server version: 10.3.39-MariaDB-log MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> grant all privileges on *.* to 'robinkool'@'%' identified by 'redhat';
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)MariaDB [(none)]> quit
Bye
dnf -y install mariadb
mysql -u robinkool -predhat -h 10.1.8.100;
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 81
Server version: 10.3.39-MariaDB-log MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> 
#登录成功
systemctl stop keepalived.service 

client1登录mysql连接正常。

systemctl stop mariadb.service 

client2登录mysql连接正常。

 Keepalived+LVS(DR)+Apache++NFS+MySql+Php

umount /var/www/html
cat > /var/www/html/phpinfo.php << 'EOF'
> <?php phpinfo(); ?>
> EOF

mysql -u root -predhat
MariaDB [(none)]> CREATE DATABASE ecshop;
MariaDB [(none)]> CREATE USER ecshop@'%' IDENTIFIED BY 'redhat';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON ecshop.* TO ecshop@'%';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> exit

dnf -y install php php-mysqlnd
systemctl restart httpd
mount -a #将nfs目录挂载
df -h #查看确认挂载

dnf -y install lrzsz
rz -E
unzip ECShop_V4.1.20_UTF8_release20250416_88250602410669.zip 
cp -a ECShop_V4.1.20_UTF8_release20250416/source/ecshop/* /data/www/
chown -R 48:48 /data/www/
rm -f /data/www/index.html 
#浏览器直接访问10.1.8.11/index.php
nmcli connection add type dummy ifname dummy2 con-name dummy2 ipv4.method manual ipv4.addresses 10.1.8.200/32
nmcli connection up dummy2
sysctl net.ipv4.conf.dummy2.arp_ignore=1
sysctl net.ipv4.conf.dummy2.arp_announce=2
! Configuration File for keepalivedglobal_defs {router_id ha1
}vrrp_instance apache {state  MASTERinterface ens160virtual_router_id 51priority 150advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {10.1.8.100/24}
}virtual_server 10.1.8.100 80 {delay_loop 6lb_algo rrlb_kind DRprotocol TCPpersistence_timeout 50real_server 10.1.8.11 80 {weight 1TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}real_server 10.1.8.12 80 {weight 1TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}real_server 10.1.8.13 80 {weight 1TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}      
}vrrp_instance db {state  BACKUPinterface ens160virtual_router_id 52priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {10.1.8.200/24}
}
virtual_server 10.1.8.200 3306 {delay_loop 6lb_algo rrlb_kind DRprotocol TCPreal_server 10.1.8.11 3306 {weight 1TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}real_server 10.1.8.12 3306 {weight 1TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}     
}
! Configuration File for keepalivedglobal_defs {router_id ha2
}vrrp_instance apache {state BACKUPinterface ens160virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {10.1.8.100/24}
}virtual_server 10.1.8.100 80 {delay_loop 6lb_algo rrlb_kind DRprotocol TCPpersistence_timeout 50real_server 10.1.8.11 80 {weight 1TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}real_server 10.1.8.12 80 {weight 1TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}real_server 10.1.8.13 80 {weight 1TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}      
}vrrp_instance db {state MASTERinterface ens160virtual_router_id 52priority 150advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {10.1.8.200/24}
}
virtual_server 10.1.8.200 3306 {delay_loop 6lb_algo rrlb_kind DRprotocol TCPreal_server 10.1.8.11 3306 {weight 1TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}real_server 10.1.8.12 3306 {weight 1TCP_CHECK {connect_timeout 3retry 3delay_before_retry 3}}  
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/diannao/90455.shtml
繁体地址,请注明出处:http://hk.pswp.cn/diannao/90455.shtml
英文地址,请注明出处:http://en.pswp.cn/diannao/90455.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

jmm,`as - if - serial` 与 `happens - before` 原则

在Java并发编程中&#xff0c;as - if - serial 与 happens - before 原则是确保程序在多线程环境下正确执行的重要规则&#xff0c;下面为你详细讲解&#xff1a; as - if - serial原则 定义&#xff1a;as - if - serial 原则是指&#xff0c;不管编译器和处理器如何优化&…

主流大模型Agent框架 AutoGPT详解

注&#xff1a;此文章内容均节选自充电了么创始人&#xff0c;CEO兼CTO陈敬雷老师的新书《GPT多模态大模型与AI Agent智能体》&#xff08;跟我一起学人工智能&#xff09;【陈敬雷编著】【清华大学出版社】 GPT多模态大模型与AI Agent智能体书籍本章配套视频课程【陈敬雷】 文…

kotlin学习,val使用get()的问题

疑问&#xff1a;定义val怎么还能使用get()代码示例&#xff1a;private val nametype:Intget()Business.carInfo?.let{carSc(it)}?:LType.AS回答&#xff1a;Kotlin 允许为属性定义自定义 getter&#xff0c;每次访问属性时会执行该方法疑问&#xff1a;这里引出另一个不解&…

解决el-select数据类型相同但是显示数字的问题

这个不是我写的&#xff0c;只是遇到的bug&#xff0c;写法问题&#xff0c;忽略了值的绑定的问题源代码bug&#xff1a;<el-selectv-model"schemeInfo.horizon"placeholder"请选择起报月份"clearablefilterable><el-option v-for"(option,i…

熟练掌握RabbitMQ和Kafka的使用及相关应用场景。异步通知与解耦,流量削峰,配合本地消息表实现事务的最终一致性并解决消息可靠、顺序消费和错误重试等问题

RabbitMQstock.#.nyse &#xff0c;#匹配多个字符&#xff0c;*匹配一个字符。 Confirm Callback 到达exchange的回调。 Return Callback 到达queue失败的回调。 Kafka Kafka生产端分区器&#xff1a; 1.直接指定partition 指定0,1。 2.设置hashkey&#xff0c;计算key的hash值…

飞算科技:以原创技术赋能数字转型

在数字科技迅猛发展的浪潮中&#xff0c;飞算数智科技&#xff08;深圳&#xff09;有限公司&#xff08;简称 “飞算科技”&#xff09;作为一家自主创新型的数字科技公司&#xff0c;同时也是国家级高新技术企业&#xff0c;正以扎实的技术实力和丰富的实践经验&#xff0c;在…

基于 Rust 的Actix Web 框架的应用与优化实例

基于 Rust 的Actix Web 框架的应用与优化实例 Actix Web 框架概述 Actix Web 是一个基于 Rust 的高性能、轻量级 Web 框架,构建于 Actix 异步运行时之上。它支持异步编程模型,适合构建高并发、低延迟的 Web 服务和 API。 核心特性 异步支持:基于 async/await 语法,充分利…

springMVC01-特点、创建项目、@RequestMapping、获取参数请求,三种域对象

一、简介 SpringMVC 就是 Spring 框架中的 MVC 模块&#xff0c;用于构建 Web 应用中的“控制层”。 SpringMVC 是 Spring 提供的一个基于 Servlet 的 Web MVC 框架模块&#xff0c;是 Spring 整个体系中的“Web 层核心”。 SpringMVC 是 Spring 的一部分&#xff0c;Spring…

Java基础,反射破坏封装性 - 单例模式的崩塌

目录一、容易出现问题的小李代码小李的单例设计看似完美&#xff0c;实则存在三个致命问题&#xff1a;1、反射攻击的天然漏洞2、序列化的隐患3、性能瓶颈二、隔壁老王的优化方案三、为什么这样优化&#xff1f;四、小结周五下午&#xff0c;代码审查会议上&#xff0c;小李自信…

Neo4j 综合练习作业

Neo4j 综合练习作业 作业说明 这个作业涵盖了 Neo4j 的多个重要知识点&#xff0c;包括节点和关系的创建、查询、更新、删除以及高级查询功能。请使用 Cypher 语句完成以下所有题目。 数据准备 首先执行以下语句创建示例数据&#xff1a; ACTED_IN: 表示出演关系 DIRECTED: 表示…

基于PA算法的FTL引导

一、抽象绑定关系 1. 什么是 AF Block,什么是 NF Block,为什么要将多个 NF Block 绑定为一个 AF Block AF Block(Allocation Flash Block) 和 NF Block(NAND Flash Block) 是在 NAND Flash 存储架构中用于管理数据的基本单位。 AF Block 定义:AF Block 是一组多个 NF…

快速入门Java中的IO操作

以下是 Java 中常用的 IO 知识点总结&#xff1a; 1. 流的分类 按数据流向&#xff1a;输入流&#xff08;读取数据&#xff09;和输出流&#xff08;写入数据&#xff09;。按数据类型&#xff1a;字节流&#xff08;处理二进制数据&#xff0c;以字节为单位&#xff09;和字符…

小程序软装: 组件库开发

本节概述 经过前面小节的学习&#xff0c;我们已经搭建起了小程序的编译构建环境&#xff0c;能够将我们开发的小程序项目编译成为对应的逻辑代码文件 logic.js&#xff0c;页面渲染文件 view.js&#xff0c;样式文件 style.css 和配置文件 config.json 在编译小程序的过程中…

250708-Debian系统安装Edge浏览器并配置最小中文输入法

在 Debian 系统上安装 Microsoft Edge 浏览器可以通过以下几种方式进行。Microsoft 官方提供了 .deb 安装包&#xff0c;适用于 Debian、Ubuntu 及其衍生系统。 A. 如何安装&#xff1f; ✅ 方法一&#xff1a;使用 .deb 安装包&#xff08;推荐&#xff09; 步骤 1&#xff…

docker所占硬盘内存指令

使用下面命令可以查看docker所占的硬盘大小&#xff0c;如&#xff1a;docker system dfdocker system df -v

A1126LLHLX-T Allegro霍尔效应锁存器,5kHz+推挽输出,汽车级转速检测专家!

A1126LLHLX-T&#xff08;Allegro&#xff09;产品解析一、产品定位A1126LLHLX-T是Allegro MicroSystems推出的全极性霍尔效应锁存器&#xff0c;采用超薄SOT-23W封装&#xff08;1mm厚度&#xff09;&#xff0c;专为高可靠性位置检测与转速测量设计&#xff0c;具有低功耗、高…

【C#】File从后往前读取文件指定行数

/// <summary>/// 从后往前读取文件最后行数据/// </summary>/// <param name"filePath"></param>/// <param name"count"></param>/// <returns></returns>public static List<string> ReadFileRe…

暑假算法日记第五天

目标​&#xff1a;刷完灵神专题训练算法题单 阶段目标&#x1f4cc;&#xff1a;【算法题单】滑动窗口与双指针 LeetCode题目:683. K 个关闭的灯泡2067. 等计数子串的数量2524. 子数组的最大频率分数2269. 找到一个数字的 K 美丽值1984. 学生分数的最小差值1461. 检查一个字符…

【05】MFC入门到精通——MFC 为对话框中的控件添加变量 和 数据交换和检验

文章目录四、 为对话框中的控件添加变量五、对话框类的5.1 为编辑框添加变量面步骤中 为对话框添加了几个控件&#xff0c;包括三个静态文本框&#xff0c;三个编辑框&#xff0c;一个按钮控件。 四、 为对话框中的控件添加变量 编辑框中的数据可能会经常变化&#xff0c;有必…

4-Kafka-partition(分区)概念

Kafka Topic 分区详解 &#x1f4cc; 一、分区核心概念 1. 什么是分区&#xff1f; 物理分片&#xff1a;Topic 被划分为多个分区&#xff08;Partition&#xff09;&#xff0c;每个分区是一个有序、不可变的消息序列存储单位&#xff1a;每个分区对应一个物理日志文件&…