Web集群_01

集群

  • 通过高速网络将很多服务器集中起来

    • 提供同一种服务, 在客户端看来像只有一个服务器
  • 可以在付出较低成本的情况下获得在 性能 , 可靠性 , 灵活性方面的较高收益

  • 任务调度 是集群系统的 核心技术

集群的目的
  1. 提高性能

  2. 降低成本

  3. 提高可扩展性

  4. 增强可靠性

集群分类
  1. 高性能计算集群HPC

    • 通过以集群开发的并行应用程序 , 解决复杂的科学问题
  2. 负载均衡 (LB) 集群

    • 客户端负载在计算机集群中尽可能平均分摊
  3. 高可用 (HA) 集群

    • 避免单点故障 , 当一个系统发生故障时 , 可以快速迁移

LVS 概述

LVS项目
  • Linux 虚拟服务器 (LVS) 是章文蒿创建的

  • LVS 可以实现高可用的 , 可伸缩的 Web , Mail , Cache和Media等可网络服务

  • 最终目标是利用Linux操作系统和LVS集群软件实现一个高可用 , 高性能 , 低成本的服务器应用集群

LVS集群组成
  • 前端 : 负载均衡层

    • 由一台或多台负载调度器构成
  • 中间 : 服务器群组层

    • 由一组实际运行应用服务的服务器组成
  • 底端 : 数据共享存储层

    • 提供共享存储空间的存储区域
LVS术语
  • 调度器:LVS服务器 , 将负载分发到真实服务器

  • 真实服务器Real Server:提供服务的服务器

  • VIP:虚拟地址,提供给用户访问的地址 , 公布给用户访问的虚拟IP地址

  • DIP:指定地址, 调度器连接后端真实服务器通信的IP地址

  • RIP:真实地址,真实服务器的地址 , 集群节点上使用的IP地址

LVS工作模式
  • VS/NAT

    • 通过网络地址转换实现虚拟服务器
    • 大并发访问时 , 调度器的性能成为瓶颈
  • VS/DR

    • 直接使用路由技术实现虚拟服务器
    • 节点服务器需要配置 虚拟地址VIP , 注意MAC地址广播
  • VS/TUN

    • 通过隧道方式实现虚拟服务器
负载均衡调度算法
  • LVS 目前实现了10种调度算法
  • 常用调度算法有四种
    • 轮询 rr : Real Server 轮流提供服务
    • 加权轮询 wrr : 更具服务器性能设置权重 , 权重大的得到的请求更多
    • 最少连接 ( 常用 ) lc : 根据真实服务器的连接数 , 找最少的分配请求
    • 加权最少连接 wlc : 类似于 wrr , 根据权重分配请求
  • 其他负载均衡算法
    • 源地址散列
    • 目标地址散列
    • 基于局部性的最少连接
    • 带复制的基于局部性最少连接
    • 最短的期望的延迟
    • 最少队列调度

LVS NAT

  1. 配置网络参数

  2. 配置ansible , 网络yum , selinux , firewalld

  3. 配置 web服务(nginx)

  4. 配置 lvs 的路由功能 , ip forward 功能

  5. 在lvs上安装 ipvsadm

  6. 在lvs上编写调度规则

1. 配置网络参数
# 创建4台虚拟机
[root@myhost ~]# vm clone client1 lvs1 web{1..2}

# 初始化虚拟机
[root@myhost ~]# virsh console client1  # 连接client1控制台
localhost login: root
Password: a

# 登陆之后,将以下内容粘贴到终端
hostnamectl set-hostname client1
nmcli connection modify "System eth0" con-name eth0
nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.88.10/24 autoconnect yes ipv4.gateway 192.168.88.5
nmcli connection down eth0
nmcli connection up eth0

# 退出
[root@localhost ~]# exit
# 退出之后,按ctrl+]可回到真机

# 真机通过ssh连接client1
[root@myhost ~]# rm -f ~/.ssh/known_hosts 
[root@myhost ~]# ssh 192.168.88.10

# 配置第2台机器作为lvs1
[root@myhost ~]# virsh console lvs1
localhost login: root
Password: a

# 登陆之后,将以下内容复制到命令行
hostnamectl set-hostname lvs1
nmcli connection modify "System eth0" con-name eth0
nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.88.5/24 autoconnect yes
nmcli connection down eth0
nmcli connection up eth0
rm -f /etc/sysconfig/network-scripts/ifcfg-eth1
nmcli connection add con-name eth1 ifname eth1 type ethernet autoconnect yes ipv4.method manual ipv4.addresses 192.168.99.5/24
reboot     # 重启系统,使得eth1网卡生效
# 按ctrl+]可回到真机

# 真机通过ssh连接lvs1
[root@myhost ~]# ssh 192.168.88.5

# 配置第3台机器作为web1
[root@myhost ~]# virsh console web1
localhost login: root
Password: a

# 登陆之后,将以下内容复制到命令行
hostnamectl set-hostname web1
nmcli connection modify "System eth0" con-name eth0
nmcli connection modify eth0 autoconnect no
rm -f /etc/sysconfig/network-scripts/ifcfg-eth1
nmcli connection add con-name eth1 ifname eth1 type ethernet autoconnect yes ipv4.method manual ipv4.addresses 192.168.99.100/24 ipv4.gateway 192.168.99.5
reboot
# 按ctrl+]可回到真机

# 真机通过ssh连接web1
[root@myhost ~]# ssh 192.168.99.100

# 配置第4台机器作为web2
[root@myhost ~]# virsh console web2
localhost login: root
Password: a

# 登陆之后,将以下内容复制到命令行
hostnamectl set-hostname web2
nmcli connection modify "System eth0" con-name eth0
nmcli connection modify eth0 autoconnect no
rm -f /etc/sysconfig/network-scripts/ifcfg-eth1
nmcli connection add con-name eth1 ifname eth1 type ethernet autoconnect yes ipv4.method manual ipv4.addresses 192.168.99.200/24 ipv4.gateway 192.168.99.5
reboot
# 按ctrl+]可回到真机

# 真机通过ssh连接web2
[root@myhost ~]# ssh 192.168.99.200
2. 配置ansible , 网络环境
# 创建工作目录
[root@pubserver ~]# mkdir cluster
[root@pubserver ~]# cd cluster/

#创建主配置文件
[root@pubserver cluster]# vim ansible.cfg
[defaults]
inventory = inventory
host_key_checking = false  # 不检查主机密钥

# 创建主机清单文件及相关变量
[root@pubserver cluster]# 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

# 创建文件目录,用于保存将要拷贝到远程主机的文件
[root@pubserver cluster]# mkdir files

# 编写yum配置文件
[root@pubserver cluster]# vim files/local88.repo
[BaseOS]
name = BaseOS
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

[root@pubserver cluster]# vim files/local99.repo
[BaseOS]
name = BaseOS
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配置文件的playbook
[root@pubserver cluster]# vim 01-upload-repo.yml
---
- name: config repos.d
  hosts: all
  tasks:
    - name: delete repos.d  # 删除repos.d目录
      file:
        path: /etc/yum.repos.d
        state: absent

    - name: create repos.d  # 创建repos.d目录
      file:
        path: /etc/yum.repos.d
        state: directory
        mode: '0755'

- name: config local88      # 上传repo文件到88网段
  hosts: clients,lb
  tasks:
    - name: upload local88
      copy:
        src: files/local88.repo
        dest: /etc/yum.repos.d/

- name: config local99      # 上传repo文件到99网段
  hosts: webservers
  tasks:
    - name: upload local99
      copy:
        src: files/local99.repo
        dest: /etc/yum.repos.d/

[root@pubserver cluster]# ansible-playbook 01-upload-repo.yml
3. 配置两台web服务器
# 创建首页文件,文件中包含ansible facts变量
[root@pubserver cluster]# vim files/index.html
Welcome from {{ansible_hostname}}

# 配置web服务器
[root@pubserver cluster]# vim 02-config-webservers.yml
---
- name: config webservers
  hosts: webservers
  tasks:
    - name: install nginx  # 安装nginx
      yum:
        name: nginx
        state: present

    - name: upload index   # 上传首页文件到web服务器
      template:
        src: files/index.html
        dest: /usr/share/nginx/html/index.html

    - name: start nginx    # 启动服务
      service:
        name: nginx
        state: started
        enabled: yes

[root@pubserver cluster]# ansible-playbook 02-config-webservers.yml

# 在lvs1上测试到web服务器的访问
[root@lvs1 ~]# curl http://192.168.99.100
Welcome from web1
[root@lvs1 ~]# curl http://192.168.99.200
Welcome from web2
4. 确保lvs1主机的ip转发 , ip_forward功能开启
# 查看ip转发功能的内核参数
[root@lvs1 ~]# sysctl -a    # 查看所有的内核参数
[root@lvs1 ~]# sysctl -a | grep ip_forward  # 查看ip_foward参数
net.ipv4.ip_forward = 1   # 1表示打开转发,0表示关闭转发

# 设置打开ip_forward功能
[root@pubserver cluster]# vim 03-sysctl.yml
---
- name: config sysctl
  hosts: lb
  tasks:
    - name: set ip_forward
      sysctl:   # 用于修改内核参数的模块
        name: net.ipv4.ip_forward       # 内核模块名
        value: '1'        # 内核模块的值
        sysctl_set: yes   # 立即设置生效
        sysctl_file: /etc/sysctl.conf   # 配置写入文件

[root@pubserver cluster]# ansible-playbook 03-sysctl.yml

# 测试从客户端到服务器的访问
[root@client1 ~]# curl http://192.168.99.100
Welcome from web1
[root@client1 ~]# curl http://192.168.99.200
Welcome from web2
5.安装lvs ( ipvsadm )
[root@pubserver cluster]# vim 04-inst-lvs.yml
---
- name: install lvs
  hosts: lb
  tasks:
    - name: install lvs  # 安装lvs
      yum:
        name: ipvsadm
        state: present

[root@pubserver cluster]# ansible-playbook 04-inst-lvs.yml
6. ipvsadm 使用说明
  • ipvsadm
    • ipvsadm 命令是LVS在应用层的管理命令
    • 可以通过这个命令去管理LVS的配置
    • ipvsadm 是一个工具 , 同时它也是条命令
    • 用于管理LVS的策略规则
  • 在集群中添加若干后端真实服务器
  • 实现同一客户端访问, 调度器分配固定服务器
  • ipvsadm 实现规则的 增 , 删 , 改
[root@lvs1 ~]# ipvsadm
-A: 添加虚拟服务器
-E: 编辑虚拟服务器
-D: 删除虚拟服务器
-t: 添加tcp服务器
-u: 添加udp服务器
-s: 指定调度算法。如轮询rr/加权轮询wrr/最少连接lc/加权最少连接wlc

-a: 添加虚拟服务器后,向虚拟服务器中加入真实服务器
-r: 指定真实服务器
-w: 设置权重
-m: 指定工作模式为NAT
-g: 指定工作模式为DR
7. 配置LVS
# 为web服务器创建虚拟服务器,使用rr调度算法
[root@lvs1 ~]# ipvsadm -A -t 192.168.88.5:80 -s rr

# 查看配置
[root@lvs1 ~]# ipvsadm -Ln  # L是列出,n是使用数字,而不是名字

# 向虚拟服务器中添加RIP
[root@lvs1 ~]# ipvsadm -a -t 192.168.88.5:80 -r 192.168.99.100 -w 1 -m
[root@lvs1 ~]# ipvsadm -a -t 192.168.88.5:80 -r 192.168.99.200 -w 2 -m
# 查看配置
[root@lvs1 ~]# ipvsadm -Ln

# 验证
[root@client1 ~]# for i in {1..6}
> do
> curl http://192.168.88.5
> done
Welcome from web2
Welcome from web1
Welcome from web2
Welcome from web1
Welcome from web2
Welcome from web1

# 删除配置。(如果配置有错,用以下命令删除重配置)
[root@lvs1 ~]# ipvsadm -D -t 192.168.88.5:80

# 修改调度模式为加权轮询
[root@lvs1 ~]# ipvsadm -E -t 192.168.88.5:80 -s wrr
# 验证配置
[root@client1 ~]# for i in {1..6}; do curl http://192.168.88.5; done
Welcome from web2
Welcome from web2
Welcome from web1
Welcome from web2
Welcome from web2
Welcome from web1

LVS DR模式

  • lvs dr:
    • 配置网络参数
    • 配置yum
    • 配置web服务
    • 在lvs的eth0上配置vip
    • 在 webservers 的 lo 上配置 vip
    • 在 webservers 上配置内核参数
    • 在lvs 上安装ipvsadm
    • 在 lvs上编写调度规则4

相关推荐

  1. Web_01

    2024-04-24 00:06:02       13 阅读
  2. Web_02

    2024-04-24 00:06:02       12 阅读
  3. Haproxy 搭建 web

    2024-04-24 00:06:02       8 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-24 00:06:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-24 00:06:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-24 00:06:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-24 00:06:02       20 阅读

热门阅读

  1. Python实战:文本内容提取

    2024-04-24 00:06:02       15 阅读
  2. Chapter 1-14. Introduction to Congestion in Storage Networks

    2024-04-24 00:06:02       14 阅读
  3. 成长&工作&思考

    2024-04-24 00:06:02       12 阅读
  4. g 对象:Flask 应用中的“临时口袋”

    2024-04-24 00:06:02       13 阅读
  5. 给rwkv_pytorch配了个流式服务和请求demo

    2024-04-24 00:06:02       13 阅读
  6. VSCode Neovim Extension

    2024-04-24 00:06:02       13 阅读
  7. js 将数组返回带children的递归数组

    2024-04-24 00:06:02       11 阅读
  8. js 数组的常用方法

    2024-04-24 00:06:02       8 阅读
  9. 第二十七章:mybatis plus 如何自定义 SQL 查询条件

    2024-04-24 00:06:02       11 阅读
  10. arduino rc522

    2024-04-24 00:06:02       13 阅读