集群——负载均衡集群
- 集群(CLUSTER)概念
- 集群就是一组计算机,它们作为一个整体向用户提供一组网络资源
- 集群核心:任务调度
- 集群目的
- 提高性能:如计算密集型应用
- 降低成本:相对超级计算机价格低廉
- 提高可扩展性:只要增加集群节点即可
- 增强可靠性:多个节点完成相同功能,有效避免单点
- 集群分类
- HA:高可用集群(High Availability Cluster)
- LBC:负载均衡集群/负载均衡系统(Load Balance Cluster)
- HPC:
- 科学计算集群(High Performance Computing Cluster)
- 高性能计算集群(High Performance Computing)
#### 一、LVS-NAT配置
> LVS:Linux Virtual Server,Linux虚拟服务器或者称为"调度服务器"
>
> Real Server:提供服务的服务器,称为真实服务器
>
> VIP:虚拟地址,提供给用户访问的地址
>
> DIP:指定地址,LVS服务器上与真实服务器通信的地址
>
> RIP:真实地址,真实服务器的地址
>
> - 常用的4种调度算法:
> - 轮询rr:Real Server轮流提供服务
> - 加权轮询wrr:根据服务器性能设置权重,权重大的得到的请求更多
> - 最少连接lc:根据Real Server的连接数分配请求
> - 加权最少连接wlc:类似于wrr,根据权重分配请求
##### 实验环境准备
##### 网络环境配置:
```shell
pubserver(双网卡):
eth0->192.168.88.240
eth1->192.168.99.240
client:
eth0->192.168.88.10,网关192.168.88.5
lvs1(双网卡):
eth0 -> 192.168.88.5;
eth1->192.168.99.5
web1:
eth1->192.168.99.100;网关192.168.99.5
web2:
eth1->192.168.99.200;网关192.168.99.5
# 显示与设置路由信息
route -n
# 测试LVS-NAT拓扑网络通信情况,在lvs主机操作
cat /proc/sys/net/ipv4/ip_forward #检查lvs1主机是否开启路由转发
# 如果得到1则无需调整,如果得到0则执行下一步临时开启路由转发功能
echo 1 > /proc/sys/net/ipv4/ip_forward
#cleint(88.10)可以ping通web1(99.100)和web2(99.200)
ping -c 2 192.168.99.100
```
##### 配置Ansible
```shell
## 编写ansible配置文件和主机清单列表文件
vim ansible.cfg inventory
# 配置ansible.cfg
[defaults]
inventory = inventory
host_key_checking = false
module_name = shell
# 配置inventory
[clients]
client ansible_ssh_host=192.168.88.10
[webservers]
web1 ansible_ssh_host=192.168.99.100
web2 ansible_ssh_host=192.168.99.200
[lbs]
lvs1 ansible_ssh_host=192.168.88.5
[all:vars]
ansible_ssh_port=22
ansible_ssh_user=root
ansible_ssh_pass=a
# 测试ansible配置
ansible all -m ping
# 更改所有主机组主机名
ansible all -a "hostnamectl set-hostname {{inventory_hostname}}"
```
##### 配置网络Yum并测试NAT模式
```shell
## 配置ftp网络Yum
# 在/root/ansible/files下创建88网段和99网段的repo文件
vim ftpYum_88.repo
cp files/ftpYum_88.repo files/ftpYum_99.repo
sed -ri '/88/s/88/99/' files/ftpYum_99.repo
---------------------------------------------------------------------
## 配置LVS-NAT模式
# 同步yum源文件vim config_Yum_NAT.yml
- name: config yum
hosts: all
tasks:
- name: rm dir #删除/etc/yum.repos.d/目录
file:
path: /etc/yum.repos.d/
state: absent
- name: create dir #创建/etc/yum.repos.d/目录
file:
path: /etc/yum.repos.d/
mode: '0755'
state: directory
- name: upload 88 yum
hosts: clients,lbs
tasks:
- name: copy repo #发送88网段的repo文件到目标主机
copy:
src: files/local88.repo
dest: /etc/yum.repos.d/
- name: upload 99 yum
hosts: webservers
tasks:
- name: copy repo #发送99网段的repo文件到目标主机
copy:
src: files/local99.repo
dest: /etc/yum.repos.d/
# 配置web服务器组
# 修改主页内容vim files/index.html.j2
Welcome to {{ ansible_hostname }}
# 修改web设置vim config_web.yml
- name: config web
hosts: webservers
tasks:
- name: install nginx #安装nginx
yum:
name: nginx
state: present
- name: copy index #发送index.html.j2生成index.html文件
template:
src: files/index.html.j2
dest: /usr/share/nginx/html/index.html
- name: start nginx #启动nginx服务
service:
name: nginx
state: started
enabled: true
# 配置lvs服务器
# 修改内核参数vim config_lvs.yml
- name: config sysctl
hosts: lbs
tasks:
- name: modify kernel args #持久开启Linux路由转发功能
sysctl:
name: net.ipv4.ip_forward
value: '1'
sysctl_set: true
sysctl_file: /etc/sysctl.conf
notify: flush args
handlers:
- name: flush args #刷新sysctl.conf文件
shell: "sysctl -p" #生效
# 在lvs主机上安装lpvsadm包vim install_lvs.yml
- name: install lvs
hosts: lbs
tasks:
- name: install soft #安装ipvsadm软件
yum:
name: ipvsadm
state: present
```
##### 配置LVS-NAT模式规则
```shell
# ipvsadm命令常用选项
-A: 添加虚拟服务器
-E: 编辑虚拟服务器
-D: 删除虚拟服务器
-C: 删除所有虚拟服务器
-t: 添加tcp服务器
-u: 添加udp服务器
-s: 指定调度算法。如轮询rr/加权轮询wrr/最少连接lc/加权最少连接wlc
-a: 向添加的虚拟服务器中加入真实服务器
-r: 指定真实服务器
-w: 设置权重
-m: 指定工作模式为NAT
-g: 指定工作模式为DR
-n: 显示具体IP地址
-L: 显示内核中的虚拟服务规则
原文链接:https://www.linuxcool.com/ipvsadm
# 配置LVS-NAT模式规则,在lvs机组下操作
ipvsadm -A -t 192.168.88.5:80 -s rr
ipvsadm -a -t 192.168.88.5:80 -r 192.168.99.100:80 -w 1 -m
ipvsadm -a -t 192.168.88.5:80 -r 192.168.99.200:80 -w 2 -m
ipvsadm -Ln
# 调整LVS-NAT规则
ipvsadm -E -t 192.168.88.5:80 -s wrr
# 访问测试,必须使用LVS集群之外的主机作为客户端
for i in {1..6}
do
curl http://192.168.88.5
done
```
#### 二、LVS-DR模式
##### 实验环境准备
##### 网络环境配置:
```
client1:
eth0-> 192.168.88.10
lvs1:
eth0->192.168.88.5
web1:
eth0->192.168.88.100
web2:
eth0->192.168.88.200
```
##### 配置Ansible
```shell
# 调整主机清单文件,将webservers主机组内主机IP地址改为88段
# 配置inventory
[clients]
client ansible_ssh_host=192.168.88.10
[webservers]
web1 ansible_ssh_host=192.168.88.100
web2 ansible_ssh_host=192.168.88.200
[lbs]
lvs1 ansible_ssh_host=192.168.88.5
[all:vars]
ansible_ssh_port=22
ansible_ssh_user=root
ansible_ssh_pass=a
```
##### 配置网络Yum并测试DR模式
>lvs的eth0上子接口:0配置vip
>web的lo上配置vip
>web服务器修改内核参数(使webservers组屏蔽交换机发送88.15的广播)
>lvs配置规则
```shell
## 配置DR模式
# 调度器配置虚拟接口IP地址,在物理网卡接口eth:0
# 真实服务器虚拟接口IP地址,在本地回环接口lo
# 配置各主机yum源config_yum_DR.yml
- name: config yum
hosts: all
tasks:
- name: rm dir #删除目录
file:
path: /etc/yum.repos.d/
state: absent
- name: create dir #创建目录
file:
path: /etc/yum.repos.d/
state: directory
mode: '0755'
- name: copy repo #发送repo文件
copy:
src: files/local88.repo
dest: /etc/yum.repos.d/
# 配置webservers主机内主机web服务
ansible-playbook config_web.yml
--------------------------------------------------------------------
# 配置虚拟IP地址(VIP) config_vip.yml
- name: install soft
hosts: lbs,webservers
tasks:
- name: install network-scripts
yum:
name: network-scripts
state: present
#配置lvs1主机eth0网卡虚拟接口IP地址
- name: config lvs vip
hosts: lbs
tasks:
- name: config file
copy:
content: |
TYPE=Ethernet
DEVICE=eth0:0
NAME=eth0:0
IPADDR=192.168.88.15
PREFIX=24
BROADCAST=192.168.88.255
ONBOOT=yes
dest: /etc/sysconfig/network-scripts/ifcfg-eth0:0
notify: active vip
handlers:
- name: active vip
shell: ifup eth0:0
#配置webservers主机lo虚拟接口IP地址
- name: config web vip
hosts: webservers
tasks:
- name: config file
copy:
content: |
DEVICE=lo:0
NAME=lo:0
IPADDR=192.168.88.15
PREFIX=32
NETWORK=192.168.88.15
BROADCAST=192.168.88.15
ONBOOT=yes
dest: /etc/sysconfig/network-scripts/ifcfg-lo:0
notify: active vip
handlers:
- name: active vip
shell: ifup lo:0
# 修改web服务器的内核参数 config_sysctl.yml
- name: config kernel args
hosts: webservers
tasks:
- name: modify kernel args
blockinfile:
path: /etc/sysctl.conf
block: |
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
notify: flush args
handlers:
- name: flush args
shell: "sysctl -p"
#- name: config kernel args
# hosts: webservers
# tasks:
# - name: config arp_ignore #配置忽略arp广播
# sysctl:
# name: "{{ item }}"
# value: "1"
# sysctl_set: true
# sysctl_file: /etc/sysctl.conf
# loop:
# - net.ipv4.conf.all.arp_ignore
# - net.ipv4.conf.lo.arp_ignore
# notify: flush args
# - name: config arp_announce #配置禁止arp宣告
# sysctl:
# name: "{{ item }}"
# value: "2"
# sysctl_set: true
# sysctl_file: /etc/sysctl.conf
# loop:
# - net.ipv4.conf.all.arp_announce
# - net.ipv4.conf.lo.arp_announce
# notify: flush args
# handlers:
# - name: flush args #刷新sysctl.conf文件配置
# shell: "sysctl -p"
```
##### 配置LVS-DR模式规则
```shell
# 配置LVS-DR模式规则,在lvs机组下操作
ipvsadm -Ln
ipvsadm -A -t 192.168.88.15:80 -s wrr
ipvsadm -a -t 192.168.88.15:80 -r 192.168.88.100:80 -w 1 -g
ipvsadm -a -t 192.168.88.15:80 -r 192.168.88.200:80 -w 2 -g
# 在client客户端下访问测试,必须使用LVS集群之外的主机作为客户端
for i in {1..6}
do
curl http://192.168.88.15
done
```