集群与集群应用
负载均衡与高可用综合实验
一、集群是什么?
是有一组独立的计算机系统构成的一个松耦合的多处理系统,作为一个整体向用户提供一组网络资源,这些单个的计算机就是集群的节点。
二、集群类型
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 要求:
- 需要配置虚拟网卡:
nmcli connection add type dummy ifname dummy con-name dummy ipv4.address 10.1.8.100/32 ipv4.method manual
- 禁止 RS 响应 VIP 的 ARP 请求:
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
- 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}}
}