集群和LVS

前言

本文先简单介绍一下集群的概念,然后在介绍LVS实现负载均衡集群

一、集群

将很多机器组织到一起,作为一个整体对外提供服务,客户视角上就一台服务器
集群在扩展性、性能方面都可以做到很灵活,低成本获得高可靠性,性价比较高
核心技术:任务调度
集群分类:
负载均衡集群:Load Balance
客户端负载在集群中尽量平均分摊
高可用集群:High Availability
避免单点故障,可以在一个系统故障是快速迁移
高性能计算:High Performance Computing
并行运行,解决复杂问题

二、LVS

Linux Virtual Server,linux虚拟服务器,可以实现高可用,可伸缩的Web,Mail,Cache和Media等网络服务。是章文嵩在国防科技大学就读博士期间创建。

集群组成

前端:负载均衡层,由一台或多台负载调度器构成
中间: 服务器群组层,由一组实际运行应用服务的服务器组成
低端:数据共享存储层,提供共享存储空间的存储区域

LVS工作模式

NAT:网络地址转换
通过网络地址转换实现的虚拟服务器,大并发访问时,调度器的性能成为瓶颈
DR:路由模式
直接使用路由技术实现虚拟服务器,节点服务器需要配置VIP ,要注意MAC地址广播
TUN:隧道模式
通过隧道方式实现虚拟服务器

术语解释

调度器:LVS服务器
真实服务器Real Server:提供服务的服务器
VIP:虚拟地址,提供给用户访问的地址
DIP:指定地址,LVS服务器上与真实服务器通信的地址
RIP:真实地址,真实服务器的地址

负载均衡调度算法

LVS实现10种,常用4
常用:
轮询rr(Round Robin):Real Server轮流提供服务
加权轮询wrr(Weighted Round Robin):根据服务器性能设置权重,权重大的得到的请求更多
最少连接lc(Least Connections):根据Real Server的连接数分配请求
加权最少连接wlc(Weighted Least Connections):类似于wrr,根据权重分配请求
其他:
源地址散列(Source Hashing)
目标地址散列(Destination Hashing)
基于局部性的最少连接
带复制的基于局部性最少连接
最短的期望的延迟
最少队列调度

三、配置LVS NAT模式

环境

系统:Linux 虚拟机软件:kvm 版本:CentOS 镜像:Rocky8.6

1、主机清单

主机名ip
pubserver192.168.88.240和192.168.99.240
client1192.168.88.10
lvs1192.168.88.5和192.168.99.5
web1192.168.99.100
web2192.168.99.200

2、配置

192.168.88.240主机
# 下载ansible
# 创建工作目录
mkdir cluster
cd cluster
# 创建主配置文件
# vim ansible.cfg
[defaults]
invebtory=inventory
# 不检查主机密钥
host_key_checking=false
# 创建主机清单文件及相关变量
# vim inventory
[clients]
client1 ansible_host=192.168.88.10
[webservers]
web1 ansible_host=192.168.99.100
web2 ansible_host=192.168.99.200
[lb]
lvs1 ansible_host=192.168.88.5
[all:vars]   # all是ansible自带的组,表示全部主机
ansible_ssh_user=root
ansible_ssh_pass=a
# 创建文件目录,用于保存将要拷贝到远程主机的文件
mkdir files
# 编写yum配置文件
# vim files/local88.repo
[BaseOS]
name=BaseOS
# 本地ftp软件仓库,真实主机可以用网络yum仓库,也可以使用镜像文件内的yum仓库
baseurl=ftp://192.168.88.240/dvd/BaseOS
enabled=1
gpgcheck=0
[AppStream]
name=AppStream
baseurl=ftp://192.168.88.240/dvd/AppStream
enabled=1
gpgcheck=0
[rpms]
name=rpms
baseurl=ftp://192.168.88.240/rpms
enabled=1
gpgcheck=0
###
# vim files/local99.repo
[BaseOS]
name=BaseOS
# 本地ftp软件仓库,真实主机可以用网络yum仓库,也可以使用镜像文件内的yum仓库
baseurl=ftp://192.168.99.240/dvd/BaseOS
enabled=1
gpgcheck=0
[AppStream]
name=AppStream
baseurl=ftp://192.168.99.240/dvd/AppStream
enabled=1
gpgcheck=0
[rpms]
name=rpms
baseurl=ftp://192.168.99.240/rpms
enabled=1
gpgcheck=0
###
# 编写配置yum的ansible文件
---
- hosts : all
  tasks :
    - name : delete repos.d
      file :
        path : /etc/yum.repos.d/
        state : absent
    - name : create repos.d
      file :
        path : /etc/yum.repos.d/
        state : directory
        mode : '0775'
- name : config local88
  hosts : clients, lb
  tasks :
    - copy :
        src : files/local88.repo
        dest : /etc/yum.repos.d/
- name : config local99
  hosts : webservers
  tasks :
    - copy :
        src : files/local99.repo
        dest : /etc/yum.repos.d/
###        
# 执行ansible
ansible-playbook 01-upload-repo.yml
# 编写网页测试文件
# vim files/index.html
Welcome from {{ansible_hostname}}
###
# 配置web服务器
# vim 02-cfgweb.yml
---
- name : config webservers
  hosts : webservers
  tasks :
    - name : install nginx
      yum :
        name : nginx
        state : present
    - name : upload index
      template :
        src : files/index.html
        dest : /usr/share/nginx/html/index.html
    - name : start nginx
      service :
        name : nginx
        state : started
        enabled : yes
###
# 运行ansible
ansible-playbook 02-cfgweb.yml
# 在lvs1上测试(lvs1主机)
curl http://192.168.99.100
curl http://192.168.99.200
# 确保lvs1转发服务开启
sysctl -a | grep ip_forward
输出
net.ipv4.ip_forward = 1 # 为1开启,为0关闭
net.ipv4.ip_forward_update_priority = 1
net.ipv4.ip_forward_use_pmtu = 0
# 若为0,则需要开起
# pubserver主机
# vim 03-sysctl.yml
---
- name : config sysctl
	hosts : lb
	tasks : 
		- name : set ip_forward
			sysctl :
				name : net.ipv4_forward
				value : '1'
				sysctl_set : yes
				sysctl_file : /etc/sysctl.conf  
###
# 运行ansible
ansible-playbook 03-sysctl.yml

# 安装LVS
# vim 04-installlvs.yml
---
- name : install lvs
  hosts : lb
  tasks :
    - name : install lvs
      yum :
        name : ipvsadm
        state : present
###
# 运行剧本
ansible-playbook 04-installlvs.yml
# 配置lvs(lvs1主机)
# 为web服务器创建虚拟服务器,使用rr调度算法
ipvsadm -A -t 192.168.88.5:80 -s rr
# 查看配置L列出,n是使用数字而不是名字
ipvsadm -Ln
# 向虚拟服务器中添加RIP
ipvsadm -a -t 192.168.88.5:80 -r 192.168.99.100 -w 1 -m
ipvsadm -a -t 192.168.88.5:80 -r 192.168.99.200 -w 2 -m
# 查看配置
ipvsadm -Ln             
#删除配置
ipvsadm -D -t 192.168.88.5:80
# 修改调度模式为加权轮询
ipvsadm -E -t 192.168.88.5:80 -s wrr

ps:ansible的yum使用ftp共享仓库,所以需要下载vsftpd,需要在配置文件中开启匿名访问,否则无法访问。
ansible有些版本没有sysctl模块,需要找对版本

3、ipvsadm使用说明

可以使用ipvsadm查询
-A: 添加虚拟服务器
-E: 编辑虚拟服务器
-D: 删除虚拟服务器
-t: 添加tcp服务器
-u: 添加udp服务器
-s: 指定调度算法。如轮询rr/加权轮询wrr/最少连接lc/加权最少连接wlc
-a: 添加虚拟服务器后,向虚拟服务器中加入真实服务器
-r: 指定真实服务器
-w: 设置权重
-m: 指定工作模式为NAT
-g: 指定工作模式为DR

四、配置LVS DR模式

LVS DR模式,LVS主机和web服务器都是单网卡。它们连在同一网络中
修改主机:

主机名ip
client1192.168.88.10
lvs1192.168.88.5
web1192.168.88.100
web1192.168.88.200

流程:
真实服务器:
配置web服务器
配置辅助ip地址,调整内核参数
调度服务器:
安装并启用ipvsadm
配置辅助ip创建虚拟服务器,添加节点
客户端:
连接测试

1、修改pubserver主机清单

先备份再修改

cp inventory inventory.bak
#vim inventory
[clients]
client1 ansible_host=192.168.88.10
[webservers]
web1 ansible_host=192.168.88.100
web2 ansible_host=192.168.88.200
[lb]
lvs1 ansible_host=192.168.88.5
[all:vars]
ansible_ssh_user=root
ansible_ssh_pass=a
###
#修改yum repo文件地址
sed -i 's/99/88/' /etc/yum.repos.d/local99.repo

2、配置

配置lvs1在eth0上vip192.168.88.15

lvs1主机上
# vim 05-cfglvsvip.yml
---
- name: config lvs vip
  hosts: lb
  tasks:
    - name: add vip
      lineinfile:   
        path: /etc/sysconfig/network-scripts/ifcfg-eth0
        line: IPADDR2=192.168.88.15
      notify: restart eth0  
  handlers:   
    - name: restart eth0
      shell: nmcli connection down System eth0; nmcli connection up System eth0
###
# 运行ansible
ansible-playbook 05-cfglvsvip.yml
# 使用ip a s 查看ip
ip a s eth0 | grep 88

pubserver上
# vim 06-config-webvip.yml
---
- name: config webservers vip
  hosts: webservers
  tasks:
    - name: install network-scripts  
      yum:
        name: network-scripts
        state: present
    - name: add lo:0   
      copy:
        dest: /etc/sysconfig/network-scripts/ifcfg-lo:0
        content: |
          DEVICE=lo:0
          NAME=lo:0
          IPADDR=192.168.88.15
          NETMASK=255.255.255.255
          NETWORK=192.168.88.15
          BROADCAST=192.168.88.15
          ONBOOT=yes
      notify: activate lo:0
  handlers:
    - name: activate lo:0  
      shell: ifup lo:0
###
# 运行ansible
ansible-playbook 06-config-webvip.yml
web1上和web2上
ifconfig  # 可以查看到lo:0网卡信息
#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
###
sysctl -p

在lvs1上
ipvsadm -A -t 192.168.88.15:80 -s wlc # 向虚拟服务器中加真实服务器
ipvsadm -a -t 192.168.88.15:80 -r 192.168.88.100 -w 1 -g
ipvsadm -a -t 192.168.88.15:80 -r 192.168.88.200 -w 2 -g
# 客户验证(client1)
for i in {1..6}; do curl http://192.168.88.15/; done
  • 24
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值