Keepalive+LVS+Nginx+NFS高可用 架构

发布于:2025-04-06 ⋅ 阅读:(11) ⋅ 点赞:(0)

在这里插入图片描述
主机规划
在这里插入图片描述

1.配置nfs服务

1.1修改主机名ip地址

hostnamectl hostname nfs
nmcli c modify ens160 ipv4.method manual ipv4.addresess 192.168.72.120/24 ipv4.dns 223.5.5.5 ipv4.gateway 192.168.72.2 connection.autoconnect yes

1.2配置仓库

vim /etc/yum.repos.d/dnf.repo
[BaseOS]
name=BaseOS
baseurl=/mnt/BaseOS
gpgcheck=0
[AppStream]
name=AppSteam
baseurl=/mnt/AppStream
gpgcheck=/mnt/AppStream

1.3挂载仓库并下载nfs服务

mount /dev/sr0 /mnt

设置开机自动挂载

vim /etc/fstab
/dev/sr0 /mnt iso9660 defaults 0 0
mount -a

下载nfs服务

dnf install nfs-utils -y

1.4关闭防火墙和selinux,配置服务

systemctl enabled --now  firewalld
setenforce 0
mkdir /nfs/data -p
vim /etc/exports
/nfs/data 192.168.72.119(rw)
/nfs/data 192.168.72.118(rw)

启动nfs服务

systemctl start nfs-server

测试

showmount -e 192.168.72.120

2.搭建rs服务器

2.1修改ip地址和主机名

hostnamectl hostname rs1
nmcli c modify ens160 ipv4.method manual ipv4.addresess 192.168.72.118/24 ipv4.dns 223.5.5.5 ipv4.gateway 192.168.72.2 connection.autoconnect yes
hostnamectl hostname rs2
nmcli c modify ens160 ipv4.method manual ipv4.addresess 192.168.72.119/24 ipv4.dns 223.5.5.5 ipv4.gateway 192.168.72.2 connection.autoconnect yes

2.2配置仓库

vim /etc/yum.repos.d/dnf.repo
[BaseOS]
name=BaseOS
baseurl=/mnt/BaseOS
gpgcheck=0
[AppStream]
name=AppSteam
baseurl=/mnt/AppStream
gpgcheck=/mnt/AppStream

2.3挂载仓库并下载nfs,nginx服务

mount /dev/sr0 /mnt

设置开机自动挂载

vim /etc/fstab
/dev/sr0 /mnt iso9660 defaults 0 0
mount -a

下载nfs,nginx服务

dnf install nfs-utils nginx -y

2.4关闭防火墙和selinux,配置服务

systemctl enabled --now  firewalld
setenforce 0

rs1:

mkdir /data/nfs -p
mount -t nfs 192.168.72.120:/nfs/data /data/nfs

rs2:

mkdir /data/nfs -p
mount -t nfs 192.168.72.120:/nfs/data /data/nfs

编写nginx相关文件:
rs1:

vim /etc/nginx/conf.d/rs1.conf
server{
	server_name  192.168.72.118;
	root /data/nfs;
	access_log /var/log/nginx/accessrs1_log;
	error_log /var/log/nginx/errorrs1_log;
}
rs2:
vim /etc/nginx/conf.d/rs2.conf
server{
	server_name  192.168.72.119;
	root /data/nfs;
	access_log /var/log/nginx/accessrs2_log;
	error_log /var/log/nginx/errorrs2_log;
}

启动nfs,nginx服务

systemctl start nfs-server nginx

测试

curl 192.168.72.120

3.配置keepalived,lvs服务

3.1修改ip地址和主机名

hostnamectl hostname lvs-backup
nmcli c modify ens160 ipv4.method manual ipv4.addresess 192.168.72.117/24 ipv4.dns 223.5.5.5 ipv4.gateway 192.168.72.2 connection.autoconnect yes
hostnamectl hostname lvs-master
nmcli c modify ens160 ipv4.method manual ipv4.addresess 192.168.72.116/24 ipv4.dns 223.5.5.5 ipv4.gateway 192.168.72.2 connection.autoconnect yes

3.2配置仓库

vim /etc/yum.repos.d/dnf.repo
[BaseOS]
name=BaseOS
baseurl=/mnt/BaseOS
gpgcheck=0
[AppStream]
name=AppSteam
baseurl=/mnt/AppStream
gpgcheck=/mnt/AppStream

2.3挂载仓库并下载keepalived,lvs服务

mount /dev/sr0 /mnt

设置开机自动挂载

vim /etc/fstab
/dev/sr0 /mnt iso9660 defaults 0 0
mount -a

下载keepalived,lvs服务

dnf install keepalived ipvsadm -y

3.4关闭防火墙和selinux,配置服务

systemctl enabled --now  firewalld
setenforce 0
lvs-master:
vim /etc/keepalived/keeplived.conf
global_defs {
   router_id lvs-master
}
vrrp_instance VI_1 {
    state MASTER
    interface ens160
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.72.100
    }
}

virtual_server 192.168.72.100 80 {
    delay_loop 3		
    lb_algo wrr			
    lb_kind DR			
    persistence_timeout 50	
    protocol TCP			
    real_server 192.168.72.118 80 {
        weight 3	
        TCP_CHECK {
            connect_timeout 3		
            retry 3					
            delay_before_retry 3
        }
    }
    real_server 192.168.72.119 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
}
 ipvsadm-save -n /etc/sysconfig/ipvsadm

backup:

 vim /etc/keepalived/keeplived.conf
global_defs {
   router_id lvs-backup
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.72.100
    }
}

virtual_server 192.168.72.100 80 {
    delay_loop 3		
    lb_algo wrr			
    lb_kind DR			
    persistence_timeout 50	
    protocol TCP			
    real_server 192.168.72.118 80 {
        weight 3	
        TCP_CHECK {
            connect_timeout 3		
            retry 3					
            delay_before_retry 3
        }
    }
    real_server 192.168.72.119 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
}

ipvsdam-save -n /etc/sysconfig/ipvsadm
启动两台服务器
systemctl start keepalived ipvsadm
查看配置规则:
ipvsadm -Ln

在rs1,rs2上配置参数

ifconfig lo:1 192.168.72.100 netmask 255.255.255.255 broadcast 192.168.72.100 up
ip a show lo
vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.ip_forward=0
sysctl -p

增加路由

route add -host 192.168.72.100 dev lo:1
route -n

在浏览器进行测试
http://192.168.72.100